Skip to content

Commit 45a53d3

Browse files
committed
add stable_mir output test
1 parent c2ec908 commit 45a53d3

File tree

7 files changed

+95
-17
lines changed

7 files changed

+95
-17
lines changed

compiler/rustc_smir/src/rustc_smir/context.rs

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! This trait is currently the main interface between the Rust compiler,
44
//! and the `stable_mir` crate.
55
6+
use crate::rustc_smir::stable_mir::opaque;
67
use rustc_middle::ty::print::{with_forced_trimmed_paths, with_no_trimmed_paths};
78
use rustc_middle::ty::{GenericPredicates, Instance, ParamEnv, ScalarInt, ValTree};
89
use rustc_span::def_id::LOCAL_CRATE;
@@ -14,6 +15,7 @@ use stable_mir::ty::{
1415
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FnDef, GenericArgs, LineInfo,
1516
RigidTy, Span, TyKind,
1617
};
18+
use stable_mir::Opaque;
1719
use stable_mir::{self, Crate, CrateItem, Error, Filename, ItemKind, Symbol};
1820
use std::cell::RefCell;
1921

@@ -230,6 +232,12 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
230232
internal(cnst).to_string()
231233
}
232234

235+
fn adt_literal(&self, adt: &AdtDef) -> Opaque {
236+
let mut tables = self.0.borrow_mut();
237+
let internal = adt.internal(&mut *tables);
238+
opaque(&internal)
239+
}
240+
233241
fn span_of_an_item(&self, def_id: stable_mir::DefId) -> Span {
234242
let mut tables = self.0.borrow_mut();
235243
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
@@ -14,8 +14,8 @@ use crate::ty::{
1414
Ty, TyKind,
1515
};
1616
use crate::{
17-
mir, Crate, CrateItem, CrateItems, DefId, Error, Filename, ImplTraitDecls, ItemKind, Symbol,
18-
TraitDecls,
17+
mir, Crate, CrateItem, CrateItems, DefId, Error, Filename, ImplTraitDecls, ItemKind, Opaque,
18+
Symbol, TraitDecls,
1919
};
2020

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

82+
/// Returns literal version of a Adt as a Opaque
83+
fn adt_literal(&self, adt: &AdtDef) -> Opaque;
84+
8285
/// `Span` of an item
8386
fn span_of_an_item(&self, def_id: DefId) -> Span;
8487

compiler/stable_mir/src/mir/body.rs

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ impl Body {
8888
Ok(())
8989
})
9090
.collect::<Result<Vec<_>, _>>()?;
91+
writeln!(w, "}}")?;
9192
Ok(())
9293
}
9394
}

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

@@ -305,6 +306,7 @@ pub fn pretty_rvalue(rval: &Rvalue) -> String {
305306
pretty.push_str(format!("(*_{})", addr.local).as_str());
306307
}
307308
Rvalue::Aggregate(aggregatekind, operands) => {
309+
// FIXME: Add pretty_aggregate function that returns a pretty string
308310
pretty.push_str(format!("{:#?}", aggregatekind).as_str());
309311
pretty.push_str("(");
310312
operands.iter().enumerate().for_each(|(i, op)| {
@@ -315,24 +317,26 @@ pub fn pretty_rvalue(rval: &Rvalue) -> String {
315317
});
316318
pretty.push_str(")");
317319
}
318-
Rvalue::BinaryOp(bin, op, op2) => {
319-
pretty.push_str(&pretty_operand(op));
320-
pretty.push_str(" ");
320+
Rvalue::BinaryOp(bin, op1, op2) => {
321321
pretty.push_str(format!("{:#?}", bin).as_str());
322-
pretty.push_str(" ");
323-
pretty.push_str(&pretty_operand(op2));
322+
pretty.push_str("(");
323+
pretty.push_str(format!("_{}", &pretty_operand(op1)).as_str());
324+
pretty.push_str(", ");
325+
pretty.push_str(format!("{}", &pretty_operand(op2)).as_str());
326+
pretty.push_str(")");
324327
}
325328
Rvalue::Cast(_, op, ty) => {
326329
pretty.push_str(&pretty_operand(op));
327330
pretty.push_str(" as ");
328331
pretty.push_str(&pretty_ty(ty.kind()));
329332
}
330333
Rvalue::CheckedBinaryOp(bin, op1, op2) => {
331-
pretty.push_str(&pretty_operand(op1));
332-
pretty.push_str(" ");
333-
pretty.push_str(format!("{:#?}", bin).as_str());
334-
pretty.push_str(" ");
335-
pretty.push_str(&pretty_operand(op2));
334+
pretty.push_str(format!("Checked{:#?}", bin).as_str());
335+
pretty.push_str("(");
336+
pretty.push_str(format!("_{}", &pretty_operand(op1)).as_str());
337+
pretty.push_str(", ");
338+
pretty.push_str(format!("{}", &pretty_operand(op2)).as_str());
339+
pretty.push_str(")");
336340
}
337341
Rvalue::CopyForDeref(deref) => {
338342
pretty.push_str("CopyForDeref");
@@ -347,8 +351,11 @@ pub fn pretty_rvalue(rval: &Rvalue) -> String {
347351
pretty.push_str(format!("{}", len.local).as_str());
348352
}
349353
Rvalue::Ref(_, borrowkind, place) => {
350-
pretty.push_str("ref");
351-
pretty.push_str(format!("{:#?}", borrowkind).as_str());
354+
match borrowkind {
355+
BorrowKind::Shared => pretty.push_str("&"),
356+
BorrowKind::Fake => pretty.push_str("&fake "),
357+
BorrowKind::Mut { .. } => pretty.push_str("&mut "),
358+
}
352359
pretty.push_str(format!("{}", place.local).as_str());
353360
}
354361
Rvalue::Repeat(op, cnst) => {
@@ -403,7 +410,7 @@ pub fn pretty_ty(ty: TyKind) -> String {
403410
FloatTy::F64 => "f64".to_string(),
404411
},
405412
RigidTy::Adt(def, _) => {
406-
format!("{:#?}", with(|cx| cx.def_ty(def.0)))
413+
format!("{}", with(|cx| cx.adt_literal(&def)))
407414
}
408415
RigidTy::Str => "str".to_string(),
409416
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// compile-flags: -Z unpretty=stable-mir -Z mir-opt-level=3
2+
// check-pass
3+
4+
fn foo(i:i32) -> i32 {
5+
i + 1
6+
}
7+
8+
fn bar(vec: &mut Vec<i32>) -> Vec<i32> {
9+
let mut new_vec = vec.clone();
10+
new_vec.push(1);
11+
new_vec
12+
}
13+
14+
fn main(){}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// WARNING: This is highly experimental output it's intended for stable-mir developers only.
2+
// If you find a bug or want to improve the output open a issue at https://github.com/rust-lang/project-stable-mir.
3+
fn foo(_0: i32) -> i32 {
4+
let mut _0: (i32, bool);
5+
6+
bb0: {
7+
_2 = CheckedAdd(_1, const 1_i32)
8+
assert(!move _2 bool),"attempt to compute `{} + {}`, which would overflow", 1, const 1_i32) -> [success: bb1, unwind continue]
9+
}
10+
bb1: {
11+
_0 = move _2
12+
return
13+
}
14+
}
15+
fn bar(_0: &mut std::vec::Vec) -> std::vec::Vec {
16+
let mut _0: std::vec::Vec;
17+
let mut _1: &std::vec::Vec;
18+
let _2: ();
19+
let mut _3: &mut std::vec::Vec;
20+
21+
bb0: {
22+
_3 = &1
23+
_2 = const <Vec<i32> as Clone>::clone(move _3) -> [return: bb1, unwind continue]
24+
}
25+
bb1: {
26+
_5 = &mut 2
27+
_4 = const Vec::<i32>::push(move _5, const 1_i32) -> [return: bb2, unwind: bb3]
28+
}
29+
bb2: {
30+
_0 = move _2
31+
return
32+
}
33+
bb3: {
34+
drop(_2) -> [return: bb4, unwind terminate]
35+
}
36+
bb4: {
37+
resume
38+
}
39+
}
40+
fn main() -> () {
41+
42+
bb0: {
43+
return
44+
}
45+
}

0 commit comments

Comments
 (0)