From dc78b1c676476885e603b8060de5365fc5da66ea Mon Sep 17 00:00:00 2001 From: ouz-a Date: Fri, 1 Dec 2023 09:13:39 +0300 Subject: [PATCH] update test, compress push_str misc fixes --- compiler/rustc_smir/src/rustc_smir/context.rs | 7 +- compiler/stable_mir/src/mir/pretty.rs | 107 +++----- src/tools/tidy/src/ui_tests.rs | 2 +- tests/ui/stable-mir-print/basic_function.rs | 12 +- .../ui/stable-mir-print/basic_function.stdout | 242 +++--------------- 5 files changed, 95 insertions(+), 275 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/context.rs b/compiler/rustc_smir/src/rustc_smir/context.rs index 8460da316c248..b2ca5b5ce29ad 100644 --- a/compiler/rustc_smir/src/rustc_smir/context.rs +++ b/compiler/rustc_smir/src/rustc_smir/context.rs @@ -3,7 +3,7 @@ //! This trait is currently the main interface between the Rust compiler, //! and the `stable_mir` crate. -use crate::rustc_smir::stable_mir::opaque; +use rustc_middle::ty; use rustc_middle::ty::print::{with_forced_trimmed_paths, with_no_trimmed_paths}; use rustc_middle::ty::{ GenericPredicates, Instance, ParamEnv, ScalarInt, TypeVisitableExt, ValTree, @@ -18,8 +18,9 @@ use stable_mir::ty::{ AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, GenericArgs, LineInfo, PolyFnSig, RigidTy, Span, Ty, TyKind, VariantDef, }; -use stable_mir::Opaque; -use stable_mir::{self, Crate, CrateItem, Error, Filename, ItemKind, Symbol}; +use stable_mir::{ + self, opaque, Crate, CrateItem, DefId, Error, Filename, ItemKind, Opaque, Symbol, +}; use std::cell::RefCell; use crate::rustc_internal::{internal, RustcInternal}; diff --git a/compiler/stable_mir/src/mir/pretty.rs b/compiler/stable_mir/src/mir/pretty.rs index 710aba27850e7..fcf20022bb945 100644 --- a/compiler/stable_mir/src/mir/pretty.rs +++ b/compiler/stable_mir/src/mir/pretty.rs @@ -12,24 +12,20 @@ use super::BorrowKind; pub fn function_name(item: CrateItem) -> String { let mut pretty_name = String::new(); let body = item.body(); - pretty_name.push_str("fn "); - pretty_name.push_str(item.name().as_str()); + pretty_name.push_str(format!("fn {}", item.name()).as_str()); if body.arg_locals().is_empty() { pretty_name.push_str("()"); } else { pretty_name.push_str("("); } body.arg_locals().iter().enumerate().for_each(|(index, local)| { - pretty_name.push_str(format!("_{}: ", index).as_str()); - pretty_name.push_str(&pretty_ty(local.ty.kind())); + pretty_name.push_str(format!("_{}: {}", index, pretty_ty(local.ty.kind())).as_str()); }); if !body.arg_locals().is_empty() { pretty_name.push_str(")"); } let return_local = body.ret_local(); - pretty_name.push_str(" -> "); - pretty_name.push_str(&pretty_ty(return_local.ty.kind())); - pretty_name.push_str(" {"); + pretty_name.push_str(format!(" -> {} {{", pretty_ty(return_local.ty.kind())).as_str()); pretty_name } @@ -37,10 +33,15 @@ pub fn function_body(body: &Body) -> String { let mut pretty_body = String::new(); body.inner_locals().iter().enumerate().for_each(|(index, local)| { pretty_body.push_str(" "); - pretty_body.push_str(format!("let {}", ret_mutability(&local.mutability)).as_str()); - pretty_body.push_str(format!("_{}: ", index).as_str()); - pretty_body.push_str(format!("{}", pretty_ty(local.ty.kind())).as_str()); - pretty_body.push_str(";\n"); + pretty_body.push_str( + format!( + "let {}_{}: {};\n", + ret_mutability(&local.mutability), + index, + pretty_ty(local.ty.kind()) + ) + .as_str(), + ); }); pretty_body } @@ -56,8 +57,7 @@ pub fn pretty_statement(statement: &StatementKind) -> String { let mut pretty = String::new(); match statement { StatementKind::Assign(place, rval) => { - pretty.push_str(format!(" _{} = ", place.local).as_str()); - pretty.push_str(format!("{}", &pretty_rvalue(rval)).as_str()); + pretty.push_str(format!(" _{} = {}", place.local, pretty_rvalue(rval)).as_str()); } // FIXME: Add rest of the statements StatementKind::FakeRead(_, _) => { @@ -117,7 +117,7 @@ pub fn pretty_terminator(terminator: &TerminatorKind, w: &mut W) - Ok(()) } (1, false) => { - write!(w, " -> {:?}", successors[0])?; + write!(w, " -> bb{:?}", successors[0])?; Ok(()) } _ => { @@ -154,9 +154,7 @@ pub fn pretty_terminator_head(terminator: &TerminatorKind) -> String { Drop { place, .. } => format!(" drop(_{:?})", place.local), Call { func, args, destination, .. } => { pretty.push_str(" "); - pretty.push_str(format!("_{} = ", destination.local).as_str()); - pretty.push_str(&pretty_operand(func)); - pretty.push_str("("); + pretty.push_str(format!("_{} = {}(", destination.local, pretty_operand(func)).as_str()); args.iter().enumerate().for_each(|(i, arg)| { if i > 0 { pretty.push_str(", "); @@ -171,9 +169,9 @@ pub fn pretty_terminator_head(terminator: &TerminatorKind) -> String { if !expected { pretty.push_str("!"); } - pretty.push_str(format!("{} bool),", &pretty_operand(cond)).as_str()); - pretty.push_str(&pretty_assert_message(msg)); - pretty.push_str(")"); + pretty.push_str( + format!("{} bool),{})", &pretty_operand(cond), pretty_assert_message(msg)).as_str(), + ); pretty } InlineAsm { .. } => todo!(), @@ -297,16 +295,14 @@ pub fn pretty_operand(operand: &Operand) -> String { let mut pretty = String::new(); match operand { Operand::Copy(copy) => { - pretty.push_str(""); - pretty.push_str(format!("{}", copy.local).as_str()); + pretty.push_str(format!("_{}", copy.local).as_str()); } Operand::Move(mv) => { - pretty.push_str("move "); - pretty.push_str(format!("_{}", mv.local).as_str()); + pretty.push_str(format!("move _{}", mv.local).as_str()); } Operand::Constant(cnst) => { - pretty.push_str("const "); - pretty.push_str(with(|cx| cx.const_literal(&cnst.literal)).as_str()); + pretty + .push_str(format!("const {}", with(|cx| cx.const_literal(&cnst.literal))).as_str()); } } pretty @@ -316,14 +312,11 @@ pub fn pretty_rvalue(rval: &Rvalue) -> String { let mut pretty = String::new(); match rval { Rvalue::AddressOf(muta, addr) => { - pretty.push_str("&raw "); - pretty.push_str(&ret_mutability(muta)); - pretty.push_str(format!("(*_{})", addr.local).as_str()); + pretty.push_str(format!("&raw {}(*_{})", &ret_mutability(muta), 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("("); + pretty.push_str(format!("{:#?} (", aggregatekind).as_str()); operands.iter().enumerate().for_each(|(i, op)| { pretty.push_str(&pretty_operand(op)); if i != operands.len() - 1 { @@ -333,37 +326,27 @@ pub fn pretty_rvalue(rval: &Rvalue) -> String { pretty.push_str(")"); } Rvalue::BinaryOp(bin, op1, op2) => { - pretty.push_str(format!("{:#?}", 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(")"); + pretty.push_str( + format!("{:#?}({}, {})", bin, &pretty_operand(op1), pretty_operand(op2)).as_str(), + ); } Rvalue::Cast(_, op, ty) => { - pretty.push_str(&pretty_operand(op)); - pretty.push_str(" as "); - pretty.push_str(&pretty_ty(ty.kind())); + pretty.push_str(format!("{} as {}", pretty_operand(op), pretty_ty(ty.kind())).as_str()); } Rvalue::CheckedBinaryOp(bin, op1, 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(")"); + pretty.push_str( + format!("Checked{:#?}({}, {})", bin, &pretty_operand(op1), pretty_operand(op2)) + .as_str(), + ); } Rvalue::CopyForDeref(deref) => { - pretty.push_str("CopyForDeref"); - pretty.push_str(format!("{}", deref.local).as_str()); + pretty.push_str(format!("CopyForDeref{}", deref.local).as_str()); } Rvalue::Discriminant(place) => { - pretty.push_str("discriminant"); - pretty.push_str(format!("{}", place.local).as_str()); + pretty.push_str(format!("discriminant{}", place.local).as_str()); } Rvalue::Len(len) => { - pretty.push_str("len"); - pretty.push_str(format!("{}", len.local).as_str()); + pretty.push_str(format!("len{}", len.local).as_str()); } Rvalue::Ref(_, borrowkind, place) => { match borrowkind { @@ -374,24 +357,19 @@ pub fn pretty_rvalue(rval: &Rvalue) -> String { pretty.push_str(format!("{}", place.local).as_str()); } Rvalue::Repeat(op, cnst) => { - pretty.push_str(&pretty_operand(op)); - pretty.push_str(" "); - pretty.push_str(&pretty_ty(cnst.ty().kind())); + pretty.push_str( + &format!("{} \" \" {}", &pretty_operand(op), pretty_ty(cnst.ty().kind())).as_str(), + ); } Rvalue::ShallowInitBox(_, _) => (), Rvalue::ThreadLocalRef(item) => { - pretty.push_str("thread_local_ref"); - pretty.push_str(format!("{:#?}", item).as_str()); + pretty.push_str(format!("thread_local_ref{:#?}", item).as_str()); } Rvalue::NullaryOp(nul, ty) => { - pretty.push_str(format!("{:#?}", nul).as_str()); - pretty.push_str(&pretty_ty(ty.kind())); - pretty.push_str(" "); + pretty.push_str(format!("{:#?} {} \" \"", nul, pretty_ty(ty.kind())).as_str()); } Rvalue::UnaryOp(un, op) => { - pretty.push_str(&pretty_operand(op)); - pretty.push_str(" "); - pretty.push_str(format!("{:#?}", un).as_str()); + pretty.push_str(format!("{} \" \" {:#?}", pretty_operand(op), un).as_str()); } Rvalue::Use(op) => pretty.push_str(&pretty_operand(op)), } @@ -458,8 +436,7 @@ pub fn pretty_ty(ty: TyKind) -> String { DynKind::Dyn => pretty.push_str("dyn "), DynKind::DynStar => pretty.push_str("dyn* "), } - pretty.push_str(format!("{:#?}", data).as_str()); - pretty.push_str(format!(" + {:#?} )", region).as_str()); + pretty.push_str(format!("{:#?} + {:#?}", data, region).as_str()); pretty } RigidTy::Never => "!".to_string(), diff --git a/src/tools/tidy/src/ui_tests.rs b/src/tools/tidy/src/ui_tests.rs index 0fb57bf6f6c80..dfa386b49de7c 100644 --- a/src/tools/tidy/src/ui_tests.rs +++ b/src/tools/tidy/src/ui_tests.rs @@ -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 = 868; +const ROOT_ENTRY_LIMIT: usize = 867; const EXPECTED_TEST_FILE_EXTENSIONS: &[&str] = &[ "rs", // test source files diff --git a/tests/ui/stable-mir-print/basic_function.rs b/tests/ui/stable-mir-print/basic_function.rs index 55fbf3f4acdf6..5af3b21302e13 100644 --- a/tests/ui/stable-mir-print/basic_function.rs +++ b/tests/ui/stable-mir-print/basic_function.rs @@ -1,9 +1,6 @@ // 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 @@ -15,4 +12,13 @@ fn bar(vec: &mut Vec) -> Vec { new_vec } +pub fn demux(input: u8) -> u8 { + match input { + 0 => 10, + 1 => 6, + 2 => 8, + _ => 0, + } +} + fn main(){} diff --git a/tests/ui/stable-mir-print/basic_function.stdout b/tests/ui/stable-mir-print/basic_function.stdout index d9b33a4257c2c..5a858a86d7b87 100644 --- a/tests/ui/stable-mir-print/basic_function.stdout +++ b/tests/ui/stable-mir-print/basic_function.stdout @@ -2,219 +2,28 @@ // If you find a bug or want to improve the output open a issue at https://github.com/rust-lang/project-stable-mir. fn foo(_0: i32) -> i32 { let mut _0: (i32, bool); -} + bb0: { - _2 = 1 Add const 1_i32 - assert(!move _2 bool),"attempt to compute `{} + {}`, which would overflow", 1, const 1_i32) -> [success: bb1, unwind continue] + _2 = CheckedAdd(_1, const 1_i32) + assert(!move _2 bool),"attempt to compute `{} + {}`, which would overflow", _1, const 1_i32) -> [success: bb1, unwind continue] } bb1: { _0 = move _2 return } -fn bar(_0: &mut Ty { - id: 10, - kind: RigidTy( - Adt( - AdtDef( - DefId { - id: 3, - name: "std::vec::Vec", - }, - ), - GenericArgs( - [ - Type( - Ty { - id: 11, - kind: Param( - ParamTy { - index: 0, - name: "T", - }, - ), - }, - ), - Type( - Ty { - id: 12, - kind: Param( - ParamTy { - index: 1, - name: "A", - }, - ), - }, - ), - ], - ), - ), - ), -}) -> Ty { - id: 10, - kind: RigidTy( - Adt( - AdtDef( - DefId { - id: 3, - name: "std::vec::Vec", - }, - ), - GenericArgs( - [ - Type( - Ty { - id: 11, - kind: Param( - ParamTy { - index: 0, - name: "T", - }, - ), - }, - ), - Type( - Ty { - id: 12, - kind: Param( - ParamTy { - index: 1, - name: "A", - }, - ), - }, - ), - ], - ), - ), - ), -} { - let mut _0: Ty { - id: 10, - kind: RigidTy( - Adt( - AdtDef( - DefId { - id: 3, - name: "std::vec::Vec", - }, - ), - GenericArgs( - [ - Type( - Ty { - id: 11, - kind: Param( - ParamTy { - index: 0, - name: "T", - }, - ), - }, - ), - Type( - Ty { - id: 12, - kind: Param( - ParamTy { - index: 1, - name: "A", - }, - ), - }, - ), - ], - ), - ), - ), -}; - let mut _1: &Ty { - id: 10, - kind: RigidTy( - Adt( - AdtDef( - DefId { - id: 3, - name: "std::vec::Vec", - }, - ), - GenericArgs( - [ - Type( - Ty { - id: 11, - kind: Param( - ParamTy { - index: 0, - name: "T", - }, - ), - }, - ), - Type( - Ty { - id: 12, - kind: Param( - ParamTy { - index: 1, - name: "A", - }, - ), - }, - ), - ], - ), - ), - ), -}; - let _2: (); - let mut _3: &mut Ty { - id: 10, - kind: RigidTy( - Adt( - AdtDef( - DefId { - id: 3, - name: "std::vec::Vec", - }, - ), - GenericArgs( - [ - Type( - Ty { - id: 11, - kind: Param( - ParamTy { - index: 0, - name: "T", - }, - ), - }, - ), - Type( - Ty { - id: 12, - kind: Param( - ParamTy { - index: 1, - name: "A", - }, - ), - }, - ), - ], - ), - ), - ), -}; } +fn bar(_0: &mut std::vec::Vec) -> std::vec::Vec { + let mut _0: std::vec::Vec; + let mut _1: &std::vec::Vec; + let _2: (); + let mut _3: &mut std::vec::Vec; + bb0: { - _3 = refShared1 + _3 = &1 _2 = const as Clone>::clone(move _3) -> [return: bb1, unwind continue] } bb1: { - _5 = refMut { - kind: TwoPhaseBorrow, -}2 + _5 = &mut 2 _4 = const Vec::::push(move _5, const 1_i32) -> [return: bb2, unwind: bb3] } bb2: { @@ -227,8 +36,35 @@ fn bar(_0: &mut Ty { bb4: { resume } -fn main() -> () { } +fn demux(_0: u8) -> u8 { + bb0: { + switchInt(__1) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb1] + } + bb1: { + _0 = const 0_u8 + goto -> bb5 + } + bb2: { + _0 = const 10_u8 + goto -> bb5 + } + bb3: { + _0 = const 6_u8 + goto -> bb5 + } + bb4: { + _0 = const 8_u8 + goto -> bb5 + } + bb5: { return } +} +fn main() -> () { + + bb0: { + return + } +}