Skip to content

Commit 9b55f76

Browse files
committed
Add some more comments on how TestKind works
1 parent 99b5e76 commit 9b55f76

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
@@ -1185,8 +1185,8 @@ pub enum TerminatorKind<'tcx> {
11851185
FalseEdges {
11861186
/// The target normal control flow will take
11871187
real_target: BasicBlock,
1188-
/// A block control flow could conceptually take, but won't
1189-
/// in practice
1188+
/// A block control flow could conceptually jump to, but won't in
1189+
/// practice
11901190
imaginary_target: BasicBlock,
11911191
},
11921192
/// 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
@@ -717,29 +717,46 @@ pub struct MatchPair<'pat, 'tcx: 'pat> {
717717

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

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

733-
// test for equality
746+
/// Test for equality with value, possibly after an unsizing coercion to
747+
/// `ty`,
734748
Eq {
735749
value: &'tcx ty::Const<'tcx>,
750+
// Integer types are handled by `SwitchInt`, and constants with ADT
751+
// types are converted back into patterns, so this can only be `&str`,
752+
// `&[T]`, `f32` or `f64`.
736753
ty: Ty<'tcx>,
737754
},
738755

739-
// test whether the value falls within an inclusive or exclusive range
756+
/// Test whether the value falls within an inclusive or exclusive range
740757
Range(PatternRange<'tcx>),
741758

742-
// test length of the slice is equal to len
759+
/// Test length of the slice is equal to len
743760
Len {
744761
len: u64,
745762
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)