Skip to content

Commit 34e4d72

Browse files
committed
Separate source_span and expn_that_defined from Definitions.
1 parent b676edd commit 34e4d72

File tree

13 files changed

+135
-135
lines changed

13 files changed

+135
-135
lines changed

compiler/rustc_ast_lowering/src/asm.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_hir as hir;
1111
use rustc_hir::def::{DefKind, Res};
1212
use rustc_hir::definitions::DefPathData;
1313
use rustc_session::parse::feature_err;
14-
use rustc_span::{sym, ExpnId, Span};
14+
use rustc_span::{sym, Span};
1515
use rustc_target::asm;
1616
use std::collections::hash_map::Entry;
1717
use std::fmt::Write;
@@ -243,13 +243,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
243243
// Wrap the expression in an AnonConst.
244244
let parent_def_id = self.current_hir_id_owner;
245245
let node_id = self.next_node_id();
246-
self.create_def(
247-
parent_def_id,
248-
node_id,
249-
DefPathData::AnonConst,
250-
ExpnId::root(),
251-
*op_sp,
252-
);
246+
self.create_def(parent_def_id, node_id, DefPathData::AnonConst);
253247
let anon_const = AnonConst { id: node_id, value: P(expr) };
254248
hir::InlineAsmOperand::SymFn {
255249
anon_const: self.lower_anon_const(&anon_const),

compiler/rustc_ast_lowering/src/expr.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc_errors::struct_span_err;
1111
use rustc_hir as hir;
1212
use rustc_hir::def::Res;
1313
use rustc_hir::definitions::DefPathData;
14-
use rustc_span::hygiene::ExpnId;
1514
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
1615
use rustc_span::symbol::{sym, Ident};
1716
use rustc_span::DUMMY_SP;
@@ -358,13 +357,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
358357
let node_id = self.next_node_id();
359358

360359
// Add a definition for the in-band const def.
361-
self.create_def(
362-
parent_def_id,
363-
node_id,
364-
DefPathData::AnonConst,
365-
ExpnId::root(),
366-
arg.span,
367-
);
360+
self.create_def(parent_def_id, node_id, DefPathData::AnonConst);
368361

369362
let anon_const = AnonConst { id: node_id, value: arg };
370363
generic_args.push(AngleBracketedArg::Arg(GenericArg::Const(anon_const)));

compiler/rustc_ast_lowering/src/lib.rs

+14-34
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use rustc_query_system::ich::StableHashingContext;
6161
use rustc_session::cstore::CrateStoreDyn;
6262
use rustc_session::parse::feature_err;
6363
use rustc_session::Session;
64-
use rustc_span::hygiene::{ExpnId, MacroKind};
64+
use rustc_span::hygiene::MacroKind;
6565
use rustc_span::source_map::DesugaringKind;
6666
use rustc_span::symbol::{kw, sym, Ident, Symbol};
6767
use rustc_span::{Span, DUMMY_SP};
@@ -391,6 +391,7 @@ fn compute_hir_hash(
391391
sess: &Session,
392392
definitions: &Definitions,
393393
cstore: &CrateStoreDyn,
394+
resolver: &ResolverOutputs,
394395
owners: &IndexVec<LocalDefId, hir::MaybeOwner<&hir::OwnerInfo<'_>>>,
395396
) -> Fingerprint {
396397
let mut hir_body_nodes: Vec<_> = owners
@@ -404,7 +405,7 @@ fn compute_hir_hash(
404405
hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
405406

406407
let mut stable_hasher = StableHasher::new();
407-
let mut hcx = StableHashingContext::new(sess, definitions, cstore);
408+
let mut hcx = StableHashingContext::new(sess, definitions, cstore, &resolver.source_span);
408409
hir_body_nodes.hash_stable(&mut hcx, &mut stable_hasher);
409410
stable_hasher.finish()
410411
}
@@ -437,7 +438,7 @@ pub fn lower_crate<'hir>(
437438
.lower_node(def_id);
438439
}
439440

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

460461
impl<'a, 'hir> LoweringContext<'a, 'hir> {
461462
fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {
462-
StableHashingContext::new(self.sess, self.definitions, self.cstore)
463+
StableHashingContext::new(
464+
self.sess,
465+
self.definitions,
466+
self.cstore,
467+
&self.resolver.source_span,
468+
)
463469
}
464470

465471
fn create_def(
466472
&mut self,
467473
parent: LocalDefId,
468474
node_id: ast::NodeId,
469475
data: DefPathData,
470-
expn_id: ExpnId,
471-
span: Span,
472476
) -> LocalDefId {
473477
assert!(
474478
self.opt_local_def_id(node_id).is_none(),
@@ -478,7 +482,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
478482
self.definitions.def_key(self.local_def_id(node_id)),
479483
);
480484

481-
let def_id = self.definitions.create_def(parent, data, expn_id, span);
485+
let def_id = self.definitions.create_def(parent, data);
482486

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

9991003
let parent_def_id = self.current_hir_id_owner;
10001004
let impl_trait_node_id = self.next_node_id();
1001-
self.create_def(
1002-
parent_def_id,
1003-
impl_trait_node_id,
1004-
DefPathData::ImplTrait,
1005-
ExpnId::root(),
1006-
constraint.span,
1007-
);
1005+
self.create_def(parent_def_id, impl_trait_node_id, DefPathData::ImplTrait);
10081006

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

11121110
// Add a definition for the in-band const def.
1113-
self.create_def(
1114-
parent_def_id,
1115-
node_id,
1116-
DefPathData::AnonConst,
1117-
ExpnId::root(),
1118-
ty.span,
1119-
);
1111+
self.create_def(parent_def_id, node_id, DefPathData::AnonConst);
11201112

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

16261618
// Add a definition for the in scope lifetime def.
1627-
self.create_def(
1628-
opaque_ty_def_id,
1629-
inner_node_id,
1630-
DefPathData::LifetimeNs(name),
1631-
ExpnId::root(),
1632-
span.with_parent(None),
1633-
);
1619+
self.create_def(opaque_ty_def_id, inner_node_id, DefPathData::LifetimeNs(name));
16341620

16351621
let (p_name, inner_res) = match outer_res {
16361622
// Input lifetime like `'a`:
@@ -1824,8 +1810,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18241810
captured_lifetimes.parent_def_id,
18251811
p_id,
18261812
DefPathData::LifetimeNs(p_name.ident().name),
1827-
ExpnId::root(),
1828-
span.with_parent(None),
18291813
);
18301814

18311815
v.insert((span, p_id, p_name, res));
@@ -1850,8 +1834,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18501834
captured_lifetimes.parent_def_id,
18511835
p_id,
18521836
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
1853-
ExpnId::root(),
1854-
span.with_parent(None),
18551837
);
18561838

18571839
v.insert((span, p_id, ParamName::Fresh, res));
@@ -1873,8 +1855,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18731855
captured_lifetimes.parent_def_id,
18741856
p_id,
18751857
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
1876-
ExpnId::root(),
1877-
span.with_parent(None),
18781858
);
18791859
captured_lifetimes
18801860
.captures

compiler/rustc_hir/src/definitions.rs

+5-51
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ use crate::def_path_hash_map::DefPathHashMap;
1111
use rustc_data_structures::fx::FxHashMap;
1212
use rustc_data_structures::stable_hasher::StableHasher;
1313
use rustc_index::vec::IndexVec;
14-
use rustc_span::hygiene::ExpnId;
1514
use rustc_span::symbol::{kw, sym, Symbol};
16-
use rustc_span::Span;
1715

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

104-
/// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
105-
expansions_that_defined: FxHashMap<LocalDefId, ExpnId>,
106-
107-
def_id_to_span: IndexVec<LocalDefId, Span>,
108-
109102
/// The [StableCrateId] of the local crate.
110103
stable_crate_id: StableCrateId,
111104
}
@@ -323,7 +316,7 @@ impl Definitions {
323316
}
324317

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

343-
let mut def_id_to_span = IndexVec::new();
344-
// A relative span's parent must be an absolute span.
345-
debug_assert_eq!(crate_span.data_untracked().parent, None);
346-
let _root = def_id_to_span.push(crate_span);
347-
debug_assert_eq!(_root, root);
348-
349-
Definitions {
350-
table,
351-
next_disambiguator: Default::default(),
352-
expansions_that_defined: Default::default(),
353-
def_id_to_span,
354-
stable_crate_id,
355-
}
336+
Definitions { table, next_disambiguator: Default::default(), stable_crate_id }
356337
}
357338

358339
/// Adds a definition with a parent definition.
359-
pub fn create_def(
360-
&mut self,
361-
parent: LocalDefId,
362-
data: DefPathData,
363-
expn_id: ExpnId,
364-
span: Span,
365-
) -> LocalDefId {
366-
debug!("create_def(parent={:?}, data={:?}, expn_id={:?})", parent, data, expn_id);
340+
pub fn create_def(&mut self, parent: LocalDefId, data: DefPathData) -> LocalDefId {
341+
debug!("create_def(parent={:?}, data={:?})", parent, data);
367342

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

388363
// Create the definition.
389-
let def_id = LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) };
390-
391-
if expn_id != ExpnId::root() {
392-
self.expansions_that_defined.insert(def_id, expn_id);
393-
}
394-
395-
// A relative span's parent must be an absolute span.
396-
debug_assert_eq!(span.data_untracked().parent, None);
397-
let _id = self.def_id_to_span.push(span);
398-
debug_assert_eq!(_id, def_id);
399-
400-
def_id
401-
}
402-
403-
pub fn expansion_that_defined(&self, id: LocalDefId) -> ExpnId {
404-
self.expansions_that_defined.get(&id).copied().unwrap_or_else(ExpnId::root)
405-
}
406-
407-
/// Retrieves the span of the given `DefId` if `DefId` is in the local crate.
408-
#[inline]
409-
pub fn def_span(&self, def_id: LocalDefId) -> Span {
410-
self.def_id_to_span[def_id]
364+
LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) }
411365
}
412366

413367
pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {

compiler/rustc_middle/src/hir/map/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,8 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
10781078

10791079
let upstream_crates = upstream_crates(tcx);
10801080

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

compiler/rustc_middle/src/hir/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1313
use rustc_hir::def_id::{DefId, LocalDefId};
1414
use rustc_hir::*;
1515
use rustc_query_system::ich::StableHashingContext;
16-
use rustc_span::DUMMY_SP;
16+
use rustc_span::{ExpnId, DUMMY_SP};
1717

1818
/// Top-level HIR node for current owner. This only contains the node for which
1919
/// `HirId::local_id == 0`, and excludes bodies.
@@ -117,7 +117,8 @@ pub fn provide(providers: &mut Providers) {
117117
};
118118
providers.hir_attrs =
119119
|tcx, id| tcx.hir_crate(()).owners[id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs);
120-
providers.source_span = |tcx, def_id| tcx.definitions_untracked().def_span(def_id);
120+
providers.source_span =
121+
|tcx, def_id| tcx.resolutions(()).source_span.get(def_id).copied().unwrap_or(DUMMY_SP);
121122
providers.def_span = |tcx, def_id| {
122123
let def_id = def_id.expect_local();
123124
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
@@ -147,7 +148,7 @@ pub fn provide(providers: &mut Providers) {
147148
providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
148149
providers.expn_that_defined = |tcx, id| {
149150
let id = id.expect_local();
150-
tcx.definitions_untracked().expansion_that_defined(id)
151+
tcx.resolutions(()).expn_that_defined.get(&id).copied().unwrap_or(ExpnId::root())
151152
};
152153
providers.in_scope_traits_map =
153154
|tcx, id| tcx.hir_crate(()).owners[id].as_owner().map(|owner_info| &owner_info.trait_map);

compiler/rustc_middle/src/query/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ rustc_queries! {
3131
/// This span is meant for dep-tracking rather than diagnostics. It should not be used outside
3232
/// of rustc_middle::hir::source_map.
3333
query source_span(key: LocalDefId) -> Span {
34-
eval_always
3534
desc { "get the source span" }
3635
}
3736

@@ -272,8 +271,6 @@ rustc_queries! {
272271
}
273272

274273
query expn_that_defined(key: DefId) -> rustc_span::ExpnId {
275-
// This query reads from untracked data in definitions.
276-
eval_always
277274
desc { |tcx| "expansion that defined `{}`", tcx.def_path_str(key) }
278275
separate_provide_extern
279276
}

0 commit comments

Comments
 (0)