Skip to content

Commit c6acd0f

Browse files
committed
Collect spans during HIR indexing.
1 parent 76a36df commit c6acd0f

File tree

2 files changed

+19
-42
lines changed

2 files changed

+19
-42
lines changed

src/librustc_middle/hir/map/collector.rs

Lines changed: 17 additions & 7 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, DUMMY_SP};
19+
use rustc_span::{Span, Symbol};
2020

2121
use std::iter::repeat;
2222

@@ -133,10 +133,15 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
133133
hcx,
134134
hir_body_nodes,
135135
map: (0..definitions.def_index_count())
136-
.map(|_| HirOwnerData { signature: None, with_bodies: None })
136+
.map(|_| HirOwnerData {
137+
spans: IndexVec::new(),
138+
signature: None,
139+
with_bodies: None,
140+
})
137141
.collect(),
138142
};
139143
collector.insert_entry(
144+
krate.item.span,
140145
hir::CRATE_HIR_ID,
141146
Entry { parent: hir::CRATE_HIR_ID, node: Node::Crate(&krate.item) },
142147
hash,
@@ -196,7 +201,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
196201
(self.map, svh)
197202
}
198203

199-
fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>, hash: Fingerprint) {
204+
fn insert_entry(&mut self, span: Span, id: HirId, entry: Entry<'hir>, hash: Fingerprint) {
200205
let i = id.local_id.as_u32() as usize;
201206

202207
let arena = self.arena;
@@ -213,6 +218,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
213218

214219
let nodes = data.with_bodies.as_mut().unwrap();
215220

221+
insert_vec_map(&mut data.spans, id.local_id, span);
216222
if i == 0 {
217223
// Overwrite the dummy hash with the real HIR owner hash.
218224
nodes.hash = hash;
@@ -262,7 +268,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
262268
}
263269
}
264270

265-
self.insert_entry(hir_id, entry, hash);
271+
self.insert_entry(span, hir_id, entry, hash);
266272
}
267273

268274
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_node_id: HirId, f: F) {
@@ -323,7 +329,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
323329

324330
fn visit_param(&mut self, param: &'hir Param<'hir>) {
325331
let node = Node::Param(param);
326-
self.insert(param.pat.span, param.hir_id, node);
332+
self.insert(param.span, param.hir_id, node);
327333
self.with_parent(param.hir_id, |this| {
328334
intravisit::walk_param(this, param);
329335
});
@@ -411,7 +417,11 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
411417
}
412418

413419
fn visit_anon_const(&mut self, constant: &'hir AnonConst) {
414-
self.insert(DUMMY_SP, constant.hir_id, Node::AnonConst(constant));
420+
self.insert(
421+
self.krate.body(constant.body).value.span,
422+
constant.hir_id,
423+
Node::AnonConst(constant),
424+
);
415425

416426
self.with_parent(constant.hir_id, |this| {
417427
intravisit::walk_anon_const(this, constant);
@@ -436,7 +446,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
436446

437447
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'hir PathSegment<'hir>) {
438448
if let Some(hir_id) = path_segment.hir_id {
439-
self.insert(path_span, hir_id, Node::PathSegment(path_segment));
449+
self.insert(path_segment.ident.span, hir_id, Node::PathSegment(path_segment));
440450
}
441451
intravisit::walk_path_segment(self, path_span, path_segment);
442452
}

src/librustc_middle/hir/map/mod.rs

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +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>>,
9091
pub(super) signature: Option<&'hir Owner<'hir>>,
9192
pub(super) with_bodies: Option<&'hir mut OwnerNodes<'hir>>,
9293
}
@@ -827,41 +828,7 @@ impl<'hir> Map<'hir> {
827828
}
828829

829830
pub fn span(&self, hir_id: HirId) -> Span {
830-
match self.find_entry(hir_id).map(|entry| entry.node) {
831-
Some(Node::Param(param)) => param.span,
832-
Some(Node::Item(item)) => item.span,
833-
Some(Node::ForeignItem(foreign_item)) => foreign_item.span,
834-
Some(Node::TraitItem(trait_method)) => trait_method.span,
835-
Some(Node::ImplItem(impl_item)) => impl_item.span,
836-
Some(Node::Variant(variant)) => variant.span,
837-
Some(Node::Field(field)) => field.span,
838-
Some(Node::AnonConst(constant)) => self.body(constant.body).value.span,
839-
Some(Node::Expr(expr)) => expr.span,
840-
Some(Node::Stmt(stmt)) => stmt.span,
841-
Some(Node::PathSegment(seg)) => seg.ident.span,
842-
Some(Node::Ty(ty)) => ty.span,
843-
Some(Node::TraitRef(tr)) => tr.path.span,
844-
Some(Node::Binding(pat)) => pat.span,
845-
Some(Node::Pat(pat)) => pat.span,
846-
Some(Node::Arm(arm)) => arm.span,
847-
Some(Node::Block(block)) => block.span,
848-
Some(Node::Ctor(..)) => match self.find(self.get_parent_node(hir_id)) {
849-
Some(Node::Item(item)) => item.span,
850-
Some(Node::Variant(variant)) => variant.span,
851-
_ => unreachable!(),
852-
},
853-
Some(Node::Lifetime(lifetime)) => lifetime.span,
854-
Some(Node::GenericParam(param)) => param.span,
855-
Some(Node::Visibility(&Spanned {
856-
node: VisibilityKind::Restricted { ref path, .. },
857-
..
858-
})) => path.span,
859-
Some(Node::Visibility(v)) => bug!("unexpected Visibility {:?}", v),
860-
Some(Node::Local(local)) => local.span,
861-
Some(Node::MacroDef(macro_def)) => macro_def.span,
862-
Some(Node::Crate(item)) => item.span,
863-
None => bug!("hir::map::Map::span: id not in map: {:?}", hir_id),
864-
}
831+
self.tcx.index_hir(LOCAL_CRATE).map[hir_id.owner].spans[hir_id.local_id].unwrap()
865832
}
866833

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

0 commit comments

Comments
 (0)