use new apis and add new function

This commit is contained in:
ouz-a 2023-11-17 14:03:54 +03:00
parent 71c990470a
commit 92657f163a
5 changed files with 20 additions and 17 deletions

View File

@ -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<_>>();
});

View File

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

View File

@ -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<W: io::Write>(&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;

View File

@ -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<()> {
@ -77,7 +76,7 @@ impl Body {
writeln!(w, " }}").unwrap();
Ok(())
})
.collect::<Vec<_>>();
.collect::<Result<Vec<_>, _>>()?;
Ok(())
}
}

View File

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