@@ -774,17 +774,16 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
774
774
}
775
775
}
776
776
777
- /// Convert to a [`print::Pat`] for diagnostic purposes.
778
- fn hoist_pat_range ( & self , range : & IntRange , ty : RevealedTy < ' tcx > ) -> print:: Pat < ' tcx > {
779
- use print:: { Pat , PatKind } ;
777
+ /// Prints an [`IntRange`] to a string for diagnostic purposes.
778
+ fn print_pat_range ( & self , range : & IntRange , ty : RevealedTy < ' tcx > ) -> String {
780
779
use MaybeInfiniteInt :: * ;
781
780
let cx = self ;
782
- let kind = if matches ! ( ( range. lo, range. hi) , ( NegInfinity , PosInfinity ) ) {
783
- PatKind :: Print ( "_" . to_string ( ) )
781
+ if matches ! ( ( range. lo, range. hi) , ( NegInfinity , PosInfinity ) ) {
782
+ "_" . to_string ( )
784
783
} else if range. is_singleton ( ) {
785
784
let lo = cx. hoist_pat_range_bdy ( range. lo , ty) ;
786
785
let value = lo. as_finite ( ) . unwrap ( ) ;
787
- PatKind :: Print ( value. to_string ( ) )
786
+ value. to_string ( )
788
787
} else {
789
788
// We convert to an inclusive range for diagnostics.
790
789
let mut end = rustc_hir:: RangeEnd :: Included ;
@@ -807,33 +806,24 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
807
806
range. hi
808
807
} ;
809
808
let hi = cx. hoist_pat_range_bdy ( hi, ty) ;
810
- PatKind :: Print ( PatRange { lo, hi, end, ty : ty. inner ( ) } . to_string ( ) )
811
- } ;
812
-
813
- Pat { ty : ty. inner ( ) , kind }
809
+ PatRange { lo, hi, end, ty : ty. inner ( ) } . to_string ( )
810
+ }
814
811
}
815
812
816
813
/// Prints a [`WitnessPat`] to an owned string, for diagnostic purposes.
814
+ ///
815
+ /// This panics for patterns that don't appear in diagnostics, like float ranges.
817
816
pub fn print_witness_pat ( & self , pat : & WitnessPat < ' p , ' tcx > ) -> String {
818
- // This works by converting the witness pattern to a `print::Pat`
819
- // and then printing that, but callers don't need to know that.
820
- self . hoist_witness_pat ( pat) . to_string ( )
821
- }
822
-
823
- /// Convert to a [`print::Pat`] for diagnostic purposes. This panics for patterns that don't
824
- /// appear in diagnostics, like float ranges.
825
- fn hoist_witness_pat ( & self , pat : & WitnessPat < ' p , ' tcx > ) -> print:: Pat < ' tcx > {
826
- use print:: { FieldPat , Pat , PatKind } ;
827
817
let cx = self ;
828
- let hoist = |p| Box :: new ( cx. hoist_witness_pat ( p ) ) ;
829
- let kind = match pat. ctor ( ) {
830
- Bool ( b) => PatKind :: Print ( b. to_string ( ) ) ,
831
- Str ( s) => PatKind :: Print ( s. to_string ( ) ) ,
832
- IntRange ( range) => return self . hoist_pat_range ( range, * pat. ty ( ) ) ,
818
+ let print = |p| cx. print_witness_pat ( p ) ;
819
+ match pat. ctor ( ) {
820
+ Bool ( b) => b. to_string ( ) ,
821
+ Str ( s) => s. to_string ( ) ,
822
+ IntRange ( range) => return self . print_pat_range ( range, * pat. ty ( ) ) ,
833
823
Struct if pat. ty ( ) . is_box ( ) => {
834
824
// Outside of the `alloc` crate, the only way to create a struct pattern
835
825
// of type `Box` is to use a `box` pattern via #[feature(box_patterns)].
836
- PatKind :: Print ( format ! ( "box {}" , hoist ( & pat. fields[ 0 ] ) ) )
826
+ format ! ( "box {}" , print ( & pat. fields[ 0 ] ) )
837
827
}
838
828
Struct | Variant ( _) | UnionField => {
839
829
let enum_info = match * pat. ty ( ) . kind ( ) {
@@ -848,9 +838,9 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
848
838
let subpatterns = pat
849
839
. iter_fields ( )
850
840
. enumerate ( )
851
- . map ( |( i, pat) | FieldPat {
841
+ . map ( |( i, pat) | print :: FieldPat {
852
842
field : FieldIdx :: new ( i) ,
853
- pattern : hoist ( pat) ,
843
+ pattern : print ( pat) ,
854
844
is_wildcard : would_print_as_wildcard ( cx. tcx , pat) ,
855
845
} )
856
846
. collect :: < Vec < _ > > ( ) ;
@@ -864,12 +854,12 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
864
854
& subpatterns,
865
855
)
866
856
. unwrap ( ) ;
867
- PatKind :: Print ( s )
857
+ s
868
858
}
869
859
Ref => {
870
860
let mut s = String :: new ( ) ;
871
- print:: write_ref_like ( & mut s, pat. ty ( ) . inner ( ) , & hoist ( & pat. fields [ 0 ] ) ) . unwrap ( ) ;
872
- PatKind :: Print ( s )
861
+ print:: write_ref_like ( & mut s, pat. ty ( ) . inner ( ) , & print ( & pat. fields [ 0 ] ) ) . unwrap ( ) ;
862
+ s
873
863
}
874
864
Slice ( slice) => {
875
865
let ( prefix_len, has_dot_dot) = match slice. kind {
@@ -897,27 +887,23 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
897
887
}
898
888
}
899
889
900
- let prefix = prefix. iter ( ) . map ( hoist ) . collect :: < Vec < _ > > ( ) ;
901
- let suffix = suffix. iter ( ) . map ( hoist ) . collect :: < Vec < _ > > ( ) ;
890
+ let prefix = prefix. iter ( ) . map ( print ) . collect :: < Vec < _ > > ( ) ;
891
+ let suffix = suffix. iter ( ) . map ( print ) . collect :: < Vec < _ > > ( ) ;
902
892
903
893
let mut s = String :: new ( ) ;
904
894
print:: write_slice_like ( & mut s, & prefix, has_dot_dot, & suffix) . unwrap ( ) ;
905
- PatKind :: Print ( s)
906
- }
907
- Never if self . tcx . features ( ) . never_patterns => PatKind :: Print ( "!" . to_string ( ) ) ,
908
- Never | Wildcard | NonExhaustive | Hidden | PrivateUninhabited => {
909
- PatKind :: Print ( "_" . to_string ( ) )
895
+ s
910
896
}
897
+ Never if self . tcx . features ( ) . never_patterns => "!" . to_string ( ) ,
898
+ Never | Wildcard | NonExhaustive | Hidden | PrivateUninhabited => "_" . to_string ( ) ,
911
899
Missing { .. } => bug ! (
912
900
"trying to convert a `Missing` constructor into a `Pat`; this is probably a bug,
913
901
`Missing` should have been processed in `apply_constructors`"
914
902
) ,
915
903
F16Range ( ..) | F32Range ( ..) | F64Range ( ..) | F128Range ( ..) | Opaque ( ..) | Or => {
916
904
bug ! ( "can't convert to pattern: {:?}" , pat)
917
905
}
918
- } ;
919
-
920
- Pat { ty : pat. ty ( ) . inner ( ) , kind }
906
+ }
921
907
}
922
908
}
923
909
@@ -993,7 +979,7 @@ impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> {
993
979
overlaps_on : IntRange ,
994
980
overlaps_with : & [ & crate :: pat:: DeconstructedPat < Self > ] ,
995
981
) {
996
- let overlap_as_pat = self . hoist_pat_range ( & overlaps_on, * pat. ty ( ) ) ;
982
+ let overlap_as_pat = self . print_pat_range ( & overlaps_on, * pat. ty ( ) ) ;
997
983
let overlaps: Vec < _ > = overlaps_with
998
984
. iter ( )
999
985
. map ( |pat| pat. data ( ) . span )
@@ -1033,7 +1019,7 @@ impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> {
1033
1019
suggested_range. end = rustc_hir:: RangeEnd :: Included ;
1034
1020
suggested_range. to_string ( )
1035
1021
} ;
1036
- let gap_as_pat = self . hoist_pat_range ( & gap, * pat. ty ( ) ) ;
1022
+ let gap_as_pat = self . print_pat_range ( & gap, * pat. ty ( ) ) ;
1037
1023
if gapped_with. is_empty ( ) {
1038
1024
// If `gapped_with` is empty, `gap == T::MAX`.
1039
1025
self . tcx . emit_node_span_lint (
0 commit comments