From 06f7ca307d7ffdbdafe05eb76cc850eaf716916d Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 4 Apr 2021 16:04:38 +0200 Subject: [PATCH] Keep def_spans collected by resolution. --- compiler/rustc_hir/src/definitions.rs | 20 +++++++++++++++++++- compiler/rustc_resolve/src/lib.rs | 13 +++---------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_hir/src/definitions.rs b/compiler/rustc_hir/src/definitions.rs index 30a367052cb..ada012b6697 100644 --- a/compiler/rustc_hir/src/definitions.rs +++ b/compiler/rustc_hir/src/definitions.rs @@ -14,6 +14,7 @@ use rustc_data_structures::unhash::UnhashMap; 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; @@ -107,6 +108,8 @@ pub struct Definitions { /// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`. expansions_that_defined: FxHashMap, + + def_id_to_span: IndexVec, } /// A unique identifier that we can use to lookup a definition @@ -324,7 +327,7 @@ impl Definitions { } /// Adds a root definition (no parent) and a few other reserved definitions. - pub fn new(stable_crate_id: StableCrateId) -> Definitions { + pub fn new(stable_crate_id: StableCrateId, crate_span: Span) -> Definitions { let key = DefKey { parent: None, disambiguated_data: DisambiguatedDefPathData { @@ -341,11 +344,16 @@ 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(); + let _root = def_id_to_span.push(crate_span); + debug_assert_eq!(_root, root); + Definitions { table, def_id_to_hir_id: Default::default(), hir_id_to_def_id: Default::default(), expansions_that_defined: Default::default(), + def_id_to_span, } } @@ -361,6 +369,7 @@ impl Definitions { data: DefPathData, expn_id: ExpnId, mut next_disambiguator: impl FnMut(LocalDefId, DefPathData) -> u32, + span: Span, ) -> LocalDefId { debug!("create_def(parent={:?}, data={:?}, expn_id={:?})", parent, data, expn_id); @@ -385,6 +394,9 @@ impl Definitions { self.expansions_that_defined.insert(def_id, expn_id); } + let _id = self.def_id_to_span.push(span); + debug_assert_eq!(_id, def_id); + def_id } @@ -412,6 +424,12 @@ impl Definitions { 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] + } + pub fn iter_local_def_id(&self) -> impl Iterator + '_ { self.def_id_to_hir_id.iter_enumerated().map(|(k, _)| k) } diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 6d2961db9e3..ead3ca9fcde 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1012,8 +1012,6 @@ pub struct Resolver<'a> { next_node_id: NodeId, - def_id_to_span: IndexVec, - node_id_to_def_id: FxHashMap, def_id_to_node_id: IndexVec, @@ -1197,9 +1195,7 @@ impl ResolverAstLowering for Resolver<'_> { disambiguator }; - let def_id = self.definitions.create_def(parent, data, expn_id, next_disambiguator); - - assert_eq!(self.def_id_to_span.push(span), def_id); + let def_id = self.definitions.create_def(parent, data, expn_id, next_disambiguator, span); // 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 @@ -1269,14 +1265,12 @@ impl<'a> Resolver<'a> { let mut module_map = FxHashMap::default(); module_map.insert(root_local_def_id, graph_root); - let definitions = Definitions::new(session.local_stable_crate_id()); + let definitions = Definitions::new(session.local_stable_crate_id(), krate.span); let root = definitions.get_root_def(); let mut visibilities = FxHashMap::default(); visibilities.insert(root_local_def_id, ty::Visibility::Public); - let mut def_id_to_span = IndexVec::default(); - assert_eq!(def_id_to_span.push(rustc_span::DUMMY_SP), root); let mut def_id_to_node_id = IndexVec::default(); assert_eq!(def_id_to_node_id.push(CRATE_NODE_ID), root); let mut node_id_to_def_id = FxHashMap::default(); @@ -1393,7 +1387,6 @@ impl<'a> Resolver<'a> { .collect(), lint_buffer: LintBuffer::default(), next_node_id: NodeId::from_u32(1), - def_id_to_span, node_id_to_def_id, def_id_to_node_id, placeholder_field_indices: Default::default(), @@ -3360,7 +3353,7 @@ impl<'a> Resolver<'a> { /// Retrieves the span of the given `DefId` if `DefId` is in the local crate. #[inline] pub fn opt_span(&self, def_id: DefId) -> Option { - if let Some(def_id) = def_id.as_local() { Some(self.def_id_to_span[def_id]) } else { None } + def_id.as_local().map(|def_id| self.definitions.def_span(def_id)) } /// Checks if an expression refers to a function marked with