diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index b29e469348f81..823cd07458a7e 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -482,16 +482,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { ) -> Result<(), ErrorGuaranteed> { let tcx = self.tcx(); let parent_def_id = self.item_def_id(); + // In this path, `Some(context)` should be `ConstArgument`: enum + // discriminants are handled earlier by resolve. We still use the helper so + // nested inline consts are checked in the outer const-argument context. if let Res::Def(DefKind::ConstParam, _) = res - && matches!(tcx.def_kind(parent_def_id), DefKind::AnonConst | DefKind::InlineConst) - && let ty::AnonConstKind::MCG = tcx.anon_const_kind(parent_def_id) + && let Some(context) = self.anon_const_forbids_generic_params() { let folder = ForbidParamUsesFolder { tcx, anon_const_def_id: parent_def_id, span, is_self_alias: false, - context: ForbidParamContext::ConstArgument, + context, }; return Err(folder.error()); } diff --git a/tests/ui/const-generics/mgca/const-param-in-nested-inline-const-arg.rs b/tests/ui/const-generics/mgca/const-param-in-nested-inline-const-arg.rs new file mode 100644 index 0000000000000..e8af7e00b470a --- /dev/null +++ b/tests/ui/const-generics/mgca/const-param-in-nested-inline-const-arg.rs @@ -0,0 +1,9 @@ +//@compile-flags: -Znext-solver=globally +#![feature(generic_const_args, min_generic_const_args)] + +struct S; +fn foo(_: S<{ const { const { N } } }>) {} +//~^ ERROR: generic parameters in const blocks are not allowed; use a named `const` item instead + + +fn main() {} diff --git a/tests/ui/const-generics/mgca/const-param-in-nested-inline-const-arg.stderr b/tests/ui/const-generics/mgca/const-param-in-nested-inline-const-arg.stderr new file mode 100644 index 0000000000000..fcf13e39e3981 --- /dev/null +++ b/tests/ui/const-generics/mgca/const-param-in-nested-inline-const-arg.stderr @@ -0,0 +1,10 @@ +error: generic parameters in const blocks are not allowed; use a named `const` item instead + --> $DIR/const-param-in-nested-inline-const-arg.rs:5:47 + | +LL | fn foo(_: S<{ const { const { N } } }>) {} + | ^ + | + = help: consider factoring the expression into a `type const` item and use it as the const argument instead + +error: aborting due to 1 previous error +