Skip to content

Commit cb192af

Browse files
committed
add stable_mir output test
1 parent 5e70254 commit cb192af

File tree

6 files changed

+40
-19
lines changed

6 files changed

+40
-19
lines changed

compiler/rustc_smir/src/rustc_smir/context.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! This trait is currently the main interface between the Rust compiler,
44
//! and the `stable_mir` crate.
55
6-
use rustc_middle::ty;
6+
use crate::rustc_smir::stable_mir::opaque;
77
use rustc_middle::ty::print::{with_forced_trimmed_paths, with_no_trimmed_paths};
88
use rustc_middle::ty::{
99
GenericPredicates, Instance, ParamEnv, ScalarInt, TypeVisitableExt, ValTree,
@@ -18,7 +18,8 @@ use stable_mir::ty::{
1818
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, GenericArgs,
1919
LineInfo, PolyFnSig, RigidTy, Span, Ty, TyKind, VariantDef,
2020
};
21-
use stable_mir::{Crate, CrateItem, DefId, Error, Filename, ItemKind, Symbol};
21+
use stable_mir::Opaque;
22+
use stable_mir::{self, Crate, CrateItem, Error, Filename, ItemKind, Symbol};
2223
use std::cell::RefCell;
2324

2425
use crate::rustc_internal::{internal, RustcInternal};
@@ -297,6 +298,12 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
297298
internal(cnst).to_string()
298299
}
299300

301+
fn adt_literal(&self, adt: &AdtDef) -> Opaque {
302+
let mut tables = self.0.borrow_mut();
303+
let internal = adt.internal(&mut *tables);
304+
opaque(&internal)
305+
}
306+
300307
fn span_of_an_item(&self, def_id: stable_mir::DefId) -> Span {
301308
let mut tables = self.0.borrow_mut();
302309
tables.tcx.def_span(tables[def_id]).stable(&mut *tables)

compiler/stable_mir/src/compiler_interface.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use crate::ty::{
1515
TraitDef, Ty, TyKind, VariantDef,
1616
};
1717
use crate::{
18-
mir, Crate, CrateItem, CrateItems, DefId, Error, Filename, ImplTraitDecls, ItemKind, Symbol,
19-
TraitDecls,
18+
mir, Crate, CrateItem, CrateItems, DefId, Error, Filename, ImplTraitDecls, ItemKind, Opaque,
19+
Symbol, TraitDecls,
2020
};
2121

2222
/// This trait defines the interface between stable_mir and the Rust compiler.
@@ -106,6 +106,9 @@ pub trait Context {
106106
/// Returns literal value of a const as a string.
107107
fn const_literal(&self, cnst: &Const) -> String;
108108

109+
/// Returns literal version of a Adt as a Opaque
110+
fn adt_literal(&self, adt: &AdtDef) -> Opaque;
111+
109112
/// `Span` of an item
110113
fn span_of_an_item(&self, def_id: DefId) -> Span;
111114

compiler/stable_mir/src/mir/body.rs

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ impl Body {
107107
Ok(())
108108
})
109109
.collect::<Result<Vec<_>, _>>()?;
110+
writeln!(w, "}}")?;
110111
Ok(())
111112
}
112113

compiler/stable_mir/src/mir/pretty.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use std::{io, iter};
77

88
use super::{AssertMessage, BinOp, TerminatorKind};
99

10+
use super::BorrowKind;
11+
1012
pub fn function_name(item: CrateItem) -> String {
1113
let mut pretty_name = String::new();
1214
let body = item.body();
@@ -40,7 +42,6 @@ pub fn function_body(body: &Body) -> String {
4042
pretty_body.push_str(format!("{}", pretty_ty(local.ty.kind())).as_str());
4143
pretty_body.push_str(";\n");
4244
});
43-
pretty_body.push_str("}");
4445
pretty_body
4546
}
4647

@@ -320,6 +321,7 @@ pub fn pretty_rvalue(rval: &Rvalue) -> String {
320321
pretty.push_str(format!("(*_{})", addr.local).as_str());
321322
}
322323
Rvalue::Aggregate(aggregatekind, operands) => {
324+
// FIXME: Add pretty_aggregate function that returns a pretty string
323325
pretty.push_str(format!("{:#?}", aggregatekind).as_str());
324326
pretty.push_str("(");
325327
operands.iter().enumerate().for_each(|(i, op)| {
@@ -330,24 +332,26 @@ pub fn pretty_rvalue(rval: &Rvalue) -> String {
330332
});
331333
pretty.push_str(")");
332334
}
333-
Rvalue::BinaryOp(bin, op, op2) => {
334-
pretty.push_str(&pretty_operand(op));
335-
pretty.push_str(" ");
335+
Rvalue::BinaryOp(bin, op1, op2) => {
336336
pretty.push_str(format!("{:#?}", bin).as_str());
337-
pretty.push_str(" ");
338-
pretty.push_str(&pretty_operand(op2));
337+
pretty.push_str("(");
338+
pretty.push_str(format!("_{}", &pretty_operand(op1)).as_str());
339+
pretty.push_str(", ");
340+
pretty.push_str(format!("{}", &pretty_operand(op2)).as_str());
341+
pretty.push_str(")");
339342
}
340343
Rvalue::Cast(_, op, ty) => {
341344
pretty.push_str(&pretty_operand(op));
342345
pretty.push_str(" as ");
343346
pretty.push_str(&pretty_ty(ty.kind()));
344347
}
345348
Rvalue::CheckedBinaryOp(bin, op1, op2) => {
346-
pretty.push_str(&pretty_operand(op1));
347-
pretty.push_str(" ");
348-
pretty.push_str(format!("{:#?}", bin).as_str());
349-
pretty.push_str(" ");
350-
pretty.push_str(&pretty_operand(op2));
349+
pretty.push_str(format!("Checked{:#?}", bin).as_str());
350+
pretty.push_str("(");
351+
pretty.push_str(format!("_{}", &pretty_operand(op1)).as_str());
352+
pretty.push_str(", ");
353+
pretty.push_str(format!("{}", &pretty_operand(op2)).as_str());
354+
pretty.push_str(")");
351355
}
352356
Rvalue::CopyForDeref(deref) => {
353357
pretty.push_str("CopyForDeref");
@@ -362,8 +366,11 @@ pub fn pretty_rvalue(rval: &Rvalue) -> String {
362366
pretty.push_str(format!("{}", len.local).as_str());
363367
}
364368
Rvalue::Ref(_, borrowkind, place) => {
365-
pretty.push_str("ref");
366-
pretty.push_str(format!("{:#?}", borrowkind).as_str());
369+
match borrowkind {
370+
BorrowKind::Shared => pretty.push_str("&"),
371+
BorrowKind::Fake => pretty.push_str("&fake "),
372+
BorrowKind::Mut { .. } => pretty.push_str("&mut "),
373+
}
367374
pretty.push_str(format!("{}", place.local).as_str());
368375
}
369376
Rvalue::Repeat(op, cnst) => {
@@ -418,7 +425,7 @@ pub fn pretty_ty(ty: TyKind) -> String {
418425
FloatTy::F64 => "f64".to_string(),
419426
},
420427
RigidTy::Adt(def, _) => {
421-
format!("{:#?}", with(|cx| cx.def_ty(def.0)))
428+
format!("{}", with(|cx| cx.adt_literal(&def)))
422429
}
423430
RigidTy::Str => "str".to_string(),
424431
RigidTy::Array(ty, len) => {

src/tools/tidy/src/ui_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::path::{Path, PathBuf};
1111
const ENTRY_LIMIT: usize = 900;
1212
// FIXME: The following limits should be reduced eventually.
1313
const ISSUES_ENTRY_LIMIT: usize = 1852;
14-
const ROOT_ENTRY_LIMIT: usize = 867;
14+
const ROOT_ENTRY_LIMIT: usize = 868;
1515

1616
const EXPECTED_TEST_FILE_EXTENSIONS: &[&str] = &[
1717
"rs", // test source files

tests/ui/stable-mir-print/basic_function.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// compile-flags: -Z unpretty=stable-mir -Z mir-opt-level=3
22
// check-pass
3+
<<<<<<< HEAD
34
// only-x86_64
5+
=======
6+
>>>>>>> 9a9a3a91e41 (add stable_mir output test)
47

58
fn foo(i:i32) -> i32 {
69
i + 1

0 commit comments

Comments
 (0)