From eb4943a63ce9aaae1e62c18753ca8de4907ee429 Mon Sep 17 00:00:00 2001 From: lapla Date: Sat, 18 Jul 2026 11:55:55 +0900 Subject: [PATCH 1/2] Don't ICE when evaluating ValTrees that contain error constants --- compiler/rustc_middle/src/mir/consts.rs | 4 +++ .../mgca/type_const-adt-expr-missing-field.rs | 14 ++++++++++ .../type_const-adt-expr-missing-field.stderr | 28 +++++++++++++++++++ ...ype_const-tuple-call-expr-missing-field.rs | 18 ++++++++++++ ...const-tuple-call-expr-missing-field.stderr | 28 +++++++++++++++++++ 5 files changed, 92 insertions(+) create mode 100644 tests/ui/const-generics/mgca/type_const-adt-expr-missing-field.rs create mode 100644 tests/ui/const-generics/mgca/type_const-adt-expr-missing-field.stderr create mode 100644 tests/ui/const-generics/mgca/type_const-tuple-call-expr-missing-field.rs create mode 100644 tests/ui/const-generics/mgca/type_const-tuple-call-expr-missing-field.stderr diff --git a/compiler/rustc_middle/src/mir/consts.rs b/compiler/rustc_middle/src/mir/consts.rs index b46beb4a29af3..d657ecb856bdd 100644 --- a/compiler/rustc_middle/src/mir/consts.rs +++ b/compiler/rustc_middle/src/mir/consts.rs @@ -320,6 +320,10 @@ impl<'tcx> Const<'tcx> { ) -> Result { match self { Const::Ty(_, c) => { + if let Err(e) = c.error_reported() { + return Err(ReportedErrorInfo::non_const_eval_error(e).into()); + } + // FIXME(generic_const_exprs): We shouldn't encounter placeholders here // and could change this to ICE when encountering them instead. if c.has_non_region_param() || c.has_non_region_placeholders() { diff --git a/tests/ui/const-generics/mgca/type_const-adt-expr-missing-field.rs b/tests/ui/const-generics/mgca/type_const-adt-expr-missing-field.rs new file mode 100644 index 0000000000000..05be2097e124f --- /dev/null +++ b/tests/ui/const-generics/mgca/type_const-adt-expr-missing-field.rs @@ -0,0 +1,14 @@ +// Regression test for https://github.com/rust-lang/rust/issues/154632 + +#![feature(generic_const_exprs)] +#![feature(min_generic_const_args)] +#![feature(macroless_generic_const_args)] +#![feature(generic_const_items)] + +type const ADD1: usize = const { N + 1 }; +//~^ ERROR: unconstrained generic constant +type const AliasFnUnused: ADD1 = ADD1::<{ Some:: {} }>; +//~^ ERROR: cannot find type `ADD1` in this scope [E0573] +//~| ERROR: struct expression with missing field initialiser for `0` + +fn main() {} diff --git a/tests/ui/const-generics/mgca/type_const-adt-expr-missing-field.stderr b/tests/ui/const-generics/mgca/type_const-adt-expr-missing-field.stderr new file mode 100644 index 0000000000000..bc4821f20cf24 --- /dev/null +++ b/tests/ui/const-generics/mgca/type_const-adt-expr-missing-field.stderr @@ -0,0 +1,28 @@ +error[E0573]: cannot find type `ADD1` in this scope + --> $DIR/type_const-adt-expr-missing-field.rs:10:27 + | +LL | type const AliasFnUnused: ADD1 = ADD1::<{ Some:: {} }>; + | ^^^^ not found in this scope + | + = note: a constant named `ADD1` exists in another namespace + +error: unconstrained generic constant + --> $DIR/type_const-adt-expr-missing-field.rs:8:1 + | +LL | type const ADD1: usize = const { N + 1 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: try adding a `where` bound + | +LL | type const ADD1: usize where [(); const { N + 1 }]: = const { N + 1 }; + | ++++++++++++++++++++++++++++ + +error: struct expression with missing field initialiser for `0` + --> $DIR/type_const-adt-expr-missing-field.rs:10:43 + | +LL | type const AliasFnUnused: ADD1 = ADD1::<{ Some:: {} }>; + | ^^^^^^^^^^^^^^^^ + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0573`. diff --git a/tests/ui/const-generics/mgca/type_const-tuple-call-expr-missing-field.rs b/tests/ui/const-generics/mgca/type_const-tuple-call-expr-missing-field.rs new file mode 100644 index 0000000000000..718619023afa0 --- /dev/null +++ b/tests/ui/const-generics/mgca/type_const-tuple-call-expr-missing-field.rs @@ -0,0 +1,18 @@ +#![feature(generic_const_exprs)] +#![feature(min_generic_const_args)] +#![feature(macroless_generic_const_args)] +#![feature(adt_const_params)] +#![feature(generic_const_items)] + +use std::marker::ConstParamTy; + +#[derive(Eq, PartialEq, ConstParamTy)] +struct Wrap(usize); + +type const ADD1: usize = const { N + 1 }; +//~^ ERROR: unconstrained generic constant +type const AliasFnUnused: ADD1 = ADD1::<{ Wrap(Some:: {}) }>; +//~^ ERROR: cannot find type `ADD1` in this scope [E0573] +//~| ERROR: struct expression with missing field initialiser for `0` + +fn main() {} diff --git a/tests/ui/const-generics/mgca/type_const-tuple-call-expr-missing-field.stderr b/tests/ui/const-generics/mgca/type_const-tuple-call-expr-missing-field.stderr new file mode 100644 index 0000000000000..e852554dcb892 --- /dev/null +++ b/tests/ui/const-generics/mgca/type_const-tuple-call-expr-missing-field.stderr @@ -0,0 +1,28 @@ +error[E0573]: cannot find type `ADD1` in this scope + --> $DIR/type_const-tuple-call-expr-missing-field.rs:14:27 + | +LL | type const AliasFnUnused: ADD1 = ADD1::<{ Wrap(Some:: {}) }>; + | ^^^^ not found in this scope + | + = note: a constant named `ADD1` exists in another namespace + +error: unconstrained generic constant + --> $DIR/type_const-tuple-call-expr-missing-field.rs:12:1 + | +LL | type const ADD1: usize = const { N + 1 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: try adding a `where` bound + | +LL | type const ADD1: usize where [(); const { N + 1 }]: = const { N + 1 }; + | ++++++++++++++++++++++++++++ + +error: struct expression with missing field initialiser for `0` + --> $DIR/type_const-tuple-call-expr-missing-field.rs:14:48 + | +LL | type const AliasFnUnused: ADD1 = ADD1::<{ Wrap(Some:: {}) }>; + | ^^^^^^^^^^^^^^^^ + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0573`. From bce6199ef8a21079c658a6bfc036ea5b971ce783 Mon Sep 17 00:00:00 2001 From: lapla Date: Sun, 9 Aug 2026 19:16:19 +0900 Subject: [PATCH 2/2] Move test/crashes/150969 to UI now that it no longer ICEs --- tests/crashes/150969.rs | 7 ---- .../mgca/none-as-usize-const-arg.rs | 14 +++++++ .../mgca/none-as-usize-const-arg.stderr | 40 +++++++++++++++++++ 3 files changed, 54 insertions(+), 7 deletions(-) delete mode 100644 tests/crashes/150969.rs create mode 100644 tests/ui/const-generics/mgca/none-as-usize-const-arg.rs create mode 100644 tests/ui/const-generics/mgca/none-as-usize-const-arg.stderr diff --git a/tests/crashes/150969.rs b/tests/crashes/150969.rs deleted file mode 100644 index 5f0e09becf36f..0000000000000 --- a/tests/crashes/150969.rs +++ /dev/null @@ -1,7 +0,0 @@ -//@ known-bug: #150969 -#![feature(generic_const_exprs)] -#![feature(min_generic_const_args)] - -fn pass_enum { - pass_enum::<{core::direct_const_arg!(None)}> -} diff --git a/tests/ui/const-generics/mgca/none-as-usize-const-arg.rs b/tests/ui/const-generics/mgca/none-as-usize-const-arg.rs new file mode 100644 index 0000000000000..c21f1d0014ad5 --- /dev/null +++ b/tests/ui/const-generics/mgca/none-as-usize-const-arg.rs @@ -0,0 +1,14 @@ +// Regression test for https://github.com/rust-lang/rust/issues/150969 + +#![feature(generic_const_exprs)] +#![feature(min_generic_const_args)] + +fn pass_enum { + //~^ ERROR: missing parameters for function definition + //~| ERROR: defaults for generic parameters are not allowed here + //~| ERROR: overly complex generic constant + pass_enum::<{ core::direct_const_arg!(None) }> + //~^ ERROR: missing generics for enum `Option` [E0107] +} + +fn main() {} diff --git a/tests/ui/const-generics/mgca/none-as-usize-const-arg.stderr b/tests/ui/const-generics/mgca/none-as-usize-const-arg.stderr new file mode 100644 index 0000000000000..dbd1ac607e88c --- /dev/null +++ b/tests/ui/const-generics/mgca/none-as-usize-const-arg.stderr @@ -0,0 +1,40 @@ +error: missing parameters for function definition + --> $DIR/none-as-usize-const-arg.rs:6:59 + | +LL | fn pass_enum { + | ^ + | +help: add a parameter list + | +LL | fn pass_enum() { + | ++ + +error: defaults for generic parameters are not allowed here + --> $DIR/none-as-usize-const-arg.rs:6:30 + | +LL | fn pass_enum { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0107]: missing generics for enum `Option` + --> $DIR/none-as-usize-const-arg.rs:10:43 + | +LL | pass_enum::<{ core::direct_const_arg!(None) }> + | ^^^^ expected 1 generic argument + | +help: add missing generic argument + | +LL | pass_enum::<{ core::direct_const_arg!(None) }> + | +++ + +error: overly complex generic constant + --> $DIR/none-as-usize-const-arg.rs:6:47 + | +LL | fn pass_enum { + | ^^^^^^^^^^^ const blocks are not supported in generic constants + | + = help: consider moving this anonymous constant into a `const` function + = note: this operation may be supported in the future + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0107`.