Skip to content

Commit 3640fb3

Browse files
committed
Directly use span map for indexing.
1 parent 6a1537c commit 3640fb3

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

src/librustc_middle/hir/map/collector.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_hir::*;
1616
use rustc_index::vec::{Idx, IndexVec};
1717
use rustc_session::{CrateDisambiguator, Session};
1818
use rustc_span::source_map::SourceMap;
19-
use rustc_span::{Span, Symbol};
19+
use rustc_span::{Span, Symbol, DUMMY_SP};
2020

2121
use std::iter::repeat;
2222

@@ -134,13 +134,14 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
134134
hcx,
135135
hir_body_nodes,
136136
map: (0..definitions.def_index_count())
137-
.map(|_| HirOwnerData {
138-
spans: IndexVec::new(),
137+
.map(|id| HirOwnerData {
138+
spans: krate.spans.get_owner(Idx::new(id)),
139139
signature: None,
140140
with_bodies: None,
141141
})
142142
.collect(),
143143
};
144+
144145
collector.insert_entry(
145146
krate.item.span,
146147
hir::CRATE_HIR_ID,
@@ -219,7 +220,12 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
219220

220221
let nodes = data.with_bodies.as_mut().unwrap();
221222

222-
insert_vec_map(&mut data.spans, id.local_id, span);
223+
// Verify the consistency of the map from HIR lowering.
224+
// Use DUMMY_SP when the span has been removed from the HIR.
225+
if span != DUMMY_SP {
226+
debug_assert!(id.local_id.index() < data.spans.len());
227+
debug_assert_eq!(data.spans[id.local_id], span);
228+
}
223229
if i == 0 {
224230
// Overwrite the dummy hash with the real HIR owner hash.
225231
nodes.hash = hash;

src/librustc_middle/hir/map/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn is_body_owner<'hir>(node: Node<'hir>, hir_id: HirId) -> bool {
8787
}
8888

8989
pub(super) struct HirOwnerData<'hir> {
90-
pub(super) spans: IndexVec<ItemLocalId, Option<Span>>,
90+
pub(super) spans: &'hir IndexVec<ItemLocalId, Span>,
9191
pub(super) signature: Option<&'hir Owner<'hir>>,
9292
pub(super) with_bodies: Option<&'hir mut OwnerNodes<'hir>>,
9393
}
@@ -828,7 +828,7 @@ impl<'hir> Map<'hir> {
828828
}
829829

830830
pub fn span(&self, hir_id: HirId) -> Span {
831-
self.tcx.hir_owner_spans(hir_id.owner)[hir_id.local_id].unwrap()
831+
self.tcx.hir_owner_spans(hir_id.owner)[hir_id.local_id]
832832
}
833833

834834
pub fn span_if_local(&self, id: DefId) -> Option<Span> {

src/librustc_middle/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ rustc_queries! {
9393
//
9494
// This can be conveniently accessed by methods on `tcx.hir()`.
9595
// Avoid calling this query directly.
96-
query hir_owner_spans(key: LocalDefId) -> &'tcx IndexVec<ItemLocalId, Option<Span>> {
96+
query hir_owner_spans(key: LocalDefId) -> &'tcx IndexVec<ItemLocalId, Span> {
9797
eval_always
9898
desc { |tcx| "HIR owner spans in `{}`", tcx.def_path_str(key.to_def_id()) }
9999
}

0 commit comments

Comments
 (0)