Skip to content

Commit 49906f0

Browse files
committed
Move usefulness-specific pattern computations to usefulness
1 parent 11f32b7 commit 49906f0

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed

compiler/rustc_pattern_analysis/src/pat.rs

+10-27
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub struct DeconstructedPat<'p, Cx: TypeCx> {
2929
/// correspond to a user-supplied pattern.
3030
data: Option<Cx::PatData>,
3131
/// Whether removing this arm would change the behavior of the match expression.
32-
useful: Cell<bool>,
32+
pub(crate) useful: Cell<bool>,
3333
}
3434

3535
impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
@@ -106,34 +106,17 @@ impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
106106
pub(crate) fn set_useful(&self) {
107107
self.useful.set(true)
108108
}
109-
pub(crate) fn is_useful(&self) -> bool {
110-
if self.useful.get() {
111-
true
112-
} else if self.is_or_pat() && self.iter_fields().any(|f| f.is_useful()) {
113-
// We always expand or patterns in the matrix, so we will never see the actual
114-
// or-pattern (the one with constructor `Or`) in the column. As such, it will not be
115-
// marked as useful itself, only its children will. We recover this information here.
116-
self.set_useful();
117-
true
118-
} else {
119-
false
109+
110+
/// Walk top-down and call `it` in each place where a pattern occurs
111+
/// starting with the root pattern `walk` is called on. If `it` returns
112+
/// false then we will descend no further but siblings will be processed.
113+
pub fn walk(&'p self, it: &mut impl FnMut(&'p Self) -> bool) {
114+
if !it(self) {
115+
return;
120116
}
121-
}
122117

123-
/// Report the subpatterns that were not useful, if any.
124-
pub(crate) fn redundant_subpatterns(&self) -> Vec<&Self> {
125-
let mut subpats = Vec::new();
126-
self.collect_redundant_subpatterns(&mut subpats);
127-
subpats
128-
}
129-
fn collect_redundant_subpatterns<'a>(&'a self, subpats: &mut Vec<&'a Self>) {
130-
// We don't look at subpatterns if we already reported the whole pattern as redundant.
131-
if !self.is_useful() {
132-
subpats.push(self);
133-
} else {
134-
for p in self.iter_fields() {
135-
p.collect_redundant_subpatterns(subpats);
136-
}
118+
for p in self.iter_fields() {
119+
p.walk(it)
137120
}
138121
}
139122
}

compiler/rustc_pattern_analysis/src/usefulness.rs

+33-6
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,38 @@ pub enum Usefulness<'p, Cx: TypeCx> {
15911591
Redundant,
15921592
}
15931593

1594+
/// Report whether this pattern was found useful, and its subpatterns that were not useful if any.
1595+
fn collect_pattern_usefulness<'p, Cx: TypeCx>(
1596+
pat: &'p DeconstructedPat<'p, Cx>,
1597+
) -> Usefulness<'p, Cx> {
1598+
fn pat_is_useful<'p, Cx: TypeCx>(pat: &'p DeconstructedPat<'p, Cx>) -> bool {
1599+
if pat.useful.get() {
1600+
true
1601+
} else if pat.is_or_pat() && pat.iter_fields().any(|f| pat_is_useful(f)) {
1602+
// We always expand or patterns in the matrix, so we will never see the actual
1603+
// or-pattern (the one with constructor `Or`) in the column. As such, it will not be
1604+
// marked as useful itself, only its children will. We recover this information here.
1605+
true
1606+
} else {
1607+
false
1608+
}
1609+
}
1610+
1611+
let mut subpats = Vec::new();
1612+
pat.walk(&mut |p| {
1613+
if pat_is_useful(p) {
1614+
// The pattern is useful, so we recurse to find redundant subpatterns.
1615+
true
1616+
} else {
1617+
// The pattern is redundant.
1618+
subpats.push(p);
1619+
false // stop recursing
1620+
}
1621+
});
1622+
1623+
if pat_is_useful(pat) { Usefulness::Useful(subpats) } else { Usefulness::Redundant }
1624+
}
1625+
15941626
/// The output of checking a match for exhaustiveness and arm usefulness.
15951627
pub struct UsefulnessReport<'p, Cx: TypeCx> {
15961628
/// For each arm of the input, whether that arm is useful after the arms above it.
@@ -1619,12 +1651,7 @@ pub fn compute_match_usefulness<'p, Cx: TypeCx>(
16191651
.copied()
16201652
.map(|arm| {
16211653
debug!(?arm);
1622-
// We warn when a pattern is not useful.
1623-
let usefulness = if arm.pat.is_useful() {
1624-
Usefulness::Useful(arm.pat.redundant_subpatterns())
1625-
} else {
1626-
Usefulness::Redundant
1627-
};
1654+
let usefulness = collect_pattern_usefulness(arm.pat);
16281655
(arm, usefulness)
16291656
})
16301657
.collect();

0 commit comments

Comments
 (0)