From c6ee50de4dd039f07ffdb1c30abe2153cf15ca27 Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Sat, 27 Jun 2026 08:25:16 +0000 Subject: [PATCH 1/2] Add test for two cases 1. const argument expression 2. const argument with static value namespace --- .../ui/const-generics/mgca/static-const-arg.rs | 18 ++++++++++++++++++ .../mgca/static-const-arg.stderr | 14 ++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 tests/ui/const-generics/mgca/static-const-arg.rs create mode 100644 tests/ui/const-generics/mgca/static-const-arg.stderr diff --git a/tests/ui/const-generics/mgca/static-const-arg.rs b/tests/ui/const-generics/mgca/static-const-arg.rs new file mode 100644 index 0000000000000..604d632589db0 --- /dev/null +++ b/tests/ui/const-generics/mgca/static-const-arg.rs @@ -0,0 +1,18 @@ +// Regression test for #132986. +// FIXME(min_generic_const_args): using statics as direct const arguments should error instead of +// ICEing until const eval can evaluate statics to valtrees for const generics. + +#![feature(min_generic_const_args)] +#![allow(incomplete_features)] + +static A: u32 = 0; + +struct Foo; + +const _: Foo<{ A }> = Foo; +//~^ ERROR complex const arguments must be placed inside of a `const` block + +const _: Foo = Foo; +//~^ ERROR complex const arguments must be placed inside of a `const` block + +fn main() {} diff --git a/tests/ui/const-generics/mgca/static-const-arg.stderr b/tests/ui/const-generics/mgca/static-const-arg.stderr new file mode 100644 index 0000000000000..cae35a2aeac8c --- /dev/null +++ b/tests/ui/const-generics/mgca/static-const-arg.stderr @@ -0,0 +1,14 @@ +error: complex const arguments must be placed inside of a `const` block + --> $DIR/static-const-arg.rs:12:16 + | +LL | const _: Foo<{ A }> = Foo; + | ^ + +error: complex const arguments must be placed inside of a `const` block + --> $DIR/static-const-arg.rs:15:14 + | +LL | const _: Foo = Foo; + | ^ + +error: aborting due to 2 previous errors + From c5de45f72008acd6c7b868d72ef1f2f2bc40496b Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Sat, 27 Jun 2026 08:26:03 +0000 Subject: [PATCH 2/2] make sure to emite ConstKindArg::Error while AST lowering itselff for both the cases --- compiler/rustc_ast_lowering/src/lib.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 06e83a7486100..deae517116bdd 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -2549,7 +2549,10 @@ impl<'hir> LoweringContext<'_, 'hir> { let is_trivial_path = path.is_potential_trivial_const_arg() && matches!(res, Res::Def(DefKind::ConstParam, _)); - let ct_kind = if is_trivial_path || tcx.features().min_generic_const_args() { + let ct_kind = if matches!(res, Res::Def(DefKind::Static { .. }, _)) { + let msg = "complex const arguments must be placed inside of a `const` block"; + hir::ConstArgKind::Error(self.dcx().struct_span_err(span, msg).emit()) + } else if is_trivial_path || tcx.features().min_generic_const_args() { let qpath = self.lower_qpath( ty_id, &None, @@ -2677,6 +2680,12 @@ impl<'hir> LoweringContext<'_, 'hir> { ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Tup(exprs), span } } ExprKind::Path(qself, path) => { + if let Some(Res::Def(DefKind::Static { .. }, _)) = + self.get_partial_res(expr.id).and_then(|partial_res| partial_res.full_res()) + { + return overly_complex_const(self); + } + let qpath = self.lower_qpath( expr.id, qself,