Skip to content

Commit 69beb66

Browse files
committed
Remove attributes from projected owners, fix tests
1 parent 1533d0b commit 69beb66

15 files changed

Lines changed: 275 additions & 216 deletions

compiler/rustc_middle/src/arena.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ macro_rules! arena_types {
119119
[] crate_inherent_impls: rustc_middle::ty::CrateInherentImpls,
120120
[] hir_owner_nodes: rustc_hir::OwnerNodes<'tcx>,
121121
[decode] token_stream: rustc_ast::tokenstream::TokenStream,
122-
[] owner_info: rustc_hir::OwnerInfo<'tcx>,
122+
[] maybe_owner: rustc_middle::hir::ProjectedMaybeOwner<'tcx>,
123+
[] owner_info: rustc_middle::hir::ProjectedOwnerInfo<'tcx>,
124+
[] parenting: rustc_hir::def_id::LocalDefIdMap<rustc_hir::ItemLocalId>,
125+
[] trait_candidates: rustc_hir::ItemLocalMap<&'tcx [rustc_hir::TraitCandidate<'tcx>]>,
126+
[] delayed_lints: rustc_hir::lints::DelayedLints,
123127
]);
124128
)
125129
}

compiler/rustc_middle/src/hir/map.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_hir_pretty as pprust_hir;
1818
use rustc_span::def_id::StableCrateId;
1919
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, with_metavar_spans};
2020

21-
use crate::hir::{MaybeOwner, ModuleItems, nested_filter};
21+
use crate::hir::{ModuleItems, ProjectedMaybeOwner, nested_filter};
2222
use crate::middle::debugger_visualizer::DebuggerVisualizerFile;
2323
use crate::query::{IntoQueryKey, LocalCrate};
2424
use crate::ty::TyCtxt;
@@ -105,25 +105,24 @@ impl<'tcx> TyCtxt<'tcx> {
105105
pub fn local_def_id_to_hir_id(self, def_id: impl IntoQueryKey<LocalDefId>) -> HirId {
106106
let def_id = def_id.into_query_key();
107107
match self.owner(def_id) {
108-
MaybeOwner::Owner(_) => HirId::make_owner(def_id),
109-
MaybeOwner::NonOwner(hir_id) => hir_id,
110-
MaybeOwner::Phantom => bug!("No HirId for {:?}", def_id),
108+
ProjectedMaybeOwner::Owner(_) => HirId::make_owner(def_id),
109+
ProjectedMaybeOwner::NonOwner(hir_id) => *hir_id,
111110
}
112111
}
113112

114113
pub fn opt_ast_lowering_delayed_lints(self, id: OwnerId) -> Option<&'tcx DelayedLints> {
115-
self.owner(id.def_id).as_owner().map(|o| &o.delayed_lints)
114+
self.owner(id.def_id).as_owner().map(|o| o.delayed_lints)
116115
}
117116

118117
pub fn in_scope_traits_map(
119118
self,
120119
id: OwnerId,
121120
) -> Option<&'tcx ItemLocalMap<&'tcx [TraitCandidate<'tcx>]>> {
122-
self.owner(id.def_id).as_owner().map(|owner_info| &owner_info.trait_map)
121+
self.owner(id.def_id).as_owner().map(|owner_info| owner_info.trait_map)
123122
}
124123

125124
pub fn opt_hir_owner_nodes(self, def_id: LocalDefId) -> Option<&'tcx OwnerNodes<'tcx>> {
126-
self.owner(def_id).as_owner().map(|i| &i.nodes)
125+
self.owner(def_id).as_owner().map(|i| i.nodes)
127126
}
128127

129128
#[inline]

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_data_structures::sync::{DynSend, DynSync, try_par_for_each_in};
1818
use rustc_hir::def::{DefKind, Res};
1919
use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdMap, LocalModDefId};
2020
use rustc_hir::definitions::PerParentDisambiguatorState;
21+
use rustc_hir::lints::DelayedLints;
2122
use rustc_hir::*;
2223
use rustc_index::IndexVec;
2324
use rustc_macros::{Decodable, Encodable, HashStable};
@@ -62,6 +63,27 @@ impl<'hir> Crate<'hir> {
6263
) -> Crate<'hir> {
6364
Crate { owners, delayed_ids, delayed_resolver, opt_hir_hash }
6465
}
66+
67+
/// Serves as an entry point for getting `MaybeOwner`. As owner can either be in
68+
/// `owners` of `hir_crate` or it can be delayed AST owner (i.e., delegations)
69+
/// we need to firstly check in `hir_crate` and then delayed AST owners.
70+
/// This method can be invoked when not all delayed AST owners are lowered.
71+
pub fn owner(&'hir self, tcx: TyCtxt<'hir>, def_id: LocalDefId) -> &'hir MaybeOwner<'hir> {
72+
// Delayed LocalDefId can be in `self.owners` if there exists non-delayed LocalDefId
73+
// which is greater than delayed LocalDefId, we use IndexVec for owners,
74+
// so we will call ensure_contains_elem which will grow it.
75+
if let Some(owner) = self.owners.get(def_id)
76+
&& (self.delayed_ids.is_empty() || !matches!(owner, MaybeOwner::Phantom))
77+
{
78+
return owner;
79+
}
80+
81+
if self.delayed_ids.contains(&def_id) {
82+
tcx.ensure_done().lower_delayed_owner(def_id);
83+
}
84+
85+
tcx.delayed_owner(def_id)
86+
}
6587
}
6688

6789
impl<Hcx: HashStableContext> HashStable<Hcx> for Crate<'_> {
@@ -439,33 +461,65 @@ pub struct Hashes {
439461
pub attrs_hash: Option<Fingerprint>,
440462
}
441463

442-
pub fn provide(providers: &mut Providers) {
443-
providers.hir_crate_items = map::hir_crate_items;
444-
providers.crate_hash = map::crate_hash;
445-
providers.hir_module_items = map::hir_module_items;
446-
providers.hir_attr_map =
447-
|tcx, id| tcx.owner(id.def_id).as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs);
448-
// Serves as an entry point for getting `MaybeOwner`. As owner can either be in
449-
// `owners` of `hir_crate` or it can be delayed AST owner (i.e., delegations)
450-
// we need to firstly check in `hir_crate` and then delayed AST owners.
451-
// This method can be invoked when not all delayed AST owners are lowered.
452-
providers.owner = |tcx, def_id| {
453-
// Delayed LocalDefId can be in `self.owners` if there exists non-delayed LocalDefId
454-
// which is greater than delayed LocalDefId, we use IndexVec for owners,
455-
// so we will call ensure_contains_elem which will grow it.
456-
let krate = tcx.hir_crate(());
457-
if let Some(owner) = krate.owners.get(def_id)
458-
&& (krate.delayed_ids.is_empty() || !matches!(owner, MaybeOwner::Phantom))
459-
{
460-
return *owner;
464+
#[derive(Debug, HashStable)]
465+
pub struct ProjectedOwnerInfo<'tcx> {
466+
/// Contents of the HIR.
467+
pub nodes: &'tcx OwnerNodes<'tcx>,
468+
469+
/// Map from each nested owner to its parent's local id.
470+
#[stable_hasher(ignore)]
471+
pub parenting: &'tcx LocalDefIdMap<ItemLocalId>,
472+
473+
/// Map indicating what traits are in scope for places where this
474+
/// is relevant; generated by resolve.
475+
pub trait_map: &'tcx ItemLocalMap<&'tcx [TraitCandidate<'tcx>]>,
476+
477+
#[stable_hasher(ignore)]
478+
pub delayed_lints: &'tcx DelayedLints,
479+
}
480+
481+
#[derive(Debug, HashStable)]
482+
pub enum ProjectedMaybeOwner<'tcx> {
483+
Owner(ProjectedOwnerInfo<'tcx>),
484+
NonOwner(HirId),
485+
}
486+
487+
impl<'tcx> From<&'tcx MaybeOwner<'tcx>> for ProjectedMaybeOwner<'tcx> {
488+
fn from(value: &'tcx MaybeOwner<'tcx>) -> Self {
489+
match value {
490+
MaybeOwner::Owner(o) => ProjectedMaybeOwner::Owner(ProjectedOwnerInfo {
491+
nodes: &o.nodes,
492+
parenting: &o.parenting,
493+
trait_map: &o.trait_map,
494+
delayed_lints: &o.delayed_lints,
495+
}),
496+
MaybeOwner::NonOwner(hir_id) => ProjectedMaybeOwner::NonOwner(*hir_id),
497+
MaybeOwner::Phantom => panic!(),
461498
}
499+
}
500+
}
462501

463-
if krate.delayed_ids.contains(&def_id) {
464-
tcx.ensure_done().lower_delayed_owner(def_id);
502+
impl<'tcx> ProjectedMaybeOwner<'tcx> {
503+
pub fn as_owner(&'tcx self) -> Option<&'tcx ProjectedOwnerInfo<'tcx>> {
504+
match self {
505+
ProjectedMaybeOwner::Owner(i) => Some(i),
506+
ProjectedMaybeOwner::NonOwner(_) => None,
465507
}
508+
}
466509

467-
tcx.delayed_owner(def_id)
510+
pub fn unwrap(&'tcx self) -> &'tcx ProjectedOwnerInfo<'tcx> {
511+
self.as_owner().unwrap_or_else(|| panic!("Not a HIR owner"))
512+
}
513+
}
514+
515+
pub fn provide(providers: &mut Providers) {
516+
providers.hir_crate_items = map::hir_crate_items;
517+
providers.crate_hash = map::crate_hash;
518+
providers.hir_module_items = map::hir_module_items;
519+
providers.hir_attr_map = |tcx, id| {
520+
tcx.hir_crate(()).owner(tcx, id.def_id).as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs)
468521
};
522+
providers.owner = |tcx, def_id| ProjectedMaybeOwner::from(tcx.hir_crate(()).owner(tcx, def_id));
469523
providers.hir_owner_parent_q = |tcx, owner_id| tcx.hir_owner_parent_impl(owner_id);
470524
providers.def_span = |tcx, def_id| tcx.hir_span(tcx.local_def_id_to_hir_id(def_id));
471525
providers.def_ident_span = |tcx, def_id| {

compiler/rustc_middle/src/queries.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,15 @@ rustc_queries! {
216216
desc { "lowering the delayed AST owner `{}`", tcx.def_path_str(def_id) }
217217
}
218218

219-
query owner(def_id: LocalDefId) -> hir::MaybeOwner<'tcx> {
219+
query owner(def_id: LocalDefId) -> &'tcx rustc_middle::hir::ProjectedMaybeOwner<'tcx> {
220220
desc { "getting owner for `{}`", tcx.def_path_str(def_id) }
221+
arena_cache
221222
feedable
222223
}
223224

224-
query delayed_owner(def_id: LocalDefId) -> hir::MaybeOwner<'tcx> {
225+
query delayed_owner(def_id: LocalDefId) -> &'tcx hir::MaybeOwner<'tcx> {
225226
feedable
227+
arena_cache
226228
desc { "getting child of lowered delayed AST owner `{}`", tcx.def_path_str(def_id) }
227229
}
228230

compiler/rustc_middle/src/ty/context.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ use rustc_hir::definitions::{DefPathData, Definitions, PerParentDisambiguatorSta
3636
use rustc_hir::intravisit::VisitorExt;
3737
use rustc_hir::lang_items::LangItem;
3838
use rustc_hir::limit::Limit;
39-
use rustc_hir::{
40-
self as hir, CRATE_HIR_ID, HirId, MaybeOwner, Node, OwnerInfo, TraitCandidate, find_attr,
41-
};
39+
use rustc_hir::{self as hir, CRATE_HIR_ID, HirId, MaybeOwner, Node, TraitCandidate, find_attr};
4240
use rustc_index::IndexVec;
4341
use rustc_macros::Diagnostic;
4442
use rustc_session::Session;
@@ -57,6 +55,7 @@ use tracing::{debug, instrument};
5755
use crate::arena::Arena;
5856
use crate::dep_graph::dep_node::make_metadata;
5957
use crate::dep_graph::{DepGraph, DepKindVTable, DepNodeIndex};
58+
use crate::hir::{ProjectedMaybeOwner, ProjectedOwnerInfo};
6059
use crate::ich::StableHashingContext;
6160
use crate::infer::canonical::{CanonicalParamEnvCache, CanonicalVarKind};
6261
use crate::lint::emit_lint_base;
@@ -763,26 +762,27 @@ impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> {
763762
pub fn feed_hir(&self) {
764763
let node = hir::OwnerNode::Synthetic;
765764
let bodies = Default::default();
766-
let attrs = hir::AttributeMap::EMPTY.clone();
765+
let attrs = hir::AttributeMap::EMPTY;
767766

768767
let rustc_middle::hir::Hashes { opt_hash_including_bodies, .. } =
769768
self.tcx.hash_owner_nodes(node, &bodies, &attrs.map, attrs.define_opaque);
770769
let node = node.into();
771770

772-
self.owner(hir::MaybeOwner::Owner(self.tcx.arena.alloc(OwnerInfo {
773-
attrs,
774-
nodes: hir::OwnerNodes {
771+
self.owner(ProjectedMaybeOwner::Owner(ProjectedOwnerInfo {
772+
nodes: self.tcx.arena.alloc(hir::OwnerNodes {
775773
opt_hash_including_bodies,
776774
nodes: IndexVec::from_elem_n(
777775
hir::ParentedNode { parent: hir::ItemLocalId::INVALID, node },
778776
1,
779777
),
780778
bodies,
781-
},
782-
parenting: Default::default(),
783-
delayed_lints: Default::default(),
784-
trait_map: Default::default(),
785-
})));
779+
}),
780+
parenting: self.tcx.arena.alloc(Default::default()),
781+
delayed_lints: self.tcx.arena.alloc(Default::default()),
782+
trait_map: self.tcx.arena.alloc(Default::default()),
783+
}));
784+
785+
self.feed_owner_id().hir_attr_map(attrs);
786786
}
787787
}
788788

tests/incremental/hashes/enum_defs.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
enum EnumVisibility { A }
3131

3232
#[cfg(not(any(bpass1,bpass4)))]
33-
#[rustc_clean(cfg="bpass2", except="owner")]
33+
#[rustc_clean(cfg="bpass2")]
3434
#[rustc_clean(cfg="bpass3")]
3535
#[rustc_clean(cfg="bpass5", except="owner")]
3636
#[rustc_clean(cfg="bpass6")]
@@ -373,9 +373,9 @@ enum EnumAddMustUse {
373373
}
374374

375375
#[cfg(not(any(bpass1,bpass4)))]
376-
#[rustc_clean(cfg="bpass2", except="owner")]
376+
#[rustc_clean(cfg="bpass2")]
377377
#[rustc_clean(cfg="bpass3")]
378-
#[rustc_clean(cfg="bpass5", except="owner")]
378+
#[rustc_clean(cfg="bpass5")]
379379
#[rustc_clean(cfg="bpass6")]
380380
#[must_use]
381381
enum EnumAddMustUse {
@@ -393,9 +393,9 @@ enum EnumAddReprC {
393393
}
394394

395395
#[cfg(not(any(bpass1,bpass4)))]
396-
#[rustc_clean(cfg="bpass2", except="type_of,owner")]
396+
#[rustc_clean(cfg="bpass2", except="type_of")]
397397
#[rustc_clean(cfg="bpass3")]
398-
#[rustc_clean(cfg="bpass5", except="type_of,owner")]
398+
#[rustc_clean(cfg="bpass5", except="type_of")]
399399
#[rustc_clean(cfg="bpass6")]
400400
#[repr(C)]
401401
enum EnumAddReprC {

0 commit comments

Comments
 (0)