Skip to content

Commit 451349a

Browse files
authored
Rollup merge of #99889 - compiler-errors:cleanup-ti, r=cjgillot
Remove `parent_pat` from `TopInfo` We can get the parent pat from the hir map.
2 parents 852bf84 + 940ec1e commit 451349a

File tree

1 file changed

+18
-36
lines changed
  • compiler/rustc_typeck/src/check

1 file changed

+18
-36
lines changed

compiler/rustc_typeck/src/check/pat.rs

+18-36
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,6 @@ struct TopInfo<'tcx> {
7272
/// found type `std::result::Result<_, _>`
7373
/// ```
7474
span: Option<Span>,
75-
/// This refers to the parent pattern. Used to provide extra diagnostic information on errors.
76-
/// ```text
77-
/// error[E0308]: mismatched types
78-
/// --> $DIR/const-in-struct-pat.rs:8:17
79-
/// |
80-
/// L | struct f;
81-
/// | --------- unit struct defined here
82-
/// ...
83-
/// L | let Thing { f } = t;
84-
/// | ^
85-
/// | |
86-
/// | expected struct `std::string::String`, found struct `f`
87-
/// | `f` is interpreted as a unit struct, not a new binding
88-
/// | help: bind the struct field to a different name instead: `f: other_f`
89-
/// ```
90-
parent_pat: Option<&'tcx Pat<'tcx>>,
9175
}
9276

9377
impl<'tcx> FnCtxt<'_, 'tcx> {
@@ -147,7 +131,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
147131
span: Option<Span>,
148132
origin_expr: bool,
149133
) {
150-
let info = TopInfo { expected, origin_expr, span, parent_pat: None };
134+
let info = TopInfo { expected, origin_expr, span };
151135
self.check_pat(pat, expected, INITIAL_BM, info);
152136
}
153137

@@ -190,9 +174,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
190174
self.check_pat_struct(pat, qpath, fields, has_rest_pat, expected, def_bm, ti)
191175
}
192176
PatKind::Or(pats) => {
193-
let parent_pat = Some(pat);
194177
for pat in pats {
195-
self.check_pat(pat, expected, def_bm, TopInfo { parent_pat, ..ti });
178+
self.check_pat(pat, expected, def_bm, ti);
196179
}
197180
expected
198181
}
@@ -621,7 +604,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
621604
}
622605

623606
if let Some(p) = sub {
624-
self.check_pat(p, expected, def_bm, TopInfo { parent_pat: Some(pat), ..ti });
607+
self.check_pat(p, expected, def_bm, ti);
625608
}
626609

627610
local_ty
@@ -782,7 +765,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
782765
let Some((variant, pat_ty)) = self.check_struct_path(qpath, pat.hir_id) else {
783766
let err = self.tcx.ty_error();
784767
for field in fields {
785-
let ti = TopInfo { parent_pat: Some(pat), ..ti };
768+
let ti = ti;
786769
self.check_pat(field.pat, err, def_bm, ti);
787770
}
788771
return err;
@@ -799,11 +782,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
799782
}
800783
}
801784

802-
fn check_pat_path<'b>(
785+
fn check_pat_path(
803786
&self,
804-
pat: &Pat<'_>,
787+
pat: &Pat<'tcx>,
805788
qpath: &hir::QPath<'_>,
806-
path_resolution: (Res, Option<Ty<'tcx>>, &'b [hir::PathSegment<'b>]),
789+
path_resolution: (Res, Option<Ty<'tcx>>, &'tcx [hir::PathSegment<'tcx>]),
807790
expected: Ty<'tcx>,
808791
ti: TopInfo<'tcx>,
809792
) -> Ty<'tcx> {
@@ -837,7 +820,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
837820
if let Some(err) =
838821
self.demand_suptype_with_origin(&self.pattern_cause(ti, pat.span), expected, pat_ty)
839822
{
840-
self.emit_bad_pat_path(err, pat.span, res, pat_res, pat_ty, segments, ti.parent_pat);
823+
self.emit_bad_pat_path(err, pat, res, pat_res, pat_ty, segments);
841824
}
842825
pat_ty
843826
}
@@ -876,16 +859,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
876859
false
877860
}
878861

879-
fn emit_bad_pat_path<'b>(
862+
fn emit_bad_pat_path(
880863
&self,
881864
mut e: DiagnosticBuilder<'_, ErrorGuaranteed>,
882-
pat_span: Span,
865+
pat: &hir::Pat<'tcx>,
883866
res: Res,
884867
pat_res: Res,
885868
pat_ty: Ty<'tcx>,
886-
segments: &'b [hir::PathSegment<'b>],
887-
parent_pat: Option<&Pat<'_>>,
869+
segments: &'tcx [hir::PathSegment<'tcx>],
888870
) {
871+
let pat_span = pat.span;
889872
if let Some(span) = self.tcx.hir().res_span(pat_res) {
890873
e.span_label(span, &format!("{} defined here", res.descr()));
891874
if let [hir::PathSegment { ident, .. }] = &*segments {
@@ -898,8 +881,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
898881
res.descr(),
899882
),
900883
);
901-
match parent_pat {
902-
Some(Pat { kind: hir::PatKind::Struct(..), .. }) => {
884+
match self.tcx.hir().get(self.tcx.hir().get_parent_node(pat.hir_id)) {
885+
hir::Node::Pat(Pat { kind: hir::PatKind::Struct(..), .. }) => {
903886
e.span_suggestion_verbose(
904887
ident.span.shrink_to_hi(),
905888
"bind the struct field to a different name instead",
@@ -960,9 +943,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
960943
) -> Ty<'tcx> {
961944
let tcx = self.tcx;
962945
let on_error = || {
963-
let parent_pat = Some(pat);
964946
for pat in subpats {
965-
self.check_pat(pat, tcx.ty_error(), def_bm, TopInfo { parent_pat, ..ti });
947+
self.check_pat(pat, tcx.ty_error(), def_bm, ti);
966948
}
967949
};
968950
let report_unexpected_res = |res: Res| {
@@ -1046,7 +1028,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10461028
};
10471029
for (i, subpat) in subpats.iter().enumerate_and_adjust(variant.fields.len(), ddpos) {
10481030
let field_ty = self.field_ty(subpat.span, &variant.fields[i], substs);
1049-
self.check_pat(subpat, field_ty, def_bm, TopInfo { parent_pat: Some(pat), ..ti });
1031+
self.check_pat(subpat, field_ty, def_bm, ti);
10501032

10511033
self.tcx.check_stability(
10521034
variant.fields[i].did,
@@ -1324,7 +1306,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13241306
}
13251307
};
13261308

1327-
self.check_pat(field.pat, field_ty, def_bm, TopInfo { parent_pat: Some(pat), ..ti });
1309+
self.check_pat(field.pat, field_ty, def_bm, ti);
13281310
}
13291311

13301312
let mut unmentioned_fields = variant
@@ -1936,7 +1918,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19361918
let err = tcx.ty_error();
19371919
(err, err)
19381920
};
1939-
self.check_pat(inner, inner_ty, def_bm, TopInfo { parent_pat: Some(pat), ..ti });
1921+
self.check_pat(inner, inner_ty, def_bm, ti);
19401922
rptr_ty
19411923
}
19421924

0 commit comments

Comments
 (0)