@@ -774,17 +774,16 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
774774 }
775775 }
776776
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 {
780779 use MaybeInfiniteInt :: * ;
781780 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 ( )
784783 } else if range. is_singleton ( ) {
785784 let lo = cx. hoist_pat_range_bdy ( range. lo , ty) ;
786785 let value = lo. as_finite ( ) . unwrap ( ) ;
787- PatKind :: Print ( value. to_string ( ) )
786+ value. to_string ( )
788787 } else {
789788 // We convert to an inclusive range for diagnostics.
790789 let mut end = rustc_hir:: RangeEnd :: Included ;
@@ -807,33 +806,24 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
807806 range. hi
808807 } ;
809808 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+ }
814811 }
815812
816813 /// 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.
817816 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 } ;
827817 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 ( ) ) ,
833823 Struct if pat. ty ( ) . is_box ( ) => {
834824 // Outside of the `alloc` crate, the only way to create a struct pattern
835825 // 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 ] ) )
837827 }
838828 Struct | Variant ( _) | UnionField => {
839829 let enum_info = match * pat. ty ( ) . kind ( ) {
@@ -848,9 +838,9 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
848838 let subpatterns = pat
849839 . iter_fields ( )
850840 . enumerate ( )
851- . map ( |( i, pat) | FieldPat {
841+ . map ( |( i, pat) | print :: FieldPat {
852842 field : FieldIdx :: new ( i) ,
853- pattern : hoist ( pat) ,
843+ pattern : print ( pat) ,
854844 is_wildcard : would_print_as_wildcard ( cx. tcx , pat) ,
855845 } )
856846 . collect :: < Vec < _ > > ( ) ;
@@ -864,12 +854,12 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
864854 & subpatterns,
865855 )
866856 . unwrap ( ) ;
867- PatKind :: Print ( s )
857+ s
868858 }
869859 Ref => {
870860 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
873863 }
874864 Slice ( slice) => {
875865 let ( prefix_len, has_dot_dot) = match slice. kind {
@@ -897,27 +887,23 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
897887 }
898888 }
899889
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 < _ > > ( ) ;
902892
903893 let mut s = String :: new ( ) ;
904894 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
910896 }
897+ Never if self . tcx . features ( ) . never_patterns => "!" . to_string ( ) ,
898+ Never | Wildcard | NonExhaustive | Hidden | PrivateUninhabited => "_" . to_string ( ) ,
911899 Missing { .. } => bug ! (
912900 "trying to convert a `Missing` constructor into a `Pat`; this is probably a bug,
913901 `Missing` should have been processed in `apply_constructors`"
914902 ) ,
915903 F16Range ( ..) | F32Range ( ..) | F64Range ( ..) | F128Range ( ..) | Opaque ( ..) | Or => {
916904 bug ! ( "can't convert to pattern: {:?}" , pat)
917905 }
918- } ;
919-
920- Pat { ty : pat. ty ( ) . inner ( ) , kind }
906+ }
921907 }
922908}
923909
@@ -993,7 +979,7 @@ impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> {
993979 overlaps_on : IntRange ,
994980 overlaps_with : & [ & crate :: pat:: DeconstructedPat < Self > ] ,
995981 ) {
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 ( ) ) ;
997983 let overlaps: Vec < _ > = overlaps_with
998984 . iter ( )
999985 . map ( |pat| pat. data ( ) . span )
@@ -1033,7 +1019,7 @@ impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> {
10331019 suggested_range. end = rustc_hir:: RangeEnd :: Included ;
10341020 suggested_range. to_string ( )
10351021 } ;
1036- let gap_as_pat = self . hoist_pat_range ( & gap, * pat. ty ( ) ) ;
1022+ let gap_as_pat = self . print_pat_range ( & gap, * pat. ty ( ) ) ;
10371023 if gapped_with. is_empty ( ) {
10381024 // If `gapped_with` is empty, `gap == T::MAX`.
10391025 self . tcx . emit_node_span_lint (
0 commit comments