Skip to content

Commit

Permalink
add stable_mir output test
Browse files Browse the repository at this point in the history
  • Loading branch information
ouz-a committed Dec 17, 2023
1 parent 5e70254 commit cb192af
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 19 deletions.
11 changes: 9 additions & 2 deletions compiler/rustc_smir/src/rustc_smir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! This trait is currently the main interface between the Rust compiler,
//! and the `stable_mir` crate.

use rustc_middle::ty;
use crate::rustc_smir::stable_mir::opaque;
use rustc_middle::ty::print::{with_forced_trimmed_paths, with_no_trimmed_paths};
use rustc_middle::ty::{
GenericPredicates, Instance, ParamEnv, ScalarInt, TypeVisitableExt, ValTree,
Expand All @@ -18,7 +18,8 @@ use stable_mir::ty::{
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, GenericArgs,
LineInfo, PolyFnSig, RigidTy, Span, Ty, TyKind, VariantDef,
};
use stable_mir::{Crate, CrateItem, DefId, Error, Filename, ItemKind, Symbol};
use stable_mir::Opaque;
use stable_mir::{self, Crate, CrateItem, Error, Filename, ItemKind, Symbol};
use std::cell::RefCell;

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

fn adt_literal(&self, adt: &AdtDef) -> Opaque {
let mut tables = self.0.borrow_mut();
let internal = adt.internal(&mut *tables);
opaque(&internal)
}

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
7 changes: 5 additions & 2 deletions compiler/stable_mir/src/compiler_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::ty::{
TraitDef, Ty, TyKind, VariantDef,
};
use crate::{
mir, Crate, CrateItem, CrateItems, DefId, Error, Filename, ImplTraitDecls, ItemKind, Symbol,
TraitDecls,
mir, Crate, CrateItem, CrateItems, DefId, Error, Filename, ImplTraitDecls, ItemKind, Opaque,
Symbol, TraitDecls,
};

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

/// Returns literal version of a Adt as a Opaque
fn adt_literal(&self, adt: &AdtDef) -> Opaque;

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

Expand Down
1 change: 1 addition & 0 deletions compiler/stable_mir/src/mir/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl Body {
Ok(())
})
.collect::<Result<Vec<_>, _>>()?;
writeln!(w, "}}")?;
Ok(())
}

Expand Down
35 changes: 21 additions & 14 deletions compiler/stable_mir/src/mir/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::{io, iter};

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

use super::BorrowKind;

pub fn function_name(item: CrateItem) -> String {
let mut pretty_name = String::new();
let body = item.body();
Expand Down Expand Up @@ -40,7 +42,6 @@ pub fn function_body(body: &Body) -> String {
pretty_body.push_str(format!("{}", pretty_ty(local.ty.kind())).as_str());
pretty_body.push_str(";\n");
});
pretty_body.push_str("}");
pretty_body
}

Expand Down Expand Up @@ -320,6 +321,7 @@ pub fn pretty_rvalue(rval: &Rvalue) -> String {
pretty.push_str(format!("(*_{})", addr.local).as_str());
}
Rvalue::Aggregate(aggregatekind, operands) => {
// FIXME: Add pretty_aggregate function that returns a pretty string
pretty.push_str(format!("{:#?}", aggregatekind).as_str());
pretty.push_str("(");
operands.iter().enumerate().for_each(|(i, op)| {
Expand All @@ -330,24 +332,26 @@ pub fn pretty_rvalue(rval: &Rvalue) -> String {
});
pretty.push_str(")");
}
Rvalue::BinaryOp(bin, op, op2) => {
pretty.push_str(&pretty_operand(op));
pretty.push_str(" ");
Rvalue::BinaryOp(bin, op1, op2) => {
pretty.push_str(format!("{:#?}", bin).as_str());
pretty.push_str(" ");
pretty.push_str(&pretty_operand(op2));
pretty.push_str("(");
pretty.push_str(format!("_{}", &pretty_operand(op1)).as_str());
pretty.push_str(", ");
pretty.push_str(format!("{}", &pretty_operand(op2)).as_str());
pretty.push_str(")");
}
Rvalue::Cast(_, op, ty) => {
pretty.push_str(&pretty_operand(op));
pretty.push_str(" as ");
pretty.push_str(&pretty_ty(ty.kind()));
}
Rvalue::CheckedBinaryOp(bin, op1, op2) => {
pretty.push_str(&pretty_operand(op1));
pretty.push_str(" ");
pretty.push_str(format!("{:#?}", bin).as_str());
pretty.push_str(" ");
pretty.push_str(&pretty_operand(op2));
pretty.push_str(format!("Checked{:#?}", bin).as_str());
pretty.push_str("(");
pretty.push_str(format!("_{}", &pretty_operand(op1)).as_str());
pretty.push_str(", ");
pretty.push_str(format!("{}", &pretty_operand(op2)).as_str());
pretty.push_str(")");
}
Rvalue::CopyForDeref(deref) => {
pretty.push_str("CopyForDeref");
Expand All @@ -362,8 +366,11 @@ pub fn pretty_rvalue(rval: &Rvalue) -> String {
pretty.push_str(format!("{}", len.local).as_str());
}
Rvalue::Ref(_, borrowkind, place) => {
pretty.push_str("ref");
pretty.push_str(format!("{:#?}", borrowkind).as_str());
match borrowkind {
BorrowKind::Shared => pretty.push_str("&"),
BorrowKind::Fake => pretty.push_str("&fake "),
BorrowKind::Mut { .. } => pretty.push_str("&mut "),
}
pretty.push_str(format!("{}", place.local).as_str());
}
Rvalue::Repeat(op, cnst) => {
Expand Down Expand Up @@ -418,7 +425,7 @@ pub fn pretty_ty(ty: TyKind) -> String {
FloatTy::F64 => "f64".to_string(),
},
RigidTy::Adt(def, _) => {
format!("{:#?}", with(|cx| cx.def_ty(def.0)))
format!("{}", with(|cx| cx.adt_literal(&def)))
}
RigidTy::Str => "str".to_string(),
RigidTy::Array(ty, len) => {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/tidy/src/ui_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::path::{Path, PathBuf};
const ENTRY_LIMIT: usize = 900;
// FIXME: The following limits should be reduced eventually.
const ISSUES_ENTRY_LIMIT: usize = 1852;
const ROOT_ENTRY_LIMIT: usize = 867;
const ROOT_ENTRY_LIMIT: usize = 868;

const EXPECTED_TEST_FILE_EXTENSIONS: &[&str] = &[
"rs", // test source files
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/stable-mir-print/basic_function.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// compile-flags: -Z unpretty=stable-mir -Z mir-opt-level=3
// check-pass
<<<<<<< HEAD
// only-x86_64
=======
>>>>>>> 9a9a3a91e41 (add stable_mir output test)

fn foo(i:i32) -> i32 {
i + 1
Expand Down

0 comments on commit cb192af

Please sign in to comment.