Skip to content

Commit 05170ac

Browse files
authored
Unrolled build for rust-lang#122801
Rollup merge of rust-lang#122801 - celinval:smir-pretty, r=compiler-errors Fix misc printing issues in emit=stable_mir Trying to continue the work that ````@ouz-a```` started here: rust-lang#118364 Few modifications beyond fixes: 1. I made the `pretty_*` functions private. 2. I added a function to print the instance body 3. Changed a bunch of signatures to write to the writer directly. 4. Added a function to translate the place to its internal representation, so we could use the internal debug implementation. 5. Also removed `pretty_ty`, replaced by Display implementation of Ty which uses the internal display.
2 parents 03994e4 + ebacf7a commit 05170ac

File tree

10 files changed

+355
-560
lines changed

10 files changed

+355
-560
lines changed

compiler/rustc_smir/src/rustc_internal/internal.rs

+45-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_span::Symbol;
1010
use stable_mir::abi::Layout;
1111
use stable_mir::mir::alloc::AllocId;
1212
use stable_mir::mir::mono::{Instance, MonoItem, StaticDef};
13-
use stable_mir::mir::{Mutability, Safety};
13+
use stable_mir::mir::{Mutability, Place, ProjectionElem, Safety};
1414
use stable_mir::ty::{
1515
Abi, AdtDef, Binder, BoundRegionKind, BoundTyKind, BoundVariableKind, ClosureKind, Const,
1616
DynKind, ExistentialPredicate, ExistentialProjection, ExistentialTraitRef, FloatTy, FnSig,
@@ -492,6 +492,50 @@ impl RustcInternal for Layout {
492492
}
493493
}
494494

495+
impl RustcInternal for Place {
496+
type T<'tcx> = rustc_middle::mir::Place<'tcx>;
497+
498+
fn internal<'tcx>(&self, tables: &mut Tables<'_>, tcx: TyCtxt<'tcx>) -> Self::T<'tcx> {
499+
rustc_middle::mir::Place {
500+
local: rustc_middle::mir::Local::from_usize(self.local),
501+
projection: tcx.mk_place_elems(&self.projection.internal(tables, tcx)),
502+
}
503+
}
504+
}
505+
506+
impl RustcInternal for ProjectionElem {
507+
type T<'tcx> = rustc_middle::mir::PlaceElem<'tcx>;
508+
509+
fn internal<'tcx>(&self, tables: &mut Tables<'_>, tcx: TyCtxt<'tcx>) -> Self::T<'tcx> {
510+
match self {
511+
ProjectionElem::Deref => rustc_middle::mir::PlaceElem::Deref,
512+
ProjectionElem::Field(idx, ty) => {
513+
rustc_middle::mir::PlaceElem::Field((*idx).into(), ty.internal(tables, tcx))
514+
}
515+
ProjectionElem::Index(idx) => rustc_middle::mir::PlaceElem::Index((*idx).into()),
516+
ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
517+
rustc_middle::mir::PlaceElem::ConstantIndex {
518+
offset: *offset,
519+
min_length: *min_length,
520+
from_end: *from_end,
521+
}
522+
}
523+
ProjectionElem::Subslice { from, to, from_end } => {
524+
rustc_middle::mir::PlaceElem::Subslice { from: *from, to: *to, from_end: *from_end }
525+
}
526+
ProjectionElem::Downcast(idx) => {
527+
rustc_middle::mir::PlaceElem::Downcast(None, idx.internal(tables, tcx))
528+
}
529+
ProjectionElem::OpaqueCast(ty) => {
530+
rustc_middle::mir::PlaceElem::OpaqueCast(ty.internal(tables, tcx))
531+
}
532+
ProjectionElem::Subtype(ty) => {
533+
rustc_middle::mir::PlaceElem::Subtype(ty.internal(tables, tcx))
534+
}
535+
}
536+
}
537+
}
538+
495539
impl<T> RustcInternal for &T
496540
where
497541
T: RustcInternal,

compiler/rustc_smir/src/rustc_internal/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn write_smir_pretty<'tcx, W: io::Write>(tcx: TyCtxt<'tcx>, w: &mut W) -> io
1414
)?;
1515
let _ = run(tcx, || {
1616
let items = stable_mir::all_local_items();
17-
let _ = items.iter().map(|item| -> io::Result<()> { item.dump(w) }).collect::<Vec<_>>();
17+
let _ = items.iter().map(|item| -> io::Result<()> { item.emit_mir(w) }).collect::<Vec<_>>();
1818
});
1919
Ok(())
2020
}

compiler/rustc_smir/src/rustc_smir/context.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use stable_mir::abi::{FnAbi, Layout, LayoutShape};
1919
use stable_mir::compiler_interface::Context;
2020
use stable_mir::mir::alloc::GlobalAlloc;
2121
use stable_mir::mir::mono::{InstanceDef, StaticDef};
22-
use stable_mir::mir::Body;
22+
use stable_mir::mir::{Body, Place};
2323
use stable_mir::target::{MachineInfo, MachineSize};
2424
use stable_mir::ty::{
2525
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, ForeignDef,
@@ -423,7 +423,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
423423
def_ty.instantiate(tables.tcx, args).stable(&mut *tables)
424424
}
425425

426-
fn const_literal(&self, cnst: &stable_mir::ty::Const) -> String {
426+
fn const_pretty(&self, cnst: &stable_mir::ty::Const) -> String {
427427
let mut tables = self.0.borrow_mut();
428428
let tcx = tables.tcx;
429429
cnst.internal(&mut *tables, tcx).to_string()
@@ -434,6 +434,11 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
434434
tables.tcx.def_span(tables[def_id]).stable(&mut *tables)
435435
}
436436

437+
fn ty_pretty(&self, ty: stable_mir::ty::Ty) -> String {
438+
let tables = self.0.borrow_mut();
439+
tables.types[ty].to_string()
440+
}
441+
437442
fn ty_kind(&self, ty: stable_mir::ty::Ty) -> TyKind {
438443
let mut tables = self.0.borrow_mut();
439444
tables.types[ty].kind().stable(&mut *tables)
@@ -654,6 +659,12 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
654659
let tcx = tables.tcx;
655660
id.internal(&mut *tables, tcx).0.stable(&mut *tables)
656661
}
662+
663+
fn place_pretty(&self, place: &Place) -> String {
664+
let mut tables = self.0.borrow_mut();
665+
let tcx = tables.tcx;
666+
format!("{:?}", place.internal(&mut *tables, tcx))
667+
}
657668
}
658669

659670
pub struct TablesWrapper<'tcx>(pub RefCell<Tables<'tcx>>);

compiler/stable_mir/src/compiler_interface.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::cell::Cell;
88
use crate::abi::{FnAbi, Layout, LayoutShape};
99
use crate::mir::alloc::{AllocId, GlobalAlloc};
1010
use crate::mir::mono::{Instance, InstanceDef, StaticDef};
11-
use crate::mir::Body;
11+
use crate::mir::{Body, Place};
1212
use crate::target::MachineInfo;
1313
use crate::ty::{
1414
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, ForeignDef,
@@ -126,11 +126,14 @@ pub trait Context {
126126
fn def_ty_with_args(&self, item: DefId, args: &GenericArgs) -> Ty;
127127

128128
/// Returns literal value of a const as a string.
129-
fn const_literal(&self, cnst: &Const) -> String;
129+
fn const_pretty(&self, cnst: &Const) -> String;
130130

131131
/// `Span` of an item
132132
fn span_of_an_item(&self, def_id: DefId) -> Span;
133133

134+
/// Obtain the representation of a type.
135+
fn ty_pretty(&self, ty: Ty) -> String;
136+
134137
/// Obtain the representation of a type.
135138
fn ty_kind(&self, ty: Ty) -> TyKind;
136139

@@ -205,6 +208,9 @@ pub trait Context {
205208

206209
/// Get the layout shape.
207210
fn layout_shape(&self, id: Layout) -> LayoutShape;
211+
212+
/// Get a debug string representation of a place.
213+
fn place_pretty(&self, place: &Place) -> String;
208214
}
209215

210216
// A thread local variable that stores a pointer to the tables mapping between TyCtxt

compiler/stable_mir/src/lib.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use crate::compiler_interface::with;
2727
pub use crate::crate_def::CrateDef;
2828
pub use crate::crate_def::DefId;
2929
pub use crate::error::*;
30-
use crate::mir::pretty::function_name;
3130
use crate::mir::Body;
3231
use crate::mir::Mutability;
3332
use crate::ty::{ForeignModuleDef, ImplDef, IndexedVal, Span, TraitDef, Ty};
@@ -148,9 +147,8 @@ impl CrateItem {
148147
with(|cx| cx.is_foreign_item(self.0))
149148
}
150149

151-
pub fn dump<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
152-
writeln!(w, "{}", function_name(*self))?;
153-
self.body().dump(w)
150+
pub fn emit_mir<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
151+
self.body().dump(w, &self.name())
154152
}
155153
}
156154

compiler/stable_mir/src/mir/body.rs

+6-24
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use crate::mir::pretty::{function_body, pretty_statement, pretty_terminator};
1+
use crate::mir::pretty::function_body;
22
use crate::ty::{
33
AdtDef, ClosureDef, Const, CoroutineDef, GenericArgs, Movability, Region, RigidTy, Ty, TyKind,
44
VariantIdx,
55
};
66
use crate::{Error, Opaque, Span, Symbol};
77
use std::io;
8+
89
/// The SMIR representation of a single function.
910
#[derive(Clone, Debug)]
1011
pub struct Body {
@@ -90,28 +91,9 @@ impl Body {
9091
self.locals.iter().enumerate()
9192
}
9293

93-
pub fn dump<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
94-
writeln!(w, "{}", function_body(self))?;
95-
self.blocks
96-
.iter()
97-
.enumerate()
98-
.map(|(index, block)| -> io::Result<()> {
99-
writeln!(w, " bb{}: {{", index)?;
100-
let _ = block
101-
.statements
102-
.iter()
103-
.map(|statement| -> io::Result<()> {
104-
writeln!(w, "{}", pretty_statement(&statement.kind))?;
105-
Ok(())
106-
})
107-
.collect::<Vec<_>>();
108-
pretty_terminator(&block.terminator.kind, w)?;
109-
writeln!(w, "").unwrap();
110-
writeln!(w, " }}").unwrap();
111-
Ok(())
112-
})
113-
.collect::<Result<Vec<_>, _>>()?;
114-
Ok(())
94+
/// Emit the body using the provided name for the signature.
95+
pub fn dump<W: io::Write>(&self, w: &mut W, fn_name: &str) -> io::Result<()> {
96+
function_body(w, self, fn_name)
11597
}
11698

11799
pub fn spread_arg(&self) -> Option<Local> {
@@ -674,7 +656,7 @@ pub enum Operand {
674656
Constant(Constant),
675657
}
676658

677-
#[derive(Clone, Debug, Eq, PartialEq)]
659+
#[derive(Clone, Eq, PartialEq)]
678660
pub struct Place {
679661
pub local: Local,
680662
/// projection out of a place (access a field, deref a pointer, etc)

compiler/stable_mir/src/mir/mono.rs

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::mir::Body;
44
use crate::ty::{Allocation, ClosureDef, ClosureKind, FnDef, GenericArgs, IndexedVal, Ty};
55
use crate::{with, CrateItem, DefId, Error, ItemKind, Opaque, Symbol};
66
use std::fmt::{Debug, Formatter};
7+
use std::io;
78

89
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
910
pub enum MonoItem {
@@ -157,6 +158,11 @@ impl Instance {
157158
pub fn try_const_eval(&self, const_ty: Ty) -> Result<Allocation, Error> {
158159
with(|cx| cx.eval_instance(self.def, const_ty))
159160
}
161+
162+
/// Emit the body of this instance if it has one.
163+
pub fn emit_mir<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
164+
if let Some(body) = self.body() { body.dump(w, &self.name()) } else { Ok(()) }
165+
}
160166
}
161167

162168
impl Debug for Instance {

0 commit comments

Comments
 (0)