Skip to content

Commit 7ab2bb6

Browse files
committed
Clarify the situation with dummy patterns and PatData
1 parent 8ba3329 commit 7ab2bb6

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
@@ -861,7 +861,7 @@ fn report_arm_reachability<'p, 'thir, 'tcx>(
861861
for (arm, is_useful) in report.arm_usefulness.iter() {
862862
match is_useful {
863863
Usefulness::Redundant => {
864-
report_unreachable_pattern(*arm.pat.data(), arm.arm_data, catchall)
864+
report_unreachable_pattern(*arm.pat.data().unwrap(), arm.arm_data, catchall)
865865
}
866866
Usefulness::Useful(redundant_subpats) if redundant_subpats.is_empty() => {}
867867
// The arm is reachable, but contains redundant subpatterns (from or-patterns).
@@ -870,12 +870,12 @@ fn report_arm_reachability<'p, 'thir, 'tcx>(
870870
// Emit lints in the order in which they occur in the file.
871871
redundant_subpats.sort_unstable_by_key(|pat| pat.data());
872872
for pat in redundant_subpats {
873-
report_unreachable_pattern(*pat.data(), arm.arm_data, None);
873+
report_unreachable_pattern(*pat.data().unwrap(), arm.arm_data, None);
874874
}
875875
}
876876
}
877877
if !arm.has_guard && catchall.is_none() && pat_is_catchall(arm.pat) {
878-
catchall = Some(*arm.pat.data());
878+
catchall = Some(*arm.pat.data().unwrap());
879879
}
880880
}
881881
}

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 + Clone + 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
@@ -208,7 +208,7 @@ pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'thir, 'tcx>(
208208
};
209209

210210
use rustc_errors::DecorateLint;
211-
let mut err = rcx.tcx.sess.struct_span_warn(*arm.pat.data(), "");
211+
let mut err = rcx.tcx.sess.struct_span_warn(*arm.pat.data().unwrap(), "");
212212
err.set_primary_message(decorator.msg());
213213
decorator.decorate_lint(&mut err);
214214
err.emit();
@@ -258,8 +258,8 @@ pub(crate) fn lint_overlapping_range_endpoints<'a, 'p, 'thir, 'tcx>(
258258
let mut suffixes: SmallVec<[_; 1]> = Default::default();
259259
// Iterate on patterns that contained `overlap`.
260260
for pat in column.iter() {
261-
let this_span = *pat.data();
262261
let Constructor::IntRange(this_range) = pat.ctor() else { continue };
262+
let this_span = *pat.data().unwrap();
263263
if this_range.is_singleton() {
264264
// Don't lint when one of the ranges is a singleton.
265265
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
@@ -425,7 +425,7 @@ impl<'p, 'thir, 'tcx> RustcMatchCheckCtxt<'p, 'thir, 'tcx> {
425425
ty::Tuple(fs) => {
426426
ctor = Struct;
427427
let mut wilds: SmallVec<[_; 2]> =
428-
fs.iter().map(|ty| DeconstructedPat::wildcard(ty, pat.span)).collect();
428+
fs.iter().map(|ty| DeconstructedPat::wildcard(ty)).collect();
429429
for pat in subpatterns {
430430
wilds[pat.field.index()] = self.lower_pat(&pat.pattern);
431431
}
@@ -448,7 +448,7 @@ impl<'p, 'thir, 'tcx> RustcMatchCheckCtxt<'p, 'thir, 'tcx> {
448448
let pat = if let Some(pat) = pattern {
449449
self.lower_pat(&pat.pattern)
450450
} else {
451-
DeconstructedPat::wildcard(args.type_at(0), pat.span)
451+
DeconstructedPat::wildcard(args.type_at(0))
452452
};
453453
ctor = Struct;
454454
fields = singleton(pat);
@@ -473,7 +473,7 @@ impl<'p, 'thir, 'tcx> RustcMatchCheckCtxt<'p, 'thir, 'tcx> {
473473
ty
474474
});
475475
let mut wilds: SmallVec<[_; 2]> =
476-
tys.map(|ty| DeconstructedPat::wildcard(ty, pat.span)).collect();
476+
tys.map(|ty| DeconstructedPat::wildcard(ty)).collect();
477477
for pat in subpatterns {
478478
if let Some(i) = field_id_to_id[pat.field.index()] {
479479
wilds[i] = self.lower_pat(&pat.pattern);

compiler/rustc_pattern_analysis/src/usefulness.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,7 @@ impl<'a, 'p, Cx: TypeCx> Matrix<'a, 'p, Cx> {
845845
scrut_ty: Cx::Ty,
846846
scrut_validity: ValidityConstraint,
847847
) -> Self {
848-
let wild_pattern =
849-
wildcard_arena.alloc(DeconstructedPat::wildcard(scrut_ty, Default::default()));
848+
let wild_pattern = wildcard_arena.alloc(DeconstructedPat::wildcard(scrut_ty));
850849
let wildcard_row = PatStack::from_pattern(wild_pattern);
851850
let mut matrix = Matrix {
852851
rows: Vec::with_capacity(arms.len()),

0 commit comments

Comments
 (0)