From ae179a04b64d56fa4d648fccf3db661301550797 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuz=20A=C4=9Fcayaz=C4=B1?= Date: Wed, 8 Nov 2023 12:37:26 +0300 Subject: [PATCH 1/7] emit basic smir --- compiler/rustc_driver_impl/Cargo.toml | 1 + compiler/rustc_driver_impl/src/pretty.rs | 6 + compiler/rustc_session/src/config.rs | 10 +- compiler/rustc_smir/src/rustc_internal/mod.rs | 7 + .../rustc_smir/src/rustc_internal/pretty.rs | 133 ++++++++++++++++++ compiler/rustc_smir/src/rustc_smir/mod.rs | 6 +- compiler/stable_mir/src/mir/body.rs | 2 + 7 files changed, 160 insertions(+), 5 deletions(-) create mode 100644 compiler/rustc_smir/src/rustc_internal/pretty.rs diff --git a/compiler/rustc_driver_impl/Cargo.toml b/compiler/rustc_driver_impl/Cargo.toml index da7c2440faad4..e9b5a32842204 100644 --- a/compiler/rustc_driver_impl/Cargo.toml +++ b/compiler/rustc_driver_impl/Cargo.toml @@ -44,6 +44,7 @@ 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_symbol_mangling = { path = "../rustc_symbol_mangling" } rustc_target = { path = "../rustc_target" } rustc_trait_selection = { path = "../rustc_trait_selection" } diff --git a/compiler/rustc_driver_impl/src/pretty.rs b/compiler/rustc_driver_impl/src/pretty.rs index cc533b9941ab4..84f8941ff662d 100644 --- a/compiler/rustc_driver_impl/src/pretty.rs +++ b/compiler/rustc_driver_impl/src/pretty.rs @@ -9,6 +9,7 @@ use rustc_middle::mir::{write_mir_graphviz, write_mir_pretty}; use rustc_middle::ty::{self, TyCtxt}; use rustc_session::config::{OutFileName, PpHirMode, PpMode, PpSourceMode}; use rustc_session::Session; +use rustc_smir::rustc_internal::pretty::write_smir_pretty; use rustc_span::symbol::Ident; use rustc_span::FileName; @@ -325,6 +326,11 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) { write_mir_graphviz(ex.tcx(), None, &mut out).unwrap(); String::from_utf8(out).unwrap() } + Smir => { + let mut out = Vec::new(); + write_smir_pretty(ex.tcx(), &mut out).unwrap(); + String::from_utf8(out).unwrap() + } ThirTree => { let tcx = ex.tcx(); let mut out = String::new(); diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index add40b83d21d3..54335645e43e2 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -2926,6 +2926,7 @@ fn parse_pretty(handler: &EarlyErrorHandler, unstable_opts: &UnstableOptions) -> "thir-tree" => ThirTree, "thir-flat" => ThirFlat, "mir" => Mir, + "smir" => Smir, "mir-cfg" => MirCFG, name => handler.early_error(format!( "argument to `unpretty` must be one of `normal`, `identified`, \ @@ -3106,6 +3107,8 @@ pub enum PpMode { Mir, /// `-Zunpretty=mir-cfg` MirCFG, + /// `-Zunpretty=smir` + Smir, } impl PpMode { @@ -3122,7 +3125,8 @@ impl PpMode { | ThirTree | ThirFlat | Mir - | MirCFG => true, + | MirCFG + | Smir => true, } } pub fn needs_hir(&self) -> bool { @@ -3130,13 +3134,13 @@ impl PpMode { match *self { Source(_) | AstTree | AstTreeExpanded => false, - Hir(_) | HirTree | ThirTree | ThirFlat | Mir | MirCFG => true, + Hir(_) | HirTree | ThirTree | ThirFlat | Mir | MirCFG | Smir => true, } } pub fn needs_analysis(&self) -> bool { use PpMode::*; - matches!(*self, Hir(PpHirMode::Typed) | Mir | MirCFG | ThirTree | ThirFlat) + matches!(*self, Hir(PpHirMode::Typed) | Mir | Smir | MirCFG | ThirTree | ThirFlat) } } diff --git a/compiler/rustc_smir/src/rustc_internal/mod.rs b/compiler/rustc_smir/src/rustc_internal/mod.rs index c82f948f195e6..7957c3ce61703 100644 --- a/compiler/rustc_smir/src/rustc_internal/mod.rs +++ b/compiler/rustc_smir/src/rustc_internal/mod.rs @@ -21,6 +21,7 @@ use std::hash::Hash; use std::ops::Index; mod internal; +pub mod pretty; pub fn stable<'tcx, S: Stable<'tcx>>(item: S) -> S::T { with_tables(|tables| item.stable(tables)) @@ -299,4 +300,10 @@ impl Index { 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 new file mode 100644 index 0000000000000..e9af5081353ee --- /dev/null +++ b/compiler/rustc_smir/src/rustc_internal/pretty.rs @@ -0,0 +1,133 @@ +use std::io; + +use rustc_middle::ty::TyCtxt; +use stable_mir::{ + ty::{RigidTy, TyKind}, + CrateItem, mir::Mutability, +}; + + +use super::{run, RustcInternal}; + +pub fn write_smir_pretty<'tcx>(tcx: TyCtxt<'tcx>, w: &mut dyn io::Write) -> io::Result<()> { + 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(); + }) + }); + Ok(()) +} + +pub fn function_name(item: CrateItem,tcx: TyCtxt<'_>) -> String { + let mut name = String::new(); + let body = item.body(); + name.push_str("fn "); + name.push_str(item.name().as_str()); + if body.arg_locals().is_empty() { + name.push_str("()"); + }else{ + name.push_str("("); + } + body.arg_locals().iter().for_each(|local| { + name.push_str(format!("_{}: ",local.local).as_str()); + name.push_str(&pretty_ty(local.ty.kind(), tcx)); + }); + if !body.arg_locals().is_empty() { + name.push_str(")"); + } + let return_local = body.ret_local(); + name.push_str(" -> "); + name.push_str(&pretty_ty(return_local.ty.kind(), tcx)); + name.push_str(" {"); + name +} + +pub fn function_body(item: CrateItem,_tcx: TyCtxt<'_>) -> String { + let mut body_str = String::new(); + let body = item.body(); + body.inner_locals().iter().for_each(|local| { + body_str.push_str(" "); + body_str.push_str(format!("let {}",ret_mutability(&local.mutability)).as_str()); + body_str.push_str(format!("_{}: ",local.local).as_str()); + body_str.push_str(format!("{}",pretty_ty(local.ty.kind(), _tcx)).as_str()); + body_str.push_str(";\n"); + + }); + body_str.push_str("}"); + body_str + +} + +pub fn ret_mutability(mutability: &Mutability) -> String { + match mutability { + Mutability::Not => "".to_string(), + Mutability::Mut => "mut ".to_string(), + } +} + +pub fn pretty_ty<'tcx>(ty: TyKind,tcx: TyCtxt<'tcx>) -> String { + let mut pretty = String::new(); + pretty.push_str(""); + match ty { + TyKind::RigidTy(rigid_ty) => match rigid_ty { + RigidTy::Bool => "bool".to_string(), + RigidTy::Char => "char".to_string(), + RigidTy::Int(i) => match i { + stable_mir::ty::IntTy::Isize => "isize".to_string(), + stable_mir::ty::IntTy::I8 => "i8".to_string(), + stable_mir::ty::IntTy::I16 => "i16".to_string(), + stable_mir::ty::IntTy::I32 => "i32".to_string(), + stable_mir::ty::IntTy::I64 => "i64".to_string(), + stable_mir::ty::IntTy::I128 => "i128".to_string(), + }, + RigidTy::Uint(u) => match u { + stable_mir::ty::UintTy::Usize => "usize".to_string(), + stable_mir::ty::UintTy::U8 => "u8".to_string(), + stable_mir::ty::UintTy::U16 => "u16".to_string(), + stable_mir::ty::UintTy::U32 => "u32".to_string(), + stable_mir::ty::UintTy::U64 => "u64".to_string(), + stable_mir::ty::UintTy::U128 => "u128".to_string(), + }, + RigidTy::Float(f) => match f { + stable_mir::ty::FloatTy::F32 => "f32".to_string(), + stable_mir::ty::FloatTy::F64 => "f64".to_string(), + }, + RigidTy::Adt(def, _) => format!("{:#?}", tcx.type_of(def.0.internal_via_tls()).instantiate_identity()), + RigidTy::Foreign(_) => format!("{:#?}", rigid_ty), + RigidTy::Str => "str".to_string(), + RigidTy::Array(_ty, len) => { + format!("[{};{:#?}]", 1,len.internal_via_tls())}, + 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::Never => "!".to_string(), + RigidTy::Tuple(tuple) => { + if tuple.is_empty(){ + "()".to_string() + }else { + let mut tuple_str = String::new(); + tuple_str.push_str("("); + tuple.iter().enumerate().for_each(|(i,ty)| { + tuple_str.push_str(&pretty_ty(ty.kind(),tcx)); + if i != tuple.len() - 1 { + tuple_str.push_str(", "); + } + }); + tuple_str.push_str(")"); + tuple_str + } + }, + }, + TyKind::Alias(_, _) => format!("{:#?}", ty), + TyKind::Param(_) => format!("{:#?}", ty), + TyKind::Bound(_, _) => format!("{:#?}", ty), + } +} diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 3df09cef1c7dc..69b0c0bb80d55 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -400,10 +400,12 @@ impl<'tcx> Stable<'tcx> for mir::Body<'tcx> { }) .collect(), self.local_decls - .iter() - .map(|decl| stable_mir::mir::LocalDecl { + .iter_enumerated() + .map(|(local, decl)| stable_mir::mir::LocalDecl { ty: decl.ty.stable(tables), span: decl.source_info.span.stable(tables), + local: local.as_usize(), + mutability: decl.mutability.stable(tables), }) .collect(), self.arg_count, diff --git a/compiler/stable_mir/src/mir/body.rs b/compiler/stable_mir/src/mir/body.rs index 351e7bb69c3f7..2981d6a8bb5c6 100644 --- a/compiler/stable_mir/src/mir/body.rs +++ b/compiler/stable_mir/src/mir/body.rs @@ -64,6 +64,8 @@ type LocalDecls = Vec; pub struct LocalDecl { pub ty: Ty, pub span: Span, + pub local: Local, + pub mutability: Mutability, } #[derive(Clone, Debug)] From 0f0e9baf199d022c6a93bb36353358a101c4b4f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuz=20A=C4=9Fcayaz=C4=B1?= Date: Wed, 8 Nov 2023 15:25:48 +0300 Subject: [PATCH 2/7] cover statements --- .../rustc_smir/src/rustc_internal/pretty.rs | 181 +++++++++++++++--- 1 file changed, 155 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_internal/pretty.rs b/compiler/rustc_smir/src/rustc_internal/pretty.rs index e9af5081353ee..ea0ccfe3e4eba 100644 --- a/compiler/rustc_smir/src/rustc_internal/pretty.rs +++ b/compiler/rustc_smir/src/rustc_internal/pretty.rs @@ -2,37 +2,43 @@ use std::io; use rustc_middle::ty::TyCtxt; use stable_mir::{ + mir::{Mutability, Operand, Rvalue, StatementKind}, ty::{RigidTy, TyKind}, - CrateItem, mir::Mutability, + CrateItem, }; - use super::{run, RustcInternal}; -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<()> { 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, "{}", function_name(*item, tcx)).unwrap(); + writeln!(w, "{}", function_body(*item, tcx)).unwrap(); + writeln!(w, "------------------").unwrap(); + item.body().blocks.iter().for_each(|block| { + block.statements.iter().for_each(|statement| { + writeln!(w, "{}", pretty_statement(&statement.kind, tcx)).unwrap(); + }); + }) }) }); Ok(()) } -pub fn function_name(item: CrateItem,tcx: TyCtxt<'_>) -> String { +pub fn function_name(item: CrateItem, tcx: TyCtxt<'_>) -> String { let mut name = String::new(); - let body = item.body(); + let body = item.body(); name.push_str("fn "); name.push_str(item.name().as_str()); if body.arg_locals().is_empty() { name.push_str("()"); - }else{ + } else { name.push_str("("); } body.arg_locals().iter().for_each(|local| { - name.push_str(format!("_{}: ",local.local).as_str()); + name.push_str(format!("_{}: ", local.local).as_str()); name.push_str(&pretty_ty(local.ty.kind(), tcx)); }); if !body.arg_locals().is_empty() { @@ -45,20 +51,18 @@ pub fn function_name(item: CrateItem,tcx: TyCtxt<'_>) -> String { name } -pub fn function_body(item: CrateItem,_tcx: TyCtxt<'_>) -> String { +pub fn function_body(item: CrateItem, _tcx: TyCtxt<'_>) -> String { let mut body_str = String::new(); - let body = item.body(); + let body = item.body(); body.inner_locals().iter().for_each(|local| { body_str.push_str(" "); - body_str.push_str(format!("let {}",ret_mutability(&local.mutability)).as_str()); - body_str.push_str(format!("_{}: ",local.local).as_str()); - body_str.push_str(format!("{}",pretty_ty(local.ty.kind(), _tcx)).as_str()); + body_str.push_str(format!("let {}", ret_mutability(&local.mutability)).as_str()); + body_str.push_str(format!("_{}: ", local.local).as_str()); + body_str.push_str(format!("{}", pretty_ty(local.ty.kind(), _tcx)).as_str()); body_str.push_str(";\n"); - }); body_str.push_str("}"); body_str - } pub fn ret_mutability(mutability: &Mutability) -> String { @@ -68,7 +72,129 @@ pub fn ret_mutability(mutability: &Mutability) -> String { } } -pub fn pretty_ty<'tcx>(ty: TyKind,tcx: TyCtxt<'tcx>) -> String { +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)) + } + StatementKind::FakeRead(_, _) => todo!(), + StatementKind::SetDiscriminant { .. } => todo!(), + StatementKind::Deinit(_) => todo!(), + StatementKind::StorageLive(_) => todo!(), + StatementKind::StorageDead(_) => todo!(), + StatementKind::Retag(_, _) => todo!(), + StatementKind::PlaceMention(_) => todo!(), + StatementKind::AscribeUserType { .. } => todo!(), + StatementKind::Coverage(_) => todo!(), + StatementKind::Intrinsic(_) => todo!(), + StatementKind::ConstEvalCounter => (), + StatementKind::Nop => (), + } + pretty +} + +pub fn pretty_operand(operand: &Operand, _tcx: TyCtxt<'_>) -> String { + let mut pretty = String::new(); + match operand { + Operand::Copy(copy) => { + pretty.push_str(""); + pretty.push_str(format!("{}", copy.local).as_str()); + } + Operand::Move(mv) => { + 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 +} + +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(&ret_mutability(&muta)); + pretty.push_str(format!("{}", addr.local).as_str()); + } + Rvalue::Aggregate(aggregatekind, operands) => { + pretty.push_str(format!("{:#?}", aggregatekind).as_str()); + pretty.push_str("("); + operands.iter().enumerate().for_each(|(i, op)| { + pretty.push_str(&pretty_operand(op, tcx)); + if i != operands.len() - 1 { + pretty.push_str(", "); + } + }); + pretty.push_str(")"); + } + Rvalue::BinaryOp(bin, op, op2) => { + pretty.push_str(&pretty_operand(op, tcx)); + pretty.push_str(" "); + pretty.push_str(format!("{:#?}", bin).as_str()); + pretty.push_str(" "); + pretty.push_str(&pretty_operand(op2, tcx)); + } + Rvalue::Cast(_, op, ty) => { + pretty.push_str(&pretty_operand(op, tcx)); + pretty.push_str(" as "); + pretty.push_str(&pretty_ty(ty.kind(), tcx)); + } + Rvalue::CheckedBinaryOp(bin, op1, op2) => { + pretty.push_str(&pretty_operand(op1, tcx)); + pretty.push_str(" "); + pretty.push_str(format!("{:#?}", bin).as_str()); + pretty.push_str(" "); + pretty.push_str(&pretty_operand(op2, tcx)); + } + Rvalue::CopyForDeref(deref) => { + pretty.push_str("CopyForDeref"); + pretty.push_str(format!("{}", deref.local).as_str()); + } + Rvalue::Discriminant(place) => { + pretty.push_str("discriminant"); + pretty.push_str(format!("{}", place.local).as_str()); + } + Rvalue::Len(len) => { + pretty.push_str("len"); + pretty.push_str(format!("{}", len.local).as_str()); + } + Rvalue::Ref(_, borrowkind, place) => { + pretty.push_str("ref"); + pretty.push_str(format!("{:#?}", borrowkind).as_str()); + pretty.push_str(format!("{}", place.local).as_str()); + } + Rvalue::Repeat(op, cnst) => { + pretty.push_str(&pretty_operand(op, tcx)); + pretty.push_str(" "); + pretty.push_str(&pretty_ty(cnst.ty().kind(), tcx)); + } + Rvalue::ShallowInitBox(_, _) => todo!(), + Rvalue::ThreadLocalRef(item) => { + pretty.push_str("thread_local_ref"); + pretty.push_str(format!("{:#?}", item).as_str()); + } + Rvalue::NullaryOp(nul, ty) => { + pretty.push_str(format!("{:#?}", nul).as_str()); + pretty.push_str(&&pretty_ty(ty.kind(), tcx)); + pretty.push_str(" "); + } + Rvalue::UnaryOp(un, op) => { + pretty.push_str(&pretty_operand(op, tcx)); + pretty.push_str(" "); + pretty.push_str(format!("{:#?}", un).as_str()); + } + Rvalue::Use(op) => pretty.push_str(&pretty_operand(op, tcx)), + } + pretty +} + +pub fn pretty_ty(ty: TyKind, tcx: TyCtxt<'_>) -> String { let mut pretty = String::new(); pretty.push_str(""); match ty { @@ -95,14 +221,17 @@ pub fn pretty_ty<'tcx>(ty: TyKind,tcx: TyCtxt<'tcx>) -> String { stable_mir::ty::FloatTy::F32 => "f32".to_string(), stable_mir::ty::FloatTy::F64 => "f64".to_string(), }, - RigidTy::Adt(def, _) => format!("{:#?}", tcx.type_of(def.0.internal_via_tls()).instantiate_identity()), + RigidTy::Adt(def, _) => { + format!("{:#?}", tcx.type_of(def.0.internal_via_tls()).instantiate_identity()) + } RigidTy::Foreign(_) => format!("{:#?}", rigid_ty), RigidTy::Str => "str".to_string(), RigidTy::Array(_ty, len) => { - format!("[{};{:#?}]", 1,len.internal_via_tls())}, - RigidTy::Slice(ty) => pretty_ty(ty.kind(),tcx), + format!("[{};{:#?}]", 1, len.internal_via_tls()) + } + 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::FnPtr(_) => format!("{:#?}", rigid_ty), RigidTy::Closure(_, _) => format!("{:#?}", rigid_ty), @@ -110,13 +239,13 @@ pub fn pretty_ty<'tcx>(ty: TyKind,tcx: TyCtxt<'tcx>) -> String { RigidTy::Dynamic(_, _, _) => format!("{:#?}", rigid_ty), RigidTy::Never => "!".to_string(), RigidTy::Tuple(tuple) => { - if tuple.is_empty(){ + if tuple.is_empty() { "()".to_string() - }else { + } else { let mut tuple_str = String::new(); tuple_str.push_str("("); - tuple.iter().enumerate().for_each(|(i,ty)| { - tuple_str.push_str(&pretty_ty(ty.kind(),tcx)); + tuple.iter().enumerate().for_each(|(i, ty)| { + tuple_str.push_str(&pretty_ty(ty.kind(), tcx)); if i != tuple.len() - 1 { tuple_str.push_str(", "); } @@ -124,7 +253,7 @@ pub fn pretty_ty<'tcx>(ty: TyKind,tcx: TyCtxt<'tcx>) -> String { tuple_str.push_str(")"); tuple_str } - }, + } }, TyKind::Alias(_, _) => format!("{:#?}", ty), TyKind::Param(_) => format!("{:#?}", ty), From ebd9c145f600245ec66d0ddf4c0129874c10ecb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuz=20A=C4=9Fcayaz=C4=B1?= Date: Thu, 9 Nov 2023 12:11:41 +0300 Subject: [PATCH 3/7] better formatting for statements --- Cargo.lock | 1 + compiler/rustc_driver_impl/Cargo.toml | 2 +- compiler/rustc_smir/src/rustc_internal/mod.rs | 6 -- .../rustc_smir/src/rustc_internal/pretty.rs | 63 ++++++++++++++----- compiler/stable_mir/src/mir/visit.rs | 2 +- 5 files changed, 49 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 92bac995bc615..9fe70870140d8 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 e9b5a32842204..545ff32e598ae 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 7957c3ce61703..fa75fd3076ce0 100644 --- a/compiler/rustc_smir/src/rustc_internal/mod.rs +++ b/compiler/rustc_smir/src/rustc_internal/mod.rs @@ -300,10 +300,4 @@ impl Index { 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 ea0ccfe3e4eba..45917630cf3e9 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 d6304d3ea398d..40bedd67352f7 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)); } From c821603484e1f0eb2d314a5e4ca842f38ac6dfab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuz=20A=C4=9Fcayaz=C4=B1?= Date: Tue, 14 Nov 2023 13:06:58 +0300 Subject: [PATCH 4/7] remove unwrap --- .../rustc_smir/src/rustc_internal/pretty.rs | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_internal/pretty.rs b/compiler/rustc_smir/src/rustc_internal/pretty.rs index 45917630cf3e9..67995b1811046 100644 --- a/compiler/rustc_smir/src/rustc_internal/pretty.rs +++ b/compiler/rustc_smir/src/rustc_internal/pretty.rs @@ -12,20 +12,25 @@ 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| { + let _ = items.iter().map(|item| -> io::Result<()> { // 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(); - 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, "{}", function_name(*item, tcx))?; + writeln!(w, "{}", function_body(*item, tcx))?; + let _ = item.body().blocks.iter().enumerate().map(|(index, block)| -> io::Result<()> { + writeln!(w, " bb{}: {{", index)?; + let _ = block.statements.iter().map(|statement| -> io::Result<()> { + writeln!(w, "{}", pretty_statement(&statement.kind, tcx))?; + Ok(()) + }).collect::>(); writeln!(w, " }}").unwrap(); - }) - }) + Ok(()) + }).collect::>(); + Ok(()) + }).collect::>(); + }); Ok(()) } From 3883645a9b228cf5b91f8766692ff57455ad6123 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuz=20A=C4=9Fcayaz=C4=B1?= Date: Tue, 14 Nov 2023 16:21:55 +0300 Subject: [PATCH 5/7] change smir to StableMir --- compiler/rustc_driver_impl/src/pretty.rs | 2 +- compiler/rustc_session/src/config.rs | 14 +++---- .../rustc_smir/src/rustc_internal/pretty.rs | 42 ++++++++++++------- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_driver_impl/src/pretty.rs b/compiler/rustc_driver_impl/src/pretty.rs index 84f8941ff662d..7cd63bc6422c3 100644 --- a/compiler/rustc_driver_impl/src/pretty.rs +++ b/compiler/rustc_driver_impl/src/pretty.rs @@ -326,7 +326,7 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) { write_mir_graphviz(ex.tcx(), None, &mut out).unwrap(); String::from_utf8(out).unwrap() } - Smir => { + StableMir => { let mut out = Vec::new(); write_smir_pretty(ex.tcx(), &mut out).unwrap(); String::from_utf8(out).unwrap() diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 54335645e43e2..d4f9122e7e384 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -2926,13 +2926,13 @@ fn parse_pretty(handler: &EarlyErrorHandler, unstable_opts: &UnstableOptions) -> "thir-tree" => ThirTree, "thir-flat" => ThirFlat, "mir" => Mir, - "smir" => Smir, + "stable-mir" => StableMir, "mir-cfg" => MirCFG, name => handler.early_error(format!( "argument to `unpretty` must be one of `normal`, `identified`, \ `expanded`, `expanded,identified`, `expanded,hygiene`, \ `ast-tree`, `ast-tree,expanded`, `hir`, `hir,identified`, \ - `hir,typed`, `hir-tree`, `thir-tree`, `thir-flat`, `mir` or \ + `hir,typed`, `hir-tree`, `thir-tree`, `thir-flat`, `mir`, `stable-mir`, or \ `mir-cfg`; got {name}" )), }; @@ -3107,8 +3107,8 @@ pub enum PpMode { Mir, /// `-Zunpretty=mir-cfg` MirCFG, - /// `-Zunpretty=smir` - Smir, + /// `-Zunpretty=stable-mir` + StableMir, } impl PpMode { @@ -3126,7 +3126,7 @@ impl PpMode { | ThirFlat | Mir | MirCFG - | Smir => true, + | StableMir => true, } } pub fn needs_hir(&self) -> bool { @@ -3134,13 +3134,13 @@ impl PpMode { match *self { Source(_) | AstTree | AstTreeExpanded => false, - Hir(_) | HirTree | ThirTree | ThirFlat | Mir | MirCFG | Smir => true, + Hir(_) | HirTree | ThirTree | ThirFlat | Mir | MirCFG | StableMir => true, } } pub fn needs_analysis(&self) -> bool { use PpMode::*; - matches!(*self, Hir(PpHirMode::Typed) | Mir | Smir | MirCFG | ThirTree | ThirFlat) + matches!(*self, Hir(PpHirMode::Typed) | Mir | StableMir | MirCFG | ThirTree | ThirFlat) } } diff --git a/compiler/rustc_smir/src/rustc_internal/pretty.rs b/compiler/rustc_smir/src/rustc_internal/pretty.rs index 67995b1811046..1c2662fe85f20 100644 --- a/compiler/rustc_smir/src/rustc_internal/pretty.rs +++ b/compiler/rustc_smir/src/rustc_internal/pretty.rs @@ -15,22 +15,34 @@ pub fn write_smir_pretty<'tcx>(tcx: TyCtxt<'tcx>, w: &mut dyn io::Write) -> io:: run(tcx, || { let items = stable_mir::all_local_items(); - let _ = items.iter().map(|item| -> io::Result<()> { - // Because we can't return a Result from a closure, we have to unwrap here. - writeln!(w, "{}", function_name(*item, tcx))?; - writeln!(w, "{}", function_body(*item, tcx))?; - let _ = item.body().blocks.iter().enumerate().map(|(index, block)| -> io::Result<()> { - writeln!(w, " bb{}: {{", index)?; - let _ = block.statements.iter().map(|statement| -> io::Result<()> { - writeln!(w, "{}", pretty_statement(&statement.kind, tcx))?; - Ok(()) - }).collect::>(); - writeln!(w, " }}").unwrap(); + let _ = items + .iter() + .map(|item| -> io::Result<()> { + // Because we can't return a Result from a closure, we have to unwrap here. + writeln!(w, "{}", function_name(*item, tcx))?; + writeln!(w, "{}", function_body(*item, tcx))?; + let _ = item + .body() + .blocks + .iter() + .enumerate() + .map(|(index, block)| -> io::Result<()> { + writeln!(w, " bb{}: {{", index)?; + let _ = block + .statements + .iter() + .map(|statement| -> io::Result<()> { + writeln!(w, "{}", pretty_statement(&statement.kind, tcx))?; + Ok(()) + }) + .collect::>(); + writeln!(w, " }}").unwrap(); + Ok(()) + }) + .collect::>(); Ok(()) - }).collect::>(); - Ok(()) - }).collect::>(); - + }) + .collect::>(); }); Ok(()) } From 71c990470a958b14b78f9627d4aba2b1d6c79144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuz=20A=C4=9Fcayaz=C4=B1?= Date: Wed, 15 Nov 2023 10:12:17 +0300 Subject: [PATCH 6/7] move pretty into stable_mir --- .../rustc_smir/src/rustc_internal/pretty.rs | 310 +----------------- compiler/rustc_smir/src/rustc_smir/mod.rs | 5 +- compiler/stable_mir/src/lib.rs | 9 +- compiler/stable_mir/src/mir.rs | 1 + compiler/stable_mir/src/mir/body.rs | 27 +- compiler/stable_mir/src/mir/pretty.rs | 261 +++++++++++++++ 6 files changed, 308 insertions(+), 305 deletions(-) create mode 100644 compiler/stable_mir/src/mir/pretty.rs diff --git a/compiler/rustc_smir/src/rustc_internal/pretty.rs b/compiler/rustc_smir/src/rustc_internal/pretty.rs index 1c2662fe85f20..19baf490a5d29 100644 --- a/compiler/rustc_smir/src/rustc_internal/pretty.rs +++ b/compiler/rustc_smir/src/rustc_internal/pretty.rs @@ -1,308 +1,20 @@ use std::io; +use super::run; use rustc_middle::ty::TyCtxt; -use stable_mir::{ - mir::{Mutability, Operand, Rvalue, StatementKind}, - ty::{RigidTy, TyKind}, - CrateItem, -}; - -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(); +pub fn write_smir_pretty<'tcx, W: io::Write>(tcx: TyCtxt<'tcx>, w: &mut W) -> io::Result<()> { + writeln!( + w, + "// WARNING: This is highly experimental output it's intended for stable-mir developers only." + )?; + 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." + )?; run(tcx, || { let items = stable_mir::all_local_items(); - let _ = items - .iter() - .map(|item| -> io::Result<()> { - // Because we can't return a Result from a closure, we have to unwrap here. - writeln!(w, "{}", function_name(*item, tcx))?; - writeln!(w, "{}", function_body(*item, tcx))?; - let _ = item - .body() - .blocks - .iter() - .enumerate() - .map(|(index, block)| -> io::Result<()> { - writeln!(w, " bb{}: {{", index)?; - let _ = block - .statements - .iter() - .map(|statement| -> io::Result<()> { - writeln!(w, "{}", pretty_statement(&statement.kind, tcx))?; - Ok(()) - }) - .collect::>(); - writeln!(w, " }}").unwrap(); - Ok(()) - }) - .collect::>(); - Ok(()) - }) - .collect::>(); + let _ = items.iter().map(|item| -> io::Result<()> { item.dump(w) }).collect::>(); }); Ok(()) } - -pub fn function_name(item: CrateItem, tcx: TyCtxt<'_>) -> String { - let mut name = String::new(); - let body = item.body(); - name.push_str("fn "); - name.push_str(item.name().as_str()); - if body.arg_locals().is_empty() { - name.push_str("()"); - } else { - name.push_str("("); - } - body.arg_locals().iter().for_each(|local| { - name.push_str(format!("_{}: ", local.local).as_str()); - name.push_str(&pretty_ty(local.ty.kind(), tcx)); - }); - if !body.arg_locals().is_empty() { - name.push_str(")"); - } - let return_local = body.ret_local(); - name.push_str(" -> "); - name.push_str(&pretty_ty(return_local.ty.kind(), tcx)); - name.push_str(" {"); - name -} - -pub fn function_body(item: CrateItem, _tcx: TyCtxt<'_>) -> String { - let mut body_str = String::new(); - let body = item.body(); - body.inner_locals().iter().for_each(|local| { - body_str.push_str(" "); - body_str.push_str(format!("let {}", ret_mutability(&local.mutability)).as_str()); - body_str.push_str(format!("_{}: ", local.local).as_str()); - body_str.push_str(format!("{}", pretty_ty(local.ty.kind(), _tcx)).as_str()); - body_str.push_str(";\n"); - }); - body_str.push_str("}"); - body_str -} - -pub fn ret_mutability(mutability: &Mutability) -> String { - match mutability { - Mutability::Not => "".to_string(), - Mutability::Mut => "mut ".to_string(), - } -} - -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(format!("{}", &pretty_rvalue(rval, tcx)).as_str()); - } - StatementKind::FakeRead(_, _) => todo!(), - StatementKind::SetDiscriminant { .. } => todo!(), - StatementKind::Deinit(_) => todo!(), - StatementKind::StorageLive(_) => todo!(), - StatementKind::StorageDead(_) => todo!(), - StatementKind::Retag(_, _) => todo!(), - StatementKind::PlaceMention(_) => todo!(), - StatementKind::AscribeUserType { .. } => todo!(), - StatementKind::Coverage(_) => todo!(), - StatementKind::Intrinsic(_) => todo!(), - StatementKind::ConstEvalCounter => (), - StatementKind::Nop => (), - } - pretty -} - -pub fn pretty_operand(operand: &Operand, _tcx: TyCtxt<'_>) -> String { - let mut pretty = String::new(); - match operand { - Operand::Copy(copy) => { - pretty.push_str(""); - pretty.push_str(format!("{}", copy.local).as_str()); - } - Operand::Move(mv) => { - pretty.push_str("move "); - pretty.push_str(format!("_{}", mv.local).as_str()); - } - Operand::Constant(cnst) => { - pretty.push_str("const "); - pretty.push_str(internal(&cnst.literal).to_string().as_str()); - } - } - pretty -} - -pub fn pretty_rvalue(rval: &Rvalue, tcx: TyCtxt<'_>) -> String { - let mut pretty = String::new(); - match rval { - Rvalue::AddressOf(muta, addr) => { - pretty.push_str("&raw "); - pretty.push_str(&ret_mutability(&muta)); - pretty.push_str(format!("(*_{})", addr.local).as_str()); - } - Rvalue::Aggregate(aggregatekind, operands) => { - pretty.push_str(format!("{:#?}", aggregatekind).as_str()); - pretty.push_str("("); - operands.iter().enumerate().for_each(|(i, op)| { - pretty.push_str(&pretty_operand(op, tcx)); - if i != operands.len() - 1 { - pretty.push_str(", "); - } - }); - pretty.push_str(")"); - } - Rvalue::BinaryOp(bin, op, op2) => { - pretty.push_str(&pretty_operand(op, tcx)); - pretty.push_str(" "); - pretty.push_str(format!("{:#?}", bin).as_str()); - pretty.push_str(" "); - pretty.push_str(&pretty_operand(op2, tcx)); - } - Rvalue::Cast(_, op, ty) => { - pretty.push_str(&pretty_operand(op, tcx)); - pretty.push_str(" as "); - pretty.push_str(&pretty_ty(ty.kind(), tcx)); - } - Rvalue::CheckedBinaryOp(bin, op1, op2) => { - pretty.push_str(&pretty_operand(op1, tcx)); - pretty.push_str(" "); - pretty.push_str(format!("{:#?}", bin).as_str()); - pretty.push_str(" "); - pretty.push_str(&pretty_operand(op2, tcx)); - } - Rvalue::CopyForDeref(deref) => { - pretty.push_str("CopyForDeref"); - pretty.push_str(format!("{}", deref.local).as_str()); - } - Rvalue::Discriminant(place) => { - pretty.push_str("discriminant"); - pretty.push_str(format!("{}", place.local).as_str()); - } - Rvalue::Len(len) => { - pretty.push_str("len"); - pretty.push_str(format!("{}", len.local).as_str()); - } - Rvalue::Ref(_, borrowkind, place) => { - pretty.push_str("ref"); - pretty.push_str(format!("{:#?}", borrowkind).as_str()); - pretty.push_str(format!("{}", place.local).as_str()); - } - Rvalue::Repeat(op, cnst) => { - pretty.push_str(&pretty_operand(op, tcx)); - pretty.push_str(" "); - pretty.push_str(&pretty_ty(cnst.ty().kind(), tcx)); - } - Rvalue::ShallowInitBox(_, _) => todo!(), - Rvalue::ThreadLocalRef(item) => { - pretty.push_str("thread_local_ref"); - pretty.push_str(format!("{:#?}", item).as_str()); - } - Rvalue::NullaryOp(nul, ty) => { - pretty.push_str(format!("{:#?}", nul).as_str()); - pretty.push_str(&&pretty_ty(ty.kind(), tcx)); - pretty.push_str(" "); - } - Rvalue::UnaryOp(un, op) => { - pretty.push_str(&pretty_operand(op, tcx)); - pretty.push_str(" "); - pretty.push_str(format!("{:#?}", un).as_str()); - } - Rvalue::Use(op) => pretty.push_str(&pretty_operand(op, tcx)), - } - pretty -} - -pub fn pretty_ty(ty: TyKind, tcx: TyCtxt<'_>) -> String { - let mut pretty = String::new(); - pretty.push_str(""); - match ty { - TyKind::RigidTy(rigid_ty) => match rigid_ty { - RigidTy::Bool => "bool".to_string(), - RigidTy::Char => "char".to_string(), - RigidTy::Int(i) => match i { - stable_mir::ty::IntTy::Isize => "isize".to_string(), - stable_mir::ty::IntTy::I8 => "i8".to_string(), - stable_mir::ty::IntTy::I16 => "i16".to_string(), - stable_mir::ty::IntTy::I32 => "i32".to_string(), - stable_mir::ty::IntTy::I64 => "i64".to_string(), - stable_mir::ty::IntTy::I128 => "i128".to_string(), - }, - RigidTy::Uint(u) => match u { - stable_mir::ty::UintTy::Usize => "usize".to_string(), - stable_mir::ty::UintTy::U8 => "u8".to_string(), - stable_mir::ty::UintTy::U16 => "u16".to_string(), - stable_mir::ty::UintTy::U32 => "u32".to_string(), - stable_mir::ty::UintTy::U64 => "u64".to_string(), - stable_mir::ty::UintTy::U128 => "u128".to_string(), - }, - RigidTy::Float(f) => match f { - stable_mir::ty::FloatTy::F32 => "f32".to_string(), - stable_mir::ty::FloatTy::F64 => "f64".to_string(), - }, - RigidTy::Adt(def, _) => { - format!("{}", tcx.type_of(internal(&def.0)).instantiate_identity()) - } - RigidTy::Foreign(_) => format!("{:#?}", rigid_ty), - RigidTy::Str => "str".to_string(), - 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::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(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() { - "()".to_string() - } else { - let mut tuple_str = String::new(); - tuple_str.push_str("("); - tuple.iter().enumerate().for_each(|(i, ty)| { - tuple_str.push_str(&pretty_ty(ty.kind(), tcx)); - if i != tuple.len() - 1 { - tuple_str.push_str(", "); - } - }); - tuple_str.push_str(")"); - tuple_str - } - } - }, - TyKind::Alias(_, _) => format!("{:#?}", ty), - TyKind::Param(param_ty) => { - format!("{:#?}", param_ty.name) - } - TyKind::Bound(_, _) => format!("{:#?}", ty), - } -} diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 69b0c0bb80d55..69e556ff3ec5a 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -400,11 +400,10 @@ impl<'tcx> Stable<'tcx> for mir::Body<'tcx> { }) .collect(), self.local_decls - .iter_enumerated() - .map(|(local, decl)| stable_mir::mir::LocalDecl { + .iter() + .map(|decl| stable_mir::mir::LocalDecl { ty: decl.ty.stable(tables), span: decl.source_info.span.stable(tables), - local: local.as_usize(), mutability: decl.mutability.stable(tables), }) .collect(), diff --git a/compiler/stable_mir/src/lib.rs b/compiler/stable_mir/src/lib.rs index 0262fb536e785..dca43a2c090d7 100644 --- a/compiler/stable_mir/src/lib.rs +++ b/compiler/stable_mir/src/lib.rs @@ -19,9 +19,9 @@ use crate::mir::mono::InstanceDef; use crate::mir::Body; -use std::cell::Cell; use std::fmt; use std::fmt::Debug; +use std::{cell::Cell, io}; use self::ty::{ GenericPredicates, Generics, ImplDef, ImplTrait, IndexedVal, LineInfo, Span, TraitDecl, @@ -37,6 +37,8 @@ pub mod ty; pub mod visitor; use crate::ty::{AdtDef, AdtKind, ClosureDef, ClosureKind}; +use crate::mir::pretty::function_name; +use crate::mir::Mutability; pub use error::*; use mir::mono::Instance; use ty::{FnDef, GenericArgs}; @@ -137,6 +139,11 @@ impl CrateItem { pub fn ty(&self) -> Ty { with(|cx| cx.def_ty(self.0)) } + + pub fn dump(&self, w: &mut W) -> io::Result<()> { + writeln!(w, "{}", function_name(*self))?; + self.body().dump(w) + } } /// Return the function where execution starts if the current diff --git a/compiler/stable_mir/src/mir.rs b/compiler/stable_mir/src/mir.rs index 2e1714b49c184..2cbe6eb4ad117 100644 --- a/compiler/stable_mir/src/mir.rs +++ b/compiler/stable_mir/src/mir.rs @@ -1,5 +1,6 @@ mod body; pub mod mono; +pub mod pretty; pub mod visit; pub use body::*; diff --git a/compiler/stable_mir/src/mir/body.rs b/compiler/stable_mir/src/mir/body.rs index 2981d6a8bb5c6..b1ba8b97418e7 100644 --- a/compiler/stable_mir/src/mir/body.rs +++ b/compiler/stable_mir/src/mir/body.rs @@ -1,7 +1,8 @@ +use crate::mir::pretty::{function_body, pretty_statement}; use crate::ty::{AdtDef, ClosureDef, Const, CoroutineDef, GenericArgs, Movability, Region, Ty}; use crate::Opaque; use crate::Span; - +use std::io; /// The SMIR representation of a single function. #[derive(Clone, Debug)] pub struct Body { @@ -56,6 +57,29 @@ impl Body { pub fn locals(&self) -> &[LocalDecl] { &self.locals } + + pub fn dump(&self, w: &mut W) -> io::Result<()> { + writeln!(w, "{}", function_body(self))?; + let _ = self + .blocks + .iter() + .enumerate() + .map(|(index, block)| -> io::Result<()> { + writeln!(w, " bb{}: {{", index)?; + let _ = block + .statements + .iter() + .map(|statement| -> io::Result<()> { + writeln!(w, "{}", pretty_statement(&statement.kind))?; + Ok(()) + }) + .collect::>(); + writeln!(w, " }}").unwrap(); + Ok(()) + }) + .collect::>(); + Ok(()) + } } type LocalDecls = Vec; @@ -64,7 +88,6 @@ type LocalDecls = Vec; pub struct LocalDecl { pub ty: Ty, pub span: Span, - pub local: Local, pub mutability: Mutability, } diff --git a/compiler/stable_mir/src/mir/pretty.rs b/compiler/stable_mir/src/mir/pretty.rs new file mode 100644 index 0000000000000..2bdcabb1bf522 --- /dev/null +++ b/compiler/stable_mir/src/mir/pretty.rs @@ -0,0 +1,261 @@ +use crate::mir::{Operand, Rvalue, StatementKind}; +use crate::ty::{DynKind, FloatTy, IntTy, RigidTy, TyKind, UintTy}; +use crate::{Body, CrateItem, Mutability}; + +pub fn function_name(item: CrateItem) -> String { + let mut pretty_name = String::new(); + let body = item.body(); + pretty_name.push_str("fn "); + pretty_name.push_str(item.name().as_str()); + if body.arg_locals().is_empty() { + pretty_name.push_str("()"); + } else { + pretty_name.push_str("("); + } + body.arg_locals().iter().enumerate().for_each(|(index, local)| { + pretty_name.push_str(format!("_{}: ", index).as_str()); + pretty_name.push_str(&pretty_ty(local.ty.kind())); + }); + if !body.arg_locals().is_empty() { + pretty_name.push_str(")"); + } + let return_local = body.ret_local(); + pretty_name.push_str(" -> "); + pretty_name.push_str(&pretty_ty(return_local.ty.kind())); + pretty_name.push_str(" {"); + pretty_name +} + +pub fn function_body(body: &Body) -> String { + let mut pretty_body = String::new(); + body.inner_locals().iter().enumerate().for_each(|(index, local)| { + pretty_body.push_str(" "); + pretty_body.push_str(format!("let {}", ret_mutability(&local.mutability)).as_str()); + pretty_body.push_str(format!("_{}: ", index).as_str()); + pretty_body.push_str(format!("{}", pretty_ty(local.ty.kind())).as_str()); + pretty_body.push_str(";\n"); + }); + pretty_body.push_str("}"); + pretty_body +} + +pub fn ret_mutability(mutability: &Mutability) -> String { + match mutability { + Mutability::Not => "".to_string(), + Mutability::Mut => "mut ".to_string(), + } +} + +pub fn pretty_statement(statement: &StatementKind) -> String { + let mut pretty = String::new(); + match statement { + StatementKind::Assign(place, rval) => { + pretty.push_str(format!(" _{} = ", place.local).as_str()); + pretty.push_str(format!("{}", &pretty_rvalue(rval)).as_str()); + } + StatementKind::FakeRead(_, _) => todo!(), + StatementKind::SetDiscriminant { .. } => todo!(), + StatementKind::Deinit(_) => todo!(), + StatementKind::StorageLive(_) => todo!(), + StatementKind::StorageDead(_) => todo!(), + StatementKind::Retag(_, _) => todo!(), + StatementKind::PlaceMention(_) => todo!(), + StatementKind::AscribeUserType { .. } => todo!(), + StatementKind::Coverage(_) => todo!(), + StatementKind::Intrinsic(_) => todo!(), + StatementKind::ConstEvalCounter => (), + StatementKind::Nop => (), + } + pretty +} + +pub fn pretty_operand(operand: &Operand) -> String { + let mut pretty = String::new(); + match operand { + Operand::Copy(copy) => { + pretty.push_str(""); + pretty.push_str(format!("{}", copy.local).as_str()); + } + Operand::Move(mv) => { + pretty.push_str("move "); + pretty.push_str(format!("_{}", mv.local).as_str()); + } + Operand::Constant(_) => { + // FIXME: Once https://github.com/rust-lang/rust/pull/117688 we will have ability to replace this + pretty.push_str("const "); + //pretty.push_str(internal(&cnst.literal).to_string().as_str()); + } + } + pretty +} + +pub fn pretty_rvalue(rval: &Rvalue) -> String { + let mut pretty = String::new(); + match rval { + Rvalue::AddressOf(muta, addr) => { + pretty.push_str("&raw "); + pretty.push_str(&ret_mutability(&muta)); + pretty.push_str(format!("(*_{})", addr.local).as_str()); + } + Rvalue::Aggregate(aggregatekind, operands) => { + pretty.push_str(format!("{:#?}", aggregatekind).as_str()); + pretty.push_str("("); + operands.iter().enumerate().for_each(|(i, op)| { + pretty.push_str(&pretty_operand(op)); + if i != operands.len() - 1 { + pretty.push_str(", "); + } + }); + pretty.push_str(")"); + } + Rvalue::BinaryOp(bin, op, op2) => { + pretty.push_str(&pretty_operand(op)); + pretty.push_str(" "); + pretty.push_str(format!("{:#?}", bin).as_str()); + pretty.push_str(" "); + pretty.push_str(&pretty_operand(op2)); + } + Rvalue::Cast(_, op, ty) => { + pretty.push_str(&pretty_operand(op)); + pretty.push_str(" as "); + pretty.push_str(&pretty_ty(ty.kind())); + } + Rvalue::CheckedBinaryOp(bin, op1, op2) => { + pretty.push_str(&pretty_operand(op1)); + pretty.push_str(" "); + pretty.push_str(format!("{:#?}", bin).as_str()); + pretty.push_str(" "); + pretty.push_str(&pretty_operand(op2)); + } + Rvalue::CopyForDeref(deref) => { + pretty.push_str("CopyForDeref"); + pretty.push_str(format!("{}", deref.local).as_str()); + } + Rvalue::Discriminant(place) => { + pretty.push_str("discriminant"); + pretty.push_str(format!("{}", place.local).as_str()); + } + Rvalue::Len(len) => { + pretty.push_str("len"); + pretty.push_str(format!("{}", len.local).as_str()); + } + Rvalue::Ref(_, borrowkind, place) => { + pretty.push_str("ref"); + pretty.push_str(format!("{:#?}", borrowkind).as_str()); + pretty.push_str(format!("{}", place.local).as_str()); + } + Rvalue::Repeat(op, cnst) => { + pretty.push_str(&pretty_operand(op)); + pretty.push_str(" "); + pretty.push_str(&pretty_ty(cnst.ty().kind())); + } + Rvalue::ShallowInitBox(_, _) => todo!(), + Rvalue::ThreadLocalRef(item) => { + pretty.push_str("thread_local_ref"); + pretty.push_str(format!("{:#?}", item).as_str()); + } + Rvalue::NullaryOp(nul, ty) => { + pretty.push_str(format!("{:#?}", nul).as_str()); + pretty.push_str(&&pretty_ty(ty.kind())); + pretty.push_str(" "); + } + Rvalue::UnaryOp(un, op) => { + pretty.push_str(&pretty_operand(op)); + pretty.push_str(" "); + pretty.push_str(format!("{:#?}", un).as_str()); + } + Rvalue::Use(op) => pretty.push_str(&pretty_operand(op)), + } + pretty +} + +pub fn pretty_ty(ty: TyKind) -> String { + let mut pretty = String::new(); + pretty.push_str(""); + match ty { + TyKind::RigidTy(rigid_ty) => match rigid_ty { + RigidTy::Bool => "bool".to_string(), + RigidTy::Char => "char".to_string(), + RigidTy::Int(i) => match i { + IntTy::Isize => "isize".to_string(), + IntTy::I8 => "i8".to_string(), + IntTy::I16 => "i16".to_string(), + IntTy::I32 => "i32".to_string(), + IntTy::I64 => "i64".to_string(), + IntTy::I128 => "i128".to_string(), + }, + RigidTy::Uint(u) => match u { + UintTy::Usize => "usize".to_string(), + UintTy::U8 => "u8".to_string(), + UintTy::U16 => "u16".to_string(), + UintTy::U32 => "u32".to_string(), + UintTy::U64 => "u64".to_string(), + UintTy::U128 => "u128".to_string(), + }, + RigidTy::Float(f) => match f { + FloatTy::F32 => "f32".to_string(), + FloatTy::F64 => "f64".to_string(), + }, + RigidTy::Adt(_, _) => { + // FIXME: Once https://github.com/rust-lang/rust/pull/117688 we will have ability to replace this + format!("{rigid_ty:#?}") + } + RigidTy::Str => "str".to_string(), + RigidTy::Array(ty, len) => { + // FIXME: Once https://github.com/rust-lang/rust/pull/117688 we will have ability to replace this + format!("[{}; {:#?}]", pretty_ty(ty.kind()), len) + } + RigidTy::Slice(ty) => { + format!("[{}]", pretty_ty(ty.kind())) + } + 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())); + pretty + } + RigidTy::Ref(_, ty, _) => pretty_ty(ty.kind()), + RigidTy::FnDef(_, _) => format!("{:#?}", rigid_ty), + RigidTy::FnPtr(_) => format!("{:#?}", rigid_ty), + RigidTy::Closure(_, _) => format!("{:#?}", rigid_ty), + RigidTy::Coroutine(_, _, _) => format!("{:#?}", rigid_ty), + RigidTy::Dynamic(data, region, repr) => { + // FIXME: Fix binder printing, it looks ugly now + pretty.push_str("("); + match repr { + DynKind::Dyn => pretty.push_str("dyn "), + 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() { + "()".to_string() + } else { + let mut tuple_str = String::new(); + tuple_str.push_str("("); + tuple.iter().enumerate().for_each(|(i, ty)| { + tuple_str.push_str(&pretty_ty(ty.kind())); + if i != tuple.len() - 1 { + tuple_str.push_str(", "); + } + }); + tuple_str.push_str(")"); + tuple_str + } + } + _ => format!("{:#?}", rigid_ty), + }, + TyKind::Alias(_, _) => format!("{:#?}", ty), + TyKind::Param(param_ty) => { + format!("{:#?}", param_ty.name) + } + TyKind::Bound(_, _) => format!("{:#?}", ty), + } +} From 92657f163ac3c30af2652c8c7ba5ee7edb14ba01 Mon Sep 17 00:00:00 2001 From: ouz-a Date: Fri, 17 Nov 2023 14:03:54 +0300 Subject: [PATCH 7/7] use new apis and add new function --- compiler/rustc_smir/src/rustc_internal/pretty.rs | 2 +- compiler/rustc_smir/src/rustc_smir/mod.rs | 6 +++++- compiler/stable_mir/src/lib.rs | 9 ++++++--- compiler/stable_mir/src/mir/body.rs | 5 ++--- compiler/stable_mir/src/mir/pretty.rs | 15 ++++++--------- 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_internal/pretty.rs b/compiler/rustc_smir/src/rustc_internal/pretty.rs index 19baf490a5d29..3ef2d28ea4734 100644 --- a/compiler/rustc_smir/src/rustc_internal/pretty.rs +++ b/compiler/rustc_smir/src/rustc_internal/pretty.rs @@ -12,7 +12,7 @@ pub fn write_smir_pretty<'tcx, W: io::Write>(tcx: TyCtxt<'tcx>, w: &mut W) -> io w, "// If you find a bug or want to improve the output open a issue at https://github.com/rust-lang/project-stable-mir." )?; - run(tcx, || { + let _ = run(tcx, || { let items = stable_mir::all_local_items(); let _ = items.iter().map(|item| -> io::Result<()> { item.dump(w) }).collect::>(); }); diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 69e556ff3ec5a..89dbf40c7b4d0 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -7,7 +7,7 @@ //! //! For now, we are developing everything inside `rustc`, thus, we keep this module private. -use crate::rustc_internal::{IndexMap, RustcInternal}; +use crate::rustc_internal::{internal, IndexMap, RustcInternal}; use crate::rustc_smir::stable_mir::ty::{BoundRegion, Region}; use rustc_hir as hir; use rustc_hir::def::DefKind; @@ -105,6 +105,10 @@ impl<'tcx> Context for TablesWrapper<'tcx> { tables.tcx.type_of(item.internal(&mut *tables)).instantiate_identity().stable(&mut *tables) } + fn const_literal(&self, cnst: &stable_mir::ty::Const) -> String { + internal(cnst).to_string() + } + fn span_of_an_item(&self, def_id: stable_mir::DefId) -> Span { let mut tables = self.0.borrow_mut(); tables.tcx.def_span(tables[def_id]).stable(&mut *tables) diff --git a/compiler/stable_mir/src/lib.rs b/compiler/stable_mir/src/lib.rs index dca43a2c090d7..79102dcce3585 100644 --- a/compiler/stable_mir/src/lib.rs +++ b/compiler/stable_mir/src/lib.rs @@ -36,12 +36,12 @@ pub mod mir; pub mod ty; pub mod visitor; -use crate::ty::{AdtDef, AdtKind, ClosureDef, ClosureKind}; use crate::mir::pretty::function_name; use crate::mir::Mutability; +use crate::ty::{AdtDef, AdtKind, ClosureDef, ClosureKind}; pub use error::*; use mir::mono::Instance; -use ty::{FnDef, GenericArgs}; +use ty::{Const, FnDef, GenericArgs}; /// Use String for now but we should replace it. pub type Symbol = String; @@ -139,7 +139,7 @@ impl CrateItem { pub fn ty(&self) -> Ty { with(|cx| cx.def_ty(self.0)) } - + pub fn dump(&self, w: &mut W) -> io::Result<()> { writeln!(w, "{}", function_name(*self))?; self.body().dump(w) @@ -230,6 +230,9 @@ pub trait Context { /// Returns the type of given crate item. fn def_ty(&self, item: DefId) -> Ty; + /// Returns literal value of a const as a string. + fn const_literal(&self, cnst: &Const) -> String; + /// `Span` of an item fn span_of_an_item(&self, def_id: DefId) -> Span; diff --git a/compiler/stable_mir/src/mir/body.rs b/compiler/stable_mir/src/mir/body.rs index b1ba8b97418e7..fa58a7ffe1554 100644 --- a/compiler/stable_mir/src/mir/body.rs +++ b/compiler/stable_mir/src/mir/body.rs @@ -60,8 +60,7 @@ impl Body { pub fn dump(&self, w: &mut W) -> io::Result<()> { writeln!(w, "{}", function_body(self))?; - let _ = self - .blocks + self.blocks .iter() .enumerate() .map(|(index, block)| -> io::Result<()> { @@ -77,7 +76,7 @@ impl Body { writeln!(w, " }}").unwrap(); Ok(()) }) - .collect::>(); + .collect::, _>>()?; Ok(()) } } diff --git a/compiler/stable_mir/src/mir/pretty.rs b/compiler/stable_mir/src/mir/pretty.rs index 2bdcabb1bf522..e52c3360ce425 100644 --- a/compiler/stable_mir/src/mir/pretty.rs +++ b/compiler/stable_mir/src/mir/pretty.rs @@ -1,6 +1,6 @@ use crate::mir::{Operand, Rvalue, StatementKind}; use crate::ty::{DynKind, FloatTy, IntTy, RigidTy, TyKind, UintTy}; -use crate::{Body, CrateItem, Mutability}; +use crate::{with, Body, CrateItem, Mutability}; pub fn function_name(item: CrateItem) -> String { let mut pretty_name = String::new(); @@ -80,10 +80,9 @@ pub fn pretty_operand(operand: &Operand) -> String { pretty.push_str("move "); pretty.push_str(format!("_{}", mv.local).as_str()); } - Operand::Constant(_) => { - // FIXME: Once https://github.com/rust-lang/rust/pull/117688 we will have ability to replace this + Operand::Constant(cnst) => { pretty.push_str("const "); - //pretty.push_str(internal(&cnst.literal).to_string().as_str()); + pretty.push_str(with(|cx| cx.const_literal(&cnst.literal)).as_str()); } } pretty @@ -196,14 +195,12 @@ pub fn pretty_ty(ty: TyKind) -> String { FloatTy::F32 => "f32".to_string(), FloatTy::F64 => "f64".to_string(), }, - RigidTy::Adt(_, _) => { - // FIXME: Once https://github.com/rust-lang/rust/pull/117688 we will have ability to replace this - format!("{rigid_ty:#?}") + RigidTy::Adt(def, _) => { + format!("{:#?}", with(|cx| cx.def_ty(def.0))) } RigidTy::Str => "str".to_string(), RigidTy::Array(ty, len) => { - // FIXME: Once https://github.com/rust-lang/rust/pull/117688 we will have ability to replace this - format!("[{}; {:#?}]", pretty_ty(ty.kind()), len) + format!("[{}; {}]", pretty_ty(ty.kind()), with(|cx| cx.const_literal(&len))) } RigidTy::Slice(ty) => { format!("[{}]", pretty_ty(ty.kind()))