@@ -667,6 +667,9 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
667
667
}
668
668
}
669
669
670
+ /// Takes a HIR Path. If the path is a constant, evaluates it and feeds
671
+ /// it to `const_to_pat`. Any other path (like enum variants without fields)
672
+ /// is converted to the corresponding pattern via `lower_variant_or_leaf`
670
673
fn lower_path ( & mut self ,
671
674
qpath : & hir:: QPath ,
672
675
id : hir:: HirId ,
@@ -722,6 +725,10 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
722
725
}
723
726
}
724
727
728
+ /// Converts literals, paths and negation of literals to patterns.
729
+ /// The special case for negation exists to allow things like -128i8
730
+ /// which would overflow if we tried to evaluate 128i8 and then negate
731
+ /// afterwards.
725
732
fn lower_lit ( & mut self , expr : & ' tcx hir:: Expr ) -> PatternKind < ' tcx > {
726
733
match expr. node {
727
734
hir:: ExprLit ( ref lit) => {
@@ -767,6 +774,9 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
767
774
}
768
775
}
769
776
777
+ /// Converts an evaluated constant to a pattern (if possible).
778
+ /// This means aggregate values (like structs and enums) are converted
779
+ /// to a pattern that matches the value (as if you'd compare via eq).
770
780
fn const_to_pat (
771
781
& self ,
772
782
instance : ty:: Instance < ' tcx > ,
@@ -844,14 +854,14 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
844
854
} ,
845
855
ConstVal :: Unevaluated ( ..) =>
846
856
span_bug ! ( span, "{:#?} is not a valid enum constant" , cv) ,
847
- }
857
+ }
848
858
} ,
849
859
ty:: TyAdt ( adt_def, _) => {
850
860
let struct_var = adt_def. non_enum_variant ( ) ;
851
861
PatternKind :: Leaf {
852
862
subpatterns : adt_subpatterns ( struct_var. fields . len ( ) , None ) ,
853
- }
854
863
}
864
+ }
855
865
ty:: TyTuple ( fields, _) => {
856
866
PatternKind :: Leaf {
857
867
subpatterns : adt_subpatterns ( fields. len ( ) , None ) ,
0 commit comments