From 9260305c9e6a15c7692b85324afbd7b36afb29ed Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Thu, 17 May 2018 18:32:47 +0300 Subject: [PATCH] rustc_mir: pretty-print all locals into their respective scopes. --- src/librustc_mir/util/pretty.rs | 126 ++++++++---------- src/test/mir-opt/box_expr.rs | 7 +- src/test/mir-opt/issue-41110.rs | 17 +-- src/test/mir-opt/issue-49232.rs | 10 +- .../mir-opt/packed-struct-drop-aligned.rs | 10 +- 5 files changed, 74 insertions(+), 96 deletions(-) diff --git a/src/librustc_mir/util/pretty.rs b/src/librustc_mir/util/pretty.rs index efae1e56f4dc6..1f55a728f9c72 100644 --- a/src/librustc_mir/util/pretty.rs +++ b/src/librustc_mir/util/pretty.rs @@ -461,9 +461,7 @@ fn comment(tcx: TyCtxt<'_, '_, '_>, SourceInfo { span, scope }: SourceInfo) -> S ) } -/// Prints user-defined variables in a scope tree. -/// -/// Returns the total number of variables printed. +/// Prints local variables in a scope tree. fn write_scope_tree( tcx: TyCtxt<'_, '_, '_>, mir: &Mir<'_>, @@ -474,57 +472,64 @@ fn write_scope_tree( ) -> io::Result<()> { let indent = depth * INDENT.len(); + // Local variable types (including the user's name in a comment). + for (local, local_decl) in mir.local_decls.iter_enumerated() { + if (1..mir.arg_count+1).contains(&local.index()) { + // Skip over argument locals, they're printed in the signature. + continue; + } + + if local_decl.source_info.scope != parent { + // Not declared in this scope. + continue; + } + + let mut_str = if local_decl.mutability == Mutability::Mut { + "mut " + } else { + "" + }; + + let mut indented_decl = format!( + "{0:1$}let {2}{3:?}: {4:?}", + INDENT, + indent, + mut_str, + local, + local_decl.ty + ); + for user_ty in local_decl.user_ty.projections() { + write!(indented_decl, " as {:?}", user_ty).unwrap(); + } + indented_decl.push_str(";"); + + let local_name = if local == RETURN_PLACE { + format!(" return place") + } else if let Some(name) = local_decl.name { + format!(" \"{}\"", name) + } else { + String::new() + }; + + writeln!( + w, + "{0:1$} //{2} in {3}", + indented_decl, + ALIGN, + local_name, + comment(tcx, local_decl.source_info), + )?; + } + let children = match scope_tree.get(&parent) { - Some(children) => children, + Some(childs) => childs, None => return Ok(()), }; for &child in children { - let data = &mir.source_scopes[child]; - assert_eq!(data.parent_scope, Some(parent)); + assert_eq!(mir.source_scopes[child].parent_scope, Some(parent)); writeln!(w, "{0:1$}scope {2} {{", "", indent, child.index())?; - - // User variable types (including the user's name in a comment). - for local in mir.vars_iter() { - let var = &mir.local_decls[local]; - let (name, source_info) = if var.source_info.scope == child { - (var.name.unwrap(), var.source_info) - } else { - // Not a variable or not declared in this scope. - continue; - }; - - let mut_str = if var.mutability == Mutability::Mut { - "mut " - } else { - "" - }; - - let indent = indent + INDENT.len(); - let mut indented_var = format!( - "{0:1$}let {2}{3:?}: {4:?}", - INDENT, - indent, - mut_str, - local, - var.ty - ); - for user_ty in var.user_ty.projections() { - write!(indented_var, " as {:?}", user_ty).unwrap(); - } - indented_var.push_str(";"); - writeln!( - w, - "{0:1$} // \"{2}\" in {3}", - indented_var, - ALIGN, - name, - comment(tcx, source_info) - )?; - } - write_scope_tree(tcx, mir, scope_tree, w, child, depth + 1)?; - writeln!(w, "{0:1$}}}", "", depth * INDENT.len())?; } @@ -556,19 +561,8 @@ pub fn write_mir_intro<'a, 'gcx, 'tcx>( } } - // Print return place - let indented_retptr = format!("{}let mut {:?}: {};", - INDENT, - RETURN_PLACE, - mir.local_decls[RETURN_PLACE].ty); - writeln!(w, "{0:1$} // return place", - indented_retptr, - ALIGN)?; - write_scope_tree(tcx, mir, &scope_tree, w, OUTERMOST_SOURCE_SCOPE, 1)?; - write_temp_decls(mir, w)?; - // Add an empty line before the first block is printed. writeln!(w, "")?; @@ -632,22 +626,6 @@ fn write_mir_sig( Ok(()) } -fn write_temp_decls(mir: &Mir<'_>, w: &mut dyn Write) -> io::Result<()> { - // Compiler-introduced temporary types. - for temp in mir.temps_iter() { - writeln!( - w, - "{}let {}{:?}: {};", - INDENT, - if mir.local_decls[temp].mutability == Mutability::Mut {"mut "} else {""}, - temp, - mir.local_decls[temp].ty - )?; - } - - Ok(()) -} - fn write_user_type_annotations(mir: &Mir<'_>, w: &mut dyn Write) -> io::Result<()> { if !mir.user_type_annotations.is_empty() { writeln!(w, "| User Type Annotations")?; diff --git a/src/test/mir-opt/box_expr.rs b/src/test/mir-opt/box_expr.rs index 14d302f0eea72..0201fed945a00 100644 --- a/src/test/mir-opt/box_expr.rs +++ b/src/test/mir-opt/box_expr.rs @@ -22,15 +22,14 @@ impl Drop for S { // END RUST SOURCE // START rustc.main.ElaborateDrops.before.mir // let mut _0: (); +// let mut _2: std::boxed::Box; +// let mut _3: (); +// let mut _4: std::boxed::Box; // scope 1 { // } // scope 2 { // let _1: std::boxed::Box; // } -// let mut _2: std::boxed::Box; -// let mut _3: (); -// let mut _4: std::boxed::Box; -// // bb0: { // StorageLive(_1); // StorageLive(_2); diff --git a/src/test/mir-opt/issue-41110.rs b/src/test/mir-opt/issue-41110.rs index 31ad1ebd9ff62..d4f545c9840df 100644 --- a/src/test/mir-opt/issue-41110.rs +++ b/src/test/mir-opt/issue-41110.rs @@ -29,27 +29,28 @@ impl S { // END RUST SOURCE // START rustc.main.ElaborateDrops.after.mir // let mut _0: (); +// let mut _2: S; +// let mut _3: S; +// let mut _4: S; +// let mut _5: bool; // scope 1 { // } // scope 2 { // let _1: (); // } -// let mut _2: S; -// let mut _3: S; -// let mut _4: S; -// let mut _5: bool; +// ... // bb0: { // END rustc.main.ElaborateDrops.after.mir // START rustc.test.ElaborateDrops.after.mir // let mut _0: (); +// let mut _3: (); +// let mut _4: S; +// let mut _5: S; +// let mut _6: bool; // ... // let mut _2: S; // ... // let _1: S; // ... -// let mut _3: (); -// let mut _4: S; -// let mut _5: S; -// let mut _6: bool; // bb0: { // END rustc.test.ElaborateDrops.after.mir diff --git a/src/test/mir-opt/issue-49232.rs b/src/test/mir-opt/issue-49232.rs index 5f4f4ab82af24..fb25e094bee02 100644 --- a/src/test/mir-opt/issue-49232.rs +++ b/src/test/mir-opt/issue-49232.rs @@ -17,16 +17,16 @@ fn main() { // START rustc.main.mir_map.0.mir // fn main() -> (){ // let mut _0: (); -// scope 1 { -// } -// scope 2 { -// let _2: i32; -// } // let mut _1: (); // let mut _3: bool; // let mut _4: !; // let mut _5: (); // let mut _6: &i32; +// scope 1 { +// } +// scope 2 { +// let _2: i32; +// } // bb0: { // goto -> bb1; // } diff --git a/src/test/mir-opt/packed-struct-drop-aligned.rs b/src/test/mir-opt/packed-struct-drop-aligned.rs index 167a6eb349eb2..f317c06de4b81 100644 --- a/src/test/mir-opt/packed-struct-drop-aligned.rs +++ b/src/test/mir-opt/packed-struct-drop-aligned.rs @@ -18,16 +18,16 @@ impl Drop for Droppy { // START rustc.main.EraseRegions.before.mir // fn main() -> () { // let mut _0: (); -// scope 1 { -// } -// scope 2 { -// let mut _1: Packed; -// } // let mut _2: Aligned; // let mut _3: Droppy; // let mut _4: Aligned; // let mut _5: Droppy; // let mut _6: Aligned; +// scope 1 { +// } +// scope 2 { +// let mut _1: Packed; +// } // // bb0: { // StorageLive(_1);