Skip to content

Commit fea2e5c

Browse files
authored
Rollup merge of #141356 - dianne:thir-lower-params-before-body-expr, r=compiler-errors
lower bodies' params to thir before the body's value Two motivations for this: - Lowering params first means errors from lowering the params are emitted before errors from lowering the body's expression. This comes up in [tests/ui/associated-consts/associated-const-type-parameter-pattern.stderr](https://github.com/rust-lang/rust/compare/master...dianne:rust:thir-lower-params-before-body-expr?expand=1#diff-acac6ea10e991af0da91633e08b2739f9f9ca0c8f826401b6ba829914d0806f2), where both the params and expression encounter errors in translating consts to patterns. This change puts the errors in the order they appear in the source file. - Guard patterns (#129967) contain expressions, so lowering params containing guard patterns may add more expressions to the THIR. However, there's a check for `-Zunpretty=thir-tree` that the final expression in the THIR corresponds to its value [(link)](https://github.com/rust-lang/rust/blob/c43786c9b7b8d8dcc3f9c604e0e3074c16ed69d3/compiler/rustc_mir_build/src/builder/mod.rs#L453-L455); lowering params last would break this. As an alternative way to get guard patterns to work, I think the pretty-printer could use the expression returned by `thir_body` and the check could be removed or changed (#141357).
2 parents eff339b + 2a403dc commit fea2e5c

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

compiler/rustc_mir_build/src/thir/cx/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ pub(crate) fn thir_body(
2727
if let Some(reported) = cx.typeck_results.tainted_by_errors {
2828
return Err(reported);
2929
}
30-
let expr = cx.mirror_expr(body.value);
3130

31+
// Lower the params before the body's expression so errors from params are shown first.
3232
let owner_id = tcx.local_def_id_to_hir_id(owner_def);
3333
if let Some(fn_decl) = tcx.hir_fn_decl_by_hir_id(owner_id) {
3434
let closure_env_param = cx.closure_env_param(owner_def, owner_id);
@@ -48,6 +48,7 @@ pub(crate) fn thir_body(
4848
}
4949
}
5050

51+
let expr = cx.mirror_expr(body.value);
5152
Ok((tcx.alloc_steal_thir(cx.thir), expr))
5253
}
5354

tests/ui/associated-consts/associated-const-type-parameter-pattern.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,31 @@ LL | B::X => println!("B::X"),
2727
| ^^^^ `const` depends on a generic parameter
2828

2929
error[E0158]: constant pattern cannot depend on generic parameters
30-
--> $DIR/associated-const-type-parameter-pattern.rs:30:9
30+
--> $DIR/associated-const-type-parameter-pattern.rs:28:48
3131
|
3232
LL | pub trait Foo {
3333
| -------------
3434
LL | const X: EFoo;
3535
| ------------- constant defined here
3636
...
3737
LL | pub fn test_let_pat<A: Foo, B: Foo>(arg: EFoo, A::X: EFoo) {
38-
| - constant depends on this generic parameter
39-
LL |
40-
LL | let A::X = arg;
41-
| ^^^^ `const` depends on a generic parameter
38+
| - ^^^^ `const` depends on a generic parameter
39+
| |
40+
| constant depends on this generic parameter
4241

4342
error[E0158]: constant pattern cannot depend on generic parameters
44-
--> $DIR/associated-const-type-parameter-pattern.rs:28:48
43+
--> $DIR/associated-const-type-parameter-pattern.rs:30:9
4544
|
4645
LL | pub trait Foo {
4746
| -------------
4847
LL | const X: EFoo;
4948
| ------------- constant defined here
5049
...
5150
LL | pub fn test_let_pat<A: Foo, B: Foo>(arg: EFoo, A::X: EFoo) {
52-
| - ^^^^ `const` depends on a generic parameter
53-
| |
54-
| constant depends on this generic parameter
51+
| - constant depends on this generic parameter
52+
LL |
53+
LL | let A::X = arg;
54+
| ^^^^ `const` depends on a generic parameter
5555

5656
error: aborting due to 4 previous errors
5757

0 commit comments

Comments
 (0)