Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Only the query once for find_by_def_id.
  • Loading branch information
cjgillot committed Jan 29, 2022
commit 680523560f9f2906a4fbab61c9254ee40d27e8e9
19 changes: 14 additions & 5 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ impl<'hir> Map<'hir> {
}

pub fn opt_def_kind(&self, local_def_id: LocalDefId) -> Option<DefKind> {
let hir_id = self.local_def_id_to_hir_id(local_def_id);
let def_kind = match self.find(hir_id)? {
let def_kind = match self.find_by_def_id(local_def_id)? {
Node::Item(item) => match item.kind {
ItemKind::Static(..) => DefKind::Static,
ItemKind::Const(..) => DefKind::Const,
Expand Down Expand Up @@ -272,6 +271,7 @@ impl<'hir> Map<'hir> {
// FIXME(eddyb) is this even possible, if we have a `Node::Ctor`?
assert_ne!(variant_data.ctor_hir_id(), None);

let hir_id = self.local_def_id_to_hir_id(local_def_id);
let ctor_of = match self.find(self.get_parent_node(hir_id)) {
Some(Node::Item(..)) => def::CtorOf::Struct,
Some(Node::Variant(..)) => def::CtorOf::Variant,
Expand All @@ -280,6 +280,7 @@ impl<'hir> Map<'hir> {
DefKind::Ctor(ctor_of, def::CtorKind::from_hir(variant_data))
}
Node::AnonConst(_) => {
let hir_id = self.local_def_id_to_hir_id(local_def_id);
let inline = match self.find(self.get_parent_node(hir_id)) {
Some(Node::Expr(&Expr {
kind: ExprKind::ConstBlock(ref anon_const), ..
Expand All @@ -292,7 +293,10 @@ impl<'hir> Map<'hir> {
Node::Expr(expr) => match expr.kind {
ExprKind::Closure(.., None) => DefKind::Closure,
ExprKind::Closure(.., Some(_)) => DefKind::Generator,
_ => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)),
_ => {
let hir_id = self.local_def_id_to_hir_id(local_def_id);
bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id))
}
},
Node::GenericParam(param) => match param.kind {
GenericParamKind::Lifetime { .. } => DefKind::LifetimeParam,
Expand Down Expand Up @@ -352,7 +356,12 @@ impl<'hir> Map<'hir> {
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
#[inline]
pub fn find_by_def_id(&self, id: LocalDefId) -> Option<Node<'hir>> {
self.find(self.local_def_id_to_hir_id(id))
let owner = self.tcx.hir_owner(id);
match owner {
MaybeOwner::Owner(o) => Some(o.node.into()),
MaybeOwner::NonOwner(hir_id) => self.find(hir_id),
MaybeOwner::Phantom => bug!("No HirId for {:?}", id),
}
}

/// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found.
Expand All @@ -367,7 +376,7 @@ impl<'hir> Map<'hir> {
}

pub fn get_if_local(&self, id: DefId) -> Option<Node<'hir>> {
id.as_local().and_then(|id| self.find(self.local_def_id_to_hir_id(id)))
id.as_local().and_then(|id| self.find_by_def_id(id))
}

pub fn get_generics(&self, id: LocalDefId) -> Option<&'hir Generics<'hir>> {
Expand Down