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, 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 +