better formatting for statements

This commit is contained in:
Oğuz Ağcayazı 2023-11-09 12:11:41 +03:00 committed by ouz-a
parent 0f0e9baf19
commit ebd9c145f6
5 changed files with 49 additions and 25 deletions

View File

@ -3815,6 +3815,7 @@ dependencies = [
"rustc_query_system", "rustc_query_system",
"rustc_resolve", "rustc_resolve",
"rustc_session", "rustc_session",
"rustc_smir",
"rustc_span", "rustc_span",
"rustc_symbol_mangling", "rustc_symbol_mangling",
"rustc_target", "rustc_target",

View File

@ -43,8 +43,8 @@ rustc_privacy = { path = "../rustc_privacy" }
rustc_query_system = { path = "../rustc_query_system" } rustc_query_system = { path = "../rustc_query_system" }
rustc_resolve = { path = "../rustc_resolve" } rustc_resolve = { path = "../rustc_resolve" }
rustc_session = { path = "../rustc_session" } rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }
rustc_smir ={ path = "../rustc_smir" } rustc_smir ={ path = "../rustc_smir" }
rustc_span = { path = "../rustc_span" }
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" } rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
rustc_target = { path = "../rustc_target" } rustc_target = { path = "../rustc_target" }
rustc_trait_selection = { path = "../rustc_trait_selection" } rustc_trait_selection = { path = "../rustc_trait_selection" }

View File

@ -300,10 +300,4 @@ fn index(&self, index: V) -> &Self::Output {
pub trait RustcInternal<'tcx> { pub trait RustcInternal<'tcx> {
type T; type T;
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::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))
}
} }

View File

@ -7,20 +7,23 @@
CrateItem, 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<()> { 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, || { run(tcx, || {
let items = stable_mir::all_local_items(); let items = stable_mir::all_local_items();
items.iter().for_each(|item| { items.iter().for_each(|item| {
// Because we can't return a Result from a closure, we have to unwrap here. // 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_name(*item, tcx)).unwrap();
writeln!(w, "{}", function_body(*item, tcx)).unwrap(); writeln!(w, "{}", function_body(*item, tcx)).unwrap();
writeln!(w, "------------------").unwrap(); item.body().blocks.iter().enumerate().for_each(|(index, block)| {
item.body().blocks.iter().for_each(|block| { writeln!(w, " bb{}: {{", index).unwrap();
block.statements.iter().for_each(|statement| { block.statements.iter().for_each(|statement| {
writeln!(w, "{}", pretty_statement(&statement.kind, tcx)).unwrap(); 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(); let mut pretty = String::new();
match statement { match statement {
StatementKind::Assign(place, rval) => { StatementKind::Assign(place, rval) => {
pretty.push_str(format!("_{} = ", place.local).as_str()); pretty.push_str(format!(" _{} = ", place.local).as_str());
pretty.push_str(&pretty_rvalue(rval, tcx)) pretty.push_str(format!("{}", &pretty_rvalue(rval, tcx)).as_str());
} }
StatementKind::FakeRead(_, _) => todo!(), StatementKind::FakeRead(_, _) => todo!(),
StatementKind::SetDiscriminant { .. } => 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()); pretty.push_str(format!("{}", copy.local).as_str());
} }
Operand::Move(mv) => { Operand::Move(mv) => {
pretty.push_str("move"); pretty.push_str("move ");
pretty.push_str(format!("{}", mv.local).as_str()); pretty.push_str(format!("_{}", mv.local).as_str());
} }
Operand::Constant(cnst) => { Operand::Constant(cnst) => {
pretty.push_str("const "); 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 pretty
@ -118,9 +121,9 @@ pub fn pretty_rvalue(rval: &Rvalue, tcx: TyCtxt<'_>) -> String {
let mut pretty = String::new(); let mut pretty = String::new();
match rval { match rval {
Rvalue::AddressOf(muta, addr) => { Rvalue::AddressOf(muta, addr) => {
pretty.push_str("address_of"); pretty.push_str("&raw ");
pretty.push_str(&ret_mutability(&muta)); 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) => { Rvalue::Aggregate(aggregatekind, operands) => {
pretty.push_str(format!("{:#?}", aggregatekind).as_str()); 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(), stable_mir::ty::FloatTy::F64 => "f64".to_string(),
}, },
RigidTy::Adt(def, _) => { 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::Foreign(_) => format!("{:#?}", rigid_ty),
RigidTy::Str => "str".to_string(), RigidTy::Str => "str".to_string(),
RigidTy::Array(_ty, len) => { RigidTy::Array(ty, len) => {
format!("[{};{:#?}]", 1, len.internal_via_tls()) 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::Ref(_, ty, _) => pretty_ty(ty.kind(), tcx),
RigidTy::FnDef(_, _) => format!("{:#?}", rigid_ty), RigidTy::FnDef(_, _) => format!("{:#?}", rigid_ty),
RigidTy::FnPtr(_) => format!("{:#?}", rigid_ty), RigidTy::FnPtr(_) => format!("{:#?}", rigid_ty),
RigidTy::Closure(_, _) => format!("{:#?}", rigid_ty), RigidTy::Closure(_, _) => format!("{:#?}", rigid_ty),
RigidTy::Coroutine(_, _, _) => 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::Never => "!".to_string(),
RigidTy::Tuple(tuple) => { RigidTy::Tuple(tuple) => {
if tuple.is_empty() { if tuple.is_empty() {
@ -256,7 +283,9 @@ pub fn pretty_ty(ty: TyKind, tcx: TyCtxt<'_>) -> String {
} }
}, },
TyKind::Alias(_, _) => format!("{:#?}", ty), TyKind::Alias(_, _) => format!("{:#?}", ty),
TyKind::Param(_) => format!("{:#?}", ty), TyKind::Param(param_ty) => {
format!("{:#?}", param_ty.name)
}
TyKind::Bound(_, _) => format!("{:#?}", ty), TyKind::Bound(_, _) => format!("{:#?}", ty),
} }
} }

View File

@ -157,7 +157,7 @@ fn super_basic_block(&mut self, bb: &BasicBlock) {
fn super_local_decl(&mut self, local: Local, decl: &LocalDecl) { fn super_local_decl(&mut self, local: Local, decl: &LocalDecl) {
let _ = local; let _ = local;
let LocalDecl { ty, span } = decl; let LocalDecl { ty, span, .. } = decl;
self.visit_ty(ty, Location(*span)); self.visit_ty(ty, Location(*span));
} }