Skip to content

Commit

Permalink
Trait impls are Items, therefore HIR owners.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Feb 15, 2021
1 parent f395f2e commit 7dd1e8c
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 22 deletions.
8 changes: 3 additions & 5 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
self_ty: ref ty,
items: ref impl_items,
}) => {
let def_id = self.resolver.local_def_id(id);

// Lower the "impl header" first. This ordering is important
// for in-band lifetimes! Consider `'a` here:
//
Expand All @@ -411,10 +409,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
// method, it will not be considered an in-band
// lifetime to be added, but rather a reference to a
// parent lifetime.
let lowered_trait_impl_id = self.lower_node_id(id);
let lowered_trait_def_id = self.lower_node_id(id).expect_owner();
let (generics, (trait_ref, lowered_ty)) = self.add_in_band_defs(
ast_generics,
def_id,
lowered_trait_def_id,
AnonymousLifetimeMode::CreateParameter,
|this, _| {
let trait_ref = trait_ref.as_ref().map(|trait_ref| {
Expand All @@ -426,7 +424,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
this.trait_impls
.entry(def_id)
.or_default()
.push(lowered_trait_impl_id);
.push(lowered_trait_def_id);
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ struct LoweringContext<'a, 'hir: 'a> {
exported_macros: Vec<hir::MacroDef<'hir>>,
non_exported_macro_attrs: Vec<ast::Attribute>,

trait_impls: BTreeMap<DefId, Vec<hir::HirId>>,
trait_impls: BTreeMap<DefId, Vec<LocalDefId>>,

modules: BTreeMap<LocalDefId, hir::ModuleItems>,

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ pub struct Crate<'hir> {
pub impl_items: BTreeMap<ImplItemId, ImplItem<'hir>>,
pub foreign_items: BTreeMap<ForeignItemId, ForeignItem<'hir>>,
pub bodies: BTreeMap<BodyId, Body<'hir>>,
pub trait_impls: BTreeMap<DefId, Vec<HirId>>,
pub trait_impls: BTreeMap<DefId, Vec<LocalDefId>>,

/// A list of the body ids written out in the order in which they
/// appear in the crate. If you're going to process all the bodies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
.hir()
.trait_impls(trait_did)
.iter()
.filter_map(|impl_node| {
let impl_did = tcx.hir().local_def_id(*impl_node);
.filter_map(|&impl_did| {
match tcx.hir().get_if_local(impl_did.to_def_id()) {
Some(Node::Item(Item {
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ impl<'hir> Map<'hir> {
}
}

pub fn trait_impls(&self, trait_did: DefId) -> &'hir [HirId] {
pub fn trait_impls(&self, trait_did: DefId) -> &'hir [LocalDefId] {
self.tcx.all_local_trait_impls(LOCAL_CRATE).get(&trait_did).map_or(&[], |xs| &xs[..])
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ rustc_queries! {
/// Passing in any other crate will cause an ICE.
///
/// [`LOCAL_CRATE`]: rustc_hir::def_id::LOCAL_CRATE
query all_local_trait_impls(local_crate: CrateNum) -> &'tcx BTreeMap<DefId, Vec<hir::HirId>> {
query all_local_trait_impls(local_crate: CrateNum) -> &'tcx BTreeMap<DefId, Vec<LocalDefId>> {
desc { "local trait impls" }
}

Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_middle/src/ty/trait_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use crate::ty::fast_reject;
use crate::ty::fold::TypeFoldable;
use crate::ty::{Ty, TyCtxt};
use rustc_hir as hir;
use rustc_hir::def_id::{CrateNum, DefId};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
use rustc_hir::definitions::DefPathHash;
use rustc_hir::HirId;

use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
Expand Down Expand Up @@ -201,7 +200,7 @@ impl<'tcx> TyCtxt<'tcx> {
pub(super) fn all_local_trait_impls<'tcx>(
tcx: TyCtxt<'tcx>,
krate: CrateNum,
) -> &'tcx BTreeMap<DefId, Vec<HirId>> {
) -> &'tcx BTreeMap<DefId, Vec<LocalDefId>> {
&tcx.hir_crate(krate).trait_impls
}

Expand Down Expand Up @@ -229,8 +228,8 @@ pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> Trait
}
}

for &hir_id in tcx.hir().trait_impls(trait_id) {
let impl_def_id = tcx.hir().local_def_id(hir_id).to_def_id();
for &impl_def_id in tcx.hir().trait_impls(trait_id) {
let impl_def_id = impl_def_id.to_def_id();

let impl_self_ty = tcx.type_of(impl_def_id);
if impl_self_ty.references_error() {
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_typeck/src/coherence/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ impl<'tcx> Checker<'tcx> {
F: FnMut(TyCtxt<'tcx>, LocalDefId),
{
if Some(self.trait_def_id) == trait_def_id {
for &impl_id in self.tcx.hir().trait_impls(self.trait_def_id) {
let impl_def_id = self.tcx.hir().local_def_id(impl_id);
for &impl_def_id in self.tcx.hir().trait_impls(self.trait_def_id) {
f(self.tcx, impl_def_id);
}
}
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_typeck/src/coherence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ fn coherent_trait(tcx: TyCtxt<'_>, def_id: DefId) {
tcx.ensure().specialization_graph_of(def_id);

let impls = tcx.hir().trait_impls(def_id);
for &hir_id in impls {
let impl_def_id = tcx.hir().local_def_id(hir_id);
for &impl_def_id in impls {
let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();

check_impl(tcx, impl_def_id, trait_ref);
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/passes/collect_trait_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ crate fn collect_trait_impls(krate: Crate, cx: &DocContext<'_>) -> Crate {
// doesn't work with it anyway, so pull them from the HIR map instead
let mut extra_attrs = Vec::new();
for &trait_did in cx.tcx.all_traits(LOCAL_CRATE).iter() {
for &impl_node in cx.tcx.hir().trait_impls(trait_did) {
let impl_did = cx.tcx.hir().local_def_id(impl_node).to_def_id();
for &impl_did in cx.tcx.hir().trait_impls(trait_did) {
let impl_did = impl_did.to_def_id();
cx.tcx.sess.prof.generic_activity("build_local_trait_impl").run(|| {
let mut parent = cx.tcx.parent(impl_did);
while let Some(did) = parent {
Expand Down

0 comments on commit 7dd1e8c

Please sign in to comment.