Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Spans from HIR -- 1/N -- Span collection #72878

Closed
wants to merge 10 commits into from
36 changes: 19 additions & 17 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_hir as hir;
use rustc_hir::def::Res;
use rustc_session::parse::feature_err;
use rustc_span::hygiene::ForLoopLoc;
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned, DUMMY_SP};
use rustc_span::symbol::{sym, Ident, Symbol};
use rustc_target::asm;
use std::collections::hash_map::Entry;
Expand Down Expand Up @@ -229,6 +229,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// Include parens in span, but only if it is a super-span.
if e.span.contains(ex.span) {
ex.span = e.span;
self.spans[ex.hir_id] = e.span;
}
// Merge attributes into the inner expression.
let mut attrs: Vec<_> = e.attrs.iter().map(|a| self.lower_attr(a)).collect();
Expand All @@ -246,7 +247,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
};

hir::Expr {
hir_id: self.lower_node_id(e.id),
hir_id: self.lower_node_id(e.id, e.span),
kind,
span: e.span,
attrs: e.attrs.iter().map(|a| self.lower_attr(a)).collect::<Vec<_>>().into(),
Expand Down Expand Up @@ -514,7 +515,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
});
hir::Arm {
hir_id: self.next_id(),
hir_id: self.next_id(arm.span),
attrs: self.lower_attrs(&arm.attrs),
pat,
guard,
Expand Down Expand Up @@ -548,7 +549,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

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

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

let body_id = self.lower_body(move |this| {
Expand All @@ -586,7 +587,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
Some(hir::Movability::Static),
);
let generator = hir::Expr {
hir_id: self.lower_node_id(closure_node_id),
hir_id: self.lower_node_id(closure_node_id, span),
kind: generator_kind,
span,
attrs: ThinVec::new(),
Expand Down Expand Up @@ -683,7 +684,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

// `::std::task::Poll::Ready(result) => break result`
let loop_node_id = self.resolver.next_node_id();
let loop_hir_id = self.lower_node_id(loop_node_id);
let loop_hir_id = self.lower_node_id(loop_node_id, span);
let ready_arm = {
let x_ident = Ident::with_dummy_span(sym::result);
let (x_pat, x_pat_hid) = self.pat_ident(span, x_ident);
Expand Down Expand Up @@ -1008,7 +1009,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let field_pats = self.arena.alloc_from_iter(fields.iter().map(|f| {
let pat = self.destructure_assign(&f.expr, eq_sign_span, assignments);
hir::FieldPat {
hir_id: self.next_id(),
hir_id: self.next_id(f.span),
ident: f.ident,
pat,
is_shorthand: f.is_shorthand,
Expand Down Expand Up @@ -1149,7 +1150,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let target_id = match destination {
Some((id, _)) => {
if let Some(loop_id) = self.resolver.get_label_res(id) {
Ok(self.lower_node_id(loop_id))
Ok(self.lower_node_id(loop_id, DUMMY_SP))
} else {
Err(hir::LoopIdError::UnresolvedLabel)
}
Expand All @@ -1158,7 +1159,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
.loop_scopes
.last()
.cloned()
.map(|id| Ok(self.lower_node_id(id)))
.map(|id| Ok(self.lower_node_id(id, DUMMY_SP)))
.unwrap_or(Err(hir::LoopIdError::OutsideLoopScope)),
};
hir::Destination { label: destination.map(|(_, label)| label), target_id }
Expand Down Expand Up @@ -1554,7 +1555,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

fn lower_field(&mut self, f: &Field) -> hir::Field<'hir> {
hir::Field {
hir_id: self.next_id(),
hir_id: self.next_id(f.span),
ident: f.ident,
expr: self.lower_expr(&f.expr),
span: f.span,
Expand Down Expand Up @@ -1619,6 +1620,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
None,
);
head.span = desugared_span;
self.spans[head.hir_id] = desugared_span;

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

Expand Down Expand Up @@ -1705,7 +1707,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// `[opt_ident]: loop { ... }`
let kind = hir::ExprKind::Loop(loop_block, opt_label, hir::LoopSource::ForLoop);
let loop_expr = self.arena.alloc(hir::Expr {
hir_id: self.lower_node_id(e.id),
hir_id: self.lower_node_id(e.id, e.span),
kind,
span: e.span,
attrs: ThinVec::new(),
Expand Down Expand Up @@ -1832,7 +1834,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let thin_attrs = ThinVec::from(attrs);
let catch_scope = self.catch_scopes.last().copied();
let ret_expr = if let Some(catch_node) = catch_scope {
let target_id = Ok(self.lower_node_id(catch_node));
let target_id = Ok(self.lower_node_id(catch_node, DUMMY_SP));
self.arena.alloc(self.expr(
try_span,
hir::ExprKind::Break(
Expand Down Expand Up @@ -2005,8 +2007,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

fn expr_unsafe(&mut self, expr: &'hir hir::Expr<'hir>) -> hir::Expr<'hir> {
let hir_id = self.next_id();
let span = expr.span;
let hir_id = self.next_id(span);
self.expr(
span,
hir::ExprKind::Block(
Expand Down Expand Up @@ -2044,16 +2046,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
kind: hir::ExprKind<'hir>,
attrs: AttrVec,
) -> hir::Expr<'hir> {
hir::Expr { hir_id: self.next_id(), kind, span, attrs }
hir::Expr { hir_id: self.next_id(span), kind, span, attrs }
}

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

fn arm(&mut self, pat: &'hir hir::Pat<'hir>, expr: &'hir hir::Expr<'hir>) -> hir::Arm<'hir> {
hir::Arm {
hir_id: self.next_id(),
hir_id: self.next_id(expr.span),
attrs: &[],
pat,
guard: None,
Expand Down
63 changes: 35 additions & 28 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::LocalDefId;
use rustc_span::source_map::{respan, DesugaringKind};
use rustc_span::symbol::{kw, sym, Ident};
use rustc_span::Span;
use rustc_span::{Span, DUMMY_SP};
use rustc_target::spec::abi;

use smallvec::{smallvec, SmallVec};
Expand All @@ -34,8 +34,8 @@ impl ItemLowerer<'_, '_, '_> {
}

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

self.lctx.modules.insert(
hir_id,
Expand Down Expand Up @@ -230,7 +230,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

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

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

Some(hir::Item { hir_id: self.lower_node_id(i.id), ident, attrs, kind, vis, span: i.span })
Some(hir::Item {
hir_id: self.lower_node_id(i.id, i.span),
ident,
attrs,
kind,
vis,
span: i.span,
})
}

fn lower_item_kind(
Expand Down Expand Up @@ -357,14 +364,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.lower_generics(generics, ImplTraitContext::disallowed()),
),
ItemKind::Struct(ref struct_def, ref generics) => {
let struct_def = self.lower_variant_data(struct_def);
let struct_def = self.lower_variant_data(span, struct_def);
hir::ItemKind::Struct(
struct_def,
self.lower_generics(generics, ImplTraitContext::disallowed()),
)
}
ItemKind::Union(ref vdata, ref generics) => {
let vdata = self.lower_variant_data(vdata);
let vdata = self.lower_variant_data(span, vdata);
hir::ItemKind::Union(
vdata,
self.lower_generics(generics, ImplTraitContext::disallowed()),
Expand Down Expand Up @@ -395,7 +402,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// method, it will not be considered an in-band
// lifetime to be added, but rather a reference to a
// parent lifetime.
let lowered_trait_impl_id = self.lower_node_id(id);
let lowered_trait_impl_id = self.lower_node_id(id, DUMMY_SP);
let (generics, (trait_ref, lowered_ty)) = self.add_in_band_defs(
ast_generics,
def_id,
Expand Down Expand Up @@ -537,7 +544,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let span = path.span;

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

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

let mut prefix = prefix.clone();

Expand Down Expand Up @@ -663,7 +670,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let segments =
self.arena.alloc_from_iter(path.segments.iter().map(|seg| hir::PathSegment {
ident: seg.ident,
hir_id: seg.hir_id.map(|_| self.next_id()),
hir_id: seg.hir_id.map(|_| self.next_id(seg.ident.span)),
res: seg.res,
args: None,
infer_args: seg.infer_args,
Expand All @@ -679,7 +686,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::VisibilityKind::Restricted { ref path, hir_id: _ } => {
hir::VisibilityKind::Restricted {
path: self.rebuild_use_path(path),
hir_id: self.next_id(),
hir_id: self.next_id(vis.span),
}
}
};
Expand All @@ -689,7 +696,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem<'hir> {
let def_id = self.resolver.local_def_id(i.id);
hir::ForeignItem {
hir_id: self.lower_node_id(i.id),
hir_id: self.lower_node_id(i.id, i.span),
ident: i.ident,
attrs: self.lower_attrs(&i.attrs),
kind: match i.kind {
Expand Down Expand Up @@ -724,7 +731,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef<'hir> {
hir::ForeignItemRef {
id: hir::ForeignItemId { hir_id: self.lower_node_id(i.id) },
id: hir::ForeignItemId { hir_id: self.lower_node_id(i.id, i.span) },
ident: i.ident,
span: i.span,
vis: self.lower_visibility(&i.vis, Some(i.id)),
Expand All @@ -738,15 +745,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_variant(&mut self, v: &Variant) -> hir::Variant<'hir> {
hir::Variant {
attrs: self.lower_attrs(&v.attrs),
data: self.lower_variant_data(&v.data),
data: self.lower_variant_data(v.span, &v.data),
disr_expr: v.disr_expr.as_ref().map(|e| self.lower_anon_const(e)),
id: self.lower_node_id(v.id),
id: self.lower_node_id(v.id, v.span),
ident: v.ident,
span: v.span,
}
}

fn lower_variant_data(&mut self, vdata: &VariantData) -> hir::VariantData<'hir> {
fn lower_variant_data(&mut self, span: Span, vdata: &VariantData) -> hir::VariantData<'hir> {
match *vdata {
VariantData::Struct(ref fields, recovered) => hir::VariantData::Struct(
self.arena
Expand All @@ -756,9 +763,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
VariantData::Tuple(ref fields, id) => hir::VariantData::Tuple(
self.arena
.alloc_from_iter(fields.iter().enumerate().map(|f| self.lower_struct_field(f))),
self.lower_node_id(id),
self.lower_node_id(id, span),
),
VariantData::Unit(id) => hir::VariantData::Unit(self.lower_node_id(id)),
VariantData::Unit(id) => hir::VariantData::Unit(self.lower_node_id(id, span)),
}
}

Expand All @@ -777,7 +784,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
};
hir::StructField {
span: f.span,
hir_id: self.lower_node_id(f.id),
hir_id: self.lower_node_id(f.id, f.span),
ident: match f.ident {
Some(ident) => ident,
// FIXME(jseyfried): positional field hygiene.
Expand Down Expand Up @@ -824,7 +831,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
};

hir::TraitItem {
hir_id: self.lower_node_id(i.id),
hir_id: self.lower_node_id(i.id, i.span),
ident: i.ident,
attrs: self.lower_attrs(&i.attrs),
generics,
Expand All @@ -844,7 +851,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
AssocItemKind::MacCall(..) => unimplemented!(),
};
let id = hir::TraitItemId { hir_id: self.lower_node_id(i.id) };
let id = hir::TraitItemId { hir_id: self.lower_node_id(i.id, i.span) };
let defaultness = hir::Defaultness::Default { has_value: has_default };
hir::TraitItemRef { id, ident: i.ident, span: i.span, defaultness, kind }
}
Expand Down Expand Up @@ -908,7 +915,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let has_value = true;
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
hir::ImplItem {
hir_id: self.lower_node_id(i.id),
hir_id: self.lower_node_id(i.id, i.span),
ident: i.ident,
attrs: self.lower_attrs(&i.attrs),
generics,
Expand All @@ -924,7 +931,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let has_value = true;
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
hir::ImplItemRef {
id: hir::ImplItemId { hir_id: self.lower_node_id(i.id) },
id: hir::ImplItemId { hir_id: self.lower_node_id(i.id, i.span) },
ident: i.ident,
span: i.span,
vis: self.lower_visibility(&i.vis, Some(i.id)),
Expand Down Expand Up @@ -956,9 +963,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
VisibilityKind::Restricted { ref path, id } => {
debug!("lower_visibility: restricted path id = {:?}", id);
let lowered_id = if let Some(owner) = explicit_owner {
self.lower_node_id_with_owner(id, owner)
self.lower_node_id_with_owner(id, owner, v.span)
} else {
self.lower_node_id(id)
self.lower_node_id(id, v.span)
};
let res = self.expect_full_res(id);
let res = self.lower_res(res);
Expand Down Expand Up @@ -1013,7 +1020,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> {
hir::Param {
attrs: self.lower_attrs(&param.attrs),
hir_id: self.lower_node_id(param.id),
hir_id: self.lower_node_id(param.id, param.span),
pat: self.lower_pat(&param.pat),
ty_span: param.ty.span,
span: param.span,
Expand Down Expand Up @@ -1463,7 +1470,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}),
WherePredicate::EqPredicate(WhereEqPredicate { id, ref lhs_ty, ref rhs_ty, span }) => {
hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
hir_id: self.lower_node_id(id),
hir_id: self.lower_node_id(id, span),
lhs_ty: self.lower_ty(lhs_ty, ImplTraitContext::disallowed()),
rhs_ty: self.lower_ty(rhs_ty, ImplTraitContext::disallowed()),
span,
Expand Down
Loading