@@ -24,6 +24,7 @@ use pattern::{FieldPattern, Pattern, PatternKind};
24
24
use pattern:: { PatternFoldable , PatternFolder } ;
25
25
26
26
use rustc:: hir:: def_id:: DefId ;
27
+ use rustc:: hir:: RangeEnd ;
27
28
use rustc:: ty:: { self , AdtKind , Ty , TyCtxt , TypeFoldable } ;
28
29
29
30
use rustc:: mir:: Field ;
@@ -206,8 +207,8 @@ pub enum Constructor {
206
207
Variant ( DefId ) ,
207
208
/// Literal values.
208
209
ConstantValue ( ConstVal ) ,
209
- /// Ranges of literal values (2..5 ).
210
- ConstantRange ( ConstVal , ConstVal ) ,
210
+ /// Ranges of literal values (` 2...5` and `2..5` ).
211
+ ConstantRange ( ConstVal , ConstVal , RangeEnd ) ,
211
212
/// Array patterns of length n.
212
213
Slice ( usize ) ,
213
214
}
@@ -686,8 +687,8 @@ fn pat_constructors(_cx: &mut MatchCheckCtxt,
686
687
Some ( vec ! [ Variant ( adt_def. variants[ variant_index] . did) ] ) ,
687
688
PatternKind :: Constant { ref value } =>
688
689
Some ( vec ! [ ConstantValue ( value. clone( ) ) ] ) ,
689
- PatternKind :: Range { ref lo, ref hi } =>
690
- Some ( vec ! [ ConstantRange ( lo. clone( ) , hi. clone( ) ) ] ) ,
690
+ PatternKind :: Range { ref lo, ref hi, ref end } =>
691
+ Some ( vec ! [ ConstantRange ( lo. clone( ) , hi. clone( ) , end . clone ( ) ) ] ) ,
691
692
PatternKind :: Array { .. } => match pcx. ty . sty {
692
693
ty:: TyArray ( _, length) => Some ( vec ! [ Slice ( length) ] ) ,
693
694
_ => span_bug ! ( pat. span, "bad ty {:?} for array pattern" , pcx. ty)
@@ -791,17 +792,33 @@ fn slice_pat_covered_by_constructor(_tcx: TyCtxt, _span: Span,
791
792
792
793
fn range_covered_by_constructor ( tcx : TyCtxt , span : Span ,
793
794
ctor : & Constructor ,
794
- from : & ConstVal , to : & ConstVal )
795
+ from : & ConstVal , to : & ConstVal ,
796
+ end : RangeEnd )
795
797
-> Result < bool , ErrorReported > {
796
- let ( c_from, c_to) = match * ctor {
797
- ConstantValue ( ref value) => ( value, value) ,
798
- ConstantRange ( ref from, ref to) => ( from, to) ,
799
- Single => return Ok ( true ) ,
800
- _ => bug ! ( )
801
- } ;
802
- let cmp_from = compare_const_vals ( tcx, span, c_from, from) ?;
803
- let cmp_to = compare_const_vals ( tcx, span, c_to, to) ?;
804
- Ok ( cmp_from != Ordering :: Less && cmp_to != Ordering :: Greater )
798
+ let cmp_from = |c_from| Ok ( compare_const_vals ( tcx, span, c_from, from) ? != Ordering :: Less ) ;
799
+ let cmp_to = |c_to| compare_const_vals ( tcx, span, c_to, to) ;
800
+ match * ctor {
801
+ ConstantValue ( ref value) => {
802
+ let to = cmp_to ( value) ?;
803
+ let end = ( to != Ordering :: Greater ) ||
804
+ ( end == RangeEnd :: Excluded && to == Ordering :: Equal ) ;
805
+ Ok ( cmp_from ( value) ? && end)
806
+ } ,
807
+ ConstantRange ( ref from, ref to, RangeEnd :: Included ) => {
808
+ let to = cmp_to ( to) ?;
809
+ let end = ( to != Ordering :: Greater ) ||
810
+ ( end == RangeEnd :: Excluded && to == Ordering :: Equal ) ;
811
+ Ok ( cmp_from ( from) ? && end)
812
+ } ,
813
+ ConstantRange ( ref from, ref to, RangeEnd :: Excluded ) => {
814
+ let to = cmp_to ( to) ?;
815
+ let end = ( to == Ordering :: Less ) ||
816
+ ( end == RangeEnd :: Excluded && to == Ordering :: Equal ) ;
817
+ Ok ( cmp_from ( from) ? && end)
818
+ }
819
+ Single => Ok ( true ) ,
820
+ _ => bug ! ( ) ,
821
+ }
805
822
}
806
823
807
824
fn patterns_for_variant < ' p , ' a : ' p , ' tcx : ' a > (
@@ -872,7 +889,7 @@ fn specialize<'p, 'a: 'p, 'tcx: 'a>(
872
889
} ,
873
890
_ => {
874
891
match range_covered_by_constructor (
875
- cx. tcx , pat. span , constructor, value, value
892
+ cx. tcx , pat. span , constructor, value, value, RangeEnd :: Included
876
893
) {
877
894
Ok ( true ) => Some ( vec ! [ ] ) ,
878
895
Ok ( false ) => None ,
@@ -882,9 +899,9 @@ fn specialize<'p, 'a: 'p, 'tcx: 'a>(
882
899
}
883
900
}
884
901
885
- PatternKind :: Range { ref lo, ref hi } => {
902
+ PatternKind :: Range { ref lo, ref hi, ref end } => {
886
903
match range_covered_by_constructor (
887
- cx. tcx , pat. span , constructor, lo, hi
904
+ cx. tcx , pat. span , constructor, lo, hi, end . clone ( )
888
905
) {
889
906
Ok ( true ) => Some ( vec ! [ ] ) ,
890
907
Ok ( false ) => None ,
0 commit comments