@@ -84,8 +84,8 @@ struct PatInfo<'tcx, 'a> {
84
84
top_info : TopInfo < ' tcx > ,
85
85
decl_origin : Option < DeclOrigin < ' a > > ,
86
86
87
- parent_kind : Option < PatKind < ' tcx > > ,
88
- current_kind : Option < PatKind < ' tcx > > ,
87
+ /// The depth of current pattern
88
+ current_depth : u32 ,
89
89
}
90
90
91
91
impl < ' tcx > FnCtxt < ' _ , ' tcx > {
@@ -155,13 +155,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
155
155
decl_origin : Option < DeclOrigin < ' tcx > > ,
156
156
) {
157
157
let info = TopInfo { expected, origin_expr, span } ;
158
- let pat_info = PatInfo {
159
- binding_mode : INITIAL_BM ,
160
- top_info : info,
161
- decl_origin,
162
- parent_kind : None ,
163
- current_kind : None ,
164
- } ;
158
+ let pat_info =
159
+ PatInfo { binding_mode : INITIAL_BM , top_info : info, decl_origin, current_depth : 0 } ;
165
160
self . check_pat ( pat, expected, pat_info) ;
166
161
}
167
162
@@ -172,7 +167,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
172
167
/// Conversely, inside this module, `check_pat_top` should never be used.
173
168
#[ instrument( level = "debug" , skip( self , pat_info) ) ]
174
169
fn check_pat ( & self , pat : & ' tcx Pat < ' tcx > , expected : Ty < ' tcx > , pat_info : PatInfo < ' tcx , ' _ > ) {
175
- let PatInfo { binding_mode : def_bm, top_info : ti, current_kind , .. } = pat_info;
170
+ let PatInfo { binding_mode : def_bm, top_info : ti, current_depth , .. } = pat_info;
176
171
177
172
let path_res = match & pat. kind {
178
173
PatKind :: Path ( qpath) => Some (
@@ -186,8 +181,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
186
181
binding_mode : def_bm,
187
182
top_info : ti,
188
183
decl_origin : pat_info. decl_origin ,
189
- parent_kind : current_kind,
190
- current_kind : Some ( pat. kind ) ,
184
+ current_depth : current_depth + 1 ,
191
185
} ;
192
186
193
187
let ty = match pat. kind {
@@ -1061,21 +1055,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1061
1055
expected : Ty < ' tcx > ,
1062
1056
pat_info : PatInfo < ' tcx , ' _ > ,
1063
1057
) -> Ty < ' tcx > {
1064
- let PatInfo { binding_mode : def_bm, top_info : ti, decl_origin, parent_kind, current_kind } =
1065
- pat_info;
1058
+ let PatInfo { binding_mode : def_bm, top_info : ti, decl_origin, current_depth } = pat_info;
1066
1059
let tcx = self . tcx ;
1067
1060
let on_error = |e| {
1068
1061
for pat in subpats {
1069
1062
self . check_pat (
1070
1063
pat,
1071
1064
Ty :: new_error ( tcx, e) ,
1072
- PatInfo {
1073
- binding_mode : def_bm,
1074
- top_info : ti,
1075
- decl_origin,
1076
- parent_kind,
1077
- current_kind,
1078
- } ,
1065
+ PatInfo { binding_mode : def_bm, top_info : ti, decl_origin, current_depth } ,
1079
1066
) ;
1080
1067
}
1081
1068
} ;
@@ -1142,13 +1129,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1142
1129
self . check_pat (
1143
1130
subpat,
1144
1131
field_ty,
1145
- PatInfo {
1146
- binding_mode : def_bm,
1147
- top_info : ti,
1148
- decl_origin,
1149
- parent_kind,
1150
- current_kind,
1151
- } ,
1132
+ PatInfo { binding_mode : def_bm, top_info : ti, decl_origin, current_depth } ,
1152
1133
) ;
1153
1134
1154
1135
self . tcx . check_stability (
@@ -2303,7 +2284,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2303
2284
expected_ty : Ty < ' tcx > ,
2304
2285
pat_info : PatInfo < ' tcx , ' _ > ,
2305
2286
) -> ErrorGuaranteed {
2306
- let PatInfo { top_info : ti, parent_kind , .. } = pat_info;
2287
+ let PatInfo { top_info : ti, current_depth , .. } = pat_info;
2307
2288
2308
2289
let mut err = struct_span_code_err ! (
2309
2290
self . dcx( ) ,
@@ -2341,8 +2322,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2341
2322
_ => ( ) ,
2342
2323
}
2343
2324
2344
- let enclosure_is_struct = parent_kind . is_some ( ) ;
2345
- if is_slice_or_array_or_vector && !enclosure_is_struct {
2325
+ let is_top_level = current_depth <= 1 ;
2326
+ if is_slice_or_array_or_vector && is_top_level {
2346
2327
err. span_suggestion (
2347
2328
span,
2348
2329
"consider slicing here" ,
0 commit comments