@@ -1073,33 +1073,8 @@ impl<'tcx> PatRangeBoundary<'tcx> {
1073
1073
}
1074
1074
}
1075
1075
1076
- impl < ' tcx > Pat < ' tcx > {
1077
- /// Prints a [`Pat`] to an owned string, for user-facing diagnostics.
1078
- ///
1079
- /// If we ever switch over to storing subpatterns as `PatId`, this will also
1080
- /// need to take a context that can resolve IDs to subpatterns.
1081
- pub fn to_string ( & self ) -> String {
1082
- format ! ( "{}" , self . display( ) )
1083
- }
1084
-
1085
- /// Used internally by [`fmt::Display`] for [`PatDisplay`].
1086
- fn display ( & self ) -> PatDisplay < ' _ , ' tcx > {
1087
- PatDisplay { pat : self }
1088
- }
1089
- }
1090
-
1091
- /// Wrapper around [`&Pat<'tcx>`][`Pat`] that implements [`fmt::Display`].
1092
- ///
1093
- /// If we ever switch over to storing subpatterns as `PatId`, this will also
1094
- /// need to hold a context that can resolve IDs to subpatterns.
1095
- struct PatDisplay < ' pat , ' tcx > {
1096
- pat : & ' pat Pat < ' tcx > ,
1097
- }
1098
-
1099
- impl < ' pat , ' tcx > fmt:: Display for PatDisplay < ' pat , ' tcx > {
1076
+ impl < ' tcx > fmt:: Display for Pat < ' tcx > {
1100
1077
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1101
- let & Self { pat } = self ;
1102
-
1103
1078
// Printing lists is a chore.
1104
1079
let mut first = true ;
1105
1080
let mut start_or_continue = |s| {
@@ -1112,22 +1087,20 @@ impl<'pat, 'tcx> fmt::Display for PatDisplay<'pat, 'tcx> {
1112
1087
} ;
1113
1088
let mut start_or_comma = || start_or_continue ( ", " ) ;
1114
1089
1115
- match pat . kind {
1090
+ match self . kind {
1116
1091
PatKind :: Wild => write ! ( f, "_" ) ,
1117
1092
PatKind :: Never => write ! ( f, "!" ) ,
1118
- PatKind :: AscribeUserType { ref subpattern, .. } => {
1119
- write ! ( f, "{}: _" , subpattern. display( ) )
1120
- }
1093
+ PatKind :: AscribeUserType { ref subpattern, .. } => write ! ( f, "{subpattern}: _" ) ,
1121
1094
PatKind :: Binding { name, mode, ref subpattern, .. } => {
1122
1095
f. write_str ( mode. prefix_str ( ) ) ?;
1123
1096
write ! ( f, "{name}" ) ?;
1124
1097
if let Some ( ref subpattern) = * subpattern {
1125
- write ! ( f, " @ {}" , subpattern . display ( ) ) ?;
1098
+ write ! ( f, " @ {subpattern}" ) ?;
1126
1099
}
1127
1100
Ok ( ( ) )
1128
1101
}
1129
1102
PatKind :: Variant { ref subpatterns, .. } | PatKind :: Leaf { ref subpatterns } => {
1130
- let variant_and_name = match pat . kind {
1103
+ let variant_and_name = match self . kind {
1131
1104
PatKind :: Variant { adt_def, variant_index, .. } => ty:: tls:: with ( |tcx| {
1132
1105
let variant = adt_def. variant ( variant_index) ;
1133
1106
let adt_did = adt_def. did ( ) ;
@@ -1140,7 +1113,7 @@ impl<'pat, 'tcx> fmt::Display for PatDisplay<'pat, 'tcx> {
1140
1113
} ;
1141
1114
Some ( ( variant, name) )
1142
1115
} ) ,
1143
- _ => pat . ty . ty_adt_def ( ) . and_then ( |adt_def| {
1116
+ _ => self . ty . ty_adt_def ( ) . and_then ( |adt_def| {
1144
1117
if !adt_def. is_enum ( ) {
1145
1118
ty:: tls:: with ( |tcx| {
1146
1119
Some ( ( adt_def. non_enum_variant ( ) , tcx. def_path_str ( adt_def. did ( ) ) ) )
@@ -1165,11 +1138,11 @@ impl<'pat, 'tcx> fmt::Display for PatDisplay<'pat, 'tcx> {
1165
1138
continue ;
1166
1139
}
1167
1140
let name = variant. fields [ p. field ] . name ;
1168
- write ! ( f, "{}{}: {}" , start_or_comma( ) , name, p. pattern. display ( ) ) ?;
1141
+ write ! ( f, "{}{}: {}" , start_or_comma( ) , name, p. pattern) ?;
1169
1142
printed += 1 ;
1170
1143
}
1171
1144
1172
- let is_union = pat . ty . ty_adt_def ( ) . is_some_and ( |adt| adt. is_union ( ) ) ;
1145
+ let is_union = self . ty . ty_adt_def ( ) . is_some_and ( |adt| adt. is_union ( ) ) ;
1173
1146
if printed < variant. fields . len ( ) && ( !is_union || printed == 0 ) {
1174
1147
write ! ( f, "{}.." , start_or_comma( ) ) ?;
1175
1148
}
@@ -1188,14 +1161,14 @@ impl<'pat, 'tcx> fmt::Display for PatDisplay<'pat, 'tcx> {
1188
1161
// Common case: the field is where we expect it.
1189
1162
if let Some ( p) = subpatterns. get ( i) {
1190
1163
if p. field . index ( ) == i {
1191
- write ! ( f, "{}" , p. pattern. display ( ) ) ?;
1164
+ write ! ( f, "{}" , p. pattern) ?;
1192
1165
continue ;
1193
1166
}
1194
1167
}
1195
1168
1196
1169
// Otherwise, we have to go looking for it.
1197
1170
if let Some ( p) = subpatterns. iter ( ) . find ( |p| p. field . index ( ) == i) {
1198
- write ! ( f, "{}" , p. pattern. display ( ) ) ?;
1171
+ write ! ( f, "{}" , p. pattern) ?;
1199
1172
} else {
1200
1173
write ! ( f, "_" ) ?;
1201
1174
}
@@ -1206,45 +1179,45 @@ impl<'pat, 'tcx> fmt::Display for PatDisplay<'pat, 'tcx> {
1206
1179
Ok ( ( ) )
1207
1180
}
1208
1181
PatKind :: Deref { ref subpattern } => {
1209
- match pat . ty . kind ( ) {
1182
+ match self . ty . kind ( ) {
1210
1183
ty:: Adt ( def, _) if def. is_box ( ) => write ! ( f, "box " ) ?,
1211
1184
ty:: Ref ( _, _, mutbl) => {
1212
1185
write ! ( f, "&{}" , mutbl. prefix_str( ) ) ?;
1213
1186
}
1214
- _ => bug ! ( "{} is a bad Deref pattern type" , pat . ty) ,
1187
+ _ => bug ! ( "{} is a bad Deref pattern type" , self . ty) ,
1215
1188
}
1216
- write ! ( f, "{}" , subpattern . display ( ) )
1189
+ write ! ( f, "{subpattern}" )
1217
1190
}
1218
1191
PatKind :: DerefPattern { ref subpattern, .. } => {
1219
- write ! ( f, "deref!({})" , subpattern . display ( ) )
1192
+ write ! ( f, "deref!({subpattern })" )
1220
1193
}
1221
1194
PatKind :: Constant { value } => write ! ( f, "{value}" ) ,
1222
1195
PatKind :: InlineConstant { def : _, ref subpattern } => {
1223
- write ! ( f, "{} (from inline const)" , subpattern. display ( ) )
1196
+ write ! ( f, "{} (from inline const)" , subpattern)
1224
1197
}
1225
1198
PatKind :: Range ( ref range) => write ! ( f, "{range}" ) ,
1226
1199
PatKind :: Slice { ref prefix, ref slice, ref suffix }
1227
1200
| PatKind :: Array { ref prefix, ref slice, ref suffix } => {
1228
1201
write ! ( f, "[" ) ?;
1229
1202
for p in prefix. iter ( ) {
1230
- write ! ( f, "{}{}" , start_or_comma( ) , p. display ( ) ) ?;
1203
+ write ! ( f, "{}{}" , start_or_comma( ) , p) ?;
1231
1204
}
1232
1205
if let Some ( ref slice) = * slice {
1233
1206
write ! ( f, "{}" , start_or_comma( ) ) ?;
1234
1207
match slice. kind {
1235
1208
PatKind :: Wild => { }
1236
- _ => write ! ( f, "{}" , slice . display ( ) ) ?,
1209
+ _ => write ! ( f, "{slice}" ) ?,
1237
1210
}
1238
1211
write ! ( f, ".." ) ?;
1239
1212
}
1240
1213
for p in suffix. iter ( ) {
1241
- write ! ( f, "{}{}" , start_or_comma( ) , p. display ( ) ) ?;
1214
+ write ! ( f, "{}{}" , start_or_comma( ) , p) ?;
1242
1215
}
1243
1216
write ! ( f, "]" )
1244
1217
}
1245
1218
PatKind :: Or { ref pats } => {
1246
1219
for pat in pats. iter ( ) {
1247
- write ! ( f, "{}{}" , start_or_continue( " | " ) , pat. display ( ) ) ?;
1220
+ write ! ( f, "{}{}" , start_or_continue( " | " ) , pat) ?;
1248
1221
}
1249
1222
Ok ( ( ) )
1250
1223
}
0 commit comments