From c524f11294c844251c81050366fd2f9952441b2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 5 Apr 2024 20:02:00 +0000 Subject: [PATCH] Do not ICE on field access check on expr with `ty::Error` Fix #123428 --- compiler/rustc_passes/src/dead.rs | 3 ++- .../do-not-ice-on-field-access-of-err-type.rs | 27 +++++++++++++++++++ ...not-ice-on-field-access-of-err-type.stderr | 22 +++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/ui/consts/do-not-ice-on-field-access-of-err-type.rs create mode 100644 tests/ui/consts/do-not-ice-on-field-access-of-err-type.stderr diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index 80f2078fff238..51f288b3c9595 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -153,7 +153,8 @@ impl<'tcx> MarkSymbolVisitor<'tcx> { self.insert_def_id(def.non_enum_variant().fields[index].did); } ty::Tuple(..) => {} - _ => span_bug!(lhs.span, "named field access on non-ADT"), + ty::Error(_) => {} + kind => span_bug!(lhs.span, "named field access on non-ADT: {kind:?}"), } } diff --git a/tests/ui/consts/do-not-ice-on-field-access-of-err-type.rs b/tests/ui/consts/do-not-ice-on-field-access-of-err-type.rs new file mode 100644 index 0000000000000..c5adc6e255b74 --- /dev/null +++ b/tests/ui/consts/do-not-ice-on-field-access-of-err-type.rs @@ -0,0 +1,27 @@ +use std::mem; + +trait Foo { + fn foo(&self) -> usize; +} +impl Foo for T { + fn foo(&self) -> usize { + mem::size_of::() + } +} + +fn main_ref() { + let array = [(); { + let mut n = 0; + while n < 5 {} //~ ERROR constant evaluation is taking a long time + 0 + }]; + + let u8_ = (7, 1u8); + let u32_ = (4u32, 5u32); + + let buf: &mut [*const dyn Foo] = &mut [&u8_, &u8_.0, &u32_, &u32_.0]; +} + +fn main() { + main_ref(); +} diff --git a/tests/ui/consts/do-not-ice-on-field-access-of-err-type.stderr b/tests/ui/consts/do-not-ice-on-field-access-of-err-type.stderr new file mode 100644 index 0000000000000..780f70c0c7e28 --- /dev/null +++ b/tests/ui/consts/do-not-ice-on-field-access-of-err-type.stderr @@ -0,0 +1,22 @@ +error: constant evaluation is taking a long time + --> $DIR/do-not-ice-on-field-access-of-err-type.rs:15:9 + | +LL | while n < 5 {} + | ^^^^^^^^^^^^^^ + | + = note: this lint makes sure the compiler doesn't get stuck due to infinite loops in const eval. + If your compilation actually takes a long time, you can safely allow the lint. +help: the constant being evaluated + --> $DIR/do-not-ice-on-field-access-of-err-type.rs:13:22 + | +LL | let array = [(); { + | ______________________^ +LL | | let mut n = 0; +LL | | while n < 5 {} +LL | | 0 +LL | | }]; + | |_____^ + = note: `#[deny(long_running_const_eval)]` on by default + +error: aborting due to 1 previous error +