Skip to content

Commit 2663ea3

Browse files
authored
Rollup merge of #141357 - dianne:unhardcode-unpretty-thir-tree-body-expr, r=compiler-errors
`unpretty=thir-tree`: don't require the final expr to be the body's value Two motivations for this: - I couldn't find a comment motivating this hard-coding. I can imagine it might be easier to read `unpretty=thir-flat` output if the final expression in the THIR is always the body's value, but if that's the reason, that should be the justification in the source. I can also imagine it's meant to check that all expressions will be visited by the pretty-printer, but the existing check doesn't quite do that either. - Guard patterns (#129967) contain expressions, so lowering params containing guard patterns may add more expressions to the THIR. Currently a body's params are lowered after its expression, so guard expressions in params would end up last, breaking this. As an alternative, the params could be lowered first (#141356).
2 parents fea2e5c + f2d94c1 commit 2663ea3

File tree

2 files changed

+4
-9
lines changed

2 files changed

+4
-9
lines changed

compiler/rustc_mir_build/src/builder/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,10 +450,6 @@ fn construct_fn<'tcx>(
450450
let span = tcx.def_span(fn_def);
451451
let fn_id = tcx.local_def_id_to_hir_id(fn_def);
452452

453-
// The representation of thir for `-Zunpretty=thir-tree` relies on
454-
// the entry expression being the last element of `thir.exprs`.
455-
assert_eq!(expr.as_usize(), thir.exprs.len() - 1);
456-
457453
// Figure out what primary body this item has.
458454
let body = tcx.hir_body_owned_by(fn_def);
459455
let span_with_body = tcx.hir_span_with_body(fn_id);

compiler/rustc_mir_build/src/thir/print.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use rustc_span::def_id::LocalDefId;
88
/// Create a THIR tree for debugging.
99
pub fn thir_tree(tcx: TyCtxt<'_>, owner_def: LocalDefId) -> String {
1010
match super::cx::thir_body(tcx, owner_def) {
11-
Ok((thir, _)) => {
11+
Ok((thir, expr)) => {
1212
let thir = thir.steal();
1313
let mut printer = ThirPrinter::new(&thir);
14-
printer.print();
14+
printer.print(expr);
1515
printer.into_buffer()
1616
}
1717
Err(_) => "error".into(),
@@ -58,16 +58,15 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
5858
}
5959
}
6060

61-
fn print(&mut self) {
61+
fn print(&mut self, body_expr: ExprId) {
6262
print_indented!(self, "params: [", 0);
6363
for param in self.thir.params.iter() {
6464
self.print_param(param, 1);
6565
}
6666
print_indented!(self, "]", 0);
6767

6868
print_indented!(self, "body:", 0);
69-
let expr = ExprId::from_usize(self.thir.exprs.len() - 1);
70-
self.print_expr(expr, 1);
69+
self.print_expr(body_expr, 1);
7170
}
7271

7372
fn into_buffer(self) -> String {

0 commit comments

Comments
 (0)