Skip to content

Commit 04c966b

Browse files
Use counter intead of two Options
1 parent fe143a2 commit 04c966b

File tree

1 file changed

+12
-31
lines changed
  • compiler/rustc_hir_typeck/src

1 file changed

+12
-31
lines changed

compiler/rustc_hir_typeck/src/pat.rs

+12-31
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ struct PatInfo<'tcx, 'a> {
8484
top_info: TopInfo<'tcx>,
8585
decl_origin: Option<DeclOrigin<'a>>,
8686

87-
parent_kind: Option<PatKind<'tcx>>,
88-
current_kind: Option<PatKind<'tcx>>,
87+
/// The depth of current pattern
88+
current_depth: u32,
8989
}
9090

9191
impl<'tcx> FnCtxt<'_, 'tcx> {
@@ -155,13 +155,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
155155
decl_origin: Option<DeclOrigin<'tcx>>,
156156
) {
157157
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 };
165160
self.check_pat(pat, expected, pat_info);
166161
}
167162

@@ -172,7 +167,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
172167
/// Conversely, inside this module, `check_pat_top` should never be used.
173168
#[instrument(level = "debug", skip(self, pat_info))]
174169
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;
176171

177172
let path_res = match &pat.kind {
178173
PatKind::Path(qpath) => Some(
@@ -186,8 +181,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
186181
binding_mode: def_bm,
187182
top_info: ti,
188183
decl_origin: pat_info.decl_origin,
189-
parent_kind: current_kind,
190-
current_kind: Some(pat.kind),
184+
current_depth: current_depth + 1,
191185
};
192186

193187
let ty = match pat.kind {
@@ -1061,21 +1055,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10611055
expected: Ty<'tcx>,
10621056
pat_info: PatInfo<'tcx, '_>,
10631057
) -> 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;
10661059
let tcx = self.tcx;
10671060
let on_error = |e| {
10681061
for pat in subpats {
10691062
self.check_pat(
10701063
pat,
10711064
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 },
10791066
);
10801067
}
10811068
};
@@ -1142,13 +1129,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11421129
self.check_pat(
11431130
subpat,
11441131
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 },
11521133
);
11531134

11541135
self.tcx.check_stability(
@@ -2303,7 +2284,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23032284
expected_ty: Ty<'tcx>,
23042285
pat_info: PatInfo<'tcx, '_>,
23052286
) -> ErrorGuaranteed {
2306-
let PatInfo { top_info: ti, parent_kind, .. } = pat_info;
2287+
let PatInfo { top_info: ti, current_depth, .. } = pat_info;
23072288

23082289
let mut err = struct_span_code_err!(
23092290
self.dcx(),
@@ -2341,8 +2322,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23412322
_ => (),
23422323
}
23432324

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 {
23462327
err.span_suggestion(
23472328
span,
23482329
"consider slicing here",

0 commit comments

Comments
 (0)