about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOğuz Ağcayazı <ouz.agz@gmail.com>2023-11-09 12:11:41 +0300
committerouz-a <ouz.agz@gmail.com>2023-11-17 13:28:07 +0300
commitebd9c145f600245ec66d0ddf4c0129874c10ecb8 (patch)
treea65fbd269f18a7ad0b228d0ac92badbba22fc786
parent0f0e9baf199d022c6a93bb36353358a101c4b4f5 (diff)
downloadrust-ebd9c145f600245ec66d0ddf4c0129874c10ecb8.tar.gz
rust-ebd9c145f600245ec66d0ddf4c0129874c10ecb8.zip
better formatting for statements
-rw-r--r--Cargo.lock1
-rw-r--r--compiler/rustc_driver_impl/Cargo.toml2
-rw-r--r--compiler/rustc_smir/src/rustc_internal/mod.rs6
-rw-r--r--compiler/rustc_smir/src/rustc_internal/pretty.rs63
-rw-r--r--compiler/stable_mir/src/mir/visit.rs2
5 files changed, 49 insertions, 25 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 92bac995bc6..9fe70870140 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3815,6 +3815,7 @@ dependencies = [
  "rustc_query_system",
  "rustc_resolve",
  "rustc_session",
+ "rustc_smir",
  "rustc_span",
  "rustc_symbol_mangling",
  "rustc_target",
diff --git a/compiler/rustc_driver_impl/Cargo.toml b/compiler/rustc_driver_impl/Cargo.toml
index e9b5a328422..545ff32e598 100644
--- a/compiler/rustc_driver_impl/Cargo.toml
+++ b/compiler/rustc_driver_impl/Cargo.toml
@@ -43,8 +43,8 @@ rustc_privacy = { path = "../rustc_privacy" }
 rustc_query_system = { path = "../rustc_query_system" }
 rustc_resolve = { path = "../rustc_resolve" }
 rustc_session = { path = "../rustc_session" }
-rustc_span = { path = "../rustc_span" }
 rustc_smir ={ path = "../rustc_smir" }
+rustc_span = { path = "../rustc_span" }
 rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
 rustc_target = { path = "../rustc_target" }
 rustc_trait_selection = { path = "../rustc_trait_selection" }
diff --git a/compiler/rustc_smir/src/rustc_internal/mod.rs b/compiler/rustc_smir/src/rustc_internal/mod.rs
index 7957c3ce617..fa75fd3076c 100644
--- a/compiler/rustc_smir/src/rustc_internal/mod.rs
+++ b/compiler/rustc_smir/src/rustc_internal/mod.rs
@@ -300,10 +300,4 @@ impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexedVal> Index<V
 pub trait RustcInternal<'tcx> {
     type T;
     fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T;
-
-    /// Use this when you want to convert to a rustc counterpart in user-code.
-    /// Do not use this within the smir crates themselves.
-    fn internal_via_tls(&self) -> Self::T {
-        with_tables(|tables| self.internal(tables))
-    }
 }
diff --git a/compiler/rustc_smir/src/rustc_internal/pretty.rs b/compiler/rustc_smir/src/rustc_internal/pretty.rs
index ea0ccfe3e4e..45917630cf3 100644
--- a/compiler/rustc_smir/src/rustc_internal/pretty.rs
+++ b/compiler/rustc_smir/src/rustc_internal/pretty.rs
@@ -7,20 +7,23 @@ use stable_mir::{
     CrateItem,
 };
 
-use super::{run, RustcInternal};
+use super::{internal, run};
 
 pub fn write_smir_pretty<'tcx>(tcx: TyCtxt<'tcx>, w: &mut dyn io::Write) -> io::Result<()> {
+    writeln!(w, "// WARNING: This is highly experimental output it's intended for stable-mir developers only.").unwrap();
+    writeln!(w, "// If you find a bug or want to improve the output open a issue at https://github.com/rust-lang/project-stable-mir.").unwrap();
     run(tcx, || {
         let items = stable_mir::all_local_items();
         items.iter().for_each(|item| {
             // Because we can't return a Result from a closure, we have to unwrap here.
             writeln!(w, "{}", function_name(*item, tcx)).unwrap();
             writeln!(w, "{}", function_body(*item, tcx)).unwrap();
-            writeln!(w, "------------------").unwrap();
-            item.body().blocks.iter().for_each(|block| {
+            item.body().blocks.iter().enumerate().for_each(|(index, block)| {
+                writeln!(w, "    bb{}: {{", index).unwrap();
                 block.statements.iter().for_each(|statement| {
                     writeln!(w, "{}", pretty_statement(&statement.kind, tcx)).unwrap();
                 });
+                writeln!(w, "    }}").unwrap();
             })
         })
     });
@@ -76,8 +79,8 @@ pub fn pretty_statement(statement: &StatementKind, tcx: TyCtxt<'_>) -> String {
     let mut pretty = String::new();
     match statement {
         StatementKind::Assign(place, rval) => {
-            pretty.push_str(format!("_{} = ", place.local).as_str());
-            pretty.push_str(&pretty_rvalue(rval, tcx))
+            pretty.push_str(format!("        _{} = ", place.local).as_str());
+            pretty.push_str(format!("{}", &pretty_rvalue(rval, tcx)).as_str());
         }
         StatementKind::FakeRead(_, _) => todo!(),
         StatementKind::SetDiscriminant { .. } => todo!(),
@@ -103,12 +106,12 @@ pub fn pretty_operand(operand: &Operand, _tcx: TyCtxt<'_>) -> String {
             pretty.push_str(format!("{}", copy.local).as_str());
         }
         Operand::Move(mv) => {
-            pretty.push_str("move");
-            pretty.push_str(format!("{}", mv.local).as_str());
+            pretty.push_str("move ");
+            pretty.push_str(format!("_{}", mv.local).as_str());
         }
         Operand::Constant(cnst) => {
             pretty.push_str("const ");
-            pretty.push_str(cnst.literal.internal_via_tls().to_string().as_str());
+            pretty.push_str(internal(&cnst.literal).to_string().as_str());
         }
     }
     pretty
@@ -118,9 +121,9 @@ pub fn pretty_rvalue(rval: &Rvalue, tcx: TyCtxt<'_>) -> String {
     let mut pretty = String::new();
     match rval {
         Rvalue::AddressOf(muta, addr) => {
-            pretty.push_str("address_of");
+            pretty.push_str("&raw ");
             pretty.push_str(&ret_mutability(&muta));
-            pretty.push_str(format!("{}", addr.local).as_str());
+            pretty.push_str(format!("(*_{})", addr.local).as_str());
         }
         Rvalue::Aggregate(aggregatekind, operands) => {
             pretty.push_str(format!("{:#?}", aggregatekind).as_str());
@@ -222,21 +225,45 @@ pub fn pretty_ty(ty: TyKind, tcx: TyCtxt<'_>) -> String {
                 stable_mir::ty::FloatTy::F64 => "f64".to_string(),
             },
             RigidTy::Adt(def, _) => {
-                format!("{:#?}", tcx.type_of(def.0.internal_via_tls()).instantiate_identity())
+                format!("{}", tcx.type_of(internal(&def.0)).instantiate_identity())
             }
             RigidTy::Foreign(_) => format!("{:#?}", rigid_ty),
             RigidTy::Str => "str".to_string(),
-            RigidTy::Array(_ty, len) => {
-                format!("[{};{:#?}]", 1, len.internal_via_tls())
+            RigidTy::Array(ty, len) => {
+                format!(
+                    "[{}; {}]",
+                    pretty_ty(ty.kind(), tcx),
+                    internal(&len).try_to_scalar().unwrap()
+                )
+            }
+            RigidTy::Slice(ty) => {
+                format!("[{}]", pretty_ty(ty.kind(), tcx))
+            }
+            RigidTy::RawPtr(ty, mutability) => {
+                pretty.push_str("*");
+                match mutability {
+                    Mutability::Not => pretty.push_str("const "),
+                    Mutability::Mut => pretty.push_str("mut "),
+                }
+                pretty.push_str(&pretty_ty(ty.kind(), tcx));
+                pretty
             }
-            RigidTy::Slice(ty) => pretty_ty(ty.kind(), tcx),
-            RigidTy::RawPtr(_, _) => format!("{:#?}", rigid_ty),
             RigidTy::Ref(_, ty, _) => pretty_ty(ty.kind(), tcx),
             RigidTy::FnDef(_, _) => format!("{:#?}", rigid_ty),
             RigidTy::FnPtr(_) => format!("{:#?}", rigid_ty),
             RigidTy::Closure(_, _) => format!("{:#?}", rigid_ty),
             RigidTy::Coroutine(_, _, _) => format!("{:#?}", rigid_ty),
-            RigidTy::Dynamic(_, _, _) => format!("{:#?}", rigid_ty),
+            RigidTy::Dynamic(data, region, repr) => {
+                // FIXME: Fix binder printing, it looks ugly now
+                pretty.push_str("(");
+                match repr {
+                    stable_mir::ty::DynKind::Dyn => pretty.push_str("dyn "),
+                    stable_mir::ty::DynKind::DynStar => pretty.push_str("dyn* "),
+                }
+                pretty.push_str(format!("{:#?}", data).as_str());
+                pretty.push_str(format!(" +  {:#?} )", region).as_str());
+                pretty
+            }
             RigidTy::Never => "!".to_string(),
             RigidTy::Tuple(tuple) => {
                 if tuple.is_empty() {
@@ -256,7 +283,9 @@ pub fn pretty_ty(ty: TyKind, tcx: TyCtxt<'_>) -> String {
             }
         },
         TyKind::Alias(_, _) => format!("{:#?}", ty),
-        TyKind::Param(_) => format!("{:#?}", ty),
+        TyKind::Param(param_ty) => {
+            format!("{:#?}", param_ty.name)
+        }
         TyKind::Bound(_, _) => format!("{:#?}", ty),
     }
 }
diff --git a/compiler/stable_mir/src/mir/visit.rs b/compiler/stable_mir/src/mir/visit.rs
index d6304d3ea39..40bedd67352 100644
--- a/compiler/stable_mir/src/mir/visit.rs
+++ b/compiler/stable_mir/src/mir/visit.rs
@@ -157,7 +157,7 @@ pub trait MirVisitor {
 
     fn super_local_decl(&mut self, local: Local, decl: &LocalDecl) {
         let _ = local;
-        let LocalDecl { ty, span } = decl;
+        let LocalDecl { ty, span, .. } = decl;
         self.visit_ty(ty, Location(*span));
     }