Skip to content

Commit 92657f1

Browse files
committed
use new apis and add new function
1 parent 71c9904 commit 92657f1

File tree

5 files changed

+20
-17
lines changed

5 files changed

+20
-17
lines changed

compiler/rustc_smir/src/rustc_internal/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn write_smir_pretty<'tcx, W: io::Write>(tcx: TyCtxt<'tcx>, w: &mut W) -> io
1212
w,
1313
"// If you find a bug or want to improve the output open a issue at https://github.com/rust-lang/project-stable-mir."
1414
)?;
15-
run(tcx, || {
15+
let _ = run(tcx, || {
1616
let items = stable_mir::all_local_items();
1717
let _ = items.iter().map(|item| -> io::Result<()> { item.dump(w) }).collect::<Vec<_>>();
1818
});

compiler/rustc_smir/src/rustc_smir/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//!
88
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
10-
use crate::rustc_internal::{IndexMap, RustcInternal};
10+
use crate::rustc_internal::{internal, IndexMap, RustcInternal};
1111
use crate::rustc_smir::stable_mir::ty::{BoundRegion, Region};
1212
use rustc_hir as hir;
1313
use rustc_hir::def::DefKind;
@@ -105,6 +105,10 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
105105
tables.tcx.type_of(item.internal(&mut *tables)).instantiate_identity().stable(&mut *tables)
106106
}
107107

108+
fn const_literal(&self, cnst: &stable_mir::ty::Const) -> String {
109+
internal(cnst).to_string()
110+
}
111+
108112
fn span_of_an_item(&self, def_id: stable_mir::DefId) -> Span {
109113
let mut tables = self.0.borrow_mut();
110114
tables.tcx.def_span(tables[def_id]).stable(&mut *tables)

compiler/stable_mir/src/lib.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ pub mod mir;
3636
pub mod ty;
3737
pub mod visitor;
3838

39-
use crate::ty::{AdtDef, AdtKind, ClosureDef, ClosureKind};
4039
use crate::mir::pretty::function_name;
4140
use crate::mir::Mutability;
41+
use crate::ty::{AdtDef, AdtKind, ClosureDef, ClosureKind};
4242
pub use error::*;
4343
use mir::mono::Instance;
44-
use ty::{FnDef, GenericArgs};
44+
use ty::{Const, FnDef, GenericArgs};
4545

4646
/// Use String for now but we should replace it.
4747
pub type Symbol = String;
@@ -139,7 +139,7 @@ impl CrateItem {
139139
pub fn ty(&self) -> Ty {
140140
with(|cx| cx.def_ty(self.0))
141141
}
142-
142+
143143
pub fn dump<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
144144
writeln!(w, "{}", function_name(*self))?;
145145
self.body().dump(w)
@@ -230,6 +230,9 @@ pub trait Context {
230230
/// Returns the type of given crate item.
231231
fn def_ty(&self, item: DefId) -> Ty;
232232

233+
/// Returns literal value of a const as a string.
234+
fn const_literal(&self, cnst: &Const) -> String;
235+
233236
/// `Span` of an item
234237
fn span_of_an_item(&self, def_id: DefId) -> Span;
235238

compiler/stable_mir/src/mir/body.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ impl Body {
6060

6161
pub fn dump<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
6262
writeln!(w, "{}", function_body(self))?;
63-
let _ = self
64-
.blocks
63+
self.blocks
6564
.iter()
6665
.enumerate()
6766
.map(|(index, block)| -> io::Result<()> {
@@ -77,7 +76,7 @@ impl Body {
7776
writeln!(w, " }}").unwrap();
7877
Ok(())
7978
})
80-
.collect::<Vec<_>>();
79+
.collect::<Result<Vec<_>, _>>()?;
8180
Ok(())
8281
}
8382
}

compiler/stable_mir/src/mir/pretty.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::mir::{Operand, Rvalue, StatementKind};
22
use crate::ty::{DynKind, FloatTy, IntTy, RigidTy, TyKind, UintTy};
3-
use crate::{Body, CrateItem, Mutability};
3+
use crate::{with, Body, CrateItem, Mutability};
44

55
pub fn function_name(item: CrateItem) -> String {
66
let mut pretty_name = String::new();
@@ -80,10 +80,9 @@ pub fn pretty_operand(operand: &Operand) -> String {
8080
pretty.push_str("move ");
8181
pretty.push_str(format!("_{}", mv.local).as_str());
8282
}
83-
Operand::Constant(_) => {
84-
// FIXME: Once https://github.com/rust-lang/rust/pull/117688 we will have ability to replace this
83+
Operand::Constant(cnst) => {
8584
pretty.push_str("const ");
86-
//pretty.push_str(internal(&cnst.literal).to_string().as_str());
85+
pretty.push_str(with(|cx| cx.const_literal(&cnst.literal)).as_str());
8786
}
8887
}
8988
pretty
@@ -196,14 +195,12 @@ pub fn pretty_ty(ty: TyKind) -> String {
196195
FloatTy::F32 => "f32".to_string(),
197196
FloatTy::F64 => "f64".to_string(),
198197
},
199-
RigidTy::Adt(_, _) => {
200-
// FIXME: Once https://github.com/rust-lang/rust/pull/117688 we will have ability to replace this
201-
format!("{rigid_ty:#?}")
198+
RigidTy::Adt(def, _) => {
199+
format!("{:#?}", with(|cx| cx.def_ty(def.0)))
202200
}
203201
RigidTy::Str => "str".to_string(),
204202
RigidTy::Array(ty, len) => {
205-
// FIXME: Once https://github.com/rust-lang/rust/pull/117688 we will have ability to replace this
206-
format!("[{}; {:#?}]", pretty_ty(ty.kind()), len)
203+
format!("[{}; {}]", pretty_ty(ty.kind()), with(|cx| cx.const_literal(&len)))
207204
}
208205
RigidTy::Slice(ty) => {
209206
format!("[{}]", pretty_ty(ty.kind()))

0 commit comments

Comments
 (0)