1
Fork 0

DeconstructedPat.data is always present now

This commit is contained in:
Nadrieril 2024-02-06 03:37:03 +01:00
parent 6ae9fa31f0
commit d339bdaa07
4 changed files with 15 additions and 17 deletions

View file

@ -420,9 +420,9 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
{ {
let mut redundant_subpats = redundant_subpats.clone(); let mut redundant_subpats = redundant_subpats.clone();
// Emit lints in the order in which they occur in the file. // Emit lints in the order in which they occur in the file.
redundant_subpats.sort_unstable_by_key(|pat| pat.data().unwrap().span); redundant_subpats.sort_unstable_by_key(|pat| pat.data().span);
for pat in redundant_subpats { for pat in redundant_subpats {
report_unreachable_pattern(cx, arm.arm_data, pat.data().unwrap().span, None) report_unreachable_pattern(cx, arm.arm_data, pat.data().span, None)
} }
} }
} }
@ -905,10 +905,10 @@ fn report_arm_reachability<'p, 'tcx>(
let mut catchall = None; let mut catchall = None;
for (arm, is_useful) in report.arm_usefulness.iter() { for (arm, is_useful) in report.arm_usefulness.iter() {
if matches!(is_useful, Usefulness::Redundant) { if matches!(is_useful, Usefulness::Redundant) {
report_unreachable_pattern(cx, arm.arm_data, arm.pat.data().unwrap().span, catchall) report_unreachable_pattern(cx, arm.arm_data, arm.pat.data().span, catchall)
} }
if !arm.has_guard && catchall.is_none() && pat_is_catchall(arm.pat) { if !arm.has_guard && catchall.is_none() && pat_is_catchall(arm.pat) {
catchall = Some(arm.pat.data().unwrap().span); catchall = Some(arm.pat.data().span);
} }
} }
} }

View file

@ -98,7 +98,7 @@ pub(crate) fn lint_nonexhaustive_missing_variants<'p, 'tcx>(
}; };
use rustc_errors::LintDiagnostic; use rustc_errors::LintDiagnostic;
let mut err = rcx.tcx.dcx().struct_span_warn(arm.pat.data().unwrap().span, ""); let mut err = rcx.tcx.dcx().struct_span_warn(arm.pat.data().span, "");
err.primary_message(decorator.msg()); err.primary_message(decorator.msg());
decorator.decorate_lint(&mut err); decorator.decorate_lint(&mut err);
err.emit(); err.emit();

View file

@ -37,9 +37,8 @@ pub struct DeconstructedPat<Cx: TypeCx> {
/// This is also the same as `self.ctor.arity(self.ty)`. /// This is also the same as `self.ctor.arity(self.ty)`.
arity: usize, arity: usize,
ty: Cx::Ty, ty: Cx::Ty,
/// Extra data to store in a pattern. `None` if the pattern is a wildcard that does not /// Extra data to store in a pattern.
/// correspond to a user-supplied pattern. data: Cx::PatData,
data: Option<Cx::PatData>,
/// Globally-unique id used to track usefulness at the level of subpatterns. /// Globally-unique id used to track usefulness at the level of subpatterns.
pub(crate) uid: PatId, pub(crate) uid: PatId,
} }
@ -52,7 +51,7 @@ impl<Cx: TypeCx> DeconstructedPat<Cx> {
ty: Cx::Ty, ty: Cx::Ty,
data: Cx::PatData, data: Cx::PatData,
) -> Self { ) -> Self {
DeconstructedPat { ctor, fields, arity, ty, data: Some(data), uid: PatId::new() } DeconstructedPat { ctor, fields, arity, ty, data, uid: PatId::new() }
} }
pub fn at_index(self, idx: usize) -> IndexedPat<Cx> { pub fn at_index(self, idx: usize) -> IndexedPat<Cx> {
@ -69,10 +68,9 @@ impl<Cx: TypeCx> DeconstructedPat<Cx> {
pub fn ty(&self) -> &Cx::Ty { pub fn ty(&self) -> &Cx::Ty {
&self.ty &self.ty
} }
/// Returns the extra data stored in a pattern. Returns `None` if the pattern is a wildcard that /// Returns the extra data stored in a pattern.
/// does not correspond to a user-supplied pattern. pub fn data(&self) -> &Cx::PatData {
pub fn data(&self) -> Option<&Cx::PatData> { &self.data
self.data.as_ref()
} }
pub fn arity(&self) -> usize { pub fn arity(&self) -> usize {
self.arity self.arity

View file

@ -904,10 +904,10 @@ impl<'p, 'tcx: 'p> TypeCx for RustcMatchCheckCtxt<'p, 'tcx> {
let overlap_as_pat = self.hoist_pat_range(&overlaps_on, *pat.ty()); let overlap_as_pat = self.hoist_pat_range(&overlaps_on, *pat.ty());
let overlaps: Vec<_> = overlaps_with let overlaps: Vec<_> = overlaps_with
.iter() .iter()
.map(|pat| pat.data().unwrap().span) .map(|pat| pat.data().span)
.map(|span| errors::Overlap { range: overlap_as_pat.clone(), span }) .map(|span| errors::Overlap { range: overlap_as_pat.clone(), span })
.collect(); .collect();
let pat_span = pat.data().unwrap().span; let pat_span = pat.data().span;
self.tcx.emit_node_span_lint( self.tcx.emit_node_span_lint(
lint::builtin::OVERLAPPING_RANGE_ENDPOINTS, lint::builtin::OVERLAPPING_RANGE_ENDPOINTS,
self.match_lint_level, self.match_lint_level,
@ -927,7 +927,7 @@ impl<'p, 'tcx: 'p> TypeCx for RustcMatchCheckCtxt<'p, 'tcx> {
gap: IntRange, gap: IntRange,
gapped_with: &[&crate::pat::DeconstructedPat<Self>], gapped_with: &[&crate::pat::DeconstructedPat<Self>],
) { ) {
let Some(&thir_pat) = pat.data() else { return }; let &thir_pat = pat.data();
let thir::PatKind::Range(range) = &thir_pat.kind else { return }; let thir::PatKind::Range(range) = &thir_pat.kind else { return };
// Only lint when the left range is an exclusive range. // Only lint when the left range is an exclusive range.
if range.end != rustc_hir::RangeEnd::Excluded { if range.end != rustc_hir::RangeEnd::Excluded {
@ -975,7 +975,7 @@ impl<'p, 'tcx: 'p> TypeCx for RustcMatchCheckCtxt<'p, 'tcx> {
gap_with: gapped_with gap_with: gapped_with
.iter() .iter()
.map(|pat| errors::GappedRange { .map(|pat| errors::GappedRange {
span: pat.data().unwrap().span, span: pat.data().span,
gap: gap_as_pat.clone(), gap: gap_as_pat.clone(),
first_range: thir_pat.clone(), first_range: thir_pat.clone(),
}) })