Skip to content

Commit

Permalink
Use counter intead of two Options
Browse files Browse the repository at this point in the history
  • Loading branch information
long-long-float committed Mar 17, 2024
1 parent fe143a2 commit 04c966b
Showing 1 changed file with 12 additions and 31 deletions.
43 changes: 12 additions & 31 deletions compiler/rustc_hir_typeck/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ struct PatInfo<'tcx, 'a> {
top_info: TopInfo<'tcx>,
decl_origin: Option<DeclOrigin<'a>>,

parent_kind: Option<PatKind<'tcx>>,
current_kind: Option<PatKind<'tcx>>,
/// The depth of current pattern
current_depth: u32,
}

impl<'tcx> FnCtxt<'_, 'tcx> {
Expand Down Expand Up @@ -155,13 +155,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
decl_origin: Option<DeclOrigin<'tcx>>,
) {
let info = TopInfo { expected, origin_expr, span };
let pat_info = PatInfo {
binding_mode: INITIAL_BM,
top_info: info,
decl_origin,
parent_kind: None,
current_kind: None,
};
let pat_info =
PatInfo { binding_mode: INITIAL_BM, top_info: info, decl_origin, current_depth: 0 };
self.check_pat(pat, expected, pat_info);
}

Expand All @@ -172,7 +167,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
/// Conversely, inside this module, `check_pat_top` should never be used.
#[instrument(level = "debug", skip(self, pat_info))]
fn check_pat(&self, pat: &'tcx Pat<'tcx>, expected: Ty<'tcx>, pat_info: PatInfo<'tcx, '_>) {
let PatInfo { binding_mode: def_bm, top_info: ti, current_kind, .. } = pat_info;
let PatInfo { binding_mode: def_bm, top_info: ti, current_depth, .. } = pat_info;

let path_res = match &pat.kind {
PatKind::Path(qpath) => Some(
Expand All @@ -186,8 +181,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
binding_mode: def_bm,
top_info: ti,
decl_origin: pat_info.decl_origin,
parent_kind: current_kind,
current_kind: Some(pat.kind),
current_depth: current_depth + 1,
};

let ty = match pat.kind {
Expand Down Expand Up @@ -1061,21 +1055,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expected: Ty<'tcx>,
pat_info: PatInfo<'tcx, '_>,
) -> Ty<'tcx> {
let PatInfo { binding_mode: def_bm, top_info: ti, decl_origin, parent_kind, current_kind } =
pat_info;
let PatInfo { binding_mode: def_bm, top_info: ti, decl_origin, current_depth } = pat_info;
let tcx = self.tcx;
let on_error = |e| {
for pat in subpats {
self.check_pat(
pat,
Ty::new_error(tcx, e),
PatInfo {
binding_mode: def_bm,
top_info: ti,
decl_origin,
parent_kind,
current_kind,
},
PatInfo { binding_mode: def_bm, top_info: ti, decl_origin, current_depth },
);
}
};
Expand Down Expand Up @@ -1142,13 +1129,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.check_pat(
subpat,
field_ty,
PatInfo {
binding_mode: def_bm,
top_info: ti,
decl_origin,
parent_kind,
current_kind,
},
PatInfo { binding_mode: def_bm, top_info: ti, decl_origin, current_depth },
);

self.tcx.check_stability(
Expand Down Expand Up @@ -2303,7 +2284,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expected_ty: Ty<'tcx>,
pat_info: PatInfo<'tcx, '_>,
) -> ErrorGuaranteed {
let PatInfo { top_info: ti, parent_kind, .. } = pat_info;
let PatInfo { top_info: ti, current_depth, .. } = pat_info;

let mut err = struct_span_code_err!(
self.dcx(),
Expand Down Expand Up @@ -2341,8 +2322,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_ => (),
}

let enclosure_is_struct = parent_kind.is_some();
if is_slice_or_array_or_vector && !enclosure_is_struct {
let is_top_level = current_depth <= 1;
if is_slice_or_array_or_vector && is_top_level {
err.span_suggestion(
span,
"consider slicing here",
Expand Down

0 comments on commit 04c966b

Please sign in to comment.