Skip to content

Commit bb347a1

Browse files
committed
Remove attributes from projected owners, fix tests
1 parent a2b9441 commit bb347a1

15 files changed

Lines changed: 278 additions & 219 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: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1616
use rustc_data_structures::steal::Steal;
1717
use rustc_data_structures::sync::{DynSend, DynSync, try_par_for_each_in};
1818
use rustc_hir::def::{DefKind, Res};
19-
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
19+
use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdMap, LocalModDefId};
20+
use rustc_hir::lints::DelayedLints;
2021
use rustc_hir::*;
2122
use rustc_index::IndexVec;
2223
use rustc_macros::{Decodable, Encodable, HashStable};
@@ -53,6 +54,27 @@ impl<'hir> Crate<'hir> {
5354
) -> Crate<'hir> {
5455
Crate { owners, delayed_ids, delayed_resolver, opt_hir_hash }
5556
}
57+
58+
/// Serves as an entry point for getting `MaybeOwner`. As owner can either be in
59+
/// `owners` of `hir_crate` or it can be delayed AST owner (i.e., delegations)
60+
/// we need to firstly check in `hir_crate` and then delayed AST owners.
61+
/// This method can be invoked when not all delayed AST owners are lowered.
62+
pub fn owner(&'hir self, tcx: TyCtxt<'hir>, def_id: LocalDefId) -> &'hir MaybeOwner<'hir> {
63+
// Delayed LocalDefId can be in `self.owners` if there exists non-delayed LocalDefId
64+
// which is greater than delayed LocalDefId, we use IndexVec for owners,
65+
// so we will call ensure_contains_elem which will grow it.
66+
if let Some(owner) = self.owners.get(def_id)
67+
&& (self.delayed_ids.is_empty() || !matches!(owner, MaybeOwner::Phantom))
68+
{
69+
return owner;
70+
}
71+
72+
if self.delayed_ids.contains(&def_id) {
73+
tcx.ensure_done().lower_delayed_owner(def_id);
74+
}
75+
76+
tcx.delayed_owner(def_id)
77+
}
5678
}
5779

5880
impl<Hcx: HashStableContext> HashStable<Hcx> for Crate<'_> {
@@ -430,33 +452,65 @@ pub struct Hashes {
430452
pub attrs_hash: Option<Fingerprint>,
431453
}
432454

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

454-
if krate.delayed_ids.contains(&def_id) {
455-
tcx.ensure_done().lower_delayed_owner(def_id);
493+
impl<'tcx> ProjectedMaybeOwner<'tcx> {
494+
pub fn as_owner(&'tcx self) -> Option<&'tcx ProjectedOwnerInfo<'tcx>> {
495+
match self {
496+
ProjectedMaybeOwner::Owner(i) => Some(i),
497+
ProjectedMaybeOwner::NonOwner(_) => None,
456498
}
499+
}
457500

458-
tcx.delayed_owner(def_id)
501+
pub fn unwrap(&'tcx self) -> &'tcx ProjectedOwnerInfo<'tcx> {
502+
self.as_owner().unwrap_or_else(|| panic!("Not a HIR owner"))
503+
}
504+
}
505+
506+
pub fn provide(providers: &mut Providers) {
507+
providers.hir_crate_items = map::hir_crate_items;
508+
providers.crate_hash = map::crate_hash;
509+
providers.hir_module_items = map::hir_module_items;
510+
providers.hir_attr_map = |tcx, id| {
511+
tcx.hir_crate(()).owner(tcx, id.def_id).as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs)
459512
};
513+
providers.owner = |tcx, def_id| ProjectedMaybeOwner::from(tcx.hir_crate(()).owner(tcx, def_id));
460514
providers.hir_owner_parent_q = |tcx, owner_id| tcx.hir_owner_parent_impl(owner_id);
461515
providers.def_span = |tcx, def_id| tcx.hir_span(tcx.local_def_id_to_hir_id(def_id));
462516
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)