From fea1fe7f01e99e0ef9eb6ceac8c97fb17ff3263e Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 29 Apr 2024 09:48:19 +0000 Subject: [PATCH] Avoid some `def_span` query calls --- .../rustc_hir_analysis/src/collect/type_of.rs | 22 ++++++++----------- compiler/rustc_middle/src/hir/map/mod.rs | 2 +- compiler/rustc_mir_build/src/build/mod.rs | 3 ++- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/collect/type_of.rs b/compiler/rustc_hir_analysis/src/collect/type_of.rs index 9d7deebac48..5ccfd06f258 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of.rs @@ -24,7 +24,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { let hir_id = tcx.local_def_id_to_hir_id(def_id); let node = tcx.hir_node(hir_id); - let Node::AnonConst(_) = node else { + let Node::AnonConst(&AnonConst { span, .. }) = node else { span_bug!( tcx.def_span(def_id), "expected anon const in `anon_const_type_of`, got {node:?}" @@ -134,7 +134,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { // I dont think it's possible to reach this but I'm not 100% sure - BoxyUwU return Ty::new_error_with_message( tcx, - tcx.def_span(def_id), + span, "unexpected non-GAT usage of an anon const", ); } @@ -152,7 +152,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { let Some(type_dependent_def) = tables.type_dependent_def_id(parent_node_id) else { return Ty::new_error_with_message( tcx, - tcx.def_span(def_id), + span, format!("unable to find type-dependent def for {parent_node_id:?}"), ); }; @@ -194,7 +194,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { } else { return Ty::new_error_with_message( tcx, - tcx.def_span(def_id), + span, format!("unable to find const parent for {hir_id} in pat {pat:?}"), ); } @@ -202,7 +202,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { _ => { return Ty::new_error_with_message( tcx, - tcx.def_span(def_id), + span, format!("unexpected const parent path {parent_node:?}"), ); } @@ -226,11 +226,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { .map(|idx| (idx, seg)) }) }) else { - return Ty::new_error_with_message( - tcx, - tcx.def_span(def_id), - "no arg matching AnonConst in path", - ); + return Ty::new_error_with_message(tcx, span, "no arg matching AnonConst in path"); }; let generics = match tcx.res_generics_def_id(segment.res) { @@ -238,7 +234,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { None => { return Ty::new_error_with_message( tcx, - tcx.def_span(def_id), + span, format!("unexpected anon const res {:?} in path: {:?}", segment.res, path), ); } @@ -250,7 +246,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { _ => { return Ty::new_error_with_message( tcx, - tcx.def_span(def_id), + span, format!("unexpected const parent in type_of(): {parent_node:?}"), ); } @@ -278,7 +274,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { } else { return Ty::new_error_with_message( tcx, - tcx.def_span(def_id), + span, format!("const generic parameter not found in {generics:?} at position {arg_idx:?}"), ); } diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index c8be8b45046..c7aea137b68 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -885,7 +885,7 @@ impl<'hir> Map<'hir> { Node::ImplItem(impl_item) => impl_item.span, Node::Variant(variant) => variant.span, Node::Field(field) => field.span, - Node::AnonConst(constant) => self.body(constant.body).value.span, + Node::AnonConst(constant) => constant.span, Node::ConstBlock(constant) => self.body(constant.body).value.span, Node::Expr(expr) => expr.span, Node::ExprField(field) => field.span, diff --git a/compiler/rustc_mir_build/src/build/mod.rs b/compiler/rustc_mir_build/src/build/mod.rs index 3c18afe1a78..8c20366c7c5 100644 --- a/compiler/rustc_mir_build/src/build/mod.rs +++ b/compiler/rustc_mir_build/src/build/mod.rs @@ -566,7 +566,8 @@ fn construct_const<'a, 'tcx>( span, .. }) => (*span, ty.span), - Node::AnonConst(_) | Node::ConstBlock(_) => { + Node::AnonConst(ct) => (ct.span, ct.span), + Node::ConstBlock(_) => { let span = tcx.def_span(def); (span, span) }