Skip to content

Commit df3de7b

Browse files
committed
Add some more comments on how TestKind works
1 parent a1d0266 commit df3de7b

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

src/librustc/mir/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1196,8 +1196,8 @@ pub enum TerminatorKind<'tcx> {
11961196
FalseEdges {
11971197
/// The target normal control flow will take
11981198
real_target: BasicBlock,
1199-
/// A block control flow could conceptually take, but won't
1200-
/// in practice
1199+
/// A block control flow could conceptually jump to, but won't in
1200+
/// practice
12011201
imaginary_target: BasicBlock,
12021202
},
12031203
/// A terminator for blocks that only take one path in reality, but where we

src/librustc_mir/build/matches/mod.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -715,29 +715,46 @@ pub struct MatchPair<'pat, 'tcx: 'pat> {
715715

716716
#[derive(Clone, Debug, PartialEq)]
717717
enum TestKind<'tcx> {
718-
// test the branches of enum
718+
/// Test the branches of enum.
719719
Switch {
720+
/// The enum being tested
720721
adt_def: &'tcx ty::AdtDef,
722+
/// The set of variants that we should create a branch for. We also
723+
/// create an additional "otherwise" case.
721724
variants: BitSet<VariantIdx>,
722725
},
723726

724-
// test the branches of enum
727+
/// Test what value an `integer`, `bool` or `char` has.
725728
SwitchInt {
729+
/// The type of the value that we're testing.
726730
switch_ty: Ty<'tcx>,
731+
/// The (ordered) set of values that we test for.
732+
///
733+
/// For integers and `char`s we create a branch to each of the values in
734+
/// `options`, as well as an "otherwise" branch for all other values, even
735+
/// in the (rare) case that options is exhaustive.
736+
///
737+
/// For `bool` we always generate two edges, one for `true` and one for
738+
/// `false`.
727739
options: Vec<u128>,
740+
/// Reverse map used to ensure that the values in `options` are unique.
728741
indices: FxHashMap<&'tcx ty::Const<'tcx>, usize>,
729742
},
730743

731-
// test for equality
744+
/// Test for equality with value, possibly after an unsizing coercion to
745+
/// `ty`,
732746
Eq {
733747
value: &'tcx ty::Const<'tcx>,
748+
// Integer types are handled by `SwitchInt`, and constants with ADT
749+
// types are converted back into patterns, so this can only be `&str`,
750+
// `&[T]`, `f32` or `f64`.
734751
ty: Ty<'tcx>,
735752
},
736753

737-
// test whether the value falls within an inclusive or exclusive range
754+
/// Test whether the value falls within an inclusive or exclusive range
738755
Range(PatternRange<'tcx>),
739756

740-
// test length of the slice is equal to len
757+
/// Test length of the slice is equal to len
741758
Len {
742759
len: u64,
743760
op: BinOp,

src/librustc_mir/build/matches/test.rs

+6
Original file line numberDiff line numberDiff line change
@@ -807,10 +807,16 @@ impl Test<'_> {
807807
2
808808
}
809809
TestKind::Switch { adt_def, .. } => {
810+
// While the switch that we generate doesn't test for all
811+
// variants, we have a target for each variant and the
812+
// otherwise case, and we make sure that all of the cases not
813+
// specified have the same block.
810814
adt_def.variants.len() + 1
811815
}
812816
TestKind::SwitchInt { switch_ty, ref options, .. } => {
813817
if switch_ty.is_bool() {
818+
// `bool` is special cased in `perform_test` to always
819+
// branch to two blocks.
814820
2
815821
} else {
816822
options.len() + 1

0 commit comments

Comments
 (0)