Skip to content

Commit

Permalink
Auto merge of #90146 - cjgillot:no-id-map, r=nagisa
Browse files Browse the repository at this point in the history
Reduce use of LocalDefId <-> HirId maps

This is an attempt to reduce the perf effect of #89278.
r? `@ghost`
  • Loading branch information
bors committed Jan 15, 2022
2 parents ec4bcaa + 8617ff0 commit f9d61cd
Show file tree
Hide file tree
Showing 87 changed files with 374 additions and 413 deletions.
5 changes: 3 additions & 2 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
let generics = tcx.generics_of(self.mir_def_id());
let param = generics.type_param(&param_ty, tcx);
if let Some(generics) = tcx
.hir()
.get_generics(tcx.typeck_root_def_id(self.mir_def_id().to_def_id()))
.typeck_root_def_id(self.mir_def_id().to_def_id())
.as_local()
.and_then(|def_id| tcx.hir().get_generics(def_id))
{
suggest_constraining_type_param(
tcx,
Expand Down
68 changes: 32 additions & 36 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,42 +628,39 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
};
(
true,
td.as_local().and_then(|tld| {
let h = hir_map.local_def_id_to_hir_id(tld);
match hir_map.find(h) {
Some(Node::Item(hir::Item {
kind: hir::ItemKind::Trait(_, _, _, _, items),
..
})) => {
let mut f_in_trait_opt = None;
for hir::TraitItemRef { id: fi, kind: k, .. } in *items {
let hi = fi.hir_id();
if !matches!(k, hir::AssocItemKind::Fn { .. }) {
continue;
}
if hir_map.name(hi) != hir_map.name(my_hir) {
continue;
}
f_in_trait_opt = Some(hi);
break;
td.as_local().and_then(|tld| match hir_map.find_by_def_id(tld) {
Some(Node::Item(hir::Item {
kind: hir::ItemKind::Trait(_, _, _, _, items),
..
})) => {
let mut f_in_trait_opt = None;
for hir::TraitItemRef { id: fi, kind: k, .. } in *items {
let hi = fi.hir_id();
if !matches!(k, hir::AssocItemKind::Fn { .. }) {
continue;
}
f_in_trait_opt.and_then(|f_in_trait| match hir_map.find(f_in_trait) {
Some(Node::TraitItem(hir::TraitItem {
kind:
hir::TraitItemKind::Fn(
hir::FnSig { decl: hir::FnDecl { inputs, .. }, .. },
_,
),
..
})) => {
let hir::Ty { span, .. } = inputs[local.index() - 1];
Some(span)
}
_ => None,
})
if hir_map.name(hi) != hir_map.name(my_hir) {
continue;
}
f_in_trait_opt = Some(hi);
break;
}
_ => None,
f_in_trait_opt.and_then(|f_in_trait| match hir_map.find(f_in_trait) {
Some(Node::TraitItem(hir::TraitItem {
kind:
hir::TraitItemKind::Fn(
hir::FnSig { decl: hir::FnDecl { inputs, .. }, .. },
_,
),
..
})) => {
let hir::Ty { span, .. } = inputs[local.index() - 1];
Some(span)
}
_ => None,
})
}
_ => None,
}),
)
}
Expand Down Expand Up @@ -896,7 +893,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
if look_at_return && hir.get_return_block(closure_id).is_some() {
// ...otherwise we are probably in the tail expression of the function, point at the
// return type.
match hir.get(hir.get_parent_item(fn_call_id)) {
match hir.get_by_def_id(hir.get_parent_item(fn_call_id)) {
hir::Node::Item(hir::Item { ident, kind: hir::ItemKind::Fn(sig, ..), .. })
| hir::Node::TraitItem(hir::TraitItem {
ident,
Expand Down Expand Up @@ -1075,8 +1072,7 @@ fn get_mut_span_in_struct_field<'tcx>(
if let ty::Adt(def, _) = ty.kind() {
let field = def.all_fields().nth(field.index())?;
// Use the HIR types to construct the diagnostic message.
let hir_id = tcx.hir().local_def_id_to_hir_id(field.did.as_local()?);
let node = tcx.hir().find(hir_id)?;
let node = tcx.hir().find_by_def_id(field.did.as_local()?)?;
// Now we're dealing with the actual struct that we're going to suggest a change to,
// we can expect a field that is an immutable reference to a type.
if let hir::Node::Field(field) = node {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/diagnostics/region_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
hir::AsyncGeneratorKind::Block => " of async block",
hir::AsyncGeneratorKind::Closure => " of async closure",
hir::AsyncGeneratorKind::Fn => {
let parent_item = hir.get(hir.get_parent_item(mir_hir_id));
let parent_item = hir.get_by_def_id(hir.get_parent_item(mir_hir_id));
let output = &parent_item
.fn_decl()
.expect("generator lowered from async fn should be in fn")
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
//
// As a result, if this id is an FFI item (foreign item) then we only
// let it through if it's included statically.
match tcx.hir().get(tcx.hir().local_def_id_to_hir_id(def_id)) {
match tcx.hir().get_by_def_id(def_id) {
Node::ForeignItem(..) => {
tcx.is_statically_included_foreign_item(def_id).then_some(def_id)
}
Expand Down
13 changes: 7 additions & 6 deletions compiler/rustc_const_eval/src/const_eval/fn_queries.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::TyCtxt;
use rustc_span::symbol::Symbol;
Expand All @@ -15,7 +15,8 @@ pub fn is_unstable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Symbol> {
}
}

pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let parent_id = tcx.hir().get_parent_node(hir_id);
matches!(
tcx.hir().get(parent_id),
Expand All @@ -29,15 +30,15 @@ pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
/// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether
/// said intrinsic has a `rustc_const_{un,}stable` attribute.
fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());

let node = tcx.hir().get(hir_id);
let def_id = def_id.expect_local();
let node = tcx.hir().get_by_def_id(def_id);

if let hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) =
node
{
// Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other
// foreign items cannot be evaluated at compile-time.
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
if let Abi::RustIntrinsic | Abi::PlatformIntrinsic = tcx.hir().get_foreign_abi(hir_id) {
tcx.lookup_const_stability(def_id).is_some()
} else {
Expand All @@ -50,7 +51,7 @@ fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool {

// If the function itself is not annotated with `const`, it may still be a `const fn`
// if it resides in a const trait impl.
is_parent_const_impl_raw(tcx, hir_id)
is_parent_const_impl_raw(tcx, def_id)
} else {
matches!(node, hir::Node::Ctor(_))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
// Prevent const trait methods from being annotated as `stable`.
// FIXME: Do this as part of stability checking.
if self.is_const_stable_const_fn() {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
if crate::const_eval::is_parent_const_impl_raw(tcx, hir_id) {
if crate::const_eval::is_parent_const_impl_raw(tcx, def_id) {
self.ccx
.tcx
.sess
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@ impl Qualif for CustomEq {
// because that component may be part of an enum variant (e.g.,
// `Option::<NonStructuralMatchTy>::Some`), in which case some values of this type may be
// structural-match (`Option::None`).
let id = cx.tcx.hir().local_def_id_to_hir_id(cx.def_id());
traits::search_for_structural_match_violation(id, cx.body.span, cx.tcx, ty).is_some()
traits::search_for_structural_match_violation(cx.body.span, cx.tcx, ty).is_some()
}

fn in_adt_inherently<'tcx>(
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_hir/src/hir_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ pub struct HirId {
}

impl HirId {
#[inline]
pub fn expect_owner(self) -> LocalDefId {
assert_eq!(self.local_id.index(), 0);
self.owner
}

#[inline]
pub fn as_owner(self) -> Option<LocalDefId> {
if self.local_id.index() == 0 { Some(self.owner) } else { None }
}
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_incremental/src/persist/dirty_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ impl<'tcx> DirtyCleanVisitor<'tcx> {
/// Return all DepNode labels that should be asserted for this item.
/// index=0 is the "name" used for error messages
fn auto_labels(&mut self, item_id: LocalDefId, attr: &Attribute) -> (&'static str, Labels) {
let hir_id = self.tcx.hir().local_def_id_to_hir_id(item_id);
let node = self.tcx.hir().get(hir_id);
let node = self.tcx.hir().get_by_def_id(item_id);
let (name, labels) = match node {
HirNode::Item(item) => {
match item.kind {
Expand Down
27 changes: 10 additions & 17 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,10 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>(
) -> (String, Span) {
let sm = tcx.sess.source_map();

let scope = region.free_region_binding_scope(tcx);
let node = tcx.hir().local_def_id_to_hir_id(scope.expect_local());
let scope = region.free_region_binding_scope(tcx).expect_local();
match *region {
ty::ReEarlyBound(ref br) => {
let mut sp = sm.guess_head_span(tcx.hir().span(node));
let mut sp = sm.guess_head_span(tcx.def_span(scope));
if let Some(param) =
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name))
{
Expand All @@ -166,7 +165,7 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>(
ty::ReFree(ty::FreeRegion {
bound_region: ty::BoundRegionKind::BrNamed(_, name), ..
}) => {
let mut sp = sm.guess_head_span(tcx.hir().span(node));
let mut sp = sm.guess_head_span(tcx.def_span(scope));
if let Some(param) =
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name))
{
Expand All @@ -181,13 +180,13 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>(
} else {
(
format!("the anonymous lifetime #{} defined here", idx + 1),
tcx.hir().span(node),
tcx.def_span(scope),
)
}
}
_ => (
format!("the lifetime `{}` as defined here", region),
sm.guess_head_span(tcx.hir().span(node)),
sm.guess_head_span(tcx.def_span(scope)),
),
},
_ => bug!(),
Expand Down Expand Up @@ -1759,8 +1758,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
if let Some(ValuePairs::PolyTraitRefs(exp_found)) = values {
if let ty::Closure(def_id, _) = exp_found.expected.skip_binder().self_ty().kind() {
if let Some(def_id) = def_id.as_local() {
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
let span = self.tcx.hir().span(hir_id);
let span = self.tcx.def_span(def_id);
diag.span_note(span, "this closure does not fulfill the lifetime requirements");
}
}
Expand Down Expand Up @@ -2212,9 +2210,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
if let Some(Node::Item(Item {
kind: ItemKind::Trait(..) | ItemKind::Impl { .. },
..
})) = hir.find(parent_id)
})) = hir.find_by_def_id(parent_id)
{
Some(self.tcx.generics_of(hir.local_def_id(parent_id).to_def_id()))
Some(self.tcx.generics_of(parent_id))
} else {
None
},
Expand Down Expand Up @@ -2245,7 +2243,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
if let Node::GenericParam(param) = hir.get(id) {
has_bounds = !param.bounds.is_empty();
}
let sp = hir.span(id);
let sp = self.tcx.def_span(def_id);
// `sp` only covers `T`, change it so that it covers
// `T:` when appropriate
let is_impl_trait = bound_kind.to_string().starts_with("impl ");
Expand Down Expand Up @@ -2291,12 +2289,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
.as_ref()
.and_then(|(_, g, _)| g.params.first())
.and_then(|param| param.def_id.as_local())
.map(|def_id| {
(
hir.span(hir.local_def_id_to_hir_id(def_id)).shrink_to_lo(),
format!("{}, ", new_lt),
)
});
.map(|def_id| (self.tcx.def_span(def_id).shrink_to_lo(), format!("{}, ", new_lt)));

let labeled_user_string = match bound_kind {
GenericKind::Param(ref p) => format!("the parameter type `{}`", p),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
| ObligationCauseCode::BlockTailExpression(hir_id) = cause.code()
{
let parent_id = tcx.hir().get_parent_item(*hir_id);
let parent_id = tcx.hir().local_def_id_to_hir_id(parent_id);
if let Some(fn_decl) = tcx.hir().fn_decl_by_hir_id(parent_id) {
let mut span: MultiSpan = fn_decl.output.span().into();
let mut add_label = true;
Expand Down Expand Up @@ -425,7 +426,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
let tcx = self.tcx();
match tcx.hir().get_if_local(def_id) {
Some(Node::ImplItem(impl_item)) => {
match tcx.hir().find(tcx.hir().get_parent_item(impl_item.hir_id())) {
match tcx.hir().find_by_def_id(tcx.hir().get_parent_item(impl_item.hir_id())) {
Some(Node::Item(Item {
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
..
Expand All @@ -434,13 +435,13 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
}
}
Some(Node::TraitItem(trait_item)) => {
let parent_id = tcx.hir().get_parent_item(trait_item.hir_id());
match tcx.hir().find(parent_id) {
let trait_did = tcx.hir().get_parent_item(trait_item.hir_id());
match tcx.hir().find_by_def_id(trait_did) {
Some(Node::Item(Item { kind: ItemKind::Trait(..), .. })) => {
// The method being called is defined in the `trait`, but the `'static`
// obligation comes from the `impl`. Find that `impl` so that we can point
// at it in the suggestion.
let trait_did = tcx.hir().local_def_id(parent_id).to_def_id();
let trait_did = trait_did.to_def_id();
match tcx
.hir()
.trait_impls(trait_did)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ fn may_define_opaque_type(tcx: TyCtxt<'_>, def_id: LocalDefId, opaque_hir_id: hi
let scope = tcx.hir().get_defining_scope(opaque_hir_id);
// We walk up the node tree until we hit the root or the scope of the opaque type.
while hir_id != scope && hir_id != hir::CRATE_HIR_ID {
hir_id = tcx.hir().get_parent_item(hir_id);
hir_id = tcx.hir().local_def_id_to_hir_id(tcx.hir().get_parent_item(hir_id));
}
// Syntactically, we are allowed to define the concrete type if:
let res = hir_id == scope;
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
// reported for missing docs.
let real_trait = trait_ref.path.res.def_id();
let Some(def_id) = real_trait.as_local() else { return };
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
let Some(Node::Item(item)) = cx.tcx.hir().find(hir_id) else { return };
let Some(Node::Item(item)) = cx.tcx.hir().find_by_def_id(def_id) else { return };
if let hir::VisibilityKind::Inherited = item.vis.node {
for impl_item_ref in items {
self.private_traits.insert(impl_item_ref.id.hir_id());
Expand Down Expand Up @@ -656,7 +655,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {

// If the method is an impl for an item with docs_hidden, don't doc.
if method_context(cx, impl_item.hir_id()) == MethodLateContext::PlainImpl {
let parent = cx.tcx.hir().get_parent_did(impl_item.hir_id());
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id());
let impl_ty = cx.tcx.type_of(parent);
let outerdef = match impl_ty.kind() {
ty::Adt(def, _) => Some(def.did),
Expand Down Expand Up @@ -1212,7 +1211,7 @@ impl<'tcx> LateLintPass<'tcx> for InvalidNoMangleItems {
check_no_mangle_on_generic_fn(
no_mangle_attr,
Some(generics),
cx.tcx.hir().get_generics(it.id.def_id.to_def_id()).unwrap(),
cx.tcx.hir().get_generics(it.id.def_id).unwrap(),
it.span,
);
}
Expand Down
Loading

0 comments on commit f9d61cd

Please sign in to comment.