Skip to content

Commit 5b5bc8d

Browse files
committed
Collect spans during lowering.
1 parent 71b5666 commit 5b5bc8d

File tree

7 files changed

+142
-104
lines changed

7 files changed

+142
-104
lines changed

src/librustc_ast_lowering/expr.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_data_structures::thin_vec::ThinVec;
99
use rustc_errors::struct_span_err;
1010
use rustc_hir as hir;
1111
use rustc_hir::def::Res;
12-
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
12+
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned, DUMMY_SP};
1313
use rustc_span::symbol::{sym, Ident, Symbol};
1414
use rustc_target::asm;
1515
use std::collections::hash_map::Entry;
@@ -203,6 +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);
206207
}
207208
// Merge attributes into the inner expression.
208209
let mut attrs = e.attrs.clone();
@@ -220,7 +221,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
220221
};
221222

222223
hir::Expr {
223-
hir_id: self.lower_node_id(e.id),
224+
hir_id: self.lower_node_id(e.id, e.span),
224225
kind,
225226
span: e.span,
226227
attrs: e.attrs.iter().map(|a| self.lower_attr(a)).collect::<Vec<_>>().into(),
@@ -473,7 +474,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
473474

474475
fn lower_arm(&mut self, arm: &Arm) -> hir::Arm<'hir> {
475476
hir::Arm {
476-
hir_id: self.next_id(),
477+
hir_id: self.next_id(arm.span),
477478
attrs: self.lower_attrs(&arm.attrs),
478479
pat: self.lower_pat(&arm.pat),
479480
guard: match arm.guard {
@@ -510,7 +511,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
510511

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

515516
// The closure/generator `FnDecl` takes a single (resume) argument of type `input_ty`.
516517
let decl = self.arena.alloc(hir::FnDecl {
@@ -526,7 +527,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
526527
Ident::with_dummy_span(sym::_task_context),
527528
hir::BindingAnnotation::Mutable,
528529
);
529-
let param = hir::Param { attrs: &[], hir_id: self.next_id(), pat, span };
530+
let param = hir::Param { attrs: &[], hir_id: self.next_id(span), pat, span };
530531
let params = arena_vec![self; param];
531532

532533
let body_id = self.lower_body(move |this| {
@@ -548,7 +549,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
548549
Some(hir::Movability::Static),
549550
);
550551
let generator = hir::Expr {
551-
hir_id: self.lower_node_id(closure_node_id),
552+
hir_id: self.lower_node_id(closure_node_id, span),
552553
kind: generator_kind,
553554
span,
554555
attrs: ThinVec::new(),
@@ -629,7 +630,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
629630
// Use of `await` outside of an async context, we cannot use `task_context` here.
630631
self.expr_err(span)
631632
};
632-
let pin_ty_id = self.next_id();
633+
let pin_ty_id = self.next_id(span);
633634
let new_unchecked_expr_kind = self.expr_call_std_assoc_fn(
634635
pin_ty_id,
635636
span,
@@ -653,7 +654,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
653654

654655
// `::std::task::Poll::Ready(result) => break result`
655656
let loop_node_id = self.resolver.next_node_id();
656-
let loop_hir_id = self.lower_node_id(loop_node_id);
657+
let loop_hir_id = self.lower_node_id(loop_node_id, span);
657658
let ready_arm = {
658659
let x_ident = Ident::with_dummy_span(sym::result);
659660
let (x_pat, x_pat_hid) = self.pat_ident(span, x_ident);
@@ -841,7 +842,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
841842

842843
/// Desugar `<start>..=<end>` into `std::ops::RangeInclusive::new(<start>, <end>)`.
843844
fn lower_expr_range_closed(&mut self, span: Span, e1: &Expr, e2: &Expr) -> hir::ExprKind<'hir> {
844-
let id = self.next_id();
845+
let id = self.next_id(span);
845846
let e1 = self.lower_expr_mut(e1);
846847
let e2 = self.lower_expr_mut(e2);
847848
self.expr_call_std_assoc_fn(
@@ -898,7 +899,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
898899
let target_id = match destination {
899900
Some((id, _)) => {
900901
if let Some(loop_id) = self.resolver.get_label_res(id) {
901-
Ok(self.lower_node_id(loop_id))
902+
Ok(self.lower_node_id(loop_id, DUMMY_SP))
902903
} else {
903904
Err(hir::LoopIdError::UnresolvedLabel)
904905
}
@@ -907,7 +908,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
907908
.loop_scopes
908909
.last()
909910
.cloned()
910-
.map(|id| Ok(self.lower_node_id(id)))
911+
.map(|id| Ok(self.lower_node_id(id, DUMMY_SP)))
911912
.unwrap_or(Err(hir::LoopIdError::OutsideLoopScope)),
912913
};
913914
hir::Destination { label: destination.map(|(_, label)| label), target_id }
@@ -1304,7 +1305,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13041305

13051306
fn lower_field(&mut self, f: &Field) -> hir::Field<'hir> {
13061307
hir::Field {
1307-
hir_id: self.next_id(),
1308+
hir_id: self.next_id(f.span),
13081309
ident: f.ident,
13091310
expr: self.lower_expr(&f.expr),
13101311
span: f.span,
@@ -1364,6 +1365,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13641365
let mut head = self.lower_expr_mut(head);
13651366
let desugared_span = self.mark_span_with_reason(DesugaringKind::ForLoop, head.span, None);
13661367
head.span = desugared_span;
1368+
self.spans.insert(head.hir_id, desugared_span);
13671369

13681370
let iter = Ident::with_dummy_span(sym::iter);
13691371

@@ -1448,7 +1450,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14481450
// `[opt_ident]: loop { ... }`
14491451
let kind = hir::ExprKind::Loop(loop_block, opt_label, hir::LoopSource::ForLoop);
14501452
let loop_expr = self.arena.alloc(hir::Expr {
1451-
hir_id: self.lower_node_id(e.id),
1453+
hir_id: self.lower_node_id(e.id, e.span),
14521454
kind,
14531455
span: e.span,
14541456
attrs: ThinVec::new(),
@@ -1554,7 +1556,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15541556
let thin_attrs = ThinVec::from(attrs);
15551557
let catch_scope = self.catch_scopes.last().copied();
15561558
let ret_expr = if let Some(catch_node) = catch_scope {
1557-
let target_id = Ok(self.lower_node_id(catch_node));
1559+
let target_id = Ok(self.lower_node_id(catch_node, DUMMY_SP));
15581560
self.arena.alloc(self.expr(
15591561
try_span,
15601562
hir::ExprKind::Break(
@@ -1758,8 +1760,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
17581760
}
17591761

17601762
fn expr_unsafe(&mut self, expr: &'hir hir::Expr<'hir>) -> hir::Expr<'hir> {
1761-
let hir_id = self.next_id();
17621763
let span = expr.span;
1764+
let hir_id = self.next_id(span);
17631765
self.expr(
17641766
span,
17651767
hir::ExprKind::Block(
@@ -1797,16 +1799,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
17971799
kind: hir::ExprKind<'hir>,
17981800
attrs: AttrVec,
17991801
) -> hir::Expr<'hir> {
1800-
hir::Expr { hir_id: self.next_id(), kind, span, attrs }
1802+
hir::Expr { hir_id: self.next_id(span), kind, span, attrs }
18011803
}
18021804

18031805
fn field(&mut self, ident: Ident, expr: &'hir hir::Expr<'hir>, span: Span) -> hir::Field<'hir> {
1804-
hir::Field { hir_id: self.next_id(), ident, span, expr, is_shorthand: false }
1806+
hir::Field { hir_id: self.next_id(span), ident, span, expr, is_shorthand: false }
18051807
}
18061808

18071809
fn arm(&mut self, pat: &'hir hir::Pat<'hir>, expr: &'hir hir::Expr<'hir>) -> hir::Arm<'hir> {
18081810
hir::Arm {
1809-
hir_id: self.next_id(),
1811+
hir_id: self.next_id(expr.span),
18101812
attrs: &[],
18111813
pat,
18121814
guard: None,

src/librustc_ast_lowering/item.rs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_hir::def::{DefKind, Res};
1414
use rustc_hir::def_id::LocalDefId;
1515
use rustc_span::source_map::{respan, DesugaringKind};
1616
use rustc_span::symbol::{kw, sym, Ident};
17-
use rustc_span::Span;
17+
use rustc_span::{Span, DUMMY_SP};
1818
use rustc_target::spec::abi;
1919

2020
use log::debug;
@@ -35,8 +35,8 @@ impl ItemLowerer<'_, '_, '_> {
3535
}
3636

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

4141
self.lctx.modules.insert(
4242
hir_id,
@@ -206,7 +206,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
206206

207207
if let ItemKind::MacroDef(MacroDef { ref body, macro_rules }) = i.kind {
208208
if !macro_rules || attr::contains_name(&i.attrs, sym::macro_export) {
209-
let hir_id = self.lower_node_id(i.id);
209+
let hir_id = self.lower_node_id(i.id, i.span);
210210
let body = P(self.lower_mac_args(body));
211211
self.exported_macros.push(hir::MacroDef {
212212
ident,
@@ -224,7 +224,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
224224

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

227-
Some(hir::Item { hir_id: self.lower_node_id(i.id), ident, attrs, kind, vis, span: i.span })
227+
Some(hir::Item {
228+
hir_id: self.lower_node_id(i.id, i.span),
229+
ident,
230+
attrs,
231+
kind,
232+
vis,
233+
span: i.span,
234+
})
228235
}
229236

230237
fn lower_item_kind(
@@ -319,14 +326,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
319326
self.lower_generics(generics, ImplTraitContext::disallowed()),
320327
),
321328
ItemKind::Struct(ref struct_def, ref generics) => {
322-
let struct_def = self.lower_variant_data(struct_def);
329+
let struct_def = self.lower_variant_data(span, struct_def);
323330
hir::ItemKind::Struct(
324331
struct_def,
325332
self.lower_generics(generics, ImplTraitContext::disallowed()),
326333
)
327334
}
328335
ItemKind::Union(ref vdata, ref generics) => {
329-
let vdata = self.lower_variant_data(vdata);
336+
let vdata = self.lower_variant_data(span, vdata);
330337
hir::ItemKind::Union(
331338
vdata,
332339
self.lower_generics(generics, ImplTraitContext::disallowed()),
@@ -357,7 +364,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
357364
// method, it will not be considered an in-band
358365
// lifetime to be added, but rather a reference to a
359366
// parent lifetime.
360-
let lowered_trait_impl_id = self.lower_node_id(id);
367+
let lowered_trait_impl_id = self.lower_node_id(id, DUMMY_SP);
361368
let (generics, (trait_ref, lowered_ty)) = self.add_in_band_defs(
362369
ast_generics,
363370
def_id,
@@ -499,7 +506,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
499506
let span = path.span;
500507

501508
self.with_hir_id_owner(new_node_id, |this| {
502-
let new_id = this.lower_node_id(new_node_id);
509+
let new_id = this.lower_node_id(new_node_id, span);
503510
let res = this.lower_res(res);
504511
let path = this.lower_path_extra(res, &path, ParamMode::Explicit, None);
505512
let kind = hir::ItemKind::Use(path, hir::UseKind::Single);
@@ -553,7 +560,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
553560

554561
// Add all the nested `PathListItem`s to the HIR.
555562
for &(ref use_tree, id) in trees {
556-
let new_hir_id = self.lower_node_id(id);
563+
let new_hir_id = self.lower_node_id(id, use_tree.span);
557564

558565
let mut prefix = prefix.clone();
559566

@@ -622,7 +629,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
622629
let segments =
623630
self.arena.alloc_from_iter(path.segments.iter().map(|seg| hir::PathSegment {
624631
ident: seg.ident,
625-
hir_id: seg.hir_id.map(|_| self.next_id()),
632+
hir_id: seg.hir_id.map(|_| self.next_id(seg.ident.span)),
626633
res: seg.res,
627634
args: None,
628635
infer_args: seg.infer_args,
@@ -638,7 +645,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
638645
hir::VisibilityKind::Restricted { ref path, hir_id: _ } => {
639646
hir::VisibilityKind::Restricted {
640647
path: self.rebuild_use_path(path),
641-
hir_id: self.next_id(),
648+
hir_id: self.next_id(vis.span),
642649
}
643650
}
644651
};
@@ -648,7 +655,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
648655
fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem<'hir> {
649656
let def_id = self.resolver.definitions().local_def_id(i.id);
650657
hir::ForeignItem {
651-
hir_id: self.lower_node_id(i.id),
658+
hir_id: self.lower_node_id(i.id, i.span),
652659
ident: i.ident,
653660
attrs: self.lower_attrs(&i.attrs),
654661
kind: match i.kind {
@@ -695,15 +702,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
695702
fn lower_variant(&mut self, v: &Variant) -> hir::Variant<'hir> {
696703
hir::Variant {
697704
attrs: self.lower_attrs(&v.attrs),
698-
data: self.lower_variant_data(&v.data),
705+
data: self.lower_variant_data(v.span, &v.data),
699706
disr_expr: v.disr_expr.as_ref().map(|e| self.lower_anon_const(e)),
700-
id: self.lower_node_id(v.id),
707+
id: self.lower_node_id(v.id, v.span),
701708
ident: v.ident,
702709
span: v.span,
703710
}
704711
}
705712

706-
fn lower_variant_data(&mut self, vdata: &VariantData) -> hir::VariantData<'hir> {
713+
fn lower_variant_data(&mut self, span: Span, vdata: &VariantData) -> hir::VariantData<'hir> {
707714
match *vdata {
708715
VariantData::Struct(ref fields, recovered) => hir::VariantData::Struct(
709716
self.arena
@@ -713,9 +720,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
713720
VariantData::Tuple(ref fields, id) => hir::VariantData::Tuple(
714721
self.arena
715722
.alloc_from_iter(fields.iter().enumerate().map(|f| self.lower_struct_field(f))),
716-
self.lower_node_id(id),
723+
self.lower_node_id(id, span),
717724
),
718-
VariantData::Unit(id) => hir::VariantData::Unit(self.lower_node_id(id)),
725+
VariantData::Unit(id) => hir::VariantData::Unit(self.lower_node_id(id, span)),
719726
}
720727
}
721728

@@ -734,7 +741,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
734741
};
735742
hir::StructField {
736743
span: f.span,
737-
hir_id: self.lower_node_id(f.id),
744+
hir_id: self.lower_node_id(f.id, f.span),
738745
ident: match f.ident {
739746
Some(ident) => ident,
740747
// FIXME(jseyfried): positional field hygiene.
@@ -781,7 +788,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
781788
};
782789

783790
hir::TraitItem {
784-
hir_id: self.lower_node_id(i.id),
791+
hir_id: self.lower_node_id(i.id, i.span),
785792
ident: i.ident,
786793
attrs: self.lower_attrs(&i.attrs),
787794
generics,
@@ -801,7 +808,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
801808
}
802809
AssocItemKind::MacCall(..) => unimplemented!(),
803810
};
804-
let id = hir::TraitItemId { hir_id: self.lower_node_id(i.id) };
811+
let id = hir::TraitItemId { hir_id: self.lower_node_id(i.id, i.span) };
805812
let defaultness = hir::Defaultness::Default { has_value: has_default };
806813
hir::TraitItemRef { id, ident: i.ident, span: i.span, defaultness, kind }
807814
}
@@ -865,7 +872,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
865872
let has_value = true;
866873
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
867874
hir::ImplItem {
868-
hir_id: self.lower_node_id(i.id),
875+
hir_id: self.lower_node_id(i.id, i.span),
869876
ident: i.ident,
870877
attrs: self.lower_attrs(&i.attrs),
871878
generics,
@@ -881,7 +888,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
881888
let has_value = true;
882889
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
883890
hir::ImplItemRef {
884-
id: hir::ImplItemId { hir_id: self.lower_node_id(i.id) },
891+
id: hir::ImplItemId { hir_id: self.lower_node_id(i.id, i.span) },
885892
ident: i.ident,
886893
span: i.span,
887894
vis: self.lower_visibility(&i.vis, Some(i.id)),
@@ -913,9 +920,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
913920
VisibilityKind::Restricted { ref path, id } => {
914921
debug!("lower_visibility: restricted path id = {:?}", id);
915922
let lowered_id = if let Some(owner) = explicit_owner {
916-
self.lower_node_id_with_owner(id, owner)
923+
self.lower_node_id_with_owner(id, owner, v.span)
917924
} else {
918-
self.lower_node_id(id)
925+
self.lower_node_id(id, v.span)
919926
};
920927
let res = self.expect_full_res(id);
921928
let res = self.lower_res(res);
@@ -970,7 +977,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
970977
fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> {
971978
hir::Param {
972979
attrs: self.lower_attrs(&param.attrs),
973-
hir_id: self.lower_node_id(param.id),
980+
hir_id: self.lower_node_id(param.id, param.span),
974981
pat: self.lower_pat(&param.pat),
975982
span: param.span,
976983
}
@@ -1413,7 +1420,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14131420
}),
14141421
WherePredicate::EqPredicate(WhereEqPredicate { id, ref lhs_ty, ref rhs_ty, span }) => {
14151422
hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
1416-
hir_id: self.lower_node_id(id),
1423+
hir_id: self.lower_node_id(id, span),
14171424
lhs_ty: self.lower_ty(lhs_ty, ImplTraitContext::disallowed()),
14181425
rhs_ty: self.lower_ty(rhs_ty, ImplTraitContext::disallowed()),
14191426
span,

0 commit comments

Comments
 (0)