diff --git a/compiler/rustc_middle/src/mir/consts.rs b/compiler/rustc_middle/src/mir/consts.rs
index c3300753fd16b..92218f6948f47 100644
--- a/compiler/rustc_middle/src/mir/consts.rs
+++ b/compiler/rustc_middle/src/mir/consts.rs
@@ -520,11 +520,13 @@ impl<'tcx> Const<'tcx> {
// types are fine though.
ty::ConstKind::Value(_) => c.ty().is_primitive(),
ty::ConstKind::Unevaluated(..) | ty::ConstKind::Expr(..) => false,
+ // This can happen if evaluation of a constant failed. The result does not matter
+ // much since compilation is doomed.
+ ty::ConstKind::Error(..) => false,
// Should not appear in runtime MIR.
ty::ConstKind::Infer(..)
| ty::ConstKind::Bound(..)
- | ty::ConstKind::Placeholder(..)
- | ty::ConstKind::Error(..) => bug!(),
+ | ty::ConstKind::Placeholder(..) => bug!(),
},
Const::Unevaluated(..) => false,
// If the same slice appears twice in the MIR, we cannot guarantee that we will
diff --git a/tests/ui/consts/const-eval/issue-50814-2.mir-opt.stderr b/tests/ui/consts/const-eval/issue-50814-2.mir-opt.stderr
new file mode 100644
index 0000000000000..6454ce3d1ae12
--- /dev/null
+++ b/tests/ui/consts/const-eval/issue-50814-2.mir-opt.stderr
@@ -0,0 +1,21 @@
+error[E0080]: evaluation of ` as Foo<()>>::BAR` failed
+ --> $DIR/issue-50814-2.rs:16:24
+ |
+LL | const BAR: usize = [5, 6, 7][T::BOO];
+ | ^^^^^^^^^^^^^^^^^ index out of bounds: the length is 3 but the index is 42
+
+note: erroneous constant encountered
+ --> $DIR/issue-50814-2.rs:20:6
+ |
+LL | & as Foo>::BAR
+ | ^^^^^^^^^^^^^^^^^^^^^
+
+note: erroneous constant encountered
+ --> $DIR/issue-50814-2.rs:20:5
+ |
+LL | & as Foo>::BAR
+ | ^^^^^^^^^^^^^^^^^^^^^^
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0080`.
diff --git a/tests/ui/consts/const-eval/issue-50814-2.stderr b/tests/ui/consts/const-eval/issue-50814-2.normal.stderr
similarity index 85%
rename from tests/ui/consts/const-eval/issue-50814-2.stderr
rename to tests/ui/consts/const-eval/issue-50814-2.normal.stderr
index 450fb00237306..c6b1df6c8f4d9 100644
--- a/tests/ui/consts/const-eval/issue-50814-2.stderr
+++ b/tests/ui/consts/const-eval/issue-50814-2.normal.stderr
@@ -1,17 +1,17 @@
error[E0080]: evaluation of ` as Foo<()>>::BAR` failed
- --> $DIR/issue-50814-2.rs:14:24
+ --> $DIR/issue-50814-2.rs:16:24
|
LL | const BAR: usize = [5, 6, 7][T::BOO];
| ^^^^^^^^^^^^^^^^^ index out of bounds: the length is 3 but the index is 42
note: erroneous constant encountered
- --> $DIR/issue-50814-2.rs:18:6
+ --> $DIR/issue-50814-2.rs:20:6
|
LL | & as Foo>::BAR
| ^^^^^^^^^^^^^^^^^^^^^
note: the above error was encountered while instantiating `fn foo::<()>`
- --> $DIR/issue-50814-2.rs:30:22
+ --> $DIR/issue-50814-2.rs:32:22
|
LL | println!("{:x}", foo::<()>() as *const usize as usize);
| ^^^^^^^^^^^
diff --git a/tests/ui/consts/const-eval/issue-50814-2.rs b/tests/ui/consts/const-eval/issue-50814-2.rs
index 53eb7b149f931..2eab93beb2016 100644
--- a/tests/ui/consts/const-eval/issue-50814-2.rs
+++ b/tests/ui/consts/const-eval/issue-50814-2.rs
@@ -1,4 +1,6 @@
// build-fail
+// revisions: normal mir-opt
+// [mir-opt]compile-flags: -Zmir-opt-level=4
trait C {
const BOO: usize;