From 2a403dc81d092ed42edfed48b94ba34c33028ae3 Mon Sep 17 00:00:00 2001 From: dianne Date: Wed, 21 May 2025 09:17:11 -0700 Subject: [PATCH] lower bodies' params to thir before the body's value --- compiler/rustc_mir_build/src/thir/cx/mod.rs | 3 ++- ...ociated-const-type-parameter-pattern.stderr | 18 +++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_mir_build/src/thir/cx/mod.rs b/compiler/rustc_mir_build/src/thir/cx/mod.rs index 2f593b9a0a741..8c817605847aa 100644 --- a/compiler/rustc_mir_build/src/thir/cx/mod.rs +++ b/compiler/rustc_mir_build/src/thir/cx/mod.rs @@ -27,8 +27,8 @@ pub(crate) fn thir_body( if let Some(reported) = cx.typeck_results.tainted_by_errors { return Err(reported); } - let expr = cx.mirror_expr(body.value); + // Lower the params before the body's expression so errors from params are shown first. let owner_id = tcx.local_def_id_to_hir_id(owner_def); if let Some(fn_decl) = tcx.hir_fn_decl_by_hir_id(owner_id) { let closure_env_param = cx.closure_env_param(owner_def, owner_id); @@ -48,6 +48,7 @@ pub(crate) fn thir_body( } } + let expr = cx.mirror_expr(body.value); Ok((tcx.alloc_steal_thir(cx.thir), expr)) } diff --git a/tests/ui/associated-consts/associated-const-type-parameter-pattern.stderr b/tests/ui/associated-consts/associated-const-type-parameter-pattern.stderr index a8256f775a68c..19b63a041d616 100644 --- a/tests/ui/associated-consts/associated-const-type-parameter-pattern.stderr +++ b/tests/ui/associated-consts/associated-const-type-parameter-pattern.stderr @@ -27,7 +27,7 @@ LL | B::X => println!("B::X"), | ^^^^ `const` depends on a generic parameter error[E0158]: constant pattern cannot depend on generic parameters - --> $DIR/associated-const-type-parameter-pattern.rs:30:9 + --> $DIR/associated-const-type-parameter-pattern.rs:28:48 | LL | pub trait Foo { | ------------- @@ -35,13 +35,12 @@ LL | const X: EFoo; | ------------- constant defined here ... LL | pub fn test_let_pat(arg: EFoo, A::X: EFoo) { - | - constant depends on this generic parameter -LL | -LL | let A::X = arg; - | ^^^^ `const` depends on a generic parameter + | - ^^^^ `const` depends on a generic parameter + | | + | constant depends on this generic parameter error[E0158]: constant pattern cannot depend on generic parameters - --> $DIR/associated-const-type-parameter-pattern.rs:28:48 + --> $DIR/associated-const-type-parameter-pattern.rs:30:9 | LL | pub trait Foo { | ------------- @@ -49,9 +48,10 @@ LL | const X: EFoo; | ------------- constant defined here ... LL | pub fn test_let_pat(arg: EFoo, A::X: EFoo) { - | - ^^^^ `const` depends on a generic parameter - | | - | constant depends on this generic parameter + | - constant depends on this generic parameter +LL | +LL | let A::X = arg; + | ^^^^ `const` depends on a generic parameter error: aborting due to 4 previous errors