Skip to content

Commit 854feae

Browse files
committed
Remove TestCase::Irrefutable
1 parent ef44273 commit 854feae

File tree

4 files changed

+38
-58
lines changed

4 files changed

+38
-58
lines changed

Diff for: compiler/rustc_mir_build/src/builder/matches/match_pair.rs

+33-31
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,23 @@ impl<'tcx> MatchPairTree<'tcx> {
116116
}
117117

118118
let place = place_builder.try_to_place(cx);
119-
let default_irrefutable = || TestCase::Irrefutable {};
120119
let mut subpairs = Vec::new();
121120
let test_case = match pattern.kind {
122-
PatKind::Wild | PatKind::Error(_) => default_irrefutable(),
121+
PatKind::Wild | PatKind::Error(_) => None,
123122

124-
PatKind::Or { ref pats } => TestCase::Or {
123+
PatKind::Or { ref pats } => Some(TestCase::Or {
125124
pats: pats.iter().map(|pat| FlatPat::new(place_builder.clone(), pat, cx)).collect(),
126-
},
125+
}),
127126

128127
PatKind::Range(ref range) => {
129128
if range.is_full_range(cx.tcx) == Some(true) {
130-
default_irrefutable()
129+
None
131130
} else {
132-
TestCase::Range(Arc::clone(range))
131+
Some(TestCase::Range(Arc::clone(range)))
133132
}
134133
}
135134

136-
PatKind::Constant { value } => TestCase::Constant { value },
135+
PatKind::Constant { value } => Some(TestCase::Constant { value }),
137136

138137
PatKind::AscribeUserType {
139138
ascription: Ascription { ref annotation, variance },
@@ -154,7 +153,7 @@ impl<'tcx> MatchPairTree<'tcx> {
154153
extra_data.ascriptions.push(super::Ascription { source, annotation, variance });
155154
}
156155

157-
default_irrefutable()
156+
None
158157
}
159158

160159
PatKind::Binding { mode, var, ref subpattern, .. } => {
@@ -199,12 +198,12 @@ impl<'tcx> MatchPairTree<'tcx> {
199198
});
200199
}
201200

202-
default_irrefutable()
201+
None
203202
}
204203

205204
PatKind::ExpandedConstant { subpattern: ref pattern, def_id: _, is_inline: false } => {
206205
MatchPairTree::for_pattern(place_builder, pattern, cx, &mut subpairs, extra_data);
207-
default_irrefutable()
206+
None
208207
}
209208
PatKind::ExpandedConstant { subpattern: ref pattern, def_id, is_inline: true } => {
210209
MatchPairTree::for_pattern(place_builder, pattern, cx, &mut subpairs, extra_data);
@@ -233,7 +232,7 @@ impl<'tcx> MatchPairTree<'tcx> {
233232
extra_data.ascriptions.push(super::Ascription { annotation, source, variance });
234233
}
235234

236-
default_irrefutable()
235+
None
237236
}
238237

239238
PatKind::Array { ref prefix, ref slice, ref suffix } => {
@@ -245,7 +244,7 @@ impl<'tcx> MatchPairTree<'tcx> {
245244
slice,
246245
suffix,
247246
);
248-
default_irrefutable()
247+
None
249248
}
250249
PatKind::Slice { ref prefix, ref slice, ref suffix } => {
251250
cx.prefix_slice_suffix(
@@ -258,12 +257,12 @@ impl<'tcx> MatchPairTree<'tcx> {
258257
);
259258

260259
if prefix.is_empty() && slice.is_some() && suffix.is_empty() {
261-
default_irrefutable()
260+
None
262261
} else {
263-
TestCase::Slice {
262+
Some(TestCase::Slice {
264263
len: prefix.len() + suffix.len(),
265264
variable_length: slice.is_some(),
266-
}
265+
})
267266
}
268267
}
269268

@@ -279,16 +278,12 @@ impl<'tcx> MatchPairTree<'tcx> {
279278
.apply_ignore_module(cx.tcx, cx.infcx.typing_env(cx.param_env))
280279
}) && (adt_def.did().is_local()
281280
|| !adt_def.is_variant_list_non_exhaustive());
282-
if irrefutable {
283-
default_irrefutable()
284-
} else {
285-
TestCase::Variant { adt_def, variant_index }
286-
}
281+
if irrefutable { None } else { Some(TestCase::Variant { adt_def, variant_index }) }
287282
}
288283

289284
PatKind::Leaf { ref subpatterns } => {
290285
cx.field_match_pairs(&mut subpairs, extra_data, place_builder, subpatterns);
291-
default_irrefutable()
286+
None
292287
}
293288

294289
PatKind::Deref { ref subpattern } => {
@@ -299,7 +294,7 @@ impl<'tcx> MatchPairTree<'tcx> {
299294
&mut subpairs,
300295
extra_data,
301296
);
302-
default_irrefutable()
297+
None
303298
}
304299

305300
PatKind::DerefPattern { ref subpattern, mutability } => {
@@ -316,18 +311,25 @@ impl<'tcx> MatchPairTree<'tcx> {
316311
&mut subpairs,
317312
extra_data,
318313
);
319-
TestCase::Deref { temp, mutability }
314+
Some(TestCase::Deref { temp, mutability })
320315
}
321316

322-
PatKind::Never => TestCase::Never,
317+
PatKind::Never => Some(TestCase::Never),
323318
};
324319

325-
match_pairs.push(MatchPairTree {
326-
place,
327-
test_case,
328-
subpairs,
329-
pattern_ty: pattern.ty,
330-
pattern_span: pattern.span,
331-
});
320+
if let Some(test_case) = test_case {
321+
// This pattern is refutable, so push a new match-pair node.
322+
match_pairs.push(MatchPairTree {
323+
place,
324+
test_case,
325+
subpairs,
326+
pattern_ty: pattern.ty,
327+
pattern_span: pattern.span,
328+
})
329+
} else {
330+
// This pattern is irrefutable, so it doesn't need its own match-pair node.
331+
// Just push its refutable subpatterns instead, if any.
332+
match_pairs.extend(subpairs);
333+
}
332334
}
333335
}

Diff for: compiler/rustc_mir_build/src/builder/matches/mod.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,6 @@ struct Candidate<'tcx> {
10541054
/// (see [`Builder::test_remaining_match_pairs_after_or`]).
10551055
///
10561056
/// Invariants:
1057-
/// - All [`TestCase::Irrefutable`] patterns have been removed by simplification.
10581057
/// - All or-patterns ([`TestCase::Or`]) have been sorted to the end.
10591058
match_pairs: Vec<MatchPairTree<'tcx>>,
10601059

@@ -1226,17 +1225,11 @@ struct Ascription<'tcx> {
12261225
/// - [`Builder::pick_test_for_match_pair`] (to choose a test)
12271226
/// - [`Builder::sort_candidate`] (to see how the test interacts with a match pair)
12281227
///
1229-
/// Two variants are unlike the others and deserve special mention:
1230-
///
1231-
/// - [`Self::Irrefutable`] is only used temporarily when building a [`MatchPairTree`].
1232-
/// They are then flattened away by [`Builder::simplify_match_pairs`], with any
1233-
/// bindings/ascriptions incorporated into the enclosing [`FlatPat`].
1234-
/// - [`Self::Or`] are not tested directly like the other variants. Instead they
1235-
/// participate in or-pattern expansion, where they are transformed into subcandidates.
1236-
/// - See [`Builder::expand_and_match_or_candidates`].
1228+
/// Note that or-patterns are not tested directly like the other variants.
1229+
/// Instead they participate in or-pattern expansion, where they are transformed into
1230+
/// subcandidates. See [`Builder::expand_and_match_or_candidates`].
12371231
#[derive(Debug, Clone)]
12381232
enum TestCase<'tcx> {
1239-
Irrefutable {},
12401233
Variant { adt_def: ty::AdtDef<'tcx>, variant_index: VariantIdx },
12411234
Constant { value: mir::Const<'tcx> },
12421235
Range(Arc<PatRange<'tcx>>),
@@ -1269,10 +1262,6 @@ pub(crate) struct MatchPairTree<'tcx> {
12691262
place: Option<Place<'tcx>>,
12701263

12711264
/// ... must pass this test...
1272-
///
1273-
/// ---
1274-
/// Invariant: after creation and simplification in [`FlatPat::new`],
1275-
/// this must not be [`TestCase::Irrefutable`].
12761265
test_case: TestCase<'tcx>,
12771266

12781267
/// ... and these subpairs must match.

Diff for: compiler/rustc_mir_build/src/builder/matches/simplify.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
4747
// match lowering forces us to lower bindings inside or-patterns last.
4848
for mut match_pair in mem::take(match_pairs) {
4949
self.simplify_match_pairs(&mut match_pair.subpairs, extra_data);
50-
if let TestCase::Irrefutable {} = match_pair.test_case {
51-
// Simplifiable pattern; we replace it with its already simplified subpairs.
52-
match_pairs.append(&mut match_pair.subpairs);
53-
} else {
54-
// Unsimplifiable pattern; we keep it.
55-
match_pairs.push(match_pair);
56-
}
50+
// Unsimplifiable pattern; we keep it.
51+
match_pairs.push(match_pair);
5752
}
5853

5954
// Move or-patterns to the end, because they can result in us

Diff for: compiler/rustc_mir_build/src/builder/matches/test.rs

-6
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5555
// Or-patterns are not tested directly; instead they are expanded into subcandidates,
5656
// which are then distinguished by testing whatever non-or patterns they contain.
5757
TestCase::Or { .. } => bug!("or-patterns should have already been handled"),
58-
59-
TestCase::Irrefutable { .. } => span_bug!(
60-
match_pair.pattern_span,
61-
"simplifiable pattern found: {:?}",
62-
match_pair.pattern_span
63-
),
6458
};
6559

6660
Test { span: match_pair.pattern_span, kind }

0 commit comments

Comments
 (0)