Skip to content

Remove Spans from HIR -- 3/N -- hir::Item #73095

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

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
3603d31
Remove useless Clone bound in IndexVec.
cjgillot May 19, 2020
76a36df
Introduce HirIdVec.
cjgillot May 19, 2020
c6acd0f
Collect spans during HIR indexing.
cjgillot May 1, 2020
71b5666
Introduce a query for HIR spans.
cjgillot May 8, 2020
5b5bc8d
Collect spans during lowering.
cjgillot May 1, 2020
6a1537c
Collect spans directly into an IndexVec.
cjgillot May 18, 2020
3640fb3
Directly use span map for indexing.
cjgillot May 1, 2020
1b9ccbf
Don't pass spans in hir::map::blocks.
cjgillot May 2, 2020
9f00730
Add comment.
cjgillot Jun 6, 2020
ff32863
Stop passing the Span in HIR visiting.
cjgillot May 2, 2020
61402a5
Pass HirId in save_analysis.
cjgillot Jun 7, 2020
a1b12d8
Remove span from hir::Param.
cjgillot May 1, 2020
dad5ec0
Remove span from hir::Variant.
cjgillot May 1, 2020
0bf0ea2
Remove span from hir::StructField.
cjgillot May 1, 2020
76f16ed
Remove span from hir::Stmt.
cjgillot May 1, 2020
8197821
Remove span from hir::Block.
cjgillot May 1, 2020
deec340
Remove span from hir::MacroDef.
cjgillot May 2, 2020
7584102
Remove span from hir::GenericParam.
cjgillot May 2, 2020
71c2c92
Remove span from hir::Arm.
cjgillot May 2, 2020
f94dac7
Remove span from hir::FieldPat.
cjgillot May 2, 2020
1f05c3d
Remove span from hir::Local.
cjgillot May 2, 2020
61504ae
Remove span from hir::Pat.
cjgillot May 2, 2020
44a58d4
Remove span from hir::Field.
cjgillot May 2, 2020
415ad17
Remove GenericArg::span.
cjgillot May 3, 2020
ed0b627
Remove Span from hir::TypeBinding.
cjgillot May 8, 2020
56ee3f9
Remove Span from hir::ConstArg.
cjgillot Jun 1, 2020
00ce893
Remove Span from hir::TraitItemRef.
cjgillot Jun 1, 2020
0f31095
Remove Span from hir::ImplItemRef.
cjgillot Jun 1, 2020
312c2fb
Fix fulldeps tests.
cjgillot May 9, 2020
d3a1ecd
Pass HirId in librustc_typeck::check.
cjgillot May 2, 2020
63db483
Pass HirId in librustc_lint.
cjgillot May 2, 2020
066195c
Pass HirId in librustc_passes::stability.
cjgillot May 2, 2020
edbbd56
Remove span from hir::Item.
cjgillot May 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Collect spans during lowering.
  • Loading branch information
cjgillot committed Jun 23, 2020
commit 5b5bc8da4c748c52e90c343f9f9680a30d94da74
38 changes: 20 additions & 18 deletions src/librustc_ast_lowering/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_data_structures::thin_vec::ThinVec;
use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def::Res;
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 @@ -203,6 +203,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.insert(ex.hir_id, e.span);
}
// Merge attributes into the inner expression.
let mut attrs = e.attrs.clone();
Expand All @@ -220,7 +221,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 @@ -473,7 +474,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

fn lower_arm(&mut self, arm: &Arm) -> hir::Arm<'hir> {
hir::Arm {
hir_id: self.next_id(),
hir_id: self.next_id(arm.span),
attrs: self.lower_attrs(&arm.attrs),
pat: self.lower_pat(&arm.pat),
guard: match arm.guard {
Expand Down Expand Up @@ -510,7 +511,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 @@ -526,7 +527,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, span };
let param = hir::Param { attrs: &[], hir_id: self.next_id(span), pat, span };
let params = arena_vec![self; param];

let body_id = self.lower_body(move |this| {
Expand All @@ -548,7 +549,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 @@ -629,7 +630,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// Use of `await` outside of an async context, we cannot use `task_context` here.
self.expr_err(span)
};
let pin_ty_id = self.next_id();
let pin_ty_id = self.next_id(span);
let new_unchecked_expr_kind = self.expr_call_std_assoc_fn(
pin_ty_id,
span,
Expand All @@ -653,7 +654,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 @@ -841,7 +842,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

/// Desugar `<start>..=<end>` into `std::ops::RangeInclusive::new(<start>, <end>)`.
fn lower_expr_range_closed(&mut self, span: Span, e1: &Expr, e2: &Expr) -> hir::ExprKind<'hir> {
let id = self.next_id();
let id = self.next_id(span);
let e1 = self.lower_expr_mut(e1);
let e2 = self.lower_expr_mut(e2);
self.expr_call_std_assoc_fn(
Expand Down Expand Up @@ -898,7 +899,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 @@ -907,7 +908,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 @@ -1304,7 +1305,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 @@ -1364,6 +1365,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let mut head = self.lower_expr_mut(head);
let desugared_span = self.mark_span_with_reason(DesugaringKind::ForLoop, head.span, None);
head.span = desugared_span;
self.spans.insert(head.hir_id, desugared_span);

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

Expand Down Expand Up @@ -1448,7 +1450,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 @@ -1554,7 +1556,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 @@ -1758,8 +1760,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 @@ -1797,16 +1799,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
61 changes: 34 additions & 27 deletions src/librustc_ast_lowering/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,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 log::debug;
Expand All @@ -35,8 +35,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 @@ -206,7 +206,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

if let ItemKind::MacroDef(MacroDef { ref body, macro_rules }) = i.kind {
if !macro_rules || attr::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 @@ -224,7 +224,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 @@ -319,14 +326,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 @@ -357,7 +364,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 @@ -499,7 +506,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 @@ -553,7 +560,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 @@ -622,7 +629,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 @@ -638,7 +645,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 @@ -648,7 +655,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem<'hir> {
let def_id = self.resolver.definitions().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 @@ -695,15 +702,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 @@ -713,9 +720,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 @@ -734,7 +741,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 @@ -781,7 +788,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 @@ -801,7 +808,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 @@ -865,7 +872,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 @@ -881,7 +888,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 @@ -913,9 +920,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 @@ -970,7 +977,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),
span: param.span,
}
Expand Down Expand Up @@ -1413,7 +1420,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