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).
This commit is contained in:
Matthias Krüger 2025-05-21 22:15:01 +02:00 committed by GitHub
commit 2663ea3051
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 4 additions and 9 deletions

View file

@ -450,10 +450,6 @@ fn construct_fn<'tcx>(
let span = tcx.def_span(fn_def);
let fn_id = tcx.local_def_id_to_hir_id(fn_def);
// The representation of thir for `-Zunpretty=thir-tree` relies on
// the entry expression being the last element of `thir.exprs`.
assert_eq!(expr.as_usize(), thir.exprs.len() - 1);
// Figure out what primary body this item has.
let body = tcx.hir_body_owned_by(fn_def);
let span_with_body = tcx.hir_span_with_body(fn_id);

View file

@ -8,10 +8,10 @@ use rustc_span::def_id::LocalDefId;
/// Create a THIR tree for debugging.
pub fn thir_tree(tcx: TyCtxt<'_>, owner_def: LocalDefId) -> String {
match super::cx::thir_body(tcx, owner_def) {
Ok((thir, _)) => {
Ok((thir, expr)) => {
let thir = thir.steal();
let mut printer = ThirPrinter::new(&thir);
printer.print();
printer.print(expr);
printer.into_buffer()
}
Err(_) => "error".into(),
@ -58,7 +58,7 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
}
}
fn print(&mut self) {
fn print(&mut self, body_expr: ExprId) {
print_indented!(self, "params: [", 0);
for param in self.thir.params.iter() {
self.print_param(param, 1);
@ -66,8 +66,7 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
print_indented!(self, "]", 0);
print_indented!(self, "body:", 0);
let expr = ExprId::from_usize(self.thir.exprs.len() - 1);
self.print_expr(expr, 1);
self.print_expr(body_expr, 1);
}
fn into_buffer(self) -> String {