Skip to content

Commit e281f67

Browse files
committed
Remove Span from hir::Expr.
1 parent 0d9d1e0 commit e281f67

File tree

281 files changed

+1946
-1443
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

281 files changed

+1946
-1443
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+25-26
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
252252
ExprKind::Err => hir::ExprKind::Err,
253253
ExprKind::Try(ref sub_expr) => self.lower_expr_try(e.span, sub_expr),
254254
ExprKind::Paren(ref ex) => {
255-
let mut ex = self.lower_expr_mut(ex);
255+
let ex = self.lower_expr_mut(ex);
256256
// Include parens in span, but only if it is a super-span.
257-
if e.span.contains(ex.span) {
258-
ex.span = e.span;
259-
self.spans[ex.hir_id] = e.span;
257+
let ex_span = &mut self.spans[ex.hir_id];
258+
if e.span.contains(*ex_span) {
259+
*ex_span = e.span;
260260
}
261261
// Merge attributes into the inner expression.
262262
if !e.attrs.is_empty() {
@@ -284,7 +284,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
284284

285285
let hir_id = self.lower_node_id(e.id, e.span);
286286
self.lower_attrs(hir_id, &e.attrs);
287-
hir::Expr { hir_id, kind, span: e.span }
287+
hir::Expr { hir_id, kind }
288288
})
289289
}
290290

@@ -535,8 +535,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
535535

536536
// Lower condition:
537537
let cond = self.with_loop_condition_scope(|this| this.lower_expr(cond));
538-
let span_block =
539-
self.mark_span_with_reason(DesugaringKind::CondTemporary, cond.span, None);
538+
let span_block = self.mark_span_with_reason(
539+
DesugaringKind::CondTemporary,
540+
self.spans[cond.hir_id],
541+
None,
542+
);
540543
// Wrap in a construct equivalent to `{ let _t = $cond; _t }`
541544
// to preserve drop semantics since `while cond { ... }` does not
542545
// let temporaries live outside of `cond`.
@@ -574,7 +577,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
574577
(
575578
this.mark_span_with_reason(
576579
DesugaringKind::TryBlock,
577-
expr.span,
580+
this.spans[expr.hir_id],
578581
this.allow_try_trait.clone(),
579582
),
580583
expr,
@@ -589,8 +592,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
589592
(try_span, this.expr_unit(try_span))
590593
};
591594

592-
let ok_wrapped_span =
593-
this.mark_span_with_reason(DesugaringKind::TryBlock, tail_expr.span, None);
595+
let ok_wrapped_span = this.mark_span_with_reason(
596+
DesugaringKind::TryBlock,
597+
this.spans[tail_expr.hir_id],
598+
None,
599+
);
594600

595601
// `::std::ops::Try::from_ok($tail_expr)`
596602
block.expr = Some(this.wrap_in_try_constructor(
@@ -692,11 +698,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
692698
span,
693699
Some(hir::Movability::Static),
694700
);
695-
let generator = hir::Expr {
696-
hir_id: self.lower_node_id(closure_node_id, span),
697-
kind: generator_kind,
698-
span,
699-
};
701+
let generator =
702+
hir::Expr { hir_id: self.lower_node_id(closure_node_id, span), kind: generator_kind };
700703

701704
// `future::from_generator`:
702705
let unstable_span =
@@ -849,7 +852,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
849852
let loop_expr = self.arena.alloc(hir::Expr {
850853
hir_id: loop_hir_id,
851854
kind: hir::ExprKind::Loop(loop_block, None, hir::LoopSource::Loop, span),
852-
span,
853855
});
854856

855857
// mut pinned => loop { ... }
@@ -1720,13 +1722,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
17201722
) -> hir::Expr<'hir> {
17211723
let orig_head_span = head.span;
17221724
// expand <head>
1723-
let mut head = self.lower_expr_mut(head);
1725+
let head = self.lower_expr_mut(head);
17241726
let desugared_span = self.mark_span_with_reason(
17251727
DesugaringKind::ForLoop(ForLoopLoc::Head),
17261728
orig_head_span,
17271729
None,
17281730
);
1729-
head.span = desugared_span;
17301731
self.spans[head.hir_id] = desugared_span;
17311732

17321733
let iter = Ident::with_dummy_span(sym::iter);
@@ -1818,11 +1819,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
18181819
hir::LoopSource::ForLoop,
18191820
e.span.with_hi(orig_head_span.hi()),
18201821
);
1821-
let loop_expr = self.arena.alloc(hir::Expr {
1822-
hir_id: self.lower_node_id(e.id, e.span),
1823-
kind,
1824-
span: e.span,
1825-
});
1822+
let loop_expr =
1823+
self.arena.alloc(hir::Expr { hir_id: self.lower_node_id(e.id, e.span), kind });
18261824

18271825
// `mut iter => { ... }`
18281826
let iter_arm = self.arm(iter_pat, loop_expr);
@@ -2118,7 +2116,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
21182116
}
21192117

21202118
fn expr_unsafe(&mut self, expr: &'hir hir::Expr<'hir>) -> hir::Expr<'hir> {
2121-
let span = expr.span;
2119+
let span = self.spans[expr.hir_id];
21222120
let hir_id = self.next_id(span);
21232121
self.expr(
21242122
span,
@@ -2159,14 +2157,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
21592157
) -> hir::Expr<'hir> {
21602158
let hir_id = self.next_id(span);
21612159
self.lower_attrs(hir_id, &attrs);
2162-
hir::Expr { hir_id, kind, span }
2160+
hir::Expr { hir_id, kind }
21632161
}
21642162

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

21692167
fn arm(&mut self, pat: &'hir hir::Pat<'hir>, expr: &'hir hir::Expr<'hir>) -> hir::Arm<'hir> {
2170-
hir::Arm { hir_id: self.next_id(expr.span), pat, guard: None, body: expr }
2168+
let span = self.spans[expr.hir_id];
2169+
hir::Arm { hir_id: self.next_id(span), pat, guard: None, body: expr }
21712170
}
21722171
}

compiler/rustc_ast_lowering/src/item.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1238,8 +1238,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
12381238
let user_body = this.lower_block_expr_opt(body_span, body);
12391239

12401240
// Transform into `drop-temps { <user-body> }`, an expression:
1241-
let desugared_span =
1242-
this.mark_span_with_reason(DesugaringKind::Async, user_body.span, None);
1241+
let desugared_span = this.mark_span_with_reason(
1242+
DesugaringKind::Async,
1243+
this.spans[user_body.hir_id],
1244+
None,
1245+
);
12431246
let user_body = this.expr_drop_temps(
12441247
desugared_span,
12451248
this.arena.alloc(user_body),

compiler/rustc_ast_lowering/src/lib.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
573573

574574
let module = self.lower_mod(&c.items, c.span);
575575
self.lower_attrs(hir::CRATE_HIR_ID, &c.attrs);
576-
let body_ids = body_ids(&self.bodies);
576+
let body_ids = body_ids(&self.bodies, &self.spans);
577577
let proc_macros =
578578
c.proc_macros.iter().map(|id| self.node_id_to_hir_id[*id].unwrap()).collect();
579579

@@ -2454,7 +2454,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24542454
fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst {
24552455
self.with_new_scopes(|this| {
24562456
let body = this.lower_const_body(c.value.span, Some(&c.value));
2457-
let span = this.bodies[&body].value.span;
2457+
let span = this.spans[this.bodies[&body].value.hir_id];
24582458
hir::AnonConst { hir_id: this.lower_node_id(c.id, span), body }
24592459
})
24602460
}
@@ -2569,7 +2569,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25692569
}
25702570

25712571
fn block_expr(&mut self, expr: &'hir hir::Expr<'hir>) -> &'hir hir::Block<'hir> {
2572-
self.block_all(expr.span, &[], Some(expr))
2572+
let span = self.spans[expr.hir_id];
2573+
self.block_all(span, &[], Some(expr))
25732574
}
25742575

25752576
fn block_all(
@@ -2868,11 +2869,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
28682869
}
28692870
}
28702871

2871-
fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body<'_>>) -> Vec<hir::BodyId> {
2872+
fn body_ids(
2873+
bodies: &BTreeMap<hir::BodyId, hir::Body<'_>>,
2874+
spans: &HirIdVec<Span>,
2875+
) -> Vec<hir::BodyId> {
28722876
// Sorting by span ensures that we get things in order within a
28732877
// file, and also puts the files in a sensible order.
28742878
let mut body_ids: Vec<_> = bodies.keys().cloned().collect();
2875-
body_ids.sort_by_key(|b| bodies[b].value.span);
2879+
body_ids.sort_by_key(|b| spans[bodies[b].value.hir_id]);
28762880
body_ids
28772881
}
28782882

compiler/rustc_hir/src/hir.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,6 @@ pub struct AnonConst {
14231423
pub struct Expr<'hir> {
14241424
pub hir_id: HirId,
14251425
pub kind: ExprKind<'hir>,
1426-
pub span: Span,
14271426
}
14281427

14291428
impl Expr<'_> {
@@ -3034,7 +3033,7 @@ impl<'hir> Node<'hir> {
30343033
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
30353034
mod size_asserts {
30363035
rustc_data_structures::static_assert_size!(super::Block<'static>, 40);
3037-
rustc_data_structures::static_assert_size!(super::Expr<'static>, 64);
3036+
rustc_data_structures::static_assert_size!(super::Expr<'static>, 56);
30383037
rustc_data_structures::static_assert_size!(super::Pat<'static>, 80);
30393038
rustc_data_structures::static_assert_size!(super::QPath<'static>, 24);
30403039
rustc_data_structures::static_assert_size!(super::Ty<'static>, 64);

compiler/rustc_hir_pretty/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ impl<'a> State<'a> {
363363
}
364364

365365
pub fn commasep_exprs(&mut self, b: Breaks, exprs: &[hir::Expr<'_>]) {
366-
self.commasep_cmnt(b, exprs, |s, e| s.print_expr(&e), |_, e| e.span)
366+
self.commasep_cmnt(b, exprs, |s, e| s.print_expr(&e), |s, e| s.span(e.hir_id))
367367
}
368368

369369
pub fn print_mod(&mut self, _mod: &hir::Mod<'_>, attrs: &[ast::Attribute]) {
@@ -1122,7 +1122,7 @@ impl<'a> State<'a> {
11221122
if let Some(ref expr) = blk.expr {
11231123
self.space_if_not_bol();
11241124
self.print_expr(&expr);
1125-
self.maybe_print_trailing_comment(expr.span, Some(span.hi()));
1125+
self.maybe_print_trailing_comment(self.span(expr.hir_id), Some(span.hi()));
11261126
}
11271127
self.bclose_maybe_open(span, close_box);
11281128
self.ann.post(self, AnnNode::Block(blk))
@@ -1368,7 +1368,7 @@ impl<'a> State<'a> {
13681368
}
13691369

13701370
pub fn print_expr(&mut self, expr: &hir::Expr<'_>) {
1371-
self.maybe_print_comment(expr.span.lo());
1371+
self.maybe_print_comment(self.span(expr.hir_id).lo());
13721372
self.print_outer_attributes(self.attrs(expr.hir_id));
13731373
self.ibox(INDENT_UNIT);
13741374
self.ann.pre(self, AnnNode::Expr(expr));
@@ -1439,7 +1439,7 @@ impl<'a> State<'a> {
14391439
self.print_ident(temp);
14401440

14411441
// Print `}`:
1442-
self.bclose_maybe_open(expr.span, true);
1442+
self.bclose_maybe_open(self.span(expr.hir_id), true);
14431443
}
14441444
hir::ExprKind::If(ref test, ref blk, ref elseopt) => {
14451445
self.print_if(&test, &blk, elseopt.as_ref().map(|e| &**e));
@@ -1463,7 +1463,7 @@ impl<'a> State<'a> {
14631463
for arm in arms {
14641464
self.print_arm(arm);
14651465
}
1466-
self.bclose(expr.span);
1466+
self.bclose(self.span(expr.hir_id));
14671467
}
14681468
hir::ExprKind::Closure(capture_clause, ref decl, body, _fn_decl_span, _gen) => {
14691469
self.print_capture_clause(capture_clause);

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ impl<'a, 'tcx> FindHirNodeVisitor<'a, 'tcx> {
7575
/// Determine whether the expression, assumed to be the callee within a `Call`,
7676
/// corresponds to the `From::from` emitted in desugaring of the `?` operator.
7777
fn is_try_conversion(&self, callee: &Expr<'tcx>) -> bool {
78-
self.infcx
79-
.trait_def_from_hir_fn(callee.hir_id)
80-
.map_or(false, |def_id| self.infcx.is_try_conversion(callee.span, def_id))
78+
self.infcx.trait_def_from_hir_fn(callee.hir_id).map_or(false, |def_id| {
79+
self.infcx.is_try_conversion(self.infcx.tcx.hir().span(callee.hir_id), def_id)
80+
})
8181
}
8282
}
8383

@@ -139,13 +139,16 @@ impl<'a, 'tcx> Visitor<'tcx> for FindHirNodeVisitor<'a, 'tcx> {
139139
// `From::from(e)` call emitted during desugaring of the `?` operator,
140140
// extract the types inferred before and after the call
141141
ExprKind::Call(callee, [arg])
142-
if self.target_span.contains(expr.span)
142+
if self.target_span.contains(self.infcx.tcx.hir().span(expr.hir_id))
143143
&& self.found_use_diagnostic.is_none()
144144
&& self.is_try_conversion(callee) =>
145145
{
146-
self.found_use_diagnostic = self.node_type_opt(arg.hir_id).map(|pre_ty| {
147-
UseDiagnostic::TryConversion { pre_ty, post_ty: ty, span: callee.span }
148-
});
146+
self.found_use_diagnostic =
147+
self.node_type_opt(arg.hir_id).map(|pre_ty| UseDiagnostic::TryConversion {
148+
pre_ty,
149+
post_ty: ty,
150+
span: self.infcx.tcx.hir().span(callee.hir_id),
151+
});
149152
}
150153
_ => {}
151154
}
@@ -230,7 +233,7 @@ fn closure_return_type_suggestion(
230233
}
231234
_ => vec![
232235
(output.span(|id| tcx.hir().span(id)), format!("{}{}{}{{ ", arrow, ret, post)),
233-
(body.value.span.shrink_to_hi(), " }".to_string()),
236+
(tcx.hir().span(body.value.hir_id).shrink_to_hi(), " }".to_string()),
234237
],
235238
};
236239
err.multipart_suggestion(
@@ -673,7 +676,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
673676
// | help: specify type like: `<Impl as Into<u32>>::into(foo_impl)`
674677
// |
675678
// = note: cannot satisfy `Impl: Into<_>`
676-
if !impl_candidates.is_empty() && e.span.contains(span) {
679+
let e_span = self.tcx.hir().span(e.hir_id);
680+
if !impl_candidates.is_empty() && e_span.contains(span) {
677681
if let Some(expr) = exprs.first() {
678682
if let ExprKind::Path(hir::QPath::Resolved(_, path)) = expr.kind {
679683
if let [path_segment] = path.segments {
@@ -685,7 +689,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
685689
)
686690
});
687691
err.span_suggestions(
688-
e.span,
692+
e_span,
689693
&format!(
690694
"use the fully qualified path for the potential candidate{}",
691695
pluralize!(candidate_len),
@@ -824,7 +828,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
824828
let sig = self.tcx.fn_sig(did);
825829
let bound_output = sig.output();
826830
let output = bound_output.skip_binder();
827-
err.span_label(e.span, &format!("this method call resolves to `{}`", output));
831+
err.span_label(
832+
self.tcx.hir().span(e.hir_id),
833+
&format!("this method call resolves to `{}`", output),
834+
);
828835
let kind = output.kind();
829836
if let ty::Projection(proj) = kind {
830837
if let Some(span) = self.tcx.hir().span_if_local(proj.item_def_id) {

compiler/rustc_lint/src/builtin.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,9 @@ impl<'tcx> LateLintPass<'tcx> for MutableTransmutes {
11981198
if to_mt == hir::Mutability::Mut && from_mt == hir::Mutability::Not {
11991199
let msg = "mutating transmuted &mut T from &T may cause undefined behavior, \
12001200
consider instead using an UnsafeCell";
1201-
cx.struct_span_lint(MUTABLE_TRANSMUTES, expr.span, |lint| lint.build(msg).emit());
1201+
cx.struct_span_lint(MUTABLE_TRANSMUTES, cx.tcx.hir().span(expr.hir_id), |lint| {
1202+
lint.build(msg).emit()
1203+
});
12021204
}
12031205
}
12041206

@@ -2555,7 +2557,8 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
25552557
if let Some((msg, span)) =
25562558
with_no_trimmed_paths(|| ty_find_init_error(cx.tcx, conjured_ty, init))
25572559
{
2558-
cx.struct_span_lint(INVALID_VALUE, expr.span, |lint| {
2560+
let expr_span = cx.tcx.hir().span(expr.hir_id);
2561+
cx.struct_span_lint(INVALID_VALUE, expr_span, |lint| {
25592562
let mut err = lint.build(&format!(
25602563
"the type `{}` does not permit {}",
25612564
conjured_ty,
@@ -2564,9 +2567,9 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
25642567
InitKind::Uninit => "being left uninitialized",
25652568
},
25662569
));
2567-
err.span_label(expr.span, "this code causes undefined behavior when executed");
2570+
err.span_label(expr_span, "this code causes undefined behavior when executed");
25682571
err.span_label(
2569-
expr.span,
2572+
expr_span,
25702573
"help: use `MaybeUninit<T>` instead, \
25712574
and only call `assume_init` after initialization is done",
25722575
);

0 commit comments

Comments
 (0)