Skip to content

Commit

Permalink
use new apis and add new function
Browse files Browse the repository at this point in the history
  • Loading branch information
ouz-a committed Nov 17, 2023
1 parent 71c9904 commit 92657f1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_smir/src/rustc_internal/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
});
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions compiler/stable_mir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -139,7 +139,7 @@ impl CrateItem {
pub fn ty(&self) -> Ty {
with(|cx| cx.def_ty(self.0))
}

pub fn dump<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
writeln!(w, "{}", function_name(*self))?;
self.body().dump(w)
Expand Down Expand Up @@ -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;

Expand Down
5 changes: 2 additions & 3 deletions compiler/stable_mir/src/mir/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ impl Body {

pub fn dump<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
writeln!(w, "{}", function_body(self))?;
let _ = self
.blocks
self.blocks
.iter()
.enumerate()
.map(|(index, block)| -> io::Result<()> {
Expand All @@ -77,7 +76,7 @@ impl Body {
writeln!(w, " }}").unwrap();
Ok(())
})
.collect::<Vec<_>>();
.collect::<Result<Vec<_>, _>>()?;
Ok(())
}
}
Expand Down
15 changes: 6 additions & 9 deletions compiler/stable_mir/src/mir/pretty.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()))
Expand Down

0 comments on commit 92657f1

Please sign in to comment.