Skip to content

Commit a7e17f9

Browse files
committed
Auto merge of #155678 - aerooneqq:single-owners-query-exp, r=<try>
experiment: single owner query
2 parents 92c7010 + 67a6a79 commit a7e17f9

47 files changed

Lines changed: 1225 additions & 1233 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,7 @@ impl Attribute {
15501550
}
15511551

15521552
/// Attributes owned by a HIR owner.
1553-
#[derive(Debug)]
1553+
#[derive(Debug, Clone)]
15541554
pub struct AttributeMap<'tcx> {
15551555
pub map: SortedMap<ItemLocalId, &'tcx [Attribute]>,
15561556
/// Preprocessed `#[define_opaque]` attribute.

compiler/rustc_incremental/src/assert_dep_graph.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,7 @@ impl<'tcx> IfThisChanged<'tcx> {
113113
for attr in attrs {
114114
if let Attribute::Parsed(AttributeKind::RustcIfThisChanged(span, dep_node)) = *attr {
115115
let dep_node = match dep_node {
116-
None => DepNode::from_def_path_hash(
117-
self.tcx,
118-
def_path_hash,
119-
DepKind::opt_hir_owner_nodes,
120-
),
116+
None => DepNode::from_def_path_hash(self.tcx, def_path_hash, DepKind::owner),
121117
Some(n) => {
122118
match DepNode::from_label_string(self.tcx, n.as_str(), def_path_hash) {
123119
Ok(n) => n,

compiler/rustc_incremental/src/persist/clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const BASE_FN: &[&str] = &[
5555
/// DepNodes for Hir, which is pretty much everything
5656
const BASE_HIR: &[&str] = &[
5757
// opt_hir_owner_nodes should be computed for all nodes
58-
label_strs::opt_hir_owner_nodes,
58+
label_strs::owner,
5959
];
6060

6161
/// `impl` implementation of struct/trait

compiler/rustc_middle/src/arena.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ 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>,
122123
]);
123124
)
124125
}

compiler/rustc_middle/src/hir/map.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ use rustc_hir::def::{DefKind, Res};
1212
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId, LocalModDefId};
1313
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
1414
use rustc_hir::intravisit::Visitor;
15+
use rustc_hir::lints::DelayedLints;
1516
use rustc_hir::*;
1617
use rustc_hir_pretty as pprust_hir;
1718
use rustc_span::def_id::StableCrateId;
1819
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, with_metavar_spans};
1920

2021
use crate::hir::{ModuleItems, nested_filter};
2122
use crate::middle::debugger_visualizer::DebuggerVisualizerFile;
22-
use crate::query::LocalCrate;
23+
use crate::query::{IntoQueryKey, LocalCrate};
2324
use crate::ty::TyCtxt;
2425

2526
/// An iterator that walks up the ancestor tree of a given `HirId`.
@@ -101,6 +102,34 @@ impl<'tcx> Iterator for ParentOwnerIterator<'tcx> {
101102
}
102103

103104
impl<'tcx> TyCtxt<'tcx> {
105+
pub fn local_def_id_to_hir_id(self, def_id: impl IntoQueryKey<LocalDefId>) -> HirId {
106+
let def_id = def_id.into_query_key();
107+
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),
111+
}
112+
}
113+
114+
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)
116+
}
117+
118+
pub fn hir_attr_map(self, id: OwnerId) -> &'tcx AttributeMap<'tcx> {
119+
self.owner(id.def_id).as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs)
120+
}
121+
122+
pub fn in_scope_traits_map(
123+
self,
124+
id: OwnerId,
125+
) -> Option<&'tcx ItemLocalMap<&'tcx [TraitCandidate<'tcx>]>> {
126+
self.owner(id.def_id).as_owner().map(|owner_info| &owner_info.trait_map)
127+
}
128+
129+
pub fn opt_hir_owner_nodes(self, def_id: LocalDefId) -> Option<&'tcx OwnerNodes<'tcx>> {
130+
self.owner(def_id).as_owner().map(|i| &i.nodes)
131+
}
132+
104133
#[inline]
105134
fn expect_hir_owner_nodes(self, def_id: LocalDefId) -> &'tcx OwnerNodes<'tcx> {
106135
self.opt_hir_owner_nodes(def_id)

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,6 @@ impl<'hir> Crate<'hir> {
6262
) -> Crate<'hir> {
6363
Crate { owners, delayed_ids, delayed_resolver, opt_hir_hash }
6464
}
65-
66-
/// Serves as an entry point for getting `MaybeOwner`. As owner can either be in
67-
/// `owners` of `hir_crate` or it can be delayed AST owner (i.e., delegations)
68-
/// we need to firstly check in `hir_crate` and then delayed AST owners.
69-
/// This method can be invoked when not all delayed AST owners are lowered.
70-
pub fn owner(&self, tcx: TyCtxt<'hir>, def_id: LocalDefId) -> MaybeOwner<'hir> {
71-
// Delayed LocalDefId can be in `self.owners` if there exists non-delayed LocalDefId
72-
// which is greater than delayed LocalDefId, we use IndexVec for owners,
73-
// so we will call ensure_contains_elem which will grow it.
74-
if let Some(owner) = self.owners.get(def_id)
75-
&& (self.delayed_ids.is_empty() || !matches!(owner, MaybeOwner::Phantom))
76-
{
77-
return *owner;
78-
}
79-
80-
if self.delayed_ids.contains(&def_id) {
81-
tcx.ensure_done().lower_delayed_owner(def_id);
82-
}
83-
84-
tcx.delayed_owner(def_id)
85-
}
8665
}
8766

8867
impl<Hcx: HashStableContext> HashStable<Hcx> for Crate<'_> {
@@ -431,8 +410,7 @@ impl<'tcx> TyCtxt<'tcx> {
431410
HirId {
432411
owner: parent_owner_id,
433412
local_id: self
434-
.hir_crate(())
435-
.owner(self, parent_owner_id.def_id)
413+
.owner(parent_owner_id.def_id)
436414
.unwrap()
437415
.parenting
438416
.get(&owner_id.def_id)
@@ -465,19 +443,28 @@ pub fn provide(providers: &mut Providers) {
465443
providers.hir_crate_items = map::hir_crate_items;
466444
providers.crate_hash = map::crate_hash;
467445
providers.hir_module_items = map::hir_module_items;
468-
providers.local_def_id_to_hir_id = |tcx, def_id| match tcx.hir_crate(()).owner(tcx, def_id) {
469-
MaybeOwner::Owner(_) => HirId::make_owner(def_id),
470-
MaybeOwner::NonOwner(hir_id) => hir_id,
471-
MaybeOwner::Phantom => bug!("No HirId for {:?}", def_id),
446+
// Serves as an entry point for getting `MaybeOwner`. As owner can either be in
447+
// `owners` of `hir_crate` or it can be delayed AST owner (i.e., delegations)
448+
// we need to firstly check in `hir_crate` and then delayed AST owners.
449+
// This method can be invoked when not all delayed AST owners are lowered.
450+
providers.owner = |tcx, def_id| {
451+
// Delayed LocalDefId can be in `self.owners` if there exists non-delayed LocalDefId
452+
// which is greater than delayed LocalDefId, we use IndexVec for owners,
453+
// so we will call ensure_contains_elem which will grow it.
454+
let krate = tcx.hir_crate(());
455+
if let Some(owner) = krate.owners.get(def_id)
456+
&& (krate.delayed_ids.is_empty() || !matches!(owner, MaybeOwner::Phantom))
457+
{
458+
return *owner;
459+
}
460+
461+
if krate.delayed_ids.contains(&def_id) {
462+
tcx.ensure_done().lower_delayed_owner(def_id);
463+
}
464+
465+
tcx.delayed_owner(def_id)
472466
};
473-
providers.opt_hir_owner_nodes =
474-
|tcx, id| tcx.hir_crate(()).owner(tcx, id).as_owner().map(|i| &i.nodes);
475467
providers.hir_owner_parent_q = |tcx, owner_id| tcx.hir_owner_parent_impl(owner_id);
476-
providers.hir_attr_map = |tcx, id| {
477-
tcx.hir_crate(()).owner(tcx, id.def_id).as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs)
478-
};
479-
providers.opt_ast_lowering_delayed_lints =
480-
|tcx, id| tcx.hir_crate(()).owner(tcx, id.def_id).as_owner().map(|o| &o.delayed_lints);
481468
providers.def_span = |tcx, def_id| tcx.hir_span(tcx.local_def_id_to_hir_id(def_id));
482469
providers.def_ident_span = |tcx, def_id| {
483470
let hir_id = tcx.local_def_id_to_hir_id(def_id);
@@ -517,7 +504,4 @@ pub fn provide(providers: &mut Providers) {
517504
|tcx, trait_id| tcx.resolutions(()).trait_impls.get(&trait_id).map_or(&[], |xs| &xs[..]);
518505
providers.expn_that_defined =
519506
|tcx, id| tcx.resolutions(()).expn_that_defined.get(&id).copied().unwrap_or(ExpnId::root());
520-
providers.in_scope_traits_map = |tcx, id| {
521-
tcx.hir_crate(()).owner(tcx, id.def_id).as_owner().map(|owner_info| &owner_info.trait_map)
522-
};
523507
}

compiler/rustc_middle/src/queries.rs

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ use rustc_hir::def_id::{
6868
CrateNum, DefId, DefIdMap, LocalDefId, LocalDefIdMap, LocalDefIdSet, LocalModDefId,
6969
};
7070
use rustc_hir::lang_items::{LangItem, LanguageItems};
71-
use rustc_hir::{ItemLocalId, ItemLocalMap, PreciseCapturingArgKind, TraitCandidate};
71+
use rustc_hir::{ItemLocalId, PreciseCapturingArgKind};
7272
use rustc_index::IndexVec;
7373
use rustc_lint_defs::LintId;
7474
use rustc_macros::rustc_queries;
@@ -216,6 +216,11 @@ 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> {
220+
desc { "getting owner for `{}`", tcx.def_path_str(def_id) }
221+
feedable
222+
}
223+
219224
query delayed_owner(def_id: LocalDefId) -> hir::MaybeOwner<'tcx> {
220225
feedable
221226
desc { "getting child of lowered delayed AST owner `{}`", tcx.def_path_str(def_id) }
@@ -238,12 +243,6 @@ rustc_queries! {
238243
cache_on_disk
239244
}
240245

241-
/// Returns HIR ID for the given `LocalDefId`.
242-
query local_def_id_to_hir_id(key: LocalDefId) -> hir::HirId {
243-
desc { "getting HIR ID of `{}`", tcx.def_path_str(key) }
244-
feedable
245-
}
246-
247246
/// Gives access to the HIR node's parent for the HIR owner `key`.
248247
///
249248
/// This can be conveniently accessed by `tcx.hir_*` methods.
@@ -252,36 +251,6 @@ rustc_queries! {
252251
desc { "getting HIR parent of `{}`", tcx.def_path_str(key) }
253252
}
254253

255-
/// Gives access to the HIR nodes and bodies inside `key` if it's a HIR owner.
256-
///
257-
/// This can be conveniently accessed by `tcx.hir_*` methods.
258-
/// Avoid calling this query directly.
259-
query opt_hir_owner_nodes(key: LocalDefId) -> Option<&'tcx hir::OwnerNodes<'tcx>> {
260-
desc { "getting HIR owner items in `{}`", tcx.def_path_str(key) }
261-
feedable
262-
}
263-
264-
/// Gives access to the HIR attributes inside the HIR owner `key`.
265-
///
266-
/// This can be conveniently accessed by `tcx.hir_*` methods.
267-
/// Avoid calling this query directly.
268-
query hir_attr_map(key: hir::OwnerId) -> &'tcx hir::AttributeMap<'tcx> {
269-
desc { "getting HIR owner attributes in `{}`", tcx.def_path_str(key) }
270-
feedable
271-
}
272-
273-
/// Gives access to lints emitted during ast lowering.
274-
///
275-
/// This can be conveniently accessed by `tcx.hir_*` methods.
276-
/// Avoid calling this query directly.
277-
query opt_ast_lowering_delayed_lints(key: hir::OwnerId) -> Option<&'tcx hir::lints::DelayedLints> {
278-
desc { "getting AST lowering delayed lints in `{}`", tcx.def_path_str(key) }
279-
// This query has to be `no_hash` and `eval_always`,
280-
// because it accesses `delayed_lints` which is not hashed as part of the HIR
281-
no_hash
282-
eval_always
283-
}
284-
285254
/// Returns the *default* of the const pararameter given by `DefId`.
286255
///
287256
/// E.g., given `struct Ty<const N: usize = 3>;` this returns `3` for `N`.
@@ -1884,10 +1853,6 @@ rustc_queries! {
18841853
query specializes(_: (DefId, DefId)) -> bool {
18851854
desc { "computing whether impls specialize one another" }
18861855
}
1887-
query in_scope_traits_map(_: hir::OwnerId)
1888-
-> Option<&'tcx ItemLocalMap<&'tcx [TraitCandidate<'tcx>]>> {
1889-
desc { "getting traits in scope at a block" }
1890-
}
18911856

18921857
/// Returns whether the impl or associated function has the `default` keyword.
18931858
/// Note: This will ICE on inherent impl items. Consider using `AssocItem::defaultness`.

compiler/rustc_middle/src/ty/context.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ 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::{self as hir, CRATE_HIR_ID, HirId, MaybeOwner, Node, TraitCandidate, find_attr};
39+
use rustc_hir::{
40+
self as hir, CRATE_HIR_ID, HirId, MaybeOwner, Node, OwnerInfo, TraitCandidate, find_attr,
41+
};
4042
use rustc_index::IndexVec;
4143
use rustc_macros::Diagnostic;
4244
use rustc_session::Session;
@@ -759,24 +761,28 @@ impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> {
759761

760762
// Fills in all the important parts needed by HIR queries
761763
pub fn feed_hir(&self) {
762-
self.local_def_id_to_hir_id(HirId::make_owner(self.def_id()));
763-
764764
let node = hir::OwnerNode::Synthetic;
765765
let bodies = Default::default();
766-
let attrs = hir::AttributeMap::EMPTY;
766+
let attrs = hir::AttributeMap::EMPTY.clone();
767767

768768
let rustc_middle::hir::Hashes { opt_hash_including_bodies, .. } =
769769
self.tcx.hash_owner_nodes(node, &bodies, &attrs.map, attrs.define_opaque);
770770
let node = node.into();
771-
self.opt_hir_owner_nodes(Some(self.tcx.arena.alloc(hir::OwnerNodes {
772-
opt_hash_including_bodies,
773-
nodes: IndexVec::from_elem_n(
774-
hir::ParentedNode { parent: hir::ItemLocalId::INVALID, node },
775-
1,
776-
),
777-
bodies,
771+
772+
self.owner(hir::MaybeOwner::Owner(self.tcx.arena.alloc(OwnerInfo {
773+
attrs,
774+
nodes: hir::OwnerNodes {
775+
opt_hash_including_bodies,
776+
nodes: IndexVec::from_elem_n(
777+
hir::ParentedNode { parent: hir::ItemLocalId::INVALID, node },
778+
1,
779+
),
780+
bodies,
781+
},
782+
parenting: Default::default(),
783+
delayed_lints: Default::default(),
784+
trait_map: Default::default(),
778785
})));
779-
self.feed_owner_id().hir_attr_map(attrs);
780786
}
781787
}
782788

tests/incremental/clean.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ mod y {
2727
use x;
2828

2929
#[rustc_clean(
30-
except="opt_hir_owner_nodes,generics_of,predicates_of,type_of,fn_sig",
30+
except="owner,generics_of,predicates_of,type_of,fn_sig",
3131
cfg="bfail2",
3232
)]
3333
pub fn y() {
34-
//[bfail2]~^ ERROR `opt_hir_owner_nodes(y)` should be dirty but is not
34+
//[bfail2]~^ ERROR `owner(y)` should be dirty but is not
3535
//[bfail2]~| ERROR `generics_of(y)` should be dirty but is not
3636
//[bfail2]~| ERROR `predicates_of(y)` should be dirty but is not
3737
//[bfail2]~| ERROR `type_of(y)` should be dirty but is not

tests/incremental/hash-module-order.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
#![feature(rustc_attrs)]
1414

1515
#[cfg(rpass1)]
16-
#[rustc_clean(cfg="rpass1",except="opt_hir_owner_nodes")]
16+
#[rustc_clean(cfg="rpass1",except="owner")]
1717
mod foo {
1818
struct First;
1919
struct Second;
2020
}
2121

2222
#[cfg(rpass2)]
23-
#[rustc_clean(cfg="rpass2",except="opt_hir_owner_nodes")]
23+
#[rustc_clean(cfg="rpass2",except="owner")]
2424
mod foo {
2525
struct Second;
2626
struct First;

0 commit comments

Comments
 (0)