Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 1 addition & 18 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ use rustc_data_structures::tagged_ptr::TaggedRef;
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle};
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
use rustc_hir::definitions::PerParentDisambiguatorState;
use rustc_hir::lints::{AttributeLint, DelayedLint, DynAttribute};
use rustc_hir::{
self as hir, AngleBrackets, ConstArg, GenericArg, HirId, ItemLocalMap, LifetimeSource,
Expand Down Expand Up @@ -94,7 +93,6 @@ pub mod stability;
struct LoweringContext<'a, 'hir> {
tcx: TyCtxt<'hir>,
resolver: &'a ResolverAstLowering<'hir>,
current_disambiguator: PerParentDisambiguatorState,

/// Used to allocate HIR nodes.
arena: &'hir hir::Arena<'hir>,
Expand Down Expand Up @@ -169,7 +167,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
Self {
tcx,
resolver,
current_disambiguator: Default::default(),
arena: tcx.hir_arena,

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

let def_id = self
.tcx
.at(span)
.create_def(parent, name, def_kind, None, &mut self.current_disambiguator)
.def_id();
let def_id = self.tcx.at(span).create_def(parent, name, def_kind, None).def_id();

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

let new_disambig = self
.resolver
.disambiguators
.get(&def_id)
.map(|s| s.steal())
.unwrap_or_else(|| PerParentDisambiguatorState::new(def_id));

let disambiguator = std::mem::replace(&mut self.current_disambiguator, new_disambig);
let current_attrs = std::mem::take(&mut self.attrs);
let current_bodies = std::mem::take(&mut self.bodies);
let current_define_opaque = std::mem::take(&mut self.define_opaque);
Expand Down Expand Up @@ -741,7 +725,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
assert!(self.impl_trait_bounds.is_empty());
let info = self.make_owner_info(item);

self.current_disambiguator = disambiguator;
self.attrs = current_attrs;
self.bodies = current_bodies;
self.define_opaque = current_define_opaque;
Expand Down
31 changes: 6 additions & 25 deletions compiler/rustc_const_eval/src/interpret/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use hir::def::DefKind;
use rustc_ast::Mutability;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
use rustc_hir::definitions::{DefPathData, PerParentDisambiguatorState};
use rustc_hir::definitions::DefPathData;
use rustc_hir::{self as hir};
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
use rustc_middle::mir::interpret::{
Expand Down Expand Up @@ -105,7 +105,6 @@ fn intern_shallow<'tcx, M: CompileTimeMachine<'tcx>>(
ecx: &mut InterpCx<'tcx, M>,
alloc_id: AllocId,
mutability: Mutability,
disambiguator: Option<&mut PerParentDisambiguatorState>,
) -> Result<impl Iterator<Item = CtfeProvenance> + 'tcx, InternError> {
trace!("intern_shallow {:?}", alloc_id);
// remove allocation
Expand All @@ -124,13 +123,7 @@ fn intern_shallow<'tcx, M: CompileTimeMachine<'tcx>>(
// link the alloc id to the actual allocation
let alloc = ecx.tcx.mk_const_alloc(alloc);
if let Some(static_id) = ecx.machine.static_def_id() {
intern_as_new_static(
ecx.tcx,
static_id,
alloc_id,
alloc,
disambiguator.expect("disambiguator needed"),
);
intern_as_new_static(ecx.tcx, static_id, alloc_id, alloc);
} else {
ecx.tcx.set_alloc_id_memory(alloc_id, alloc);
}
Expand All @@ -144,18 +137,12 @@ fn intern_as_new_static<'tcx>(
static_id: LocalDefId,
alloc_id: AllocId,
alloc: ConstAllocation<'tcx>,
disambiguator: &mut PerParentDisambiguatorState,
) {
// `intern_const_alloc_recursive` is called once per static and it contains the `PerParentDisambiguatorState`.
// The `<static_id>::{{nested}}` path is thus unique to `intern_const_alloc_recursive` and the
// `PerParentDisambiguatorState` ensures the generated path is unique for this call as we generate
// `<static_id>::{{nested#n}}` where `n` is the `n`th `intern_as_new_static` call.
let feed = tcx.create_def(
static_id,
None,
DefKind::Static { safety: hir::Safety::Safe, mutability: alloc.0.mutability, nested: true },
Some(DefPathData::NestedStatic),
disambiguator,
);
tcx.set_nested_alloc_id_static(alloc_id, feed.def_id());

Expand Down Expand Up @@ -205,10 +192,6 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx>>(
intern_kind: InternKind,
ret: &MPlaceTy<'tcx>,
) -> Result<(), InternError> {
let mut disambiguator =
ecx.machine.static_def_id().map(|id| PerParentDisambiguatorState::new(id));
let mut disambiguator = disambiguator.as_mut();

// We are interning recursively, and for mutability we are distinguishing the "root" allocation
// that we are starting in, and all other allocations that we are encountering recursively.
let (base_mutability, inner_mutability, is_static) = match intern_kind {
Expand Down Expand Up @@ -246,15 +229,13 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx>>(
// This gives us the initial set of nested allocations, which will then all be processed
// recursively in the loop below.
let mut todo: Vec<_> = if is_static {
assert!(disambiguator.is_some());
// Do not steal the root allocation, we need it later to create the return value of `eval_static_initializer`.
// But still change its mutability to match the requested one.
let (kind, alloc) = ecx.memory.alloc_map.get_mut(&base_alloc_id).unwrap();
prepare_alloc(*ecx.tcx, *kind, alloc, base_mutability)?;
alloc.provenance().ptrs().iter().map(|&(_, prov)| prov).collect()
} else {
assert!(disambiguator.is_none());
intern_shallow(ecx, base_alloc_id, base_mutability, None)?.collect()
intern_shallow(ecx, base_alloc_id, base_mutability)?.collect()
};
// We need to distinguish "has just been interned" from "was already in `tcx`",
// so we track this in a separate set.
Expand Down Expand Up @@ -336,7 +317,7 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx>>(
// okay with losing some potential for immutability here. This can anyway only affect
// `static mut`.
just_interned.insert(alloc_id);
let next = intern_shallow(ecx, alloc_id, inner_mutability, disambiguator.as_deref_mut())?;
let next = intern_shallow(ecx, alloc_id, inner_mutability)?;
todo.extend(next);
}
if found_bad_mutable_ptr {
Expand Down Expand Up @@ -366,7 +347,7 @@ pub fn intern_const_alloc_for_constprop<'tcx, M: CompileTimeMachine<'tcx>>(
return interp_ok(());
}
// Move allocation to `tcx`.
if let Some(_) = intern_shallow(ecx, alloc_id, Mutability::Not, None).unwrap().next() {
if let Some(_) = intern_shallow(ecx, alloc_id, Mutability::Not).unwrap().next() {
// We are not doing recursive interning, so we don't currently support provenance.
// (If this assertion ever triggers, we should just implement a
// proper recursive interning loop -- or just call `intern_const_alloc_recursive`.
Expand All @@ -391,7 +372,7 @@ impl<'tcx> InterpCx<'tcx, DummyMachine> {
let dest = self.allocate(layout, MemoryKind::Stack)?;
f(self, &dest.clone().into())?;
let alloc_id = dest.ptr().provenance.unwrap().alloc_id(); // this was just allocated, it must have provenance
for prov in intern_shallow(self, alloc_id, Mutability::Not, None).unwrap() {
for prov in intern_shallow(self, alloc_id, Mutability::Not).unwrap() {
// We are not doing recursive interning, so we don't currently support provenance.
// (If this assertion ever triggers, we should just implement a
// proper recursive interning loop -- or just call `intern_const_alloc_recursive`.
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_hashes::Hash64;
use rustc_index::IndexVec;
use rustc_macros::{BlobDecodable, Decodable, Encodable, extension};
use rustc_macros::{BlobDecodable, Decodable, Encodable, HashStable_Generic, extension};
use rustc_span::def_id::LocalDefIdMap;
use rustc_span::{Symbol, kw, sym};
use tracing::{debug, instrument};
Expand Down Expand Up @@ -277,7 +277,7 @@ impl DefPath {
}

/// New variants should only be added in synchronization with `enum DefKind`.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, BlobDecodable)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, BlobDecodable, HashStable_Generic)]
pub enum DefPathData {
// Root: these should only be used for the root nodes, because
// they are treated specially by the `def_path` function.
Expand Down
12 changes: 3 additions & 9 deletions compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use rustc_ast::visit::walk_list;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_errors::ErrorGuaranteed;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::LocalDefIdMap;
use rustc_hir::definitions::{DefPathData, PerParentDisambiguatorsMap};
use rustc_hir::definitions::DefPathData;
use rustc_hir::intravisit::{self, InferKind, Visitor, VisitorExt};
use rustc_hir::{
self as hir, AmbigArg, GenericArg, GenericParam, GenericParamKind, HirId, LifetimeKind, Node,
Expand All @@ -31,7 +30,6 @@ use rustc_span::{Ident, Span, sym};
use tracing::{debug, debug_span, instrument};

use crate::errors;
use crate::hir::definitions::PerParentDisambiguatorState;

#[extension(trait RegionExt)]
impl ResolvedArg {
Expand Down Expand Up @@ -66,7 +64,6 @@ impl ResolvedArg {
struct BoundVarContext<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
rbv: &'a mut ResolveBoundVars<'tcx>,
disambiguators: &'a mut LocalDefIdMap<PerParentDisambiguatorState>,
scope: ScopeRef<'a, 'tcx>,
opaque_capture_errors: RefCell<Option<OpaqueHigherRankedLifetimeCaptureErrors>>,
}
Expand Down Expand Up @@ -261,7 +258,6 @@ fn resolve_bound_vars(tcx: TyCtxt<'_>, local_def_id: hir::OwnerId) -> ResolveBou
tcx,
rbv: &mut rbv,
scope: &Scope::Root { opt_parent_item: None },
disambiguators: &mut Default::default(),
opaque_capture_errors: RefCell::new(None),
};
match tcx.hir_owner_node(local_def_id) {
Expand Down Expand Up @@ -1106,12 +1102,11 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
where
F: for<'b> FnOnce(&mut BoundVarContext<'b, 'tcx>),
{
let BoundVarContext { tcx, rbv, disambiguators, .. } = self;
let BoundVarContext { tcx, rbv, .. } = self;
let nested_errors = RefCell::new(self.opaque_capture_errors.borrow_mut().take());
let mut this = BoundVarContext {
tcx: *tcx,
rbv,
disambiguators,
scope: &wrap_scope,
opaque_capture_errors: nested_errors,
};
Expand Down Expand Up @@ -1519,13 +1514,12 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
// `opaque_def_id` is unique to the `BoundVarContext` pass which is executed once
// per `resolve_bound_vars` query. This is the only location that creates
// `OpaqueLifetime` paths. `<opaque_def_id>::OpaqueLifetime(..)` is thus unique
// to this query and duplicates within the query are handled by `self.disambiguator`.
// to this query.
let feed = self.tcx.create_def(
opaque_def_id,
None,
DefKind::LifetimeParam,
Some(DefPathData::OpaqueLifetime(ident.name)),
self.disambiguators.get_or_create(opaque_def_id),
);
feed.def_span(ident.span);
feed.def_ident_span(Some(ident.span));
Expand Down
16 changes: 4 additions & 12 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_codegen_ssa::{CompiledModules, CrateInfo};
use rustc_data_structures::indexmap::IndexMap;
use rustc_data_structures::steal::Steal;
use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, WorkerLocal, par_fns};
use rustc_data_structures::sync::{WorkerLocal, par_fns};
use rustc_data_structures::thousands;
use rustc_errors::DiagCallback;
use rustc_errors::timings::TimingSection;
use rustc_expand::base::{ExtCtxt, LintStoreExpand};
use rustc_feature::Features;
use rustc_fs_util::try_canonicalize;
use rustc_hir::attrs::AttributeKind;
use rustc_hir::def_id::{LOCAL_CRATE, StableCrateId, StableCrateIdMap};
use rustc_hir::definitions::Definitions;
use rustc_hir::def_id::{LOCAL_CRATE, StableCrateId};
use rustc_hir::limit::Limit;
use rustc_hir::lints::DelayedLint;
use rustc_hir::{Attribute, MaybeOwner, Target, find_attr};
Expand All @@ -39,7 +38,6 @@ use rustc_passes::{abi_test, input_stats, layout_test};
use rustc_resolve::{Resolver, ResolverOutputs};
use rustc_session::Session;
use rustc_session::config::{CrateType, Input, OutFileName, OutputFilenames, OutputType};
use rustc_session::cstore::Untracked;
use rustc_session::output::{filename_for_input, invalid_output_for_target};
use rustc_session::parse::feature_err;
use rustc_session::search_paths::PathKind;
Expand Down Expand Up @@ -946,13 +944,7 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(

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

let cstore =
FreezeLock::new(Box::new(CStore::new(compiler.codegen_backend.metadata_loader())) as _);
let definitions = FreezeLock::new(Definitions::new(stable_crate_id));

let stable_crate_ids = FreezeLock::new(StableCrateIdMap::default());
let untracked =
Untracked { cstore, source_span: AppendOnlyIndexVec::new(), definitions, stable_crate_ids };
let cstore = Box::new(CStore::new(compiler.codegen_backend.metadata_loader())) as _;

// We're constructing the HIR here; we don't care what we will
// read, since we haven't even constructed the *input* to
Expand Down Expand Up @@ -993,7 +985,7 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
stable_crate_id,
&arena,
&hir_arena,
untracked,
cstore,
dep_graph,
rustc_query_impl::make_dep_kind_vtables(&arena),
rustc_query_impl::query_system(
Expand Down
13 changes: 11 additions & 2 deletions compiler/rustc_middle/src/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ use std::fmt;
use std::hash::Hash;

use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
use rustc_data_structures::stable_hasher::{StableHasher, StableOrd, ToStableHashKey};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableOrd, ToStableHashKey};
use rustc_hir::def_id::DefId;
use rustc_hir::definitions::DefPathHash;
use rustc_macros::{Decodable, Encodable, HashStable};
use rustc_span::Symbol;

use super::{KeyFingerprintStyle, SerializedDepNodeIndex};
use crate::dep_graph::DepNodeKey;
use crate::ich::StableHashingContext;
use crate::mono::MonoItem;
use crate::ty::{TyCtxt, tls};

Expand Down Expand Up @@ -152,6 +153,14 @@ impl fmt::Debug for DepNode {
}
}

impl HashStable<StableHashingContext<'_>> for DepNode {
fn hash_stable(&self, hcx: &mut StableHashingContext<'_>, hasher: &mut StableHasher) {
let DepNode { kind, key_fingerprint } = self;
kind.hash_stable(hcx, hasher);
Fingerprint::from(*key_fingerprint).hash_stable(hcx, hasher);
}
}

/// This struct stores function pointers and other metadata for a particular DepKind.
///
/// Information is retrieved by indexing the `DEP_KINDS` array using the integer value
Expand Down Expand Up @@ -269,7 +278,7 @@ macro_rules! define_dep_nodes {
// encoding. The derived Encodable/Decodable uses leb128 encoding which is
// dense when only considering this enum. But DepKind is encoded in a larger
// struct, and there we can take advantage of the unused bits in the u16.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, HashStable)]
#[allow(non_camel_case_types)]
#[repr(u16)] // Must be kept in sync with the rest of `DepKind`.
pub enum DepKind {
Expand Down
Loading
Loading