@@ -14,6 +14,7 @@ use rustc_data_structures::unhash::UnhashMap;
14
14
use rustc_index:: vec:: IndexVec ;
15
15
use rustc_span:: hygiene:: ExpnId ;
16
16
use rustc_span:: symbol:: { kw, sym, Symbol } ;
17
+ use rustc_span:: Span ;
17
18
18
19
use std:: fmt:: { self , Write } ;
19
20
use std:: hash:: Hash ;
@@ -107,6 +108,8 @@ pub struct Definitions {
107
108
108
109
/// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
109
110
expansions_that_defined : FxHashMap < LocalDefId , ExpnId > ,
111
+
112
+ def_id_to_span : IndexVec < LocalDefId , Span > ,
110
113
}
111
114
112
115
/// A unique identifier that we can use to lookup a definition
@@ -324,7 +327,7 @@ impl Definitions {
324
327
}
325
328
326
329
/// 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 {
328
331
let key = DefKey {
329
332
parent : None ,
330
333
disambiguated_data : DisambiguatedDefPathData {
@@ -341,11 +344,16 @@ impl Definitions {
341
344
let root = LocalDefId { local_def_index : table. allocate ( key, def_path_hash) } ;
342
345
assert_eq ! ( root. local_def_index, CRATE_DEF_INDEX ) ;
343
346
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
+
344
351
Definitions {
345
352
table,
346
353
def_id_to_hir_id : Default :: default ( ) ,
347
354
hir_id_to_def_id : Default :: default ( ) ,
348
355
expansions_that_defined : Default :: default ( ) ,
356
+ def_id_to_span,
349
357
}
350
358
}
351
359
@@ -361,6 +369,7 @@ impl Definitions {
361
369
data : DefPathData ,
362
370
expn_id : ExpnId ,
363
371
mut next_disambiguator : impl FnMut ( LocalDefId , DefPathData ) -> u32 ,
372
+ span : Span ,
364
373
) -> LocalDefId {
365
374
debug ! ( "create_def(parent={:?}, data={:?}, expn_id={:?})" , parent, data, expn_id) ;
366
375
@@ -385,6 +394,9 @@ impl Definitions {
385
394
self . expansions_that_defined . insert ( def_id, expn_id) ;
386
395
}
387
396
397
+ let _id = self . def_id_to_span . push ( span) ;
398
+ debug_assert_eq ! ( _id, def_id) ;
399
+
388
400
def_id
389
401
}
390
402
@@ -412,6 +424,12 @@ impl Definitions {
412
424
self . expansions_that_defined . get ( & id) . copied ( ) . unwrap_or_else ( ExpnId :: root)
413
425
}
414
426
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
+
415
433
pub fn iter_local_def_id ( & self ) -> impl Iterator < Item = LocalDefId > + ' _ {
416
434
self . def_id_to_hir_id . iter_enumerated ( ) . map ( |( k, _) | k)
417
435
}
0 commit comments