Skip to content

Commit

Permalink
Reduce use of local_def_id_to_hir_id.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Jan 15, 2022
1 parent ebcc847 commit 67727aa
Show file tree
Hide file tree
Showing 39 changed files with 182 additions and 237 deletions.
5 changes: 3 additions & 2 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
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
66 changes: 31 additions & 35 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
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 @@ -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_codegen_ssa/src/back/symbol_export.rs
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
@@ -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
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
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
3 changes: 1 addition & 2 deletions compiler/rustc_incremental/src/persist/dirty_clean.rs
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
23 changes: 8 additions & 15 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
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 @@ -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
5 changes: 2 additions & 3 deletions compiler/rustc_lint/src/builtin.rs
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 @@ -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
14 changes: 6 additions & 8 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Expand Up @@ -1579,12 +1579,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}
}

fn encode_info_for_closure(&mut self, def_id: LocalDefId) {
fn encode_info_for_closure(&mut self, hir_id: hir::HirId) {
let def_id = self.tcx.hir().local_def_id(hir_id);
debug!("EncodeContext::encode_info_for_closure({:?})", def_id);

// NOTE(eddyb) `tcx.type_of(def_id)` isn't used because it's fully generic,
// including on the signature, which is inferred in `typeck.
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
let ty = self.tcx.typeck(def_id).node_type(hir_id);

match ty.kind() {
Expand All @@ -1605,9 +1605,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}
}

fn encode_info_for_anon_const(&mut self, def_id: LocalDefId) {
fn encode_info_for_anon_const(&mut self, id: hir::HirId) {
let def_id = self.tcx.hir().local_def_id(id);
debug!("EncodeContext::encode_info_for_anon_const({:?})", def_id);
let id = self.tcx.hir().local_def_id_to_hir_id(def_id);
let body_id = self.tcx.hir().body_owned_by(id);
let const_data = self.encode_rendered_const_for_body(body_id);
let qualifs = self.tcx.mir_const_qualif(def_id);
Expand Down Expand Up @@ -1928,8 +1928,7 @@ impl<'a, 'tcx> Visitor<'tcx> for EncodeContext<'a, 'tcx> {
}
fn visit_anon_const(&mut self, c: &'tcx AnonConst) {
intravisit::walk_anon_const(self, c);
let def_id = self.tcx.hir().local_def_id(c.hir_id);
self.encode_info_for_anon_const(def_id);
self.encode_info_for_anon_const(c.hir_id);
}
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
intravisit::walk_item(self, item);
Expand Down Expand Up @@ -1983,8 +1982,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {

fn encode_info_for_expr(&mut self, expr: &hir::Expr<'_>) {
if let hir::ExprKind::Closure(..) = expr.kind {
let def_id = self.tcx.hir().local_def_id(expr.hir_id);
self.encode_info_for_closure(def_id);
self.encode_info_for_closure(expr.hir_id);
}
}

Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_middle/src/hir/map/mod.rs
Expand Up @@ -361,8 +361,7 @@ impl<'hir> Map<'hir> {
id.as_local().and_then(|id| self.find(self.local_def_id_to_hir_id(id)))
}

pub fn get_generics(&self, id: DefId) -> Option<&'hir Generics<'hir>> {
let id = id.as_local()?;
pub fn get_generics(&self, id: LocalDefId) -> Option<&'hir Generics<'hir>> {
let node = self.tcx.hir_owner(id)?;
match node.node {
OwnerNode::ImplItem(impl_item) => Some(&impl_item.generics),
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_middle/src/mir/mod.rs
Expand Up @@ -2449,15 +2449,14 @@ impl<'tcx> Debug for Rvalue<'tcx> {

AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| {
if let Some(def_id) = def_id.as_local() {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let name = if tcx.sess.opts.debugging_opts.span_free_formats {
let substs = tcx.lift(substs).unwrap();
format!(
"[closure@{}]",
tcx.def_path_str_with_substs(def_id.to_def_id(), substs),
)
} else {
let span = tcx.hir().span(hir_id);
let span = tcx.def_span(def_id);
format!(
"[closure@{}]",
tcx.sess.source_map().span_to_diagnostic_string(span)
Expand All @@ -2481,8 +2480,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {

AggregateKind::Generator(def_id, _, _) => ty::tls::with(|tcx| {
if let Some(def_id) = def_id.as_local() {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let name = format!("[generator@{:?}]", tcx.hir().span(hir_id));
let name = format!("[generator@{:?}]", tcx.def_span(def_id));
let mut struct_fmt = fmt.debug_struct(&name);

// FIXME(project-rfc-2229#48): This should be a list of capture names/places
Expand Down
12 changes: 4 additions & 8 deletions compiler/rustc_middle/src/mir/mono.rs
Expand Up @@ -179,15 +179,11 @@ impl<'tcx> MonoItem<'tcx> {

pub fn local_span(&self, tcx: TyCtxt<'tcx>) -> Option<Span> {
match *self {
MonoItem::Fn(Instance { def, .. }) => {
def.def_id().as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
}
MonoItem::Static(def_id) => {
def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
}
MonoItem::GlobalAsm(item_id) => Some(item_id.hir_id()),
MonoItem::Fn(Instance { def, .. }) => def.def_id().as_local(),
MonoItem::Static(def_id) => def_id.as_local(),
MonoItem::GlobalAsm(item_id) => Some(item_id.def_id),
}
.map(|hir_id| tcx.hir().span(hir_id))
.map(|def_id| tcx.def_span(def_id))
}

// Only used by rustc_codegen_cranelift
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_middle/src/mir/spanview.rs
Expand Up @@ -665,9 +665,7 @@ fn trim_span_hi(span: Span, to_pos: BytePos) -> Span {
}

fn fn_span<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Span {
let hir_id =
tcx.hir().local_def_id_to_hir_id(def_id.as_local().expect("expected DefId is local"));
let fn_decl_span = tcx.hir().span(hir_id);
let fn_decl_span = tcx.def_span(def_id);
if let Some(body_span) = hir_body(tcx, def_id).map(|hir_body| hir_body.value.span) {
if fn_decl_span.ctxt() == body_span.ctxt() { fn_decl_span.to(body_span) } else { body_span }
} else {
Expand Down

0 comments on commit 67727aa

Please sign in to comment.