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()))