From 07d5f1942680c18f88e27678d9ca995a77b4c15c Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 7 Jan 2024 21:20:16 +0100 Subject: [PATCH 1/4] Add an error path to the algorithm --- .../src/thir/pattern/check_match.rs | 7 ++++-- compiler/rustc_pattern_analysis/src/lib.rs | 14 +++++++---- compiler/rustc_pattern_analysis/src/lints.rs | 24 ++++++++++--------- compiler/rustc_pattern_analysis/src/rustc.rs | 2 ++ .../rustc_pattern_analysis/src/usefulness.rs | 21 ++++++++-------- 5 files changed, 40 insertions(+), 28 deletions(-) diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index e9da12d118e..74d1afcea4d 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -430,7 +430,10 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> { } let scrut_ty = scrut.ty; - let report = analyze_match(&cx, &tarms, scrut_ty); + let Ok(report) = analyze_match(&cx, &tarms, scrut_ty).map_err(|err| self.error = Err(err)) + else { + return; + }; match source { // Don't report arm reachability of desugared `match $iter.into_iter() { iter => .. }` @@ -544,7 +547,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> { let cx = self.new_cx(refutability, None, scrut, pat.span); let pat = self.lower_pattern(&cx, pat)?; let arms = [MatchArm { pat, arm_data: self.lint_level, has_guard: false }]; - let report = analyze_match(&cx, &arms, pat.ty().inner()); + let report = analyze_match(&cx, &arms, pat.ty().inner())?; Ok((cx, report)) } diff --git a/compiler/rustc_pattern_analysis/src/lib.rs b/compiler/rustc_pattern_analysis/src/lib.rs index 8ea8dd61ab4..4d2d66c88a5 100644 --- a/compiler/rustc_pattern_analysis/src/lib.rs +++ b/compiler/rustc_pattern_analysis/src/lib.rs @@ -24,6 +24,8 @@ use std::fmt; use rustc_index::Idx; #[cfg(feature = "rustc")] use rustc_middle::ty::Ty; +#[cfg(feature = "rustc")] +use rustc_span::ErrorGuaranteed; use crate::constructor::{Constructor, ConstructorSet}; #[cfg(feature = "rustc")] @@ -52,6 +54,8 @@ impl<'a, T: ?Sized> Captures<'a> for T {} pub trait TypeCx: Sized + fmt::Debug { /// The type of a pattern. type Ty: Copy + Clone + fmt::Debug; // FIXME: remove Copy + /// Errors that can abort analysis. + type Error: fmt::Debug; /// The index of an enum variant. type VariantIdx: Clone + Idx; /// A string literal @@ -109,25 +113,25 @@ pub fn analyze_match<'p, 'tcx>( tycx: &RustcMatchCheckCtxt<'p, 'tcx>, arms: &[rustc::MatchArm<'p, 'tcx>], scrut_ty: Ty<'tcx>, -) -> rustc::UsefulnessReport<'p, 'tcx> { +) -> Result, ErrorGuaranteed> { // Arena to store the extra wildcards we construct during analysis. let wildcard_arena = tycx.pattern_arena; let scrut_ty = tycx.reveal_opaque_ty(scrut_ty); let scrut_validity = ValidityConstraint::from_bool(tycx.known_valid_scrutinee); let cx = MatchCtxt { tycx, wildcard_arena }; - let report = compute_match_usefulness(cx, arms, scrut_ty, scrut_validity); + let report = compute_match_usefulness(cx, arms, scrut_ty, scrut_validity)?; let pat_column = PatternColumn::new(arms); // Lint on ranges that overlap on their endpoints, which is likely a mistake. - lint_overlapping_range_endpoints(cx, &pat_column); + lint_overlapping_range_endpoints(cx, &pat_column)?; // Run the non_exhaustive_omitted_patterns lint. Only run on refutable patterns to avoid hitting // `if let`s. Only run if the match is exhaustive otherwise the error is redundant. if tycx.refutable && report.non_exhaustiveness_witnesses.is_empty() { - lint_nonexhaustive_missing_variants(cx, arms, &pat_column, scrut_ty) + lint_nonexhaustive_missing_variants(cx, arms, &pat_column, scrut_ty)?; } - report + Ok(report) } diff --git a/compiler/rustc_pattern_analysis/src/lints.rs b/compiler/rustc_pattern_analysis/src/lints.rs index f1237ecf83c..f74e00342d0 100644 --- a/compiler/rustc_pattern_analysis/src/lints.rs +++ b/compiler/rustc_pattern_analysis/src/lints.rs @@ -4,7 +4,7 @@ use rustc_data_structures::captures::Captures; use rustc_middle::ty; use rustc_session::lint; use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS; -use rustc_span::Span; +use rustc_span::{ErrorGuaranteed, Span}; use crate::constructor::{IntRange, MaybeInfiniteInt}; use crate::errors::{ @@ -110,9 +110,9 @@ impl<'p, 'tcx> PatternColumn<'p, 'tcx> { fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>( cx: MatchCtxt<'a, 'p, 'tcx>, column: &PatternColumn<'p, 'tcx>, -) -> Vec> { +) -> Result>, ErrorGuaranteed> { let Some(ty) = column.head_ty() else { - return Vec::new(); + return Ok(Vec::new()); }; let pcx = &PlaceCtxt::new_dummy(cx, ty); @@ -121,7 +121,7 @@ fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>( // We can't consistently handle the case where no constructors are present (since this would // require digging deep through any type in case there's a non_exhaustive enum somewhere), // so for consistency we refuse to handle the top-level case, where we could handle it. - return vec![]; + return Ok(Vec::new()); } let mut witnesses = Vec::new(); @@ -141,7 +141,7 @@ fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>( let wild_pat = WitnessPat::wild_from_ctor(pcx, ctor); for (i, col_i) in specialized_columns.iter().enumerate() { // Compute witnesses for each column. - let wits_for_col_i = collect_nonexhaustive_missing_variants(cx, col_i); + let wits_for_col_i = collect_nonexhaustive_missing_variants(cx, col_i)?; // For each witness, we build a new pattern in the shape of `ctor(_, _, wit, _, _)`, // adding enough wildcards to match `arity`. for wit in wits_for_col_i { @@ -151,7 +151,7 @@ fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>( } } } - witnesses + Ok(witnesses) } pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>( @@ -159,13 +159,13 @@ pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>( arms: &[MatchArm<'p, 'tcx>], pat_column: &PatternColumn<'p, 'tcx>, scrut_ty: RevealedTy<'tcx>, -) { +) -> Result<(), ErrorGuaranteed> { let rcx: &RustcMatchCheckCtxt<'_, '_> = cx.tycx; if !matches!( rcx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, rcx.match_lint_level).0, rustc_session::lint::Level::Allow ) { - let witnesses = collect_nonexhaustive_missing_variants(cx, pat_column); + let witnesses = collect_nonexhaustive_missing_variants(cx, pat_column)?; if !witnesses.is_empty() { // Report that a match of a `non_exhaustive` enum marked with `non_exhaustive_omitted_patterns` // is not exhaustive enough. @@ -204,6 +204,7 @@ pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>( } } } + Ok(()) } /// Traverse the patterns to warn the user about ranges that overlap on their endpoints. @@ -211,9 +212,9 @@ pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>( pub(crate) fn lint_overlapping_range_endpoints<'a, 'p, 'tcx>( cx: MatchCtxt<'a, 'p, 'tcx>, column: &PatternColumn<'p, 'tcx>, -) { +) -> Result<(), ErrorGuaranteed> { let Some(ty) = column.head_ty() else { - return; + return Ok(()); }; let pcx = &PlaceCtxt::new_dummy(cx, ty); let rcx: &RustcMatchCheckCtxt<'_, '_> = cx.tycx; @@ -275,8 +276,9 @@ pub(crate) fn lint_overlapping_range_endpoints<'a, 'p, 'tcx>( // Recurse into the fields. for ctor in set.present { for col in column.specialize(pcx, &ctor) { - lint_overlapping_range_endpoints(cx, &col); + lint_overlapping_range_endpoints(cx, &col)?; } } } + Ok(()) } diff --git a/compiler/rustc_pattern_analysis/src/rustc.rs b/compiler/rustc_pattern_analysis/src/rustc.rs index b6f67b7c56f..d757c5be602 100644 --- a/compiler/rustc_pattern_analysis/src/rustc.rs +++ b/compiler/rustc_pattern_analysis/src/rustc.rs @@ -13,6 +13,7 @@ use rustc_middle::mir::{self, Const}; use rustc_middle::thir::{FieldPat, Pat, PatKind, PatRange, PatRangeBoundary}; use rustc_middle::ty::layout::IntegerExt; use rustc_middle::ty::{self, OpaqueTypeKey, Ty, TyCtxt, VariantDef}; +use rustc_span::ErrorGuaranteed; use rustc_span::{Span, DUMMY_SP}; use rustc_target::abi::{FieldIdx, Integer, VariantIdx, FIRST_VARIANT}; use smallvec::SmallVec; @@ -944,6 +945,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> { impl<'p, 'tcx> TypeCx for RustcMatchCheckCtxt<'p, 'tcx> { type Ty = RevealedTy<'tcx>; + type Error = ErrorGuaranteed; type VariantIdx = VariantIdx; type StrLit = Const<'tcx>; type ArmData = HirId; diff --git a/compiler/rustc_pattern_analysis/src/usefulness.rs b/compiler/rustc_pattern_analysis/src/usefulness.rs index 16bf709881b..8d81a79b796 100644 --- a/compiler/rustc_pattern_analysis/src/usefulness.rs +++ b/compiler/rustc_pattern_analysis/src/usefulness.rs @@ -1341,14 +1341,14 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>( mcx: MatchCtxt<'a, 'p, Cx>, matrix: &mut Matrix<'p, Cx>, is_top_level: bool, -) -> WitnessMatrix { +) -> Result, Cx::Error> { debug_assert!(matrix.rows().all(|r| r.len() == matrix.column_count())); if !matrix.wildcard_row_is_relevant && matrix.rows().all(|r| !r.pats.relevant) { // Here we know that nothing will contribute further to exhaustiveness or usefulness. This // is purely an optimization: skipping this check doesn't affect correctness. See the top of // the file for details. - return WitnessMatrix::empty(); + return Ok(WitnessMatrix::empty()); } let Some(ty) = matrix.head_ty() else { @@ -1360,16 +1360,16 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>( // When there's an unguarded row, the match is exhaustive and any subsequent row is not // useful. if !row.is_under_guard { - return WitnessMatrix::empty(); + return Ok(WitnessMatrix::empty()); } } // No (unguarded) rows, so the match is not exhaustive. We return a new witness unless // irrelevant. return if matrix.wildcard_row_is_relevant { - WitnessMatrix::unit_witness() + Ok(WitnessMatrix::unit_witness()) } else { // We choose to not report anything here; see at the top for details. - WitnessMatrix::empty() + Ok(WitnessMatrix::empty()) }; }; @@ -1422,7 +1422,7 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>( let mut spec_matrix = matrix.specialize_constructor(pcx, &ctor, ctor_is_relevant); let mut witnesses = ensure_sufficient_stack(|| { compute_exhaustiveness_and_usefulness(mcx, &mut spec_matrix, false) - }); + })?; // Transform witnesses for `spec_matrix` into witnesses for `matrix`. witnesses.apply_constructor(pcx, &missing_ctors, &ctor, report_individual_missing_ctors); @@ -1443,7 +1443,7 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>( } } - ret + Ok(ret) } /// Indicates whether or not a given arm is useful. @@ -1474,9 +1474,10 @@ pub fn compute_match_usefulness<'p, Cx: TypeCx>( arms: &[MatchArm<'p, Cx>], scrut_ty: Cx::Ty, scrut_validity: ValidityConstraint, -) -> UsefulnessReport<'p, Cx> { +) -> Result, Cx::Error> { let mut matrix = Matrix::new(arms, scrut_ty, scrut_validity); - let non_exhaustiveness_witnesses = compute_exhaustiveness_and_usefulness(cx, &mut matrix, true); + let non_exhaustiveness_witnesses = + compute_exhaustiveness_and_usefulness(cx, &mut matrix, true)?; let non_exhaustiveness_witnesses: Vec<_> = non_exhaustiveness_witnesses.single_column(); let arm_usefulness: Vec<_> = arms @@ -1493,5 +1494,5 @@ pub fn compute_match_usefulness<'p, Cx: TypeCx>( (arm, usefulness) }) .collect(); - UsefulnessReport { arm_usefulness, non_exhaustiveness_witnesses } + Ok(UsefulnessReport { arm_usefulness, non_exhaustiveness_witnesses }) } From 4b2e8bc8416003663530c45c84eac70116d01f92 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 1 Jan 2024 23:54:20 +0100 Subject: [PATCH 2/4] Abort analysis on type error --- compiler/rustc_pattern_analysis/src/lib.rs | 2 +- compiler/rustc_pattern_analysis/src/lints.rs | 12 +++++--- compiler/rustc_pattern_analysis/src/rustc.rs | 17 ++++++++--- .../rustc_pattern_analysis/src/usefulness.rs | 4 +-- .../usefulness/issue-119493-type-error-ice.rs | 13 ++++++++ .../issue-119493-type-error-ice.stderr | 30 +++++++++++++++++++ 6 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 tests/ui/pattern/usefulness/issue-119493-type-error-ice.rs create mode 100644 tests/ui/pattern/usefulness/issue-119493-type-error-ice.stderr diff --git a/compiler/rustc_pattern_analysis/src/lib.rs b/compiler/rustc_pattern_analysis/src/lib.rs index 4d2d66c88a5..b52643adcc9 100644 --- a/compiler/rustc_pattern_analysis/src/lib.rs +++ b/compiler/rustc_pattern_analysis/src/lib.rs @@ -77,7 +77,7 @@ pub trait TypeCx: Sized + fmt::Debug { /// The set of all the constructors for `ty`. /// /// This must follow the invariants of `ConstructorSet` - fn ctors_for_ty(&self, ty: Self::Ty) -> ConstructorSet; + fn ctors_for_ty(&self, ty: Self::Ty) -> Result, Self::Error>; /// Best-effort `Debug` implementation. fn debug_pat(f: &mut fmt::Formatter<'_>, pat: &DeconstructedPat<'_, Self>) -> fmt::Result; diff --git a/compiler/rustc_pattern_analysis/src/lints.rs b/compiler/rustc_pattern_analysis/src/lints.rs index f74e00342d0..08a1a6bcbc4 100644 --- a/compiler/rustc_pattern_analysis/src/lints.rs +++ b/compiler/rustc_pattern_analysis/src/lints.rs @@ -52,9 +52,13 @@ impl<'p, 'tcx> PatternColumn<'p, 'tcx> { } /// Do constructor splitting on the constructors of the column. - fn analyze_ctors(&self, pcx: &PlaceCtxt<'_, 'p, 'tcx>) -> SplitConstructorSet<'p, 'tcx> { + fn analyze_ctors( + &self, + pcx: &PlaceCtxt<'_, 'p, 'tcx>, + ) -> Result, ErrorGuaranteed> { let column_ctors = self.patterns.iter().map(|p| p.ctor()); - pcx.ctors_for_ty().split(pcx, column_ctors) + let ctors_for_ty = &pcx.ctors_for_ty()?; + Ok(ctors_for_ty.split(pcx, column_ctors)) } fn iter(&self) -> impl Iterator> + Captures<'_> { @@ -116,7 +120,7 @@ fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>( }; let pcx = &PlaceCtxt::new_dummy(cx, ty); - let set = column.analyze_ctors(pcx); + let set = column.analyze_ctors(pcx)?; if set.present.is_empty() { // We can't consistently handle the case where no constructors are present (since this would // require digging deep through any type in case there's a non_exhaustive enum somewhere), @@ -219,7 +223,7 @@ pub(crate) fn lint_overlapping_range_endpoints<'a, 'p, 'tcx>( let pcx = &PlaceCtxt::new_dummy(cx, ty); let rcx: &RustcMatchCheckCtxt<'_, '_> = cx.tycx; - let set = column.analyze_ctors(pcx); + let set = column.analyze_ctors(pcx)?; if matches!(ty.kind(), ty::Char | ty::Int(_) | ty::Uint(_)) { let emit_lint = |overlap: &IntRange, this_span: Span, overlapped_spans: &[Span]| { diff --git a/compiler/rustc_pattern_analysis/src/rustc.rs b/compiler/rustc_pattern_analysis/src/rustc.rs index d757c5be602..a8d1bece613 100644 --- a/compiler/rustc_pattern_analysis/src/rustc.rs +++ b/compiler/rustc_pattern_analysis/src/rustc.rs @@ -12,6 +12,7 @@ use rustc_middle::mir::interpret::Scalar; use rustc_middle::mir::{self, Const}; use rustc_middle::thir::{FieldPat, Pat, PatKind, PatRange, PatRangeBoundary}; use rustc_middle::ty::layout::IntegerExt; +use rustc_middle::ty::TypeVisitableExt; use rustc_middle::ty::{self, OpaqueTypeKey, Ty, TyCtxt, VariantDef}; use rustc_span::ErrorGuaranteed; use rustc_span::{Span, DUMMY_SP}; @@ -303,7 +304,10 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> { /// /// See [`crate::constructor`] for considerations of emptiness. #[instrument(level = "debug", skip(self), ret)] - pub fn ctors_for_ty(&self, ty: RevealedTy<'tcx>) -> ConstructorSet<'p, 'tcx> { + pub fn ctors_for_ty( + &self, + ty: RevealedTy<'tcx>, + ) -> Result, ErrorGuaranteed> { let cx = self; let make_uint_range = |start, end| { IntRange::from_range( @@ -312,9 +316,11 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> { RangeEnd::Included, ) }; + // Abort on type error. + ty.error_reported()?; // This determines the set of all possible constructors for the type `ty`. For numbers, // arrays and slices we use ranges and variable-length slices when appropriate. - match ty.kind() { + Ok(match ty.kind() { ty::Bool => ConstructorSet::Bool, ty::Char => { // The valid Unicode Scalar Value ranges. @@ -424,7 +430,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> { ty::CoroutineWitness(_, _) | ty::Bound(_, _) | ty::Placeholder(_) | ty::Infer(_) => { bug!("Encountered unexpected type in `ConstructorSet::for_ty`: {ty:?}") } - } + }) } pub(crate) fn lower_pat_range_bdy( @@ -965,7 +971,10 @@ impl<'p, 'tcx> TypeCx for RustcMatchCheckCtxt<'p, 'tcx> { ) -> &[Self::Ty] { self.ctor_sub_tys(ctor, ty) } - fn ctors_for_ty(&self, ty: Self::Ty) -> crate::constructor::ConstructorSet { + fn ctors_for_ty( + &self, + ty: Self::Ty, + ) -> Result, Self::Error> { self.ctors_for_ty(ty) } diff --git a/compiler/rustc_pattern_analysis/src/usefulness.rs b/compiler/rustc_pattern_analysis/src/usefulness.rs index 8d81a79b796..d227bca5022 100644 --- a/compiler/rustc_pattern_analysis/src/usefulness.rs +++ b/compiler/rustc_pattern_analysis/src/usefulness.rs @@ -753,7 +753,7 @@ impl<'a, 'p, Cx: TypeCx> PlaceCtxt<'a, 'p, Cx> { pub(crate) fn ctor_sub_tys(&self, ctor: &Constructor) -> &[Cx::Ty] { self.mcx.tycx.ctor_sub_tys(ctor, self.ty) } - pub(crate) fn ctors_for_ty(&self) -> ConstructorSet { + pub(crate) fn ctors_for_ty(&self) -> Result, Cx::Error> { self.mcx.tycx.ctors_for_ty(self.ty) } } @@ -1383,7 +1383,7 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>( // Analyze the constructors present in this column. let ctors = matrix.heads().map(|p| p.ctor()); - let ctors_for_ty = pcx.ctors_for_ty(); + let ctors_for_ty = pcx.ctors_for_ty()?; let is_integers = matches!(ctors_for_ty, ConstructorSet::Integers { .. }); // For diagnostics. let split_set = ctors_for_ty.split(pcx, ctors); let all_missing = split_set.present.is_empty(); diff --git a/tests/ui/pattern/usefulness/issue-119493-type-error-ice.rs b/tests/ui/pattern/usefulness/issue-119493-type-error-ice.rs new file mode 100644 index 00000000000..6cf459eb82e --- /dev/null +++ b/tests/ui/pattern/usefulness/issue-119493-type-error-ice.rs @@ -0,0 +1,13 @@ +fn main() {} + +fn foo() { + #[derive(Copy, Clone)] + struct Foo(NonExistent); + //~^ ERROR cannot find type + //~| ERROR cannot find type + + type U = impl Copy; + //~^ ERROR `impl Trait` in type aliases is unstable + let foo: U = Foo(()); + let Foo(()) = foo; +} diff --git a/tests/ui/pattern/usefulness/issue-119493-type-error-ice.stderr b/tests/ui/pattern/usefulness/issue-119493-type-error-ice.stderr new file mode 100644 index 00000000000..6d74feb7a9f --- /dev/null +++ b/tests/ui/pattern/usefulness/issue-119493-type-error-ice.stderr @@ -0,0 +1,30 @@ +error[E0412]: cannot find type `NonExistent` in this scope + --> $DIR/issue-119493-type-error-ice.rs:5:16 + | +LL | struct Foo(NonExistent); + | ^^^^^^^^^^^ not found in this scope + +error[E0412]: cannot find type `NonExistent` in this scope + --> $DIR/issue-119493-type-error-ice.rs:5:16 + | +LL | struct Foo(NonExistent); + | ^^^^^^^^^^^ not found in this scope + | +help: you might be missing a type parameter + | +LL | struct Foo(NonExistent); + | +++++++++++++ + +error[E0658]: `impl Trait` in type aliases is unstable + --> $DIR/issue-119493-type-error-ice.rs:9:14 + | +LL | type U = impl Copy; + | ^^^^^^^^^ + | + = note: see issue #63063 for more information + = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0412, E0658. +For more information about an error, try `rustc --explain E0412`. From f0511851bc4555346641efd2249328433b781355 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 9 Jan 2024 16:37:23 +0100 Subject: [PATCH 3/4] Don't mix combinators and `let else` Co-authored-by: Michael Goulet --- compiler/rustc_mir_build/src/thir/pattern/check_match.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index 74d1afcea4d..47941525cc2 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -430,9 +430,12 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> { } let scrut_ty = scrut.ty; - let Ok(report) = analyze_match(&cx, &tarms, scrut_ty).map_err(|err| self.error = Err(err)) - else { - return; + let report = match analyze_match(&cx, &tarms, scrut_ty) { + Ok(report) => report, + Err(err) => { + self.error = Err(err); + return; + } }; match source { From dee657f9f908136daa268540debf2c025855bb83 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 10 Jan 2024 14:50:48 +0100 Subject: [PATCH 4/4] Add test case for #119778 --- .../usefulness/issue-119778-type-error-ice.rs | 13 +++++++ .../issue-119778-type-error-ice.stderr | 35 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 tests/ui/pattern/usefulness/issue-119778-type-error-ice.rs create mode 100644 tests/ui/pattern/usefulness/issue-119778-type-error-ice.stderr diff --git a/tests/ui/pattern/usefulness/issue-119778-type-error-ice.rs b/tests/ui/pattern/usefulness/issue-119778-type-error-ice.rs new file mode 100644 index 00000000000..47333385bc1 --- /dev/null +++ b/tests/ui/pattern/usefulness/issue-119778-type-error-ice.rs @@ -0,0 +1,13 @@ +fn main() {} + +fn foo() { + #[derive(Copy, Clone)] + struct Foo([u8; S]); + //~^ ERROR cannot find value `S` + //~| ERROR cannot find value `S` + + type U = impl Copy; + //~^ ERROR `impl Trait` in type aliases is unstable + let foo: U = Foo(()); + let Foo(()) = foo; +} diff --git a/tests/ui/pattern/usefulness/issue-119778-type-error-ice.stderr b/tests/ui/pattern/usefulness/issue-119778-type-error-ice.stderr new file mode 100644 index 00000000000..93ef05decd2 --- /dev/null +++ b/tests/ui/pattern/usefulness/issue-119778-type-error-ice.stderr @@ -0,0 +1,35 @@ +error[E0425]: cannot find value `S` in this scope + --> $DIR/issue-119778-type-error-ice.rs:5:21 + | +LL | struct Foo([u8; S]); + | ^ not found in this scope + | +help: you might be missing a const parameter + | +LL | struct Foo([u8; S]); + | +++++++++++++++++++++ + +error[E0425]: cannot find value `S` in this scope + --> $DIR/issue-119778-type-error-ice.rs:5:21 + | +LL | struct Foo([u8; S]); + | ^ not found in this scope + | +help: you might be missing a const parameter + | +LL | struct Foo([u8; S]); + | +++++++++++++++++++++ + +error[E0658]: `impl Trait` in type aliases is unstable + --> $DIR/issue-119778-type-error-ice.rs:9:14 + | +LL | type U = impl Copy; + | ^^^^^^^^^ + | + = note: see issue #63063 for more information + = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0425, E0658. +For more information about an error, try `rustc --explain E0425`.