Skip to content

Commit

Permalink
Separate source_span and expn_that_defined from Definitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Jun 14, 2022
1 parent b676edd commit 34e4d72
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 135 deletions.
10 changes: 2 additions & 8 deletions compiler/rustc_ast_lowering/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::definitions::DefPathData;
use rustc_session::parse::feature_err;
use rustc_span::{sym, ExpnId, Span};
use rustc_span::{sym, Span};
use rustc_target::asm;
use std::collections::hash_map::Entry;
use std::fmt::Write;
Expand Down Expand Up @@ -243,13 +243,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// Wrap the expression in an AnonConst.
let parent_def_id = self.current_hir_id_owner;
let node_id = self.next_node_id();
self.create_def(
parent_def_id,
node_id,
DefPathData::AnonConst,
ExpnId::root(),
*op_sp,
);
self.create_def(parent_def_id, node_id, DefPathData::AnonConst);
let anon_const = AnonConst { id: node_id, value: P(expr) };
hir::InlineAsmOperand::SymFn {
anon_const: self.lower_anon_const(&anon_const),
Expand Down
9 changes: 1 addition & 8 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def::Res;
use rustc_hir::definitions::DefPathData;
use rustc_span::hygiene::ExpnId;
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
use rustc_span::symbol::{sym, Ident};
use rustc_span::DUMMY_SP;
Expand Down Expand Up @@ -358,13 +357,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let node_id = self.next_node_id();

// Add a definition for the in-band const def.
self.create_def(
parent_def_id,
node_id,
DefPathData::AnonConst,
ExpnId::root(),
arg.span,
);
self.create_def(parent_def_id, node_id, DefPathData::AnonConst);

let anon_const = AnonConst { id: node_id, value: arg };
generic_args.push(AngleBracketedArg::Arg(GenericArg::Const(anon_const)));
Expand Down
48 changes: 14 additions & 34 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ use rustc_query_system::ich::StableHashingContext;
use rustc_session::cstore::CrateStoreDyn;
use rustc_session::parse::feature_err;
use rustc_session::Session;
use rustc_span::hygiene::{ExpnId, MacroKind};
use rustc_span::hygiene::MacroKind;
use rustc_span::source_map::DesugaringKind;
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{Span, DUMMY_SP};
Expand Down Expand Up @@ -391,6 +391,7 @@ fn compute_hir_hash(
sess: &Session,
definitions: &Definitions,
cstore: &CrateStoreDyn,
resolver: &ResolverOutputs,
owners: &IndexVec<LocalDefId, hir::MaybeOwner<&hir::OwnerInfo<'_>>>,
) -> Fingerprint {
let mut hir_body_nodes: Vec<_> = owners
Expand All @@ -404,7 +405,7 @@ fn compute_hir_hash(
hir_body_nodes.sort_unstable_by_key(|bn| bn.0);

let mut stable_hasher = StableHasher::new();
let mut hcx = StableHashingContext::new(sess, definitions, cstore);
let mut hcx = StableHashingContext::new(sess, definitions, cstore, &resolver.source_span);
hir_body_nodes.hash_stable(&mut hcx, &mut stable_hasher);
stable_hasher.finish()
}
Expand Down Expand Up @@ -437,7 +438,7 @@ pub fn lower_crate<'hir>(
.lower_node(def_id);
}

let hir_hash = compute_hir_hash(sess, definitions, cstore, &owners);
let hir_hash = compute_hir_hash(sess, definitions, cstore, resolver, &owners);
let krate = hir::Crate { owners, hir_hash };
arena.alloc(krate)
}
Expand All @@ -459,16 +460,19 @@ enum ParenthesizedGenericArgs {

impl<'a, 'hir> LoweringContext<'a, 'hir> {
fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {
StableHashingContext::new(self.sess, self.definitions, self.cstore)
StableHashingContext::new(
self.sess,
self.definitions,
self.cstore,
&self.resolver.source_span,
)
}

fn create_def(
&mut self,
parent: LocalDefId,
node_id: ast::NodeId,
data: DefPathData,
expn_id: ExpnId,
span: Span,
) -> LocalDefId {
assert!(
self.opt_local_def_id(node_id).is_none(),
Expand All @@ -478,7 +482,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.definitions.def_key(self.local_def_id(node_id)),
);

let def_id = self.definitions.create_def(parent, data, expn_id, span);
let def_id = self.definitions.create_def(parent, data);

// Some things for which we allocate `LocalDefId`s don't correspond to
// anything in the AST, so they don't have a `NodeId`. For these cases
Expand Down Expand Up @@ -998,13 +1002,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

let parent_def_id = self.current_hir_id_owner;
let impl_trait_node_id = self.next_node_id();
self.create_def(
parent_def_id,
impl_trait_node_id,
DefPathData::ImplTrait,
ExpnId::root(),
constraint.span,
);
self.create_def(parent_def_id, impl_trait_node_id, DefPathData::ImplTrait);

self.with_dyn_type_scope(false, |this| {
let node_id = this.next_node_id();
Expand Down Expand Up @@ -1110,13 +1108,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let node_id = self.next_node_id();

// Add a definition for the in-band const def.
self.create_def(
parent_def_id,
node_id,
DefPathData::AnonConst,
ExpnId::root(),
ty.span,
);
self.create_def(parent_def_id, node_id, DefPathData::AnonConst);

let span = self.lower_span(ty.span);
let path_expr = Expr {
Expand Down Expand Up @@ -1624,13 +1616,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let inner_node_id = self.next_node_id();

// Add a definition for the in scope lifetime def.
self.create_def(
opaque_ty_def_id,
inner_node_id,
DefPathData::LifetimeNs(name),
ExpnId::root(),
span.with_parent(None),
);
self.create_def(opaque_ty_def_id, inner_node_id, DefPathData::LifetimeNs(name));

let (p_name, inner_res) = match outer_res {
// Input lifetime like `'a`:
Expand Down Expand Up @@ -1824,8 +1810,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
captured_lifetimes.parent_def_id,
p_id,
DefPathData::LifetimeNs(p_name.ident().name),
ExpnId::root(),
span.with_parent(None),
);

v.insert((span, p_id, p_name, res));
Expand All @@ -1850,8 +1834,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
captured_lifetimes.parent_def_id,
p_id,
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
ExpnId::root(),
span.with_parent(None),
);

v.insert((span, p_id, ParamName::Fresh, res));
Expand All @@ -1873,8 +1855,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
captured_lifetimes.parent_def_id,
p_id,
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
ExpnId::root(),
span.with_parent(None),
);
captured_lifetimes
.captures
Expand Down
56 changes: 5 additions & 51 deletions compiler/rustc_hir/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ use crate::def_path_hash_map::DefPathHashMap;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_index::vec::IndexVec;
use rustc_span::hygiene::ExpnId;
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::Span;

use std::fmt::{self, Write};
use std::hash::Hash;
Expand Down Expand Up @@ -101,11 +99,6 @@ pub struct Definitions {
table: DefPathTable,
next_disambiguator: FxHashMap<(LocalDefId, DefPathData), u32>,

/// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
expansions_that_defined: FxHashMap<LocalDefId, ExpnId>,

def_id_to_span: IndexVec<LocalDefId, Span>,

/// The [StableCrateId] of the local crate.
stable_crate_id: StableCrateId,
}
Expand Down Expand Up @@ -323,7 +316,7 @@ impl Definitions {
}

/// Adds a root definition (no parent) and a few other reserved definitions.
pub fn new(stable_crate_id: StableCrateId, crate_span: Span) -> Definitions {
pub fn new(stable_crate_id: StableCrateId) -> Definitions {
let key = DefKey {
parent: None,
disambiguated_data: DisambiguatedDefPathData {
Expand All @@ -340,30 +333,12 @@ impl Definitions {
let root = LocalDefId { local_def_index: table.allocate(key, def_path_hash) };
assert_eq!(root.local_def_index, CRATE_DEF_INDEX);

let mut def_id_to_span = IndexVec::new();
// A relative span's parent must be an absolute span.
debug_assert_eq!(crate_span.data_untracked().parent, None);
let _root = def_id_to_span.push(crate_span);
debug_assert_eq!(_root, root);

Definitions {
table,
next_disambiguator: Default::default(),
expansions_that_defined: Default::default(),
def_id_to_span,
stable_crate_id,
}
Definitions { table, next_disambiguator: Default::default(), stable_crate_id }
}

/// Adds a definition with a parent definition.
pub fn create_def(
&mut self,
parent: LocalDefId,
data: DefPathData,
expn_id: ExpnId,
span: Span,
) -> LocalDefId {
debug!("create_def(parent={:?}, data={:?}, expn_id={:?})", parent, data, expn_id);
pub fn create_def(&mut self, parent: LocalDefId, data: DefPathData) -> LocalDefId {
debug!("create_def(parent={:?}, data={:?})", parent, data);

// The root node must be created with `create_root_def()`.
assert!(data != DefPathData::CrateRoot);
Expand All @@ -386,28 +361,7 @@ impl Definitions {
debug!("create_def: after disambiguation, key = {:?}", key);

// Create the definition.
let def_id = LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) };

if expn_id != ExpnId::root() {
self.expansions_that_defined.insert(def_id, expn_id);
}

// A relative span's parent must be an absolute span.
debug_assert_eq!(span.data_untracked().parent, None);
let _id = self.def_id_to_span.push(span);
debug_assert_eq!(_id, def_id);

def_id
}

pub fn expansion_that_defined(&self, id: LocalDefId) -> ExpnId {
self.expansions_that_defined.get(&id).copied().unwrap_or_else(ExpnId::root)
}

/// Retrieves the span of the given `DefId` if `DefId` is in the local crate.
#[inline]
pub fn def_span(&self, def_id: LocalDefId) -> Span {
self.def_id_to_span[def_id]
LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) }
}

pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,8 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {

let upstream_crates = upstream_crates(tcx);

let resolutions = tcx.resolutions(());

// We hash the final, remapped names of all local source files so we
// don't have to include the path prefix remapping commandline args.
// If we included the full mapping in the SVH, we could only have
Expand Down Expand Up @@ -1107,7 +1109,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
.filter_map(|(def_id, info)| {
let _ = info.as_owner()?;
let def_path_hash = definitions.def_path_hash(def_id);
let span = definitions.def_span(def_id);
let span = resolutions.source_span[def_id];
debug_assert_eq!(span.parent(), None);
Some((def_path_hash, span))
})
Expand All @@ -1118,7 +1120,6 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher);
tcx.sess.local_stable_crate_id().hash_stable(&mut hcx, &mut stable_hasher);
// Hash visibility information since it does not appear in HIR.
let resolutions = tcx.resolutions(());
resolutions.visibilities.hash_stable(&mut hcx, &mut stable_hasher);
resolutions.has_pub_restricted.hash_stable(&mut hcx, &mut stable_hasher);

Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_middle/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::*;
use rustc_query_system::ich::StableHashingContext;
use rustc_span::DUMMY_SP;
use rustc_span::{ExpnId, DUMMY_SP};

/// Top-level HIR node for current owner. This only contains the node for which
/// `HirId::local_id == 0`, and excludes bodies.
Expand Down Expand Up @@ -117,7 +117,8 @@ pub fn provide(providers: &mut Providers) {
};
providers.hir_attrs =
|tcx, id| tcx.hir_crate(()).owners[id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs);
providers.source_span = |tcx, def_id| tcx.definitions_untracked().def_span(def_id);
providers.source_span =
|tcx, def_id| tcx.resolutions(()).source_span.get(def_id).copied().unwrap_or(DUMMY_SP);
providers.def_span = |tcx, def_id| {
let def_id = def_id.expect_local();
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
Expand Down Expand Up @@ -147,7 +148,7 @@ pub fn provide(providers: &mut Providers) {
providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
providers.expn_that_defined = |tcx, id| {
let id = id.expect_local();
tcx.definitions_untracked().expansion_that_defined(id)
tcx.resolutions(()).expn_that_defined.get(&id).copied().unwrap_or(ExpnId::root())
};
providers.in_scope_traits_map =
|tcx, id| tcx.hir_crate(()).owners[id].as_owner().map(|owner_info| &owner_info.trait_map);
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ rustc_queries! {
/// This span is meant for dep-tracking rather than diagnostics. It should not be used outside
/// of rustc_middle::hir::source_map.
query source_span(key: LocalDefId) -> Span {
eval_always
desc { "get the source span" }
}

Expand Down Expand Up @@ -272,8 +271,6 @@ rustc_queries! {
}

query expn_that_defined(key: DefId) -> rustc_span::ExpnId {
// This query reads from untracked data in definitions.
eval_always
desc { |tcx| "expansion that defined `{}`", tcx.def_path_str(key) }
separate_provide_extern
}
Expand Down
Loading

0 comments on commit 34e4d72

Please sign in to comment.