Skip to content

Commit 7769c1a

Browse files
committed
Auto merge of #155678 - aerooneqq:single-owners-query-exp, r=<try>
experiment: single owner query
2 parents cf79d03 + ba05724 commit 7769c1a

45 files changed

Lines changed: 1163 additions & 1106 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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +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+
[] 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,
122127
]);
123128
)
124129
}

compiler/rustc_middle/src/hir/map.rs

Lines changed: 26 additions & 2 deletions
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

20-
use crate::hir::{ModuleItems, nested_filter};
21+
use crate::hir::{ModuleItems, ProjectedMaybeOwner, 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,29 @@ 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+
ProjectedMaybeOwner::Owner(_) => HirId::make_owner(def_id),
109+
ProjectedMaybeOwner::NonOwner(hir_id) => *hir_id,
110+
}
111+
}
112+
113+
pub fn opt_ast_lowering_delayed_lints(self, id: OwnerId) -> Option<&'tcx DelayedLints> {
114+
self.owner(id.def_id).as_owner().map(|o| o.delayed_lints)
115+
}
116+
117+
pub fn in_scope_traits_map(
118+
self,
119+
id: OwnerId,
120+
) -> Option<&'tcx ItemLocalMap<&'tcx [TraitCandidate<'tcx>]>> {
121+
self.owner(id.def_id).as_owner().map(|owner_info| owner_info.trait_map)
122+
}
123+
124+
pub fn opt_hir_owner_nodes(self, def_id: LocalDefId) -> Option<&'tcx OwnerNodes<'tcx>> {
125+
self.owner(def_id).as_owner().map(|i| i.nodes)
126+
}
127+
104128
#[inline]
105129
fn expect_hir_owner_nodes(self, def_id: LocalDefId) -> &'tcx OwnerNodes<'tcx> {
106130
self.opt_hir_owner_nodes(def_id)

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 56 additions & 17 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};
@@ -67,14 +68,14 @@ impl<'hir> Crate<'hir> {
6768
/// `owners` of `hir_crate` or it can be delayed AST owner (i.e., delegations)
6869
/// we need to firstly check in `hir_crate` and then delayed AST owners.
6970
/// 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+
pub fn owner(&'hir self, tcx: TyCtxt<'hir>, def_id: LocalDefId) -> &'hir MaybeOwner<'hir> {
7172
// Delayed LocalDefId can be in `self.owners` if there exists non-delayed LocalDefId
7273
// which is greater than delayed LocalDefId, we use IndexVec for owners,
7374
// so we will call ensure_contains_elem which will grow it.
7475
if let Some(owner) = self.owners.get(def_id)
7576
&& (self.delayed_ids.is_empty() || !matches!(owner, MaybeOwner::Phantom))
7677
{
77-
return *owner;
78+
return owner;
7879
}
7980

8081
if self.delayed_ids.contains(&def_id) {
@@ -431,8 +432,7 @@ impl<'tcx> TyCtxt<'tcx> {
431432
HirId {
432433
owner: parent_owner_id,
433434
local_id: self
434-
.hir_crate(())
435-
.owner(self, parent_owner_id.def_id)
435+
.owner(parent_owner_id.def_id)
436436
.unwrap()
437437
.parenting
438438
.get(&owner_id.def_id)
@@ -461,23 +461,65 @@ pub struct Hashes {
461461
pub attrs_hash: Option<Fingerprint>,
462462
}
463463

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+
pub parenting: &'tcx LocalDefIdMap<ItemLocalId>,
471+
472+
/// Map indicating what traits are in scope for places where this
473+
/// is relevant; generated by resolve.
474+
pub trait_map: &'tcx ItemLocalMap<&'tcx [TraitCandidate<'tcx>]>,
475+
476+
#[stable_hasher(ignore)]
477+
pub delayed_lints: &'tcx DelayedLints,
478+
}
479+
480+
#[derive(Debug, HashStable)]
481+
pub enum ProjectedMaybeOwner<'tcx> {
482+
Owner(ProjectedOwnerInfo<'tcx>),
483+
NonOwner(HirId),
484+
}
485+
486+
impl<'tcx> From<&'tcx MaybeOwner<'tcx>> for ProjectedMaybeOwner<'tcx> {
487+
fn from(value: &'tcx MaybeOwner<'tcx>) -> Self {
488+
match value {
489+
MaybeOwner::Owner(o) => ProjectedMaybeOwner::Owner(ProjectedOwnerInfo {
490+
nodes: &o.nodes,
491+
parenting: &o.parenting,
492+
trait_map: &o.trait_map,
493+
delayed_lints: &o.delayed_lints,
494+
}),
495+
MaybeOwner::NonOwner(hir_id) => ProjectedMaybeOwner::NonOwner(*hir_id),
496+
MaybeOwner::Phantom => panic!(),
497+
}
498+
}
499+
}
500+
501+
impl<'tcx> ProjectedMaybeOwner<'tcx> {
502+
pub fn as_owner(&'tcx self) -> Option<&'tcx ProjectedOwnerInfo<'tcx>> {
503+
match self {
504+
ProjectedMaybeOwner::Owner(i) => Some(i),
505+
ProjectedMaybeOwner::NonOwner(_) => None,
506+
}
507+
}
508+
509+
pub fn unwrap(&'tcx self) -> &'tcx ProjectedOwnerInfo<'tcx> {
510+
self.as_owner().unwrap_or_else(|| panic!("Not a HIR owner"))
511+
}
512+
}
513+
464514
pub fn provide(providers: &mut Providers) {
465515
providers.hir_crate_items = map::hir_crate_items;
466516
providers.crate_hash = map::crate_hash;
467517
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),
472-
};
473-
providers.opt_hir_owner_nodes =
474-
|tcx, id| tcx.hir_crate(()).owner(tcx, id).as_owner().map(|i| &i.nodes);
475-
providers.hir_owner_parent_q = |tcx, owner_id| tcx.hir_owner_parent_impl(owner_id);
476518
providers.hir_attr_map = |tcx, id| {
477519
tcx.hir_crate(()).owner(tcx, id.def_id).as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs)
478520
};
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);
521+
providers.owner = |tcx, def_id| ProjectedMaybeOwner::from(tcx.hir_crate(()).owner(tcx, def_id));
522+
providers.hir_owner_parent_q = |tcx, owner_id| tcx.hir_owner_parent_impl(owner_id);
481523
providers.def_span = |tcx, def_id| tcx.hir_span(tcx.local_def_id_to_hir_id(def_id));
482524
providers.def_ident_span = |tcx, def_id| {
483525
let hir_id = tcx.local_def_id_to_hir_id(def_id);
@@ -517,7 +559,4 @@ pub fn provide(providers: &mut Providers) {
517559
|tcx, trait_id| tcx.resolutions(()).trait_impls.get(&trait_id).map_or(&[], |xs| &xs[..]);
518560
providers.expn_that_defined =
519561
|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-
};
523562
}

compiler/rustc_middle/src/queries.rs

Lines changed: 9 additions & 33 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,8 +216,15 @@ rustc_queries! {
216216
desc { "lowering the delayed AST owner `{}`", tcx.def_path_str(def_id) }
217217
}
218218

219-
query delayed_owner(def_id: LocalDefId) -> hir::MaybeOwner<'tcx> {
219+
query owner(def_id: LocalDefId) -> &'tcx rustc_middle::hir::ProjectedMaybeOwner<'tcx> {
220+
desc { "getting owner for `{}`", tcx.def_path_str(def_id) }
221+
arena_cache
222+
feedable
223+
}
224+
225+
query delayed_owner(def_id: LocalDefId) -> &'tcx hir::MaybeOwner<'tcx> {
220226
feedable
227+
arena_cache
221228
desc { "getting child of lowered delayed AST owner `{}`", tcx.def_path_str(def_id) }
222229
}
223230

@@ -238,12 +245,6 @@ rustc_queries! {
238245
cache_on_disk
239246
}
240247

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-
247248
/// Gives access to the HIR node's parent for the HIR owner `key`.
248249
///
249250
/// This can be conveniently accessed by `tcx.hir_*` methods.
@@ -252,15 +253,6 @@ rustc_queries! {
252253
desc { "getting HIR parent of `{}`", tcx.def_path_str(key) }
253254
}
254255

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-
264256
/// Gives access to the HIR attributes inside the HIR owner `key`.
265257
///
266258
/// This can be conveniently accessed by `tcx.hir_*` methods.
@@ -270,18 +262,6 @@ rustc_queries! {
270262
feedable
271263
}
272264

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-
285265
/// Returns the *default* of the const pararameter given by `DefId`.
286266
///
287267
/// E.g., given `struct Ty<const N: usize = 3>;` this returns `3` for `N`.
@@ -1884,10 +1864,6 @@ rustc_queries! {
18841864
query specializes(_: (DefId, DefId)) -> bool {
18851865
desc { "computing whether impls specialize one another" }
18861866
}
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-
}
18911867

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

compiler/rustc_middle/src/ty/context.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ use tracing::{debug, instrument};
5555
use crate::arena::Arena;
5656
use crate::dep_graph::dep_node::make_metadata;
5757
use crate::dep_graph::{DepGraph, DepKindVTable, DepNodeIndex};
58+
use crate::hir::{ProjectedMaybeOwner, ProjectedOwnerInfo};
5859
use crate::ich::StableHashingContext;
5960
use crate::infer::canonical::{CanonicalParamEnvCache, CanonicalVarKind};
6061
use crate::lint::emit_lint_base;
@@ -759,23 +760,28 @@ impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> {
759760

760761
// Fills in all the important parts needed by HIR queries
761762
pub fn feed_hir(&self) {
762-
self.local_def_id_to_hir_id(HirId::make_owner(self.def_id()));
763-
764763
let node = hir::OwnerNode::Synthetic;
765764
let bodies = Default::default();
766765
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();
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,
778-
})));
770+
771+
self.owner(ProjectedMaybeOwner::Owner(ProjectedOwnerInfo {
772+
nodes: self.tcx.arena.alloc(hir::OwnerNodes {
773+
opt_hash_including_bodies,
774+
nodes: IndexVec::from_elem_n(
775+
hir::ParentedNode { parent: hir::ItemLocalId::INVALID, node },
776+
1,
777+
),
778+
bodies,
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+
779785
self.feed_owner_id().hir_attr_map(attrs);
780786
}
781787
}

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)