Skip to content

Commit 0b793a3

Browse files
committed
Collect spans during lowering.
Collect spans directly into an IndexVec.
1 parent 6758a85 commit 0b793a3

File tree

7 files changed

+152
-106
lines changed

7 files changed

+152
-106
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_hir as hir;
1111
use rustc_hir::def::Res;
1212
use rustc_session::parse::feature_err;
1313
use rustc_span::hygiene::ForLoopLoc;
14-
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
14+
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned, DUMMY_SP};
1515
use rustc_span::symbol::{sym, Ident, Symbol};
1616
use rustc_target::asm;
1717
use std::collections::hash_map::Entry;
@@ -229,6 +229,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
229229
// Include parens in span, but only if it is a super-span.
230230
if e.span.contains(ex.span) {
231231
ex.span = e.span;
232+
self.spans[ex.hir_id] = e.span;
232233
}
233234
// Merge attributes into the inner expression.
234235
let mut attrs: Vec<_> = e.attrs.iter().map(|a| self.lower_attr(a)).collect();
@@ -246,7 +247,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
246247
};
247248

248249
hir::Expr {
249-
hir_id: self.lower_node_id(e.id),
250+
hir_id: self.lower_node_id(e.id, e.span),
250251
kind,
251252
span: e.span,
252253
attrs: e.attrs.iter().map(|a| self.lower_attr(a)).collect::<Vec<_>>().into(),
@@ -514,7 +515,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
514515
}
515516
});
516517
hir::Arm {
517-
hir_id: self.next_id(),
518+
hir_id: self.next_id(arm.span),
518519
attrs: self.lower_attrs(&arm.attrs),
519520
pat,
520521
guard,
@@ -548,7 +549,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
548549

549550
// Resume argument type. We let the compiler infer this to simplify the lowering. It is
550551
// fully constrained by `future::from_generator`.
551-
let input_ty = hir::Ty { hir_id: self.next_id(), kind: hir::TyKind::Infer, span };
552+
let input_ty = hir::Ty { hir_id: self.next_id(span), kind: hir::TyKind::Infer, span };
552553

553554
// The closure/generator `FnDecl` takes a single (resume) argument of type `input_ty`.
554555
let decl = self.arena.alloc(hir::FnDecl {
@@ -564,7 +565,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
564565
Ident::with_dummy_span(sym::_task_context),
565566
hir::BindingAnnotation::Mutable,
566567
);
567-
let param = hir::Param { attrs: &[], hir_id: self.next_id(), pat, ty_span: span, span };
568+
let param = hir::Param { attrs: &[], hir_id: self.next_id(span), pat, ty_span: span, span };
568569
let params = arena_vec![self; param];
569570

570571
let body_id = self.lower_body(move |this| {
@@ -586,7 +587,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
586587
Some(hir::Movability::Static),
587588
);
588589
let generator = hir::Expr {
589-
hir_id: self.lower_node_id(closure_node_id),
590+
hir_id: self.lower_node_id(closure_node_id, span),
590591
kind: generator_kind,
591592
span,
592593
attrs: ThinVec::new(),
@@ -683,7 +684,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
683684

684685
// `::std::task::Poll::Ready(result) => break result`
685686
let loop_node_id = self.resolver.next_node_id();
686-
let loop_hir_id = self.lower_node_id(loop_node_id);
687+
let loop_hir_id = self.lower_node_id(loop_node_id, span);
687688
let ready_arm = {
688689
let x_ident = Ident::with_dummy_span(sym::result);
689690
let (x_pat, x_pat_hid) = self.pat_ident(span, x_ident);
@@ -1008,7 +1009,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10081009
let field_pats = self.arena.alloc_from_iter(fields.iter().map(|f| {
10091010
let pat = self.destructure_assign(&f.expr, eq_sign_span, assignments);
10101011
hir::FieldPat {
1011-
hir_id: self.next_id(),
1012+
hir_id: self.next_id(f.span),
10121013
ident: f.ident,
10131014
pat,
10141015
is_shorthand: f.is_shorthand,
@@ -1149,7 +1150,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11491150
let target_id = match destination {
11501151
Some((id, _)) => {
11511152
if let Some(loop_id) = self.resolver.get_label_res(id) {
1152-
Ok(self.lower_node_id(loop_id))
1153+
Ok(self.lower_node_id(loop_id, DUMMY_SP))
11531154
} else {
11541155
Err(hir::LoopIdError::UnresolvedLabel)
11551156
}
@@ -1158,7 +1159,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11581159
.loop_scopes
11591160
.last()
11601161
.cloned()
1161-
.map(|id| Ok(self.lower_node_id(id)))
1162+
.map(|id| Ok(self.lower_node_id(id, DUMMY_SP)))
11621163
.unwrap_or(Err(hir::LoopIdError::OutsideLoopScope)),
11631164
};
11641165
hir::Destination { label: destination.map(|(_, label)| label), target_id }
@@ -1554,7 +1555,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15541555

15551556
fn lower_field(&mut self, f: &Field) -> hir::Field<'hir> {
15561557
hir::Field {
1557-
hir_id: self.next_id(),
1558+
hir_id: self.next_id(f.span),
15581559
ident: f.ident,
15591560
expr: self.lower_expr(&f.expr),
15601561
span: f.span,
@@ -1619,6 +1620,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16191620
None,
16201621
);
16211622
head.span = desugared_span;
1623+
self.spans[head.hir_id] = desugared_span;
16221624

16231625
let iter = Ident::with_dummy_span(sym::iter);
16241626

@@ -1705,7 +1707,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
17051707
// `[opt_ident]: loop { ... }`
17061708
let kind = hir::ExprKind::Loop(loop_block, opt_label, hir::LoopSource::ForLoop);
17071709
let loop_expr = self.arena.alloc(hir::Expr {
1708-
hir_id: self.lower_node_id(e.id),
1710+
hir_id: self.lower_node_id(e.id, e.span),
17091711
kind,
17101712
span: e.span,
17111713
attrs: ThinVec::new(),
@@ -1832,7 +1834,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
18321834
let thin_attrs = ThinVec::from(attrs);
18331835
let catch_scope = self.catch_scopes.last().copied();
18341836
let ret_expr = if let Some(catch_node) = catch_scope {
1835-
let target_id = Ok(self.lower_node_id(catch_node));
1837+
let target_id = Ok(self.lower_node_id(catch_node, DUMMY_SP));
18361838
self.arena.alloc(self.expr(
18371839
try_span,
18381840
hir::ExprKind::Break(
@@ -2005,8 +2007,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
20052007
}
20062008

20072009
fn expr_unsafe(&mut self, expr: &'hir hir::Expr<'hir>) -> hir::Expr<'hir> {
2008-
let hir_id = self.next_id();
20092010
let span = expr.span;
2011+
let hir_id = self.next_id(span);
20102012
self.expr(
20112013
span,
20122014
hir::ExprKind::Block(
@@ -2044,16 +2046,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
20442046
kind: hir::ExprKind<'hir>,
20452047
attrs: AttrVec,
20462048
) -> hir::Expr<'hir> {
2047-
hir::Expr { hir_id: self.next_id(), kind, span, attrs }
2049+
hir::Expr { hir_id: self.next_id(span), kind, span, attrs }
20482050
}
20492051

20502052
fn field(&mut self, ident: Ident, expr: &'hir hir::Expr<'hir>, span: Span) -> hir::Field<'hir> {
2051-
hir::Field { hir_id: self.next_id(), ident, span, expr, is_shorthand: false }
2053+
hir::Field { hir_id: self.next_id(span), ident, span, expr, is_shorthand: false }
20522054
}
20532055

20542056
fn arm(&mut self, pat: &'hir hir::Pat<'hir>, expr: &'hir hir::Expr<'hir>) -> hir::Arm<'hir> {
20552057
hir::Arm {
2056-
hir_id: self.next_id(),
2058+
hir_id: self.next_id(expr.span),
20572059
attrs: &[],
20582060
pat,
20592061
guard: None,

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_hir::def::{DefKind, Res};
1313
use rustc_hir::def_id::LocalDefId;
1414
use rustc_span::source_map::{respan, DesugaringKind};
1515
use rustc_span::symbol::{kw, sym, Ident};
16-
use rustc_span::Span;
16+
use rustc_span::{Span, DUMMY_SP};
1717
use rustc_target::spec::abi;
1818

1919
use smallvec::{smallvec, SmallVec};
@@ -34,8 +34,8 @@ impl ItemLowerer<'_, '_, '_> {
3434
}
3535

3636
impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
37-
fn visit_mod(&mut self, m: &'a Mod, _s: Span, _attrs: &[Attribute], n: NodeId) {
38-
let hir_id = self.lctx.lower_node_id(n);
37+
fn visit_mod(&mut self, m: &'a Mod, span: Span, _attrs: &[Attribute], n: NodeId) {
38+
let hir_id = self.lctx.lower_node_id(n, span);
3939

4040
self.lctx.modules.insert(
4141
hir_id,
@@ -230,7 +230,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
230230

231231
if let ItemKind::MacroDef(MacroDef { ref body, macro_rules }) = i.kind {
232232
if !macro_rules || self.sess.contains_name(&i.attrs, sym::macro_export) {
233-
let hir_id = self.lower_node_id(i.id);
233+
let hir_id = self.lower_node_id(i.id, i.span);
234234
let body = P(self.lower_mac_args(body));
235235
self.exported_macros.push(hir::MacroDef {
236236
ident,
@@ -248,7 +248,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
248248

249249
let kind = self.lower_item_kind(i.span, i.id, &mut ident, attrs, &mut vis, &i.kind);
250250

251-
Some(hir::Item { hir_id: self.lower_node_id(i.id), ident, attrs, kind, vis, span: i.span })
251+
Some(hir::Item {
252+
hir_id: self.lower_node_id(i.id, i.span),
253+
ident,
254+
attrs,
255+
kind,
256+
vis,
257+
span: i.span,
258+
})
252259
}
253260

254261
fn lower_item_kind(
@@ -357,14 +364,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
357364
self.lower_generics(generics, ImplTraitContext::disallowed()),
358365
),
359366
ItemKind::Struct(ref struct_def, ref generics) => {
360-
let struct_def = self.lower_variant_data(struct_def);
367+
let struct_def = self.lower_variant_data(span, struct_def);
361368
hir::ItemKind::Struct(
362369
struct_def,
363370
self.lower_generics(generics, ImplTraitContext::disallowed()),
364371
)
365372
}
366373
ItemKind::Union(ref vdata, ref generics) => {
367-
let vdata = self.lower_variant_data(vdata);
374+
let vdata = self.lower_variant_data(span, vdata);
368375
hir::ItemKind::Union(
369376
vdata,
370377
self.lower_generics(generics, ImplTraitContext::disallowed()),
@@ -395,7 +402,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
395402
// method, it will not be considered an in-band
396403
// lifetime to be added, but rather a reference to a
397404
// parent lifetime.
398-
let lowered_trait_impl_id = self.lower_node_id(id);
405+
let lowered_trait_impl_id = self.lower_node_id(id, DUMMY_SP);
399406
let (generics, (trait_ref, lowered_ty)) = self.add_in_band_defs(
400407
ast_generics,
401408
def_id,
@@ -537,7 +544,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
537544
let span = path.span;
538545

539546
self.with_hir_id_owner(new_node_id, |this| {
540-
let new_id = this.lower_node_id(new_node_id);
547+
let new_id = this.lower_node_id(new_node_id, span);
541548
let res = this.lower_res(res);
542549
let path = this.lower_path_extra(res, &path, ParamMode::Explicit, None);
543550
let kind = hir::ItemKind::Use(path, hir::UseKind::Single);
@@ -594,7 +601,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
594601

595602
// Add all the nested `PathListItem`s to the HIR.
596603
for &(ref use_tree, id) in trees {
597-
let new_hir_id = self.lower_node_id(id);
604+
let new_hir_id = self.lower_node_id(id, use_tree.span);
598605

599606
let mut prefix = prefix.clone();
600607

@@ -663,7 +670,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
663670
let segments =
664671
self.arena.alloc_from_iter(path.segments.iter().map(|seg| hir::PathSegment {
665672
ident: seg.ident,
666-
hir_id: seg.hir_id.map(|_| self.next_id()),
673+
hir_id: seg.hir_id.map(|_| self.next_id(seg.ident.span)),
667674
res: seg.res,
668675
args: None,
669676
infer_args: seg.infer_args,
@@ -679,7 +686,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
679686
hir::VisibilityKind::Restricted { ref path, hir_id: _ } => {
680687
hir::VisibilityKind::Restricted {
681688
path: self.rebuild_use_path(path),
682-
hir_id: self.next_id(),
689+
hir_id: self.next_id(vis.span),
683690
}
684691
}
685692
};
@@ -689,7 +696,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
689696
fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem<'hir> {
690697
let def_id = self.resolver.local_def_id(i.id);
691698
hir::ForeignItem {
692-
hir_id: self.lower_node_id(i.id),
699+
hir_id: self.lower_node_id(i.id, i.span),
693700
ident: i.ident,
694701
attrs: self.lower_attrs(&i.attrs),
695702
kind: match i.kind {
@@ -724,7 +731,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
724731

725732
fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef<'hir> {
726733
hir::ForeignItemRef {
727-
id: hir::ForeignItemId { hir_id: self.lower_node_id(i.id) },
734+
id: hir::ForeignItemId { hir_id: self.lower_node_id(i.id, i.span) },
728735
ident: i.ident,
729736
span: i.span,
730737
vis: self.lower_visibility(&i.vis, Some(i.id)),
@@ -738,15 +745,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
738745
fn lower_variant(&mut self, v: &Variant) -> hir::Variant<'hir> {
739746
hir::Variant {
740747
attrs: self.lower_attrs(&v.attrs),
741-
data: self.lower_variant_data(&v.data),
748+
data: self.lower_variant_data(v.span, &v.data),
742749
disr_expr: v.disr_expr.as_ref().map(|e| self.lower_anon_const(e)),
743-
id: self.lower_node_id(v.id),
750+
id: self.lower_node_id(v.id, v.span),
744751
ident: v.ident,
745752
span: v.span,
746753
}
747754
}
748755

749-
fn lower_variant_data(&mut self, vdata: &VariantData) -> hir::VariantData<'hir> {
756+
fn lower_variant_data(&mut self, span: Span, vdata: &VariantData) -> hir::VariantData<'hir> {
750757
match *vdata {
751758
VariantData::Struct(ref fields, recovered) => hir::VariantData::Struct(
752759
self.arena
@@ -756,9 +763,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
756763
VariantData::Tuple(ref fields, id) => hir::VariantData::Tuple(
757764
self.arena
758765
.alloc_from_iter(fields.iter().enumerate().map(|f| self.lower_struct_field(f))),
759-
self.lower_node_id(id),
766+
self.lower_node_id(id, span),
760767
),
761-
VariantData::Unit(id) => hir::VariantData::Unit(self.lower_node_id(id)),
768+
VariantData::Unit(id) => hir::VariantData::Unit(self.lower_node_id(id, span)),
762769
}
763770
}
764771

@@ -777,7 +784,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
777784
};
778785
hir::StructField {
779786
span: f.span,
780-
hir_id: self.lower_node_id(f.id),
787+
hir_id: self.lower_node_id(f.id, f.span),
781788
ident: match f.ident {
782789
Some(ident) => ident,
783790
// FIXME(jseyfried): positional field hygiene.
@@ -824,7 +831,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
824831
};
825832

826833
hir::TraitItem {
827-
hir_id: self.lower_node_id(i.id),
834+
hir_id: self.lower_node_id(i.id, i.span),
828835
ident: i.ident,
829836
attrs: self.lower_attrs(&i.attrs),
830837
generics,
@@ -844,7 +851,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
844851
}
845852
AssocItemKind::MacCall(..) => unimplemented!(),
846853
};
847-
let id = hir::TraitItemId { hir_id: self.lower_node_id(i.id) };
854+
let id = hir::TraitItemId { hir_id: self.lower_node_id(i.id, i.span) };
848855
let defaultness = hir::Defaultness::Default { has_value: has_default };
849856
hir::TraitItemRef { id, ident: i.ident, span: i.span, defaultness, kind }
850857
}
@@ -908,7 +915,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
908915
let has_value = true;
909916
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
910917
hir::ImplItem {
911-
hir_id: self.lower_node_id(i.id),
918+
hir_id: self.lower_node_id(i.id, i.span),
912919
ident: i.ident,
913920
attrs: self.lower_attrs(&i.attrs),
914921
generics,
@@ -924,7 +931,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
924931
let has_value = true;
925932
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
926933
hir::ImplItemRef {
927-
id: hir::ImplItemId { hir_id: self.lower_node_id(i.id) },
934+
id: hir::ImplItemId { hir_id: self.lower_node_id(i.id, i.span) },
928935
ident: i.ident,
929936
span: i.span,
930937
vis: self.lower_visibility(&i.vis, Some(i.id)),
@@ -956,9 +963,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
956963
VisibilityKind::Restricted { ref path, id } => {
957964
debug!("lower_visibility: restricted path id = {:?}", id);
958965
let lowered_id = if let Some(owner) = explicit_owner {
959-
self.lower_node_id_with_owner(id, owner)
966+
self.lower_node_id_with_owner(id, owner, v.span)
960967
} else {
961-
self.lower_node_id(id)
968+
self.lower_node_id(id, v.span)
962969
};
963970
let res = self.expect_full_res(id);
964971
let res = self.lower_res(res);
@@ -1013,7 +1020,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10131020
fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> {
10141021
hir::Param {
10151022
attrs: self.lower_attrs(&param.attrs),
1016-
hir_id: self.lower_node_id(param.id),
1023+
hir_id: self.lower_node_id(param.id, param.span),
10171024
pat: self.lower_pat(&param.pat),
10181025
ty_span: param.ty.span,
10191026
span: param.span,
@@ -1463,7 +1470,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14631470
}),
14641471
WherePredicate::EqPredicate(WhereEqPredicate { id, ref lhs_ty, ref rhs_ty, span }) => {
14651472
hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
1466-
hir_id: self.lower_node_id(id),
1473+
hir_id: self.lower_node_id(id, span),
14671474
lhs_ty: self.lower_ty(lhs_ty, ImplTraitContext::disallowed()),
14681475
rhs_ty: self.lower_ty(rhs_ty, ImplTraitContext::disallowed()),
14691476
span,

0 commit comments

Comments
 (0)