Skip to content

Commit

Permalink
Add some more comments on how TestKind works
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjasper committed May 25, 2019
1 parent 99b5e76 commit 9b55f76
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1185,8 +1185,8 @@ pub enum TerminatorKind<'tcx> {
FalseEdges {
/// The target normal control flow will take
real_target: BasicBlock,
/// A block control flow could conceptually take, but won't
/// in practice
/// A block control flow could conceptually jump to, but won't in
/// practice
imaginary_target: BasicBlock,
},
/// A terminator for blocks that only take one path in reality, but where we
Expand Down
27 changes: 22 additions & 5 deletions src/librustc_mir/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,29 +717,46 @@ pub struct MatchPair<'pat, 'tcx: 'pat> {

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

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

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

// test whether the value falls within an inclusive or exclusive range
/// Test whether the value falls within an inclusive or exclusive range
Range(PatternRange<'tcx>),

// test length of the slice is equal to len
/// Test length of the slice is equal to len
Len {
len: u64,
op: BinOp,
Expand Down
6 changes: 6 additions & 0 deletions src/librustc_mir/build/matches/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,10 +807,16 @@ impl Test<'_> {
2
}
TestKind::Switch { adt_def, .. } => {
// While the switch that we generate doesn't test for all
// variants, we have a target for each variant and the
// otherwise case, and we make sure that all of the cases not
// specified have the same block.
adt_def.variants.len() + 1
}
TestKind::SwitchInt { switch_ty, ref options, .. } => {
if switch_ty.is_bool() {
// `bool` is special cased in `perform_test` to always
// branch to two blocks.
2
} else {
options.len() + 1
Expand Down

0 comments on commit 9b55f76

Please sign in to comment.