Skip to content

Commit 5fccaee

Browse files
committed
Clarify the situation with dummy patterns and PatData
Use an explicit `Option` instead of requiring a `Default` bound
1 parent f6af747 commit 5fccaee

File tree

6 files changed

+22
-20
lines changed

6 files changed

+22
-20
lines changed

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ fn report_arm_reachability<'p, 'tcx>(
860860
for (arm, is_useful) in report.arm_usefulness.iter() {
861861
match is_useful {
862862
Usefulness::Redundant => {
863-
report_unreachable_pattern(*arm.pat.data(), arm.arm_data, catchall)
863+
report_unreachable_pattern(*arm.pat.data().unwrap(), arm.arm_data, catchall)
864864
}
865865
Usefulness::Useful(redundant_subpats) if redundant_subpats.is_empty() => {}
866866
// The arm is reachable, but contains redundant subpatterns (from or-patterns).
@@ -869,12 +869,12 @@ fn report_arm_reachability<'p, 'tcx>(
869869
// Emit lints in the order in which they occur in the file.
870870
redundant_subpats.sort_unstable_by_key(|pat| pat.data());
871871
for pat in redundant_subpats {
872-
report_unreachable_pattern(*pat.data(), arm.arm_data, None);
872+
report_unreachable_pattern(*pat.data().unwrap(), arm.arm_data, None);
873873
}
874874
}
875875
}
876876
if !arm.has_guard && catchall.is_none() && pat_is_catchall(arm.pat) {
877-
catchall = Some(*arm.pat.data());
877+
catchall = Some(*arm.pat.data().unwrap());
878878
}
879879
}
880880
}

compiler/rustc_pattern_analysis/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ pub trait TypeCx: Sized + fmt::Debug {
5858
type StrLit: Clone + PartialEq + fmt::Debug;
5959
/// Extra data to store in a match arm.
6060
type ArmData: Copy + Clone + fmt::Debug;
61-
/// Extra data to store in a pattern. `Default` needed when we create fictitious wildcard
62-
/// patterns during analysis.
63-
type PatData: Clone + Default;
61+
/// Extra data to store in a pattern.
62+
type PatData: Clone;
6463

6564
/// FIXME(Nadrieril): `Cx` should only give us revealed types.
6665
fn reveal_opaque_ty(&self, ty: Self::Ty) -> Self::Ty;

compiler/rustc_pattern_analysis/src/lints.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
203203
};
204204

205205
use rustc_errors::DecorateLint;
206-
let mut err = rcx.tcx.sess.struct_span_warn(*arm.pat.data(), "");
206+
let mut err = rcx.tcx.sess.struct_span_warn(*arm.pat.data().unwrap(), "");
207207
err.set_primary_message(decorator.msg());
208208
decorator.decorate_lint(&mut err);
209209
err.emit();
@@ -253,8 +253,8 @@ pub(crate) fn lint_overlapping_range_endpoints<'a, 'p, 'tcx>(
253253
let mut suffixes: SmallVec<[_; 1]> = Default::default();
254254
// Iterate on patterns that contained `overlap`.
255255
for pat in column.iter() {
256-
let this_span = *pat.data();
257256
let Constructor::IntRange(this_range) = pat.ctor() else { continue };
257+
let this_span = *pat.data().unwrap();
258258
if this_range.is_singleton() {
259259
// Don't lint when one of the ranges is a singleton.
260260
continue;

compiler/rustc_pattern_analysis/src/pat.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ pub struct DeconstructedPat<'p, Cx: TypeCx> {
2626
ctor: Constructor<Cx>,
2727
fields: &'p [DeconstructedPat<'p, Cx>],
2828
ty: Cx::Ty,
29-
data: Cx::PatData,
29+
/// Extra data to store in a pattern. `None` if the pattern is a wildcard that does not
30+
/// correspond to a user-supplied pattern.
31+
data: Option<Cx::PatData>,
3032
/// Whether removing this arm would change the behavior of the match expression.
3133
useful: Cell<bool>,
3234
}
3335

3436
impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
35-
pub fn wildcard(ty: Cx::Ty, data: Cx::PatData) -> Self {
36-
Self::new(Wildcard, &[], ty, data)
37+
pub fn wildcard(ty: Cx::Ty) -> Self {
38+
DeconstructedPat { ctor: Wildcard, fields: &[], ty, data: None, useful: Cell::new(false) }
3739
}
3840

3941
pub fn new(
@@ -42,7 +44,7 @@ impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
4244
ty: Cx::Ty,
4345
data: Cx::PatData,
4446
) -> Self {
45-
DeconstructedPat { ctor, fields, ty, data, useful: Cell::new(false) }
47+
DeconstructedPat { ctor, fields, ty, data: Some(data), useful: Cell::new(false) }
4648
}
4749

4850
pub(crate) fn is_or_pat(&self) -> bool {
@@ -63,8 +65,10 @@ impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
6365
pub fn ty(&self) -> Cx::Ty {
6466
self.ty
6567
}
66-
pub fn data(&self) -> &Cx::PatData {
67-
&self.data
68+
/// Returns the extra data stored in a pattern. Returns `None` if the pattern is a wildcard that
69+
/// does not correspond to a user-supplied pattern.
70+
pub fn data(&self) -> Option<&Cx::PatData> {
71+
self.data.as_ref()
6872
}
6973

7074
pub fn iter_fields<'a>(
@@ -83,7 +87,7 @@ impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
8387
let wildcard_sub_tys = || {
8488
let tys = pcx.ctor_sub_tys(other_ctor);
8589
tys.iter()
86-
.map(|ty| DeconstructedPat::wildcard(*ty, Cx::PatData::default()))
90+
.map(|ty| DeconstructedPat::wildcard(*ty))
8791
.map(|pat| pcx.mcx.wildcard_arena.alloc(pat) as &_)
8892
.collect()
8993
};

compiler/rustc_pattern_analysis/src/rustc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
416416
ty::Tuple(fs) => {
417417
ctor = Struct;
418418
let mut wilds: SmallVec<[_; 2]> =
419-
fs.iter().map(|ty| DeconstructedPat::wildcard(ty, pat.span)).collect();
419+
fs.iter().map(|ty| DeconstructedPat::wildcard(ty)).collect();
420420
for pat in subpatterns {
421421
wilds[pat.field.index()] = self.lower_pat(&pat.pattern);
422422
}
@@ -439,7 +439,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
439439
let pat = if let Some(pat) = pattern {
440440
self.lower_pat(&pat.pattern)
441441
} else {
442-
DeconstructedPat::wildcard(args.type_at(0), pat.span)
442+
DeconstructedPat::wildcard(args.type_at(0))
443443
};
444444
ctor = Struct;
445445
fields = singleton(pat);
@@ -464,7 +464,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
464464
ty
465465
});
466466
let mut wilds: SmallVec<[_; 2]> =
467-
tys.map(|ty| DeconstructedPat::wildcard(ty, pat.span)).collect();
467+
tys.map(|ty| DeconstructedPat::wildcard(ty)).collect();
468468
for pat in subpatterns {
469469
if let Some(i) = field_id_to_id[pat.field.index()] {
470470
wilds[i] = self.lower_pat(&pat.pattern);

compiler/rustc_pattern_analysis/src/usefulness.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -840,8 +840,7 @@ impl<'a, 'p, Cx: TypeCx> Matrix<'a, 'p, Cx> {
840840
scrut_ty: Cx::Ty,
841841
scrut_validity: ValidityConstraint,
842842
) -> Self {
843-
let wild_pattern =
844-
wildcard_arena.alloc(DeconstructedPat::wildcard(scrut_ty, Default::default()));
843+
let wild_pattern = wildcard_arena.alloc(DeconstructedPat::wildcard(scrut_ty));
845844
let wildcard_row = PatStack::from_pattern(wild_pattern);
846845
let mut matrix = Matrix {
847846
rows: Vec::with_capacity(arms.len()),

0 commit comments

Comments
 (0)