1
Fork 0

Avoid passing a full Pat when only the Span/HirId is used

This commit is contained in:
Oli Scherer 2024-12-11 20:39:50 +00:00
parent 9942b77c5b
commit 5df69191cb

View file

@ -279,7 +279,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.check_pat_tuple_struct(pat, qpath, subpats, ddpos, expected, pat_info) self.check_pat_tuple_struct(pat, qpath, subpats, ddpos, expected, pat_info)
} }
PatKind::Path(ref qpath) => { PatKind::Path(ref qpath) => {
self.check_pat_path(pat, qpath, path_res.unwrap(), expected, ti) self.check_pat_path(pat.hir_id, pat.span, qpath, path_res.unwrap(), expected, ti)
} }
PatKind::Struct(ref qpath, fields, has_rest_pat) => { PatKind::Struct(ref qpath, fields, has_rest_pat) => {
self.check_pat_struct(pat, qpath, fields, has_rest_pat, expected, pat_info) self.check_pat_struct(pat, qpath, fields, has_rest_pat, expected, pat_info)
@ -1053,7 +1053,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
fn check_pat_path( fn check_pat_path(
&self, &self,
pat: &Pat<'tcx>, hir_id: HirId,
span: Span,
qpath: &hir::QPath<'_>, qpath: &hir::QPath<'_>,
path_resolution: (Res, Option<LoweredTy<'tcx>>, &'tcx [hir::PathSegment<'tcx>]), path_resolution: (Res, Option<LoweredTy<'tcx>>, &'tcx [hir::PathSegment<'tcx>]),
expected: Ty<'tcx>, expected: Ty<'tcx>,
@ -1072,8 +1073,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} }
Res::Def(DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn) | DefKind::Variant, _) => { Res::Def(DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn) | DefKind::Variant, _) => {
let expected = "unit struct, unit variant or constant"; let expected = "unit struct, unit variant or constant";
let e = let e = report_unexpected_variant_res(tcx, res, None, qpath, span, E0533, expected);
report_unexpected_variant_res(tcx, res, None, qpath, pat.span, E0533, expected);
return Ty::new_error(tcx, e); return Ty::new_error(tcx, e);
} }
Res::SelfCtor(def_id) => { Res::SelfCtor(def_id) => {
@ -1088,7 +1088,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
res, res,
None, None,
qpath, qpath,
pat.span, span,
E0533, E0533,
"unit struct", "unit struct",
); );
@ -1107,11 +1107,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Type-check the path. // Type-check the path.
let (pat_ty, pat_res) = let (pat_ty, pat_res) =
self.instantiate_value_path(segments, opt_ty, res, pat.span, pat.span, pat.hir_id); self.instantiate_value_path(segments, opt_ty, res, span, span, hir_id);
if let Err(err) = if let Err(err) =
self.demand_suptype_with_origin(&self.pattern_cause(ti, pat.span), expected, pat_ty) self.demand_suptype_with_origin(&self.pattern_cause(ti, span), expected, pat_ty)
{ {
self.emit_bad_pat_path(err, pat, res, pat_res, pat_ty, segments); self.emit_bad_pat_path(err, hir_id, span, res, pat_res, pat_ty, segments);
} }
pat_ty pat_ty
} }
@ -1154,13 +1154,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
fn emit_bad_pat_path( fn emit_bad_pat_path(
&self, &self,
mut e: Diag<'_>, mut e: Diag<'_>,
pat: &hir::Pat<'tcx>, hir_id: HirId,
pat_span: Span,
res: Res, res: Res,
pat_res: Res, pat_res: Res,
pat_ty: Ty<'tcx>, pat_ty: Ty<'tcx>,
segments: &'tcx [hir::PathSegment<'tcx>], segments: &'tcx [hir::PathSegment<'tcx>],
) { ) {
let pat_span = pat.span;
if let Some(span) = self.tcx.hir().res_span(pat_res) { if let Some(span) = self.tcx.hir().res_span(pat_res) {
e.span_label(span, format!("{} defined here", res.descr())); e.span_label(span, format!("{} defined here", res.descr()));
if let [hir::PathSegment { ident, .. }] = &*segments { if let [hir::PathSegment { ident, .. }] = &*segments {
@ -1173,7 +1173,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
res.descr(), res.descr(),
), ),
); );
match self.tcx.parent_hir_node(pat.hir_id) { match self.tcx.parent_hir_node(hir_id) {
hir::Node::PatField(..) => { hir::Node::PatField(..) => {
e.span_suggestion_verbose( e.span_suggestion_verbose(
ident.span.shrink_to_hi(), ident.span.shrink_to_hi(),