Skip to content

Commit 06f7ca3

Browse files
committed
Keep def_spans collected by resolution.
1 parent 7bf0736 commit 06f7ca3

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

compiler/rustc_hir/src/definitions.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_data_structures::unhash::UnhashMap;
1414
use rustc_index::vec::IndexVec;
1515
use rustc_span::hygiene::ExpnId;
1616
use rustc_span::symbol::{kw, sym, Symbol};
17+
use rustc_span::Span;
1718

1819
use std::fmt::{self, Write};
1920
use std::hash::Hash;
@@ -107,6 +108,8 @@ pub struct Definitions {
107108

108109
/// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
109110
expansions_that_defined: FxHashMap<LocalDefId, ExpnId>,
111+
112+
def_id_to_span: IndexVec<LocalDefId, Span>,
110113
}
111114

112115
/// A unique identifier that we can use to lookup a definition
@@ -324,7 +327,7 @@ impl Definitions {
324327
}
325328

326329
/// Adds a root definition (no parent) and a few other reserved definitions.
327-
pub fn new(stable_crate_id: StableCrateId) -> Definitions {
330+
pub fn new(stable_crate_id: StableCrateId, crate_span: Span) -> Definitions {
328331
let key = DefKey {
329332
parent: None,
330333
disambiguated_data: DisambiguatedDefPathData {
@@ -341,11 +344,16 @@ impl Definitions {
341344
let root = LocalDefId { local_def_index: table.allocate(key, def_path_hash) };
342345
assert_eq!(root.local_def_index, CRATE_DEF_INDEX);
343346

347+
let mut def_id_to_span = IndexVec::new();
348+
let _root = def_id_to_span.push(crate_span);
349+
debug_assert_eq!(_root, root);
350+
344351
Definitions {
345352
table,
346353
def_id_to_hir_id: Default::default(),
347354
hir_id_to_def_id: Default::default(),
348355
expansions_that_defined: Default::default(),
356+
def_id_to_span,
349357
}
350358
}
351359

@@ -361,6 +369,7 @@ impl Definitions {
361369
data: DefPathData,
362370
expn_id: ExpnId,
363371
mut next_disambiguator: impl FnMut(LocalDefId, DefPathData) -> u32,
372+
span: Span,
364373
) -> LocalDefId {
365374
debug!("create_def(parent={:?}, data={:?}, expn_id={:?})", parent, data, expn_id);
366375

@@ -385,6 +394,9 @@ impl Definitions {
385394
self.expansions_that_defined.insert(def_id, expn_id);
386395
}
387396

397+
let _id = self.def_id_to_span.push(span);
398+
debug_assert_eq!(_id, def_id);
399+
388400
def_id
389401
}
390402

@@ -412,6 +424,12 @@ impl Definitions {
412424
self.expansions_that_defined.get(&id).copied().unwrap_or_else(ExpnId::root)
413425
}
414426

427+
/// Retrieves the span of the given `DefId` if `DefId` is in the local crate.
428+
#[inline]
429+
pub fn def_span(&self, def_id: LocalDefId) -> Span {
430+
self.def_id_to_span[def_id]
431+
}
432+
415433
pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {
416434
self.def_id_to_hir_id.iter_enumerated().map(|(k, _)| k)
417435
}

compiler/rustc_resolve/src/lib.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,8 +1012,6 @@ pub struct Resolver<'a> {
10121012

10131013
next_node_id: NodeId,
10141014

1015-
def_id_to_span: IndexVec<LocalDefId, Span>,
1016-
10171015
node_id_to_def_id: FxHashMap<ast::NodeId, LocalDefId>,
10181016
def_id_to_node_id: IndexVec<LocalDefId, ast::NodeId>,
10191017

@@ -1197,9 +1195,7 @@ impl ResolverAstLowering for Resolver<'_> {
11971195
disambiguator
11981196
};
11991197

1200-
let def_id = self.definitions.create_def(parent, data, expn_id, next_disambiguator);
1201-
1202-
assert_eq!(self.def_id_to_span.push(span), def_id);
1198+
let def_id = self.definitions.create_def(parent, data, expn_id, next_disambiguator, span);
12031199

12041200
// Some things for which we allocate `LocalDefId`s don't correspond to
12051201
// anything in the AST, so they don't have a `NodeId`. For these cases
@@ -1269,14 +1265,12 @@ impl<'a> Resolver<'a> {
12691265
let mut module_map = FxHashMap::default();
12701266
module_map.insert(root_local_def_id, graph_root);
12711267

1272-
let definitions = Definitions::new(session.local_stable_crate_id());
1268+
let definitions = Definitions::new(session.local_stable_crate_id(), krate.span);
12731269
let root = definitions.get_root_def();
12741270

12751271
let mut visibilities = FxHashMap::default();
12761272
visibilities.insert(root_local_def_id, ty::Visibility::Public);
12771273

1278-
let mut def_id_to_span = IndexVec::default();
1279-
assert_eq!(def_id_to_span.push(rustc_span::DUMMY_SP), root);
12801274
let mut def_id_to_node_id = IndexVec::default();
12811275
assert_eq!(def_id_to_node_id.push(CRATE_NODE_ID), root);
12821276
let mut node_id_to_def_id = FxHashMap::default();
@@ -1393,7 +1387,6 @@ impl<'a> Resolver<'a> {
13931387
.collect(),
13941388
lint_buffer: LintBuffer::default(),
13951389
next_node_id: NodeId::from_u32(1),
1396-
def_id_to_span,
13971390
node_id_to_def_id,
13981391
def_id_to_node_id,
13991392
placeholder_field_indices: Default::default(),
@@ -3360,7 +3353,7 @@ impl<'a> Resolver<'a> {
33603353
/// Retrieves the span of the given `DefId` if `DefId` is in the local crate.
33613354
#[inline]
33623355
pub fn opt_span(&self, def_id: DefId) -> Option<Span> {
3363-
if let Some(def_id) = def_id.as_local() { Some(self.def_id_to_span[def_id]) } else { None }
3356+
def_id.as_local().map(|def_id| self.definitions.def_span(def_id))
33643357
}
33653358

33663359
/// Checks if an expression refers to a function marked with

0 commit comments

Comments
 (0)