Skip to content

Commit 70b9551

Browse files
committed
better formatting for statements
1 parent 2537b87 commit 70b9551

File tree

4 files changed

+48
-24
lines changed

4 files changed

+48
-24
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3779,6 +3779,7 @@ dependencies = [
37793779
"rustc_query_system",
37803780
"rustc_resolve",
37813781
"rustc_session",
3782+
"rustc_smir",
37823783
"rustc_span",
37833784
"rustc_symbol_mangling",
37843785
"rustc_target",

compiler/rustc_driver_impl/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ rustc_privacy = { path = "../rustc_privacy" }
4646
rustc_query_system = { path = "../rustc_query_system" }
4747
rustc_resolve = { path = "../rustc_resolve" }
4848
rustc_session = { path = "../rustc_session" }
49-
rustc_span = { path = "../rustc_span" }
5049
rustc_smir ={ path = "../rustc_smir" }
50+
rustc_span = { path = "../rustc_span" }
5151
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
5252
rustc_target = { path = "../rustc_target" }
5353
rustc_trait_selection = { path = "../rustc_trait_selection" }

compiler/rustc_smir/src/rustc_internal/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,4 @@ impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexedVal> Index<V
290290
pub trait RustcInternal<'tcx> {
291291
type T;
292292
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T;
293-
294-
/// Use this when you want to convert to a rustc counterpart in user-code.
295-
/// Do not use this within the smir crates themselves.
296-
fn internal_via_tls(&self) -> Self::T {
297-
with_tables(|tables| self.internal(tables))
298-
}
299293
}

compiler/rustc_smir/src/rustc_internal/pretty.rs

+46-17
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@ use stable_mir::{
77
CrateItem,
88
};
99

10-
use super::{run, RustcInternal};
10+
use super::{internal, run};
1111

1212
pub fn write_smir_pretty<'tcx>(tcx: TyCtxt<'tcx>, w: &mut dyn io::Write) -> io::Result<()> {
13+
writeln!(w, "// WARNING: This is highly experimental output it's intended for stable-mir developers only.").unwrap();
14+
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();
1315
run(tcx, || {
1416
let items = stable_mir::all_local_items();
1517
items.iter().for_each(|item| {
1618
// Because we can't return a Result from a closure, we have to unwrap here.
1719
writeln!(w, "{}", function_name(*item, tcx)).unwrap();
1820
writeln!(w, "{}", function_body(*item, tcx)).unwrap();
19-
writeln!(w, "------------------").unwrap();
20-
item.body().blocks.iter().for_each(|block| {
21+
item.body().blocks.iter().enumerate().for_each(|(index, block)| {
22+
writeln!(w, " bb{}: {{", index).unwrap();
2123
block.statements.iter().for_each(|statement| {
2224
writeln!(w, "{}", pretty_statement(&statement.kind, tcx)).unwrap();
2325
});
26+
writeln!(w, " }}").unwrap();
2427
})
2528
})
2629
});
@@ -76,8 +79,8 @@ pub fn pretty_statement(statement: &StatementKind, tcx: TyCtxt<'_>) -> String {
7679
let mut pretty = String::new();
7780
match statement {
7881
StatementKind::Assign(place, rval) => {
79-
pretty.push_str(format!("_{} = ", place.local).as_str());
80-
pretty.push_str(&pretty_rvalue(rval, tcx))
82+
pretty.push_str(format!(" _{} = ", place.local).as_str());
83+
pretty.push_str(format!("{}", &pretty_rvalue(rval, tcx)).as_str());
8184
}
8285
StatementKind::FakeRead(_, _) => todo!(),
8386
StatementKind::SetDiscriminant { .. } => todo!(),
@@ -103,12 +106,12 @@ pub fn pretty_operand(operand: &Operand, _tcx: TyCtxt<'_>) -> String {
103106
pretty.push_str(format!("{}", copy.local).as_str());
104107
}
105108
Operand::Move(mv) => {
106-
pretty.push_str("move");
107-
pretty.push_str(format!("{}", mv.local).as_str());
109+
pretty.push_str("move ");
110+
pretty.push_str(format!("_{}", mv.local).as_str());
108111
}
109112
Operand::Constant(cnst) => {
110113
pretty.push_str("const ");
111-
pretty.push_str(cnst.literal.internal_via_tls().to_string().as_str());
114+
pretty.push_str(internal(&cnst.literal).to_string().as_str());
112115
}
113116
}
114117
pretty
@@ -118,9 +121,9 @@ pub fn pretty_rvalue(rval: &Rvalue, tcx: TyCtxt<'_>) -> String {
118121
let mut pretty = String::new();
119122
match rval {
120123
Rvalue::AddressOf(muta, addr) => {
121-
pretty.push_str("address_of");
124+
pretty.push_str("&raw ");
122125
pretty.push_str(&ret_mutability(&muta));
123-
pretty.push_str(format!("{}", addr.local).as_str());
126+
pretty.push_str(format!("(*_{})", addr.local).as_str());
124127
}
125128
Rvalue::Aggregate(aggregatekind, operands) => {
126129
pretty.push_str(format!("{:#?}", aggregatekind).as_str());
@@ -222,21 +225,45 @@ pub fn pretty_ty(ty: TyKind, tcx: TyCtxt<'_>) -> String {
222225
stable_mir::ty::FloatTy::F64 => "f64".to_string(),
223226
},
224227
RigidTy::Adt(def, _) => {
225-
format!("{:#?}", tcx.type_of(def.0.internal_via_tls()).instantiate_identity())
228+
format!("{}", tcx.type_of(internal(&def.0)).instantiate_identity())
226229
}
227230
RigidTy::Foreign(_) => format!("{:#?}", rigid_ty),
228231
RigidTy::Str => "str".to_string(),
229-
RigidTy::Array(_ty, len) => {
230-
format!("[{};{:#?}]", 1, len.internal_via_tls())
232+
RigidTy::Array(ty, len) => {
233+
format!(
234+
"[{}; {}]",
235+
pretty_ty(ty.kind(), tcx),
236+
internal(&len).try_to_scalar().unwrap()
237+
)
238+
}
239+
RigidTy::Slice(ty) => {
240+
format!("[{}]", pretty_ty(ty.kind(), tcx))
241+
}
242+
RigidTy::RawPtr(ty, mutability) => {
243+
pretty.push_str("*");
244+
match mutability {
245+
Mutability::Not => pretty.push_str("const "),
246+
Mutability::Mut => pretty.push_str("mut "),
247+
}
248+
pretty.push_str(&pretty_ty(ty.kind(), tcx));
249+
pretty
231250
}
232-
RigidTy::Slice(ty) => pretty_ty(ty.kind(), tcx),
233-
RigidTy::RawPtr(_, _) => format!("{:#?}", rigid_ty),
234251
RigidTy::Ref(_, ty, _) => pretty_ty(ty.kind(), tcx),
235252
RigidTy::FnDef(_, _) => format!("{:#?}", rigid_ty),
236253
RigidTy::FnPtr(_) => format!("{:#?}", rigid_ty),
237254
RigidTy::Closure(_, _) => format!("{:#?}", rigid_ty),
238255
RigidTy::Coroutine(_, _, _) => format!("{:#?}", rigid_ty),
239-
RigidTy::Dynamic(_, _, _) => format!("{:#?}", rigid_ty),
256+
RigidTy::Dynamic(data, region, repr) => {
257+
// FIXME: Fix binder printing, it looks ugly now
258+
pretty.push_str("(");
259+
match repr {
260+
stable_mir::ty::DynKind::Dyn => pretty.push_str("dyn "),
261+
stable_mir::ty::DynKind::DynStar => pretty.push_str("dyn* "),
262+
}
263+
pretty.push_str(format!("{:#?}", data).as_str());
264+
pretty.push_str(format!(" + {:#?} )", region).as_str());
265+
pretty
266+
}
240267
RigidTy::Never => "!".to_string(),
241268
RigidTy::Tuple(tuple) => {
242269
if tuple.is_empty() {
@@ -256,7 +283,9 @@ pub fn pretty_ty(ty: TyKind, tcx: TyCtxt<'_>) -> String {
256283
}
257284
},
258285
TyKind::Alias(_, _) => format!("{:#?}", ty),
259-
TyKind::Param(_) => format!("{:#?}", ty),
286+
TyKind::Param(param_ty) => {
287+
format!("{:#?}", param_ty.name)
288+
}
260289
TyKind::Bound(_, _) => format!("{:#?}", ty),
261290
}
262291
}

0 commit comments

Comments
 (0)