Skip to content

Commit fee3d49

Browse files
committed
Auto merge of #155777 - cjgillot:query-create-def, r=<try>
Make `create_def` a query
2 parents 9838411 + 9126582 commit fee3d49

15 files changed

Lines changed: 167 additions & 151 deletions

File tree

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ use rustc_data_structures::tagged_ptr::TaggedRef;
5151
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle};
5252
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5353
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
54-
use rustc_hir::definitions::PerParentDisambiguatorState;
5554
use rustc_hir::lints::{AttributeLint, DelayedLint, DynAttribute};
5655
use rustc_hir::{
5756
self as hir, AngleBrackets, ConstArg, GenericArg, HirId, ItemLocalMap, LifetimeSource,
@@ -94,7 +93,6 @@ pub mod stability;
9493
struct LoweringContext<'a, 'hir> {
9594
tcx: TyCtxt<'hir>,
9695
resolver: &'a ResolverAstLowering<'hir>,
97-
current_disambiguator: PerParentDisambiguatorState,
9896

9997
/// Used to allocate HIR nodes.
10098
arena: &'hir hir::Arena<'hir>,
@@ -169,7 +167,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
169167
Self {
170168
tcx,
171169
resolver,
172-
current_disambiguator: Default::default(),
173170
arena: tcx.hir_arena,
174171

175172
// HirId handling.
@@ -641,11 +638,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
641638
self.tcx.hir_def_key(self.local_def_id(node_id)),
642639
);
643640

644-
let def_id = self
645-
.tcx
646-
.at(span)
647-
.create_def(parent, name, def_kind, None, &mut self.current_disambiguator)
648-
.def_id();
641+
let def_id = self.tcx.at(span).create_def(parent, name, def_kind, None).def_id();
649642

650643
debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
651644
self.node_id_to_def_id.insert(node_id, def_id);
@@ -697,16 +690,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
697690
f: impl FnOnce(&mut Self) -> hir::OwnerNode<'hir>,
698691
) {
699692
let owner_id = self.owner_id(owner);
700-
let def_id = owner_id.def_id;
701693

702-
let new_disambig = self
703-
.resolver
704-
.disambiguators
705-
.get(&def_id)
706-
.map(|s| s.steal())
707-
.unwrap_or_else(|| PerParentDisambiguatorState::new(def_id));
708-
709-
let disambiguator = std::mem::replace(&mut self.current_disambiguator, new_disambig);
710694
let current_attrs = std::mem::take(&mut self.attrs);
711695
let current_bodies = std::mem::take(&mut self.bodies);
712696
let current_define_opaque = std::mem::take(&mut self.define_opaque);
@@ -741,7 +725,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
741725
assert!(self.impl_trait_bounds.is_empty());
742726
let info = self.make_owner_info(item);
743727

744-
self.current_disambiguator = disambiguator;
745728
self.attrs = current_attrs;
746729
self.bodies = current_bodies;
747730
self.define_opaque = current_define_opaque;

compiler/rustc_const_eval/src/interpret/intern.rs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use hir::def::DefKind;
1717
use rustc_ast::Mutability;
1818
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
19-
use rustc_hir::definitions::{DefPathData, PerParentDisambiguatorState};
19+
use rustc_hir::definitions::DefPathData;
2020
use rustc_hir::{self as hir};
2121
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
2222
use rustc_middle::mir::interpret::{
@@ -105,7 +105,6 @@ fn intern_shallow<'tcx, M: CompileTimeMachine<'tcx>>(
105105
ecx: &mut InterpCx<'tcx, M>,
106106
alloc_id: AllocId,
107107
mutability: Mutability,
108-
disambiguator: Option<&mut PerParentDisambiguatorState>,
109108
) -> Result<impl Iterator<Item = CtfeProvenance> + 'tcx, InternError> {
110109
trace!("intern_shallow {:?}", alloc_id);
111110
// remove allocation
@@ -124,13 +123,7 @@ fn intern_shallow<'tcx, M: CompileTimeMachine<'tcx>>(
124123
// link the alloc id to the actual allocation
125124
let alloc = ecx.tcx.mk_const_alloc(alloc);
126125
if let Some(static_id) = ecx.machine.static_def_id() {
127-
intern_as_new_static(
128-
ecx.tcx,
129-
static_id,
130-
alloc_id,
131-
alloc,
132-
disambiguator.expect("disambiguator needed"),
133-
);
126+
intern_as_new_static(ecx.tcx, static_id, alloc_id, alloc);
134127
} else {
135128
ecx.tcx.set_alloc_id_memory(alloc_id, alloc);
136129
}
@@ -144,18 +137,12 @@ fn intern_as_new_static<'tcx>(
144137
static_id: LocalDefId,
145138
alloc_id: AllocId,
146139
alloc: ConstAllocation<'tcx>,
147-
disambiguator: &mut PerParentDisambiguatorState,
148140
) {
149-
// `intern_const_alloc_recursive` is called once per static and it contains the `PerParentDisambiguatorState`.
150-
// The `<static_id>::{{nested}}` path is thus unique to `intern_const_alloc_recursive` and the
151-
// `PerParentDisambiguatorState` ensures the generated path is unique for this call as we generate
152-
// `<static_id>::{{nested#n}}` where `n` is the `n`th `intern_as_new_static` call.
153141
let feed = tcx.create_def(
154142
static_id,
155143
None,
156144
DefKind::Static { safety: hir::Safety::Safe, mutability: alloc.0.mutability, nested: true },
157145
Some(DefPathData::NestedStatic),
158-
disambiguator,
159146
);
160147
tcx.set_nested_alloc_id_static(alloc_id, feed.def_id());
161148

@@ -205,10 +192,6 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx>>(
205192
intern_kind: InternKind,
206193
ret: &MPlaceTy<'tcx>,
207194
) -> Result<(), InternError> {
208-
let mut disambiguator =
209-
ecx.machine.static_def_id().map(|id| PerParentDisambiguatorState::new(id));
210-
let mut disambiguator = disambiguator.as_mut();
211-
212195
// We are interning recursively, and for mutability we are distinguishing the "root" allocation
213196
// that we are starting in, and all other allocations that we are encountering recursively.
214197
let (base_mutability, inner_mutability, is_static) = match intern_kind {
@@ -246,15 +229,13 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx>>(
246229
// This gives us the initial set of nested allocations, which will then all be processed
247230
// recursively in the loop below.
248231
let mut todo: Vec<_> = if is_static {
249-
assert!(disambiguator.is_some());
250232
// Do not steal the root allocation, we need it later to create the return value of `eval_static_initializer`.
251233
// But still change its mutability to match the requested one.
252234
let (kind, alloc) = ecx.memory.alloc_map.get_mut(&base_alloc_id).unwrap();
253235
prepare_alloc(*ecx.tcx, *kind, alloc, base_mutability)?;
254236
alloc.provenance().ptrs().iter().map(|&(_, prov)| prov).collect()
255237
} else {
256-
assert!(disambiguator.is_none());
257-
intern_shallow(ecx, base_alloc_id, base_mutability, None)?.collect()
238+
intern_shallow(ecx, base_alloc_id, base_mutability)?.collect()
258239
};
259240
// We need to distinguish "has just been interned" from "was already in `tcx`",
260241
// so we track this in a separate set.
@@ -336,7 +317,7 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx>>(
336317
// okay with losing some potential for immutability here. This can anyway only affect
337318
// `static mut`.
338319
just_interned.insert(alloc_id);
339-
let next = intern_shallow(ecx, alloc_id, inner_mutability, disambiguator.as_deref_mut())?;
320+
let next = intern_shallow(ecx, alloc_id, inner_mutability)?;
340321
todo.extend(next);
341322
}
342323
if found_bad_mutable_ptr {
@@ -366,7 +347,7 @@ pub fn intern_const_alloc_for_constprop<'tcx, M: CompileTimeMachine<'tcx>>(
366347
return interp_ok(());
367348
}
368349
// Move allocation to `tcx`.
369-
if let Some(_) = intern_shallow(ecx, alloc_id, Mutability::Not, None).unwrap().next() {
350+
if let Some(_) = intern_shallow(ecx, alloc_id, Mutability::Not).unwrap().next() {
370351
// We are not doing recursive interning, so we don't currently support provenance.
371352
// (If this assertion ever triggers, we should just implement a
372353
// proper recursive interning loop -- or just call `intern_const_alloc_recursive`.
@@ -391,7 +372,7 @@ impl<'tcx> InterpCx<'tcx, DummyMachine> {
391372
let dest = self.allocate(layout, MemoryKind::Stack)?;
392373
f(self, &dest.clone().into())?;
393374
let alloc_id = dest.ptr().provenance.unwrap().alloc_id(); // this was just allocated, it must have provenance
394-
for prov in intern_shallow(self, alloc_id, Mutability::Not, None).unwrap() {
375+
for prov in intern_shallow(self, alloc_id, Mutability::Not).unwrap() {
395376
// We are not doing recursive interning, so we don't currently support provenance.
396377
// (If this assertion ever triggers, we should just implement a
397378
// proper recursive interning loop -- or just call `intern_const_alloc_recursive`.

compiler/rustc_hir/src/definitions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_data_structures::fx::FxHashMap;
1111
use rustc_data_structures::stable_hasher::StableHasher;
1212
use rustc_hashes::Hash64;
1313
use rustc_index::IndexVec;
14-
use rustc_macros::{BlobDecodable, Decodable, Encodable, extension};
14+
use rustc_macros::{BlobDecodable, Decodable, Encodable, HashStable_Generic, extension};
1515
use rustc_span::def_id::LocalDefIdMap;
1616
use rustc_span::{Symbol, kw, sym};
1717
use tracing::{debug, instrument};
@@ -277,7 +277,7 @@ impl DefPath {
277277
}
278278

279279
/// New variants should only be added in synchronization with `enum DefKind`.
280-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, BlobDecodable)]
280+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, BlobDecodable, HashStable_Generic)]
281281
pub enum DefPathData {
282282
// Root: these should only be used for the root nodes, because
283283
// they are treated specially by the `def_path` function.

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use rustc_ast::visit::walk_list;
1414
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
1515
use rustc_errors::ErrorGuaranteed;
1616
use rustc_hir::def::{DefKind, Res};
17-
use rustc_hir::def_id::LocalDefIdMap;
18-
use rustc_hir::definitions::{DefPathData, PerParentDisambiguatorsMap};
17+
use rustc_hir::definitions::DefPathData;
1918
use rustc_hir::intravisit::{self, InferKind, Visitor, VisitorExt};
2019
use rustc_hir::{
2120
self as hir, AmbigArg, GenericArg, GenericParam, GenericParamKind, HirId, LifetimeKind, Node,
@@ -31,7 +30,6 @@ use rustc_span::{Ident, Span, sym};
3130
use tracing::{debug, debug_span, instrument};
3231

3332
use crate::errors;
34-
use crate::hir::definitions::PerParentDisambiguatorState;
3533

3634
#[extension(trait RegionExt)]
3735
impl ResolvedArg {
@@ -66,7 +64,6 @@ impl ResolvedArg {
6664
struct BoundVarContext<'a, 'tcx> {
6765
tcx: TyCtxt<'tcx>,
6866
rbv: &'a mut ResolveBoundVars<'tcx>,
69-
disambiguators: &'a mut LocalDefIdMap<PerParentDisambiguatorState>,
7067
scope: ScopeRef<'a, 'tcx>,
7168
opaque_capture_errors: RefCell<Option<OpaqueHigherRankedLifetimeCaptureErrors>>,
7269
}
@@ -261,7 +258,6 @@ fn resolve_bound_vars(tcx: TyCtxt<'_>, local_def_id: hir::OwnerId) -> ResolveBou
261258
tcx,
262259
rbv: &mut rbv,
263260
scope: &Scope::Root { opt_parent_item: None },
264-
disambiguators: &mut Default::default(),
265261
opaque_capture_errors: RefCell::new(None),
266262
};
267263
match tcx.hir_owner_node(local_def_id) {
@@ -1106,12 +1102,11 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
11061102
where
11071103
F: for<'b> FnOnce(&mut BoundVarContext<'b, 'tcx>),
11081104
{
1109-
let BoundVarContext { tcx, rbv, disambiguators, .. } = self;
1105+
let BoundVarContext { tcx, rbv, .. } = self;
11101106
let nested_errors = RefCell::new(self.opaque_capture_errors.borrow_mut().take());
11111107
let mut this = BoundVarContext {
11121108
tcx: *tcx,
11131109
rbv,
1114-
disambiguators,
11151110
scope: &wrap_scope,
11161111
opaque_capture_errors: nested_errors,
11171112
};
@@ -1519,13 +1514,12 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
15191514
// `opaque_def_id` is unique to the `BoundVarContext` pass which is executed once
15201515
// per `resolve_bound_vars` query. This is the only location that creates
15211516
// `OpaqueLifetime` paths. `<opaque_def_id>::OpaqueLifetime(..)` is thus unique
1522-
// to this query and duplicates within the query are handled by `self.disambiguator`.
1517+
// to this query.
15231518
let feed = self.tcx.create_def(
15241519
opaque_def_id,
15251520
None,
15261521
DefKind::LifetimeParam,
15271522
Some(DefPathData::OpaqueLifetime(ident.name)),
1528-
self.disambiguators.get_or_create(opaque_def_id),
15291523
);
15301524
feed.def_span(ident.span);
15311525
feed.def_ident_span(Some(ident.span));

compiler/rustc_interface/src/passes.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ use rustc_codegen_ssa::traits::CodegenBackend;
1111
use rustc_codegen_ssa::{CompiledModules, CrateInfo};
1212
use rustc_data_structures::indexmap::IndexMap;
1313
use rustc_data_structures::steal::Steal;
14-
use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, WorkerLocal, par_fns};
14+
use rustc_data_structures::sync::{WorkerLocal, par_fns};
1515
use rustc_data_structures::thousands;
1616
use rustc_errors::DiagCallback;
1717
use rustc_errors::timings::TimingSection;
1818
use rustc_expand::base::{ExtCtxt, LintStoreExpand};
1919
use rustc_feature::Features;
2020
use rustc_fs_util::try_canonicalize;
2121
use rustc_hir::attrs::AttributeKind;
22-
use rustc_hir::def_id::{LOCAL_CRATE, StableCrateId, StableCrateIdMap};
23-
use rustc_hir::definitions::Definitions;
22+
use rustc_hir::def_id::{LOCAL_CRATE, StableCrateId};
2423
use rustc_hir::limit::Limit;
2524
use rustc_hir::lints::DelayedLint;
2625
use rustc_hir::{Attribute, MaybeOwner, Target, find_attr};
@@ -39,7 +38,6 @@ use rustc_passes::{abi_test, input_stats, layout_test};
3938
use rustc_resolve::{Resolver, ResolverOutputs};
4039
use rustc_session::Session;
4140
use rustc_session::config::{CrateType, Input, OutFileName, OutputFilenames, OutputType};
42-
use rustc_session::cstore::Untracked;
4341
use rustc_session::output::{filename_for_input, invalid_output_for_target};
4442
use rustc_session::parse::feature_err;
4543
use rustc_session::search_paths::PathKind;
@@ -946,13 +944,7 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
946944

947945
let dep_graph = setup_dep_graph(sess, crate_name, stable_crate_id);
948946

949-
let cstore =
950-
FreezeLock::new(Box::new(CStore::new(compiler.codegen_backend.metadata_loader())) as _);
951-
let definitions = FreezeLock::new(Definitions::new(stable_crate_id));
952-
953-
let stable_crate_ids = FreezeLock::new(StableCrateIdMap::default());
954-
let untracked =
955-
Untracked { cstore, source_span: AppendOnlyIndexVec::new(), definitions, stable_crate_ids };
947+
let cstore = Box::new(CStore::new(compiler.codegen_backend.metadata_loader())) as _;
956948

957949
// We're constructing the HIR here; we don't care what we will
958950
// read, since we haven't even constructed the *input* to
@@ -993,7 +985,7 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
993985
stable_crate_id,
994986
&arena,
995987
&hir_arena,
996-
untracked,
988+
cstore,
997989
dep_graph,
998990
rustc_query_impl::make_dep_kind_vtables(&arena),
999991
rustc_query_impl::query_system(

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ use std::fmt;
5151
use std::hash::Hash;
5252

5353
use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
54-
use rustc_data_structures::stable_hasher::{StableHasher, StableOrd, ToStableHashKey};
54+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableOrd, ToStableHashKey};
5555
use rustc_hir::def_id::DefId;
5656
use rustc_hir::definitions::DefPathHash;
5757
use rustc_macros::{Decodable, Encodable, HashStable};
5858
use rustc_span::Symbol;
5959

6060
use super::{KeyFingerprintStyle, SerializedDepNodeIndex};
6161
use crate::dep_graph::DepNodeKey;
62+
use crate::ich::StableHashingContext;
6263
use crate::mono::MonoItem;
6364
use crate::ty::{TyCtxt, tls};
6465

@@ -152,6 +153,14 @@ impl fmt::Debug for DepNode {
152153
}
153154
}
154155

156+
impl HashStable<StableHashingContext<'_>> for DepNode {
157+
fn hash_stable(&self, hcx: &mut StableHashingContext<'_>, hasher: &mut StableHasher) {
158+
let DepNode { kind, key_fingerprint } = self;
159+
kind.hash_stable(hcx, hasher);
160+
Fingerprint::from(*key_fingerprint).hash_stable(hcx, hasher);
161+
}
162+
}
163+
155164
/// This struct stores function pointers and other metadata for a particular DepKind.
156165
///
157166
/// Information is retrieved by indexing the `DEP_KINDS` array using the integer value
@@ -269,7 +278,7 @@ macro_rules! define_dep_nodes {
269278
// encoding. The derived Encodable/Decodable uses leb128 encoding which is
270279
// dense when only considering this enum. But DepKind is encoded in a larger
271280
// struct, and there we can take advantage of the unused bits in the u16.
272-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
281+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, HashStable)]
273282
#[allow(non_camel_case_types)]
274283
#[repr(u16)] // Must be kept in sync with the rest of `DepKind`.
275284
pub enum DepKind {

0 commit comments

Comments
 (0)