Skip to content

Commit 7f6f522

Browse files
committed
Rename TestCase to TestableCase
1 parent 6e7dd2c commit 7f6f522

File tree

5 files changed

+64
-54
lines changed

5 files changed

+64
-54
lines changed

compiler/rustc_mir_build/src/builder/matches/buckets.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use tracing::debug;
77

88
use crate::builder::Builder;
99
use crate::builder::matches::test::is_switch_ty;
10-
use crate::builder::matches::{Candidate, Test, TestBranch, TestCase, TestKind};
10+
use crate::builder::matches::{Candidate, Test, TestBranch, TestKind, TestableCase};
1111

1212
/// Output of [`Builder::partition_candidates_into_buckets`].
1313
pub(crate) struct PartitionedCandidates<'tcx, 'b, 'c> {
@@ -140,12 +140,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
140140
// branch, so it can be removed. If false, the match pair is _compatible_
141141
// with its test branch, but still needs a more specific test.
142142
let fully_matched;
143-
let ret = match (&test.kind, &match_pair.test_case) {
143+
let ret = match (&test.kind, &match_pair.testable_case) {
144144
// If we are performing a variant switch, then this
145145
// informs variant patterns, but nothing else.
146146
(
147147
&TestKind::Switch { adt_def: tested_adt_def },
148-
&TestCase::Variant { adt_def, variant_index },
148+
&TestableCase::Variant { adt_def, variant_index },
149149
) => {
150150
assert_eq!(adt_def, tested_adt_def);
151151
fully_matched = true;
@@ -159,24 +159,23 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
159159
// things out here, in some cases.
160160
//
161161
// FIXME(Zalathar): Is the `is_switch_ty` test unnecessary?
162-
(TestKind::SwitchInt, &TestCase::Constant { value })
162+
(TestKind::SwitchInt, &TestableCase::Constant { value })
163163
if is_switch_ty(match_pair.pattern_ty) =>
164164
{
165165
// An important invariant of candidate bucketing is that a candidate
166166
// must not match in multiple branches. For `SwitchInt` tests, adding
167167
// a new value might invalidate that property for range patterns that
168168
// have already been partitioned into the failure arm, so we must take care
169169
// not to add such values here.
170-
let is_covering_range = |test_case: &TestCase<'tcx>| {
171-
test_case.as_range().is_some_and(|range| {
170+
let is_covering_range = |testable_case: &TestableCase<'tcx>| {
171+
testable_case.as_range().is_some_and(|range| {
172172
matches!(range.contains(value, self.tcx), None | Some(true))
173173
})
174174
};
175175
let is_conflicting_candidate = |candidate: &&mut Candidate<'tcx>| {
176-
candidate
177-
.match_pairs
178-
.iter()
179-
.any(|mp| mp.place == Some(test_place) && is_covering_range(&mp.test_case))
176+
candidate.match_pairs.iter().any(|mp| {
177+
mp.place == Some(test_place) && is_covering_range(&mp.testable_case)
178+
})
180179
};
181180
if prior_candidates
182181
.get(&TestBranch::Failure)
@@ -189,7 +188,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
189188
Some(TestBranch::Constant(value))
190189
}
191190
}
192-
(TestKind::SwitchInt, TestCase::Range(range)) => {
191+
(TestKind::SwitchInt, TestableCase::Range(range)) => {
193192
// When performing a `SwitchInt` test, a range pattern can be
194193
// sorted into the failure arm if it doesn't contain _any_ of
195194
// the values being tested. (This restricts what values can be
@@ -207,7 +206,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
207206
})
208207
}
209208

210-
(TestKind::If, TestCase::Constant { value }) => {
209+
(TestKind::If, TestableCase::Constant { value }) => {
211210
fully_matched = true;
212211
let value = value.try_to_bool().unwrap_or_else(|| {
213212
span_bug!(test.span, "expected boolean value but got {value:?}")
@@ -217,7 +216,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
217216

218217
(
219218
&TestKind::Len { len: test_len, op: BinOp::Eq },
220-
&TestCase::Slice { len, variable_length },
219+
&TestableCase::Slice { len, variable_length },
221220
) => {
222221
match (test_len.cmp(&(len as u64)), variable_length) {
223222
(Ordering::Equal, false) => {
@@ -249,7 +248,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
249248
}
250249
(
251250
&TestKind::Len { len: test_len, op: BinOp::Ge },
252-
&TestCase::Slice { len, variable_length },
251+
&TestableCase::Slice { len, variable_length },
253252
) => {
254253
// the test is `$actual_len >= test_len`
255254
match (test_len.cmp(&(len as u64)), variable_length) {
@@ -281,7 +280,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
281280
}
282281
}
283282

284-
(TestKind::Range(test), TestCase::Range(pat)) => {
283+
(TestKind::Range(test), TestableCase::Range(pat)) => {
285284
if test == pat {
286285
fully_matched = true;
287286
Some(TestBranch::Success)
@@ -292,7 +291,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
292291
if !test.overlaps(pat, self.tcx)? { Some(TestBranch::Failure) } else { None }
293292
}
294293
}
295-
(TestKind::Range(range), &TestCase::Constant { value }) => {
294+
(TestKind::Range(range), &TestableCase::Constant { value }) => {
296295
fully_matched = false;
297296
if !range.contains(value, self.tcx)? {
298297
// `value` is not contained in the testing range,
@@ -303,7 +302,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
303302
}
304303
}
305304

306-
(TestKind::Eq { value: test_val, .. }, TestCase::Constant { value: case_val }) => {
305+
(TestKind::Eq { value: test_val, .. }, TestableCase::Constant { value: case_val }) => {
307306
if test_val == case_val {
308307
fully_matched = true;
309308
Some(TestBranch::Success)
@@ -313,7 +312,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
313312
}
314313
}
315314

316-
(TestKind::Deref { temp: test_temp, .. }, TestCase::Deref { temp, .. })
315+
(TestKind::Deref { temp: test_temp, .. }, TestableCase::Deref { temp, .. })
317316
if test_temp == temp =>
318317
{
319318
fully_matched = true;

compiler/rustc_mir_build/src/builder/matches/match_pair.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::ty::{self, Pinnedness, Ty, TypeVisitableExt};
88

99
use crate::builder::Builder;
1010
use crate::builder::expr::as_place::{PlaceBase, PlaceBuilder};
11-
use crate::builder::matches::{FlatPat, MatchPairTree, PatternExtraData, TestCase};
11+
use crate::builder::matches::{FlatPat, MatchPairTree, PatternExtraData, TestableCase};
1212

1313
impl<'a, 'tcx> Builder<'a, 'tcx> {
1414
/// Builds and pushes [`MatchPairTree`] subtrees, one for each pattern in
@@ -132,7 +132,7 @@ impl<'tcx> MatchPairTree<'tcx> {
132132

133133
let place = place_builder.try_to_place(cx);
134134
let mut subpairs = Vec::new();
135-
let test_case = match pattern.kind {
135+
let testable_case = match pattern.kind {
136136
PatKind::Missing | PatKind::Wild | PatKind::Error(_) => None,
137137

138138
PatKind::Or { ref pats } => {
@@ -146,18 +146,18 @@ impl<'tcx> MatchPairTree<'tcx> {
146146
// FIXME(@dianne): this needs updating/removing if we always merge or-patterns
147147
extra_data.bindings.push(super::SubpatternBindings::FromOrPattern);
148148
}
149-
Some(TestCase::Or { pats })
149+
Some(TestableCase::Or { pats })
150150
}
151151

152152
PatKind::Range(ref range) => {
153153
if range.is_full_range(cx.tcx) == Some(true) {
154154
None
155155
} else {
156-
Some(TestCase::Range(Arc::clone(range)))
156+
Some(TestableCase::Range(Arc::clone(range)))
157157
}
158158
}
159159

160-
PatKind::Constant { value } => Some(TestCase::Constant { value }),
160+
PatKind::Constant { value } => Some(TestableCase::Constant { value }),
161161

162162
PatKind::AscribeUserType {
163163
ascription: Ascription { ref annotation, variance },
@@ -256,7 +256,7 @@ impl<'tcx> MatchPairTree<'tcx> {
256256
if prefix.is_empty() && slice.is_some() && suffix.is_empty() {
257257
None
258258
} else {
259-
Some(TestCase::Slice {
259+
Some(TestableCase::Slice {
260260
len: prefix.len() + suffix.len(),
261261
variable_length: slice.is_some(),
262262
})
@@ -275,7 +275,11 @@ impl<'tcx> MatchPairTree<'tcx> {
275275
cx.def_id.into(),
276276
)
277277
}) && !adt_def.variant_list_has_applicable_non_exhaustive();
278-
if irrefutable { None } else { Some(TestCase::Variant { adt_def, variant_index }) }
278+
if irrefutable {
279+
None
280+
} else {
281+
Some(TestableCase::Variant { adt_def, variant_index })
282+
}
279283
}
280284

281285
PatKind::Leaf { ref subpatterns } => {
@@ -331,17 +335,17 @@ impl<'tcx> MatchPairTree<'tcx> {
331335
&mut subpairs,
332336
extra_data,
333337
);
334-
Some(TestCase::Deref { temp, mutability })
338+
Some(TestableCase::Deref { temp, mutability })
335339
}
336340

337-
PatKind::Never => Some(TestCase::Never),
341+
PatKind::Never => Some(TestableCase::Never),
338342
};
339343

340-
if let Some(test_case) = test_case {
344+
if let Some(testable_case) = testable_case {
341345
// This pattern is refutable, so push a new match-pair node.
342346
match_pairs.push(MatchPairTree {
343347
place,
344-
test_case,
348+
testable_case,
345349
subpairs,
346350
pattern_ty: pattern.ty,
347351
pattern_span: pattern.span,

compiler/rustc_mir_build/src/builder/matches/mod.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ struct Candidate<'tcx> {
10781078
/// (see [`Builder::test_remaining_match_pairs_after_or`]).
10791079
///
10801080
/// Invariants:
1081-
/// - All or-patterns ([`TestCase::Or`]) have been sorted to the end.
1081+
/// - All or-patterns ([`TestableCase::Or`]) have been sorted to the end.
10821082
match_pairs: Vec<MatchPairTree<'tcx>>,
10831083

10841084
/// ...and if this is non-empty, one of these subcandidates also has to match...
@@ -1164,12 +1164,15 @@ impl<'tcx> Candidate<'tcx> {
11641164

11651165
/// Restores the invariant that or-patterns must be sorted to the end.
11661166
fn sort_match_pairs(&mut self) {
1167-
self.match_pairs.sort_by_key(|pair| matches!(pair.test_case, TestCase::Or { .. }));
1167+
self.match_pairs.sort_by_key(|pair| matches!(pair.testable_case, TestableCase::Or { .. }));
11681168
}
11691169

11701170
/// Returns whether the first match pair of this candidate is an or-pattern.
11711171
fn starts_with_or_pattern(&self) -> bool {
1172-
matches!(&*self.match_pairs, [MatchPairTree { test_case: TestCase::Or { .. }, .. }, ..])
1172+
matches!(
1173+
&*self.match_pairs,
1174+
[MatchPairTree { testable_case: TestableCase::Or { .. }, .. }, ..]
1175+
)
11731176
}
11741177

11751178
/// Visit the leaf candidates (those with no subcandidates) contained in
@@ -1261,7 +1264,7 @@ struct Ascription<'tcx> {
12611264
/// Instead they participate in or-pattern expansion, where they are transformed into
12621265
/// subcandidates. See [`Builder::expand_and_match_or_candidates`].
12631266
#[derive(Debug, Clone)]
1264-
enum TestCase<'tcx> {
1267+
enum TestableCase<'tcx> {
12651268
Variant { adt_def: ty::AdtDef<'tcx>, variant_index: VariantIdx },
12661269
Constant { value: ty::Value<'tcx> },
12671270
Range(Arc<PatRange<'tcx>>),
@@ -1271,7 +1274,7 @@ enum TestCase<'tcx> {
12711274
Or { pats: Box<[FlatPat<'tcx>]> },
12721275
}
12731276

1274-
impl<'tcx> TestCase<'tcx> {
1277+
impl<'tcx> TestableCase<'tcx> {
12751278
fn as_range(&self) -> Option<&PatRange<'tcx>> {
12761279
if let Self::Range(v) = self { Some(v.as_ref()) } else { None }
12771280
}
@@ -1289,12 +1292,12 @@ pub(crate) struct MatchPairTree<'tcx> {
12891292
/// ---
12901293
/// This can be `None` if it referred to a non-captured place in a closure.
12911294
///
1292-
/// Invariant: Can only be `None` when `test_case` is `Or`.
1295+
/// Invariant: Can only be `None` when `testable_case` is `Or`.
12931296
/// Therefore this must be `Some(_)` after or-pattern expansion.
12941297
place: Option<Place<'tcx>>,
12951298

12961299
/// ... must pass this test...
1297-
test_case: TestCase<'tcx>,
1300+
testable_case: TestableCase<'tcx>,
12981301

12991302
/// ... and these subpairs must match.
13001303
///
@@ -1317,7 +1320,7 @@ enum TestKind<'tcx> {
13171320
/// Test what enum variant a value is.
13181321
///
13191322
/// The subset of expected variants is not stored here; instead they are
1320-
/// extracted from the [`TestCase`]s of the candidates participating in the
1323+
/// extracted from the [`TestableCase`]s of the candidates participating in the
13211324
/// test.
13221325
Switch {
13231326
/// The enum type being tested.
@@ -1327,7 +1330,7 @@ enum TestKind<'tcx> {
13271330
/// Test what value an integer or `char` has.
13281331
///
13291332
/// The test's target values are not stored here; instead they are extracted
1330-
/// from the [`TestCase`]s of the candidates participating in the test.
1333+
/// from the [`TestableCase`]s of the candidates participating in the test.
13311334
SwitchInt,
13321335

13331336
/// Test whether a `bool` is `true` or `false`.
@@ -1948,7 +1951,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
19481951
candidate: &mut Candidate<'tcx>,
19491952
match_pair: MatchPairTree<'tcx>,
19501953
) {
1951-
let TestCase::Or { pats } = match_pair.test_case else { bug!() };
1954+
let TestableCase::Or { pats } = match_pair.testable_case else { bug!() };
19521955
debug!("expanding or-pattern: candidate={:#?}\npats={:#?}", candidate, pats);
19531956
candidate.or_span = Some(match_pair.pattern_span);
19541957
candidate.subcandidates = pats
@@ -2116,7 +2119,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
21162119
debug_assert!(
21172120
remaining_match_pairs
21182121
.iter()
2119-
.all(|match_pair| matches!(match_pair.test_case, TestCase::Or { .. }))
2122+
.all(|match_pair| matches!(match_pair.testable_case, TestableCase::Or { .. }))
21202123
);
21212124

21222125
// Visit each leaf candidate within this subtree, add a copy of the remaining

compiler/rustc_mir_build/src/builder/matches/test.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_span::{DUMMY_SP, Span, Symbol, sym};
1919
use tracing::{debug, instrument};
2020

2121
use crate::builder::Builder;
22-
use crate::builder::matches::{MatchPairTree, Test, TestBranch, TestCase, TestKind};
22+
use crate::builder::matches::{MatchPairTree, Test, TestBranch, TestKind, TestableCase};
2323

2424
impl<'a, 'tcx> Builder<'a, 'tcx> {
2525
/// Identifies what test is needed to decide if `match_pair` is applicable.
@@ -29,30 +29,34 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2929
&mut self,
3030
match_pair: &MatchPairTree<'tcx>,
3131
) -> Test<'tcx> {
32-
let kind = match match_pair.test_case {
33-
TestCase::Variant { adt_def, variant_index: _ } => TestKind::Switch { adt_def },
32+
let kind = match match_pair.testable_case {
33+
TestableCase::Variant { adt_def, variant_index: _ } => TestKind::Switch { adt_def },
3434

35-
TestCase::Constant { .. } if match_pair.pattern_ty.is_bool() => TestKind::If,
36-
TestCase::Constant { .. } if is_switch_ty(match_pair.pattern_ty) => TestKind::SwitchInt,
37-
TestCase::Constant { value } => TestKind::Eq { value, cast_ty: match_pair.pattern_ty },
35+
TestableCase::Constant { .. } if match_pair.pattern_ty.is_bool() => TestKind::If,
36+
TestableCase::Constant { .. } if is_switch_ty(match_pair.pattern_ty) => {
37+
TestKind::SwitchInt
38+
}
39+
TestableCase::Constant { value } => {
40+
TestKind::Eq { value, cast_ty: match_pair.pattern_ty }
41+
}
3842

39-
TestCase::Range(ref range) => {
43+
TestableCase::Range(ref range) => {
4044
assert_eq!(range.ty, match_pair.pattern_ty);
4145
TestKind::Range(Arc::clone(range))
4246
}
4347

44-
TestCase::Slice { len, variable_length } => {
48+
TestableCase::Slice { len, variable_length } => {
4549
let op = if variable_length { BinOp::Ge } else { BinOp::Eq };
4650
TestKind::Len { len: len as u64, op }
4751
}
4852

49-
TestCase::Deref { temp, mutability } => TestKind::Deref { temp, mutability },
53+
TestableCase::Deref { temp, mutability } => TestKind::Deref { temp, mutability },
5054

51-
TestCase::Never => TestKind::Never,
55+
TestableCase::Never => TestKind::Never,
5256

5357
// Or-patterns are not tested directly; instead they are expanded into subcandidates,
5458
// which are then distinguished by testing whatever non-or patterns they contain.
55-
TestCase::Or { .. } => bug!("or-patterns should have already been handled"),
59+
TestableCase::Or { .. } => bug!("or-patterns should have already been handled"),
5660
};
5761

5862
Test { span: match_pair.pattern_span, kind }

compiler/rustc_mir_build/src/builder/matches/util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use tracing::debug;
66

77
use crate::builder::Builder;
88
use crate::builder::expr::as_place::PlaceBase;
9-
use crate::builder::matches::{Binding, Candidate, FlatPat, MatchPairTree, TestCase};
9+
use crate::builder::matches::{Binding, Candidate, FlatPat, MatchPairTree, TestableCase};
1010

1111
impl<'a, 'tcx> Builder<'a, 'tcx> {
1212
/// Creates a false edge to `imaginary_target` and a real edge to
@@ -159,11 +159,11 @@ impl<'a, 'b, 'tcx> FakeBorrowCollector<'a, 'b, 'tcx> {
159159
}
160160

161161
fn visit_match_pair(&mut self, match_pair: &MatchPairTree<'tcx>) {
162-
if let TestCase::Or { pats, .. } = &match_pair.test_case {
162+
if let TestableCase::Or { pats, .. } = &match_pair.testable_case {
163163
for flat_pat in pats.iter() {
164164
self.visit_flat_pat(flat_pat)
165165
}
166-
} else if matches!(match_pair.test_case, TestCase::Deref { .. }) {
166+
} else if matches!(match_pair.testable_case, TestableCase::Deref { .. }) {
167167
// The subpairs of a deref pattern are all places relative to the deref temporary, so we
168168
// don't fake borrow them. Problem is, if we only shallowly fake-borrowed
169169
// `match_pair.place`, this would allow:

0 commit comments

Comments
 (0)