@@ -14,6 +14,7 @@ use rustc_data_structures::unhash::UnhashMap;
1414use rustc_index:: vec:: IndexVec ;
1515use rustc_span:: hygiene:: ExpnId ;
1616use rustc_span:: symbol:: { kw, sym, Symbol } ;
17+ use rustc_span:: Span ;
1718
1819use std:: fmt:: { self , Write } ;
1920use 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 }
0 commit comments