Skip to content

Commit 6a1537c

Browse files
committed
Collect spans directly into an IndexVec.
1 parent 5b5bc8d commit 6a1537c

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

src/librustc_ast_lowering/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
203203
// Include parens in span, but only if it is a super-span.
204204
if e.span.contains(ex.span) {
205205
ex.span = e.span;
206-
self.spans.insert(ex.hir_id, e.span);
206+
self.spans[ex.hir_id] = e.span;
207207
}
208208
// Merge attributes into the inner expression.
209209
let mut attrs = e.attrs.clone();
@@ -1365,7 +1365,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13651365
let mut head = self.lower_expr_mut(head);
13661366
let desugared_span = self.mark_span_with_reason(DesugaringKind::ForLoop, head.span, None);
13671367
head.span = desugared_span;
1368-
self.spans.insert(head.hir_id, desugared_span);
1368+
self.spans[head.hir_id] = desugared_span;
13691369

13701370
let iter = Ident::with_dummy_span(sym::iter);
13711371

src/librustc_ast_lowering/lib.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ use rustc_hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
5353
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX};
5454
use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
5555
use rustc_hir::intravisit;
56-
use rustc_hir::{ConstArg, GenericArg, ParamName};
57-
use rustc_index::vec::IndexVec;
56+
use rustc_hir::{ConstArg, GenericArg, HirIdVec, ParamName};
57+
use rustc_index::vec::{Idx, IndexVec};
5858
use rustc_session::config::nightly_options;
5959
use rustc_session::lint::{builtin::BARE_TRAIT_OBJECTS, BuiltinLintDiagnostics, LintBuffer};
6060
use rustc_session::parse::ParseSess;
@@ -115,7 +115,7 @@ struct LoweringContext<'a, 'hir: 'a> {
115115
modules: BTreeMap<hir::HirId, hir::ModuleItems>,
116116

117117
/// Collected spans from the AST.
118-
spans: BTreeMap<hir::HirId, Span>,
118+
spans: HirIdVec<Span>,
119119

120120
generator_kind: Option<hir::GeneratorKind>,
121121

@@ -307,7 +307,7 @@ pub fn lower_crate<'a, 'hir>(
307307
bodies: BTreeMap::new(),
308308
trait_impls: BTreeMap::new(),
309309
modules: BTreeMap::new(),
310-
spans: BTreeMap::new(),
310+
spans: Default::default(),
311311
exported_macros: Vec::new(),
312312
non_exported_macro_attrs: Vec::new(),
313313
catch_scopes: Vec::new(),
@@ -570,6 +570,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
570570

571571
self.resolver.definitions().init_node_id_to_hir_id_mapping(self.node_id_to_hir_id);
572572

573+
//FIXME(cjgillot) Ideally, each LocalDefId would be a HIR owner.
574+
// In the mean time, allocate the missing empty vectors.
575+
self.spans.push_owner(Idx::new(self.resolver.definitions().def_index_count() - 1));
576+
573577
hir::Crate {
574578
item: hir::CrateItem { module, attrs, span: c.span },
575579
exported_macros: self.arena.alloc_from_iter(self.exported_macros),
@@ -619,22 +623,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
619623
}
620624

621625
let hir_id = if let Some(existing_hir_id) = self.node_id_to_hir_id[ast_node_id] {
626+
if span != DUMMY_SP {
627+
debug_assert!(
628+
self.spans[existing_hir_id] == DUMMY_SP || self.spans[existing_hir_id] == span
629+
);
630+
self.spans[existing_hir_id] = span;
631+
}
622632
existing_hir_id
623633
} else {
624634
// Generate a new `HirId`.
625635
let hir_id = alloc_hir_id(self);
626636
self.node_id_to_hir_id[ast_node_id] = Some(hir_id);
637+
self.spans.push(hir_id, span);
627638

628639
hir_id
629640
};
630641

631-
let stored_span = self.spans.entry(hir_id).or_insert(span);
632-
if *stored_span == DUMMY_SP {
633-
*stored_span = span;
634-
} else if span != DUMMY_SP {
635-
assert_eq!(*stored_span, span);
636-
}
637-
638642
hir_id
639643
}
640644

src/librustc_hir/hir.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::def::{DefKind, Namespace, Res};
22
use crate::def_id::DefId;
33
crate use crate::hir_id::HirId;
4+
use crate::hir_id::HirIdVec;
45
use crate::itemlikevisit;
56

67
use rustc_ast::ast::{self, CrateSugar, LlvmAsmDialect};
@@ -643,7 +644,7 @@ pub struct Crate<'hir> {
643644
pub trait_map: BTreeMap<HirId, Vec<TraitCandidate>>,
644645

645646
/// Collected spans from the AST.
646-
pub spans: BTreeMap<HirId, Span>,
647+
pub spans: HirIdVec<Span>,
647648
}
648649

649650
impl Crate<'hir> {

0 commit comments

Comments
 (0)