From eaf979e8dd98f006767632f0910cd2fdecf8541d Mon Sep 17 00:00:00 2001 From: yukang Date: Wed, 5 Nov 2025 20:24:20 +0800 Subject: [PATCH] Fix ICE from lit_to_mir_constant caused by type error --- .../src/error_reporting/traits/mod.rs | 41 ++++++++++--------- .../ice-from-type-error-issue-148515.rs | 18 ++++++++ .../ice-from-type-error-issue-148515.stderr | 24 +++++++++++ 3 files changed, 63 insertions(+), 20 deletions(-) create mode 100644 tests/ui/enum-discriminant/ice-from-type-error-issue-148515.rs create mode 100644 tests/ui/enum-discriminant/ice-from-type-error-issue-148515.stderr diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs index b98547c42789d..1825719a30774 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs @@ -250,29 +250,30 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { } let mut reported = None; - for from_expansion in [false, true] { for (error, suppressed) in iter::zip(&errors, &is_suppressed) { - if !suppressed - && error.obligation.cause.span.from_expansion() == from_expansion - && !error.references_error() - { - let guar = self.report_fulfillment_error(error); - self.infcx.set_tainted_by_errors(guar); - reported = Some(guar); - // We want to ignore desugarings here: spans are equivalent even - // if one is the result of a desugaring and the other is not. - let mut span = error.obligation.cause.span; - let expn_data = span.ctxt().outer_expn_data(); - if let ExpnKind::Desugaring(_) = expn_data.kind { - span = expn_data.call_site; + if !suppressed && error.obligation.cause.span.from_expansion() == from_expansion { + if !error.references_error() { + let guar = self.report_fulfillment_error(error); + self.infcx.set_tainted_by_errors(guar); + reported = Some(guar); + // We want to ignore desugarings here: spans are equivalent even + // if one is the result of a desugaring and the other is not. + let mut span = error.obligation.cause.span; + let expn_data = span.ctxt().outer_expn_data(); + if let ExpnKind::Desugaring(_) = expn_data.kind { + span = expn_data.call_site; + } + self.reported_trait_errors + .borrow_mut() + .entry(span) + .or_insert_with(|| (vec![], guar)) + .0 + .push(error.obligation.as_goal()); + } + if let Some(guar) = self.dcx().has_errors() { + self.infcx.set_tainted_by_errors(guar); } - self.reported_trait_errors - .borrow_mut() - .entry(span) - .or_insert_with(|| (vec![], guar)) - .0 - .push(error.obligation.as_goal()); } } } diff --git a/tests/ui/enum-discriminant/ice-from-type-error-issue-148515.rs b/tests/ui/enum-discriminant/ice-from-type-error-issue-148515.rs new file mode 100644 index 0000000000000..e9ba69307c404 --- /dev/null +++ b/tests/ui/enum-discriminant/ice-from-type-error-issue-148515.rs @@ -0,0 +1,18 @@ +//@ edition: 2024 + +enum Test { + Value = -5 >> 1_usize, +} + +fn test1(x: impl Iterator) { + //~^ ERROR cannot find type `Foo` in this scope + assert_eq!(Test::Value as u8, -3); +} + +fn test2(_: impl Iterator) { + //~^ ERROR cannot find type `Foo` in this scope + 0u8 == -3; + //~^ ERROR cannot apply unary operator `-` to type `u8` +} + +fn main() {} diff --git a/tests/ui/enum-discriminant/ice-from-type-error-issue-148515.stderr b/tests/ui/enum-discriminant/ice-from-type-error-issue-148515.stderr new file mode 100644 index 0000000000000..d9f40c7c0f738 --- /dev/null +++ b/tests/ui/enum-discriminant/ice-from-type-error-issue-148515.stderr @@ -0,0 +1,24 @@ +error[E0412]: cannot find type `Foo` in this scope + --> $DIR/ice-from-type-error-issue-148515.rs:7:34 + | +LL | fn test1(x: impl Iterator) { + | ^^^ not found in this scope + +error[E0412]: cannot find type `Foo` in this scope + --> $DIR/ice-from-type-error-issue-148515.rs:12:34 + | +LL | fn test2(_: impl Iterator) { + | ^^^ not found in this scope + +error[E0600]: cannot apply unary operator `-` to type `u8` + --> $DIR/ice-from-type-error-issue-148515.rs:14:12 + | +LL | 0u8 == -3; + | ^^ cannot apply unary operator `-` + | + = note: unsigned values cannot be negated + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0412, E0600. +For more information about an error, try `rustc --explain E0412`.