Skip to content

Commit 060dba6

Browse files
committed
Add loop head span to hir
1 parent a701ff9 commit 060dba6

File tree

15 files changed

+35
-25
lines changed

15 files changed

+35
-25
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use rustc_errors::struct_span_err;
1010
use rustc_hir as hir;
1111
use rustc_hir::def::Res;
1212
use rustc_session::parse::feature_err;
13-
use rustc_span::hygiene::ForLoopLoc;
1413
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
1514
use rustc_span::symbol::{sym, Ident, Symbol};
15+
use rustc_span::{hygiene::ForLoopLoc, DUMMY_SP};
1616
use rustc_target::asm;
1717
use std::collections::hash_map::Entry;
1818
use std::fmt::Write;
@@ -102,6 +102,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
102102
this.lower_block(body, false),
103103
opt_label,
104104
hir::LoopSource::Loop,
105+
DUMMY_SP,
105106
)
106107
}),
107108
ExprKind::TryBlock(ref body) => self.lower_expr_try_block(body),
@@ -453,7 +454,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
453454
self.expr_match(span, scrutinee, arena_vec![self; then_arm, else_arm], desugar);
454455

455456
// `[opt_ident]: loop { ... }`
456-
hir::ExprKind::Loop(self.block_expr(self.arena.alloc(match_expr)), opt_label, source)
457+
hir::ExprKind::Loop(
458+
self.block_expr(self.arena.alloc(match_expr)),
459+
opt_label,
460+
source,
461+
span.with_hi(cond.span.hi()),
462+
)
457463
}
458464

459465
/// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::Try::from_ok(<expr>) }`,
@@ -748,7 +754,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
748754
// loop { .. }
749755
let loop_expr = self.arena.alloc(hir::Expr {
750756
hir_id: loop_hir_id,
751-
kind: hir::ExprKind::Loop(loop_block, None, hir::LoopSource::Loop),
757+
kind: hir::ExprKind::Loop(loop_block, None, hir::LoopSource::Loop, span),
752758
span,
753759
attrs: ThinVec::new(),
754760
});
@@ -1709,7 +1715,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
17091715
);
17101716

17111717
// `[opt_ident]: loop { ... }`
1712-
let kind = hir::ExprKind::Loop(loop_block, opt_label, hir::LoopSource::ForLoop);
1718+
let kind = hir::ExprKind::Loop(
1719+
loop_block,
1720+
opt_label,
1721+
hir::LoopSource::ForLoop,
1722+
e.span.with_hi(orig_head_span.hi()),
1723+
);
17131724
let loop_expr = self.arena.alloc(hir::Expr {
17141725
hir_id: self.lower_node_id(e.id),
17151726
kind,

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1617,7 +1617,9 @@ pub enum ExprKind<'hir> {
16171617
/// A conditionless loop (can be exited with `break`, `continue`, or `return`).
16181618
///
16191619
/// I.e., `'label: loop { <block> }`.
1620-
Loop(&'hir Block<'hir>, Option<Label>, LoopSource),
1620+
///
1621+
/// The `Span` is the loop header (`for x in y`/`while let pat = expr`).
1622+
Loop(&'hir Block<'hir>, Option<Label>, LoopSource, Span),
16211623
/// A `match` block, with a source that indicates whether or not it is
16221624
/// the result of a desugaring, and if so, which kind.
16231625
Match(&'hir Expr<'hir>, &'hir [Arm<'hir>], MatchSource),

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
11511151
visitor.visit_expr(then);
11521152
walk_list!(visitor, visit_expr, else_opt);
11531153
}
1154-
ExprKind::Loop(ref block, ref opt_label, _) => {
1154+
ExprKind::Loop(ref block, ref opt_label, _, _) => {
11551155
walk_list!(visitor, visit_label, opt_label);
11561156
visitor.visit_block(block);
11571157
}

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ impl<'a> State<'a> {
13961396
hir::ExprKind::If(ref test, ref blk, ref elseopt) => {
13971397
self.print_if(&test, &blk, elseopt.as_ref().map(|e| &**e));
13981398
}
1399-
hir::ExprKind::Loop(ref blk, opt_label, _) => {
1399+
hir::ExprKind::Loop(ref blk, opt_label, _, _) => {
14001400
if let Some(label) = opt_label {
14011401
self.print_ident(label.ident);
14021402
self.word_space(":");

compiler/rustc_mir_build/src/thir/cx/expr.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,7 @@ fn make_mirror_unadjusted<'a, 'tcx>(
546546
scrutinee: discr.to_ref(),
547547
arms: arms.iter().map(|a| convert_arm(cx, a)).collect(),
548548
},
549-
hir::ExprKind::Loop(ref body, _, _) => {
550-
ExprKind::Loop { body: block::to_expr_ref(cx, body) }
551-
}
549+
hir::ExprKind::Loop(ref body, ..) => ExprKind::Loop { body: block::to_expr_ref(cx, body) },
552550
hir::ExprKind::Field(ref source, ..) => ExprKind::Field {
553551
lhs: source.to_ref(),
554552
name: Field::new(cx.tcx.field_index(expr.hir_id, cx.typeck_results)),

compiler/rustc_passes/src/check_const.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl<'tcx> Visitor<'tcx> for CheckConstVisitor<'tcx> {
199199
// Skip the following checks if we are not currently in a const context.
200200
_ if self.const_kind.is_none() => {}
201201

202-
hir::ExprKind::Loop(_, _, source) => {
202+
hir::ExprKind::Loop(_, _, source, _) => {
203203
self.const_check_violated(NonConstExpr::Loop(*source), e.span);
204204
}
205205

compiler/rustc_passes/src/liveness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
844844

845845
// Note that labels have been resolved, so we don't need to look
846846
// at the label ident
847-
hir::ExprKind::Loop(ref blk, _, _) => self.propagate_through_loop(expr, &blk, succ),
847+
hir::ExprKind::Loop(ref blk, ..) => self.propagate_through_loop(expr, &blk, succ),
848848

849849
hir::ExprKind::If(ref cond, ref then, ref else_opt) => {
850850
//

compiler/rustc_passes/src/loops.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
5353

5454
fn visit_expr(&mut self, e: &'hir hir::Expr<'hir>) {
5555
match e.kind {
56-
hir::ExprKind::Loop(ref b, _, source) => {
56+
hir::ExprKind::Loop(ref b, _, source, _) => {
5757
self.with_context(Loop(source), |v| v.visit_block(&b));
5858
}
5959
hir::ExprKind::Closure(_, ref function_decl, b, span, movability) => {
@@ -89,8 +89,7 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
8989
Err(hir::LoopIdError::UnresolvedLabel) => None,
9090
};
9191

92-
if let Some(loop_id) = loop_id {
93-
if let Node::Block(_) = self.hir_map.find(loop_id).unwrap() {
92+
if let Some(Node::Block(_)) = loop_id.and_then(|id| self.hir_map.find(id)) {
9493
return;
9594
}
9695
}

compiler/rustc_passes/src/region.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx h
252252
terminating(then.hir_id.local_id);
253253
}
254254

255-
hir::ExprKind::Loop(ref body, _, _) => {
255+
hir::ExprKind::Loop(ref body, _, _, _) => {
256256
terminating(body.hir_id.local_id);
257257
}
258258

compiler/rustc_resolve/src/late/lifetimes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ fn extract_labels(ctxt: &mut LifetimeContext<'_, '_>, body: &hir::Body<'_>) {
11731173
}
11741174

11751175
fn expression_label(ex: &hir::Expr<'_>) -> Option<Ident> {
1176-
if let hir::ExprKind::Loop(_, Some(label), _) = ex.kind { Some(label.ident) } else { None }
1176+
if let hir::ExprKind::Loop(_, Some(label), ..) = ex.kind { Some(label.ident) } else { None }
11771177
}
11781178

11791179
fn check_if_label_shadows_lifetime(tcx: TyCtxt<'_>, mut scope: ScopeRef<'_>, label: Ident) {

compiler/rustc_typeck/src/check/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
266266
}
267267
}
268268
ExprKind::Ret(ref expr_opt) => self.check_expr_return(expr_opt.as_deref(), expr),
269-
ExprKind::Loop(ref body, _, source) => {
269+
ExprKind::Loop(ref body, _, source, _) => {
270270
self.check_expr_loop(body, source, expected, expr)
271271
}
272272
ExprKind::Match(ref discrim, ref arms, match_src) => {

compiler/rustc_typeck/src/expr_use_visitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
289289
| hir::ExprKind::ConstBlock(..)
290290
| hir::ExprKind::Err => {}
291291

292-
hir::ExprKind::Loop(ref blk, _, _) => {
292+
hir::ExprKind::Loop(ref blk, ..) => {
293293
self.walk_block(blk);
294294
}
295295

src/tools/clippy/clippy_lints/src/loops.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ impl<'tcx> LateLintPass<'tcx> for Loops {
533533
}
534534

535535
// check for never_loop
536-
if let ExprKind::Loop(ref block, _, _) = expr.kind {
536+
if let ExprKind::Loop(ref block, _, _, _) = expr.kind {
537537
match never_loop_block(block, expr.hir_id) {
538538
NeverLoopResult::AlwaysBreak => span_lint(cx, NEVER_LOOP, expr.span, "this loop never actually loops"),
539539
NeverLoopResult::MayContinueMainLoop | NeverLoopResult::Otherwise => (),
@@ -543,7 +543,7 @@ impl<'tcx> LateLintPass<'tcx> for Loops {
543543
// check for `loop { if let {} else break }` that could be `while let`
544544
// (also matches an explicit "match" instead of "if let")
545545
// (even if the "match" or "if let" is used for declaration)
546-
if let ExprKind::Loop(ref block, _, LoopSource::Loop) = expr.kind {
546+
if let ExprKind::Loop(ref block, _, LoopSource::Loop, _) = expr.kind {
547547
// also check for empty `loop {}` statements, skipping those in #[panic_handler]
548548
if block.stmts.is_empty() && block.expr.is_none() && !is_in_panic_handler(cx, expr) {
549549
let msg = "empty `loop {}` wastes CPU cycles";
@@ -738,7 +738,7 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult {
738738
| ExprKind::Assign(ref e1, ref e2, _)
739739
| ExprKind::AssignOp(_, ref e1, ref e2)
740740
| ExprKind::Index(ref e1, ref e2) => never_loop_expr_all(&mut [&**e1, &**e2].iter().cloned(), main_loop_id),
741-
ExprKind::Loop(ref b, _, _) => {
741+
ExprKind::Loop(ref b, _, _, _) => {
742742
// Break can come from the inner loop so remove them.
743743
absorb_break(&never_loop_block(b, main_loop_id))
744744
},
@@ -1314,7 +1314,7 @@ impl<'a, 'tcx> Visitor<'tcx> for SameItemPushVisitor<'a, 'tcx> {
13141314
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
13151315
match &expr.kind {
13161316
// Non-determinism may occur ... don't give a lint
1317-
ExprKind::Loop(_, _, _) | ExprKind::Match(_, _, _) => self.should_lint = false,
1317+
ExprKind::Loop(..) | ExprKind::Match(..) => self.should_lint = false,
13181318
ExprKind::Block(block, _) => self.visit_block(block),
13191319
_ => {},
13201320
}

src/tools/clippy/clippy_lints/src/needless_continue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ where
221221
{
222222
if let ast::ExprKind::While(_, loop_block, label)
223223
| ast::ExprKind::ForLoop(_, _, loop_block, label)
224-
| ast::ExprKind::Loop(loop_block, label) = &expr.kind
224+
| ast::ExprKind::Loop(loop_block, label, _) = &expr.kind
225225
{
226226
func(loop_block, label.as_ref());
227227
}

src/tools/clippy/clippy_lints/src/shadow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ fn check_expr<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, bindings: &mut
325325
| ExprKind::Field(ref e, _)
326326
| ExprKind::AddrOf(_, _, ref e)
327327
| ExprKind::Box(ref e) => check_expr(cx, e, bindings),
328-
ExprKind::Block(ref block, _) | ExprKind::Loop(ref block, _, _) => check_block(cx, block, bindings),
328+
ExprKind::Block(ref block, _) | ExprKind::Loop(ref block, ..) => check_block(cx, block, bindings),
329329
// ExprKind::Call
330330
// ExprKind::MethodCall
331331
ExprKind::Array(v) | ExprKind::Tup(v) => {

0 commit comments

Comments
 (0)