Skip to content

Commit a697aa6

Browse files
committed
Use more lowered spans in for loop
1 parent bd1a1e4 commit a697aa6

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13351335
let head = self.lower_expr_mut(head);
13361336
let desugared_span =
13371337
self.mark_span_with_reason(DesugaringKind::ForLoop(ForLoopLoc::Head), head.span, None);
1338+
let e_span = self.lower_span(e.span);
13381339

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

@@ -1348,23 +1349,24 @@ impl<'hir> LoweringContext<'_, 'hir> {
13481349
// `::std::option::Option::Some(val) => __next = val`
13491350
let pat_arm = {
13501351
let val_ident = Ident::with_dummy_span(sym::val);
1351-
let (val_pat, val_pat_hid) = self.pat_ident(pat.span, val_ident);
1352-
let val_expr = self.expr_ident(pat.span, val_ident, val_pat_hid);
1353-
let next_expr = self.expr_ident(pat.span, next_ident, next_pat_hid);
1352+
let pat_span = self.lower_span(pat.span);
1353+
let (val_pat, val_pat_hid) = self.pat_ident(pat_span, val_ident);
1354+
let val_expr = self.expr_ident(pat_span, val_ident, val_pat_hid);
1355+
let next_expr = self.expr_ident(pat_span, next_ident, next_pat_hid);
13541356
let assign = self.arena.alloc(self.expr(
1355-
pat.span,
1356-
hir::ExprKind::Assign(next_expr, val_expr, self.lower_span(pat.span)),
1357+
pat_span,
1358+
hir::ExprKind::Assign(next_expr, val_expr, self.lower_span(pat_span)),
13571359
ThinVec::new(),
13581360
));
1359-
let some_pat = self.pat_some(pat.span, val_pat);
1361+
let some_pat = self.pat_some(pat_span, val_pat);
13601362
self.arm(some_pat, assign)
13611363
};
13621364

13631365
// `::std::option::Option::None => break`
13641366
let break_arm = {
13651367
let break_expr =
1366-
self.with_loop_scope(e.id, |this| this.expr_break_alloc(e.span, ThinVec::new()));
1367-
let pat = self.pat_none(e.span);
1368+
self.with_loop_scope(e.id, |this| this.expr_break_alloc(e_span, ThinVec::new()));
1369+
let pat = self.pat_none(e_span);
13681370
self.arm(pat, break_expr)
13691371
};
13701372

@@ -1410,10 +1412,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
14101412

14111413
let body_block = self.with_loop_scope(e.id, |this| this.lower_block(body, false));
14121414
let body_expr = self.expr_block(body_block, ThinVec::new());
1413-
let body_stmt = self.stmt_expr(body.span, body_expr);
1415+
let body_stmt = self.stmt_expr(body_block.span, body_expr);
14141416

14151417
let loop_block = self.block_all(
1416-
e.span,
1418+
e_span,
14171419
arena_vec![self; next_let, match_stmt, pat_let, body_stmt],
14181420
None,
14191421
);
@@ -1423,7 +1425,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14231425
loop_block,
14241426
self.lower_label(opt_label),
14251427
hir::LoopSource::ForLoop,
1426-
self.lower_span(e.span.with_hi(head.span.hi())),
1428+
self.lower_span(e_span.with_hi(head.span.hi())),
14271429
);
14281430
let loop_expr = self.arena.alloc(hir::Expr {
14291431
hir_id: self.lower_node_id(e.id),
@@ -1452,7 +1454,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14521454
// #82462: to correctly diagnose borrow errors, the block that contains
14531455
// the iter expr needs to have a span that covers the loop body.
14541456
let desugared_full_span =
1455-
self.mark_span_with_reason(DesugaringKind::ForLoop(ForLoopLoc::Head), e.span, None);
1457+
self.mark_span_with_reason(DesugaringKind::ForLoop(ForLoopLoc::Head), e_span, None);
14561458

14571459
let match_expr = self.arena.alloc(self.expr_match(
14581460
desugared_full_span,

src/test/incremental/hashes/for_loops.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub fn change_iterable() {
108108
#[cfg(not(any(cfail1,cfail4)))]
109109
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes, promoted_mir")]
110110
#[rustc_clean(cfg="cfail3")]
111-
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes, promoted_mir, optimized_mir")]
111+
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes, promoted_mir")]
112112
#[rustc_clean(cfg="cfail6")]
113113
pub fn change_iterable() {
114114
let mut _x = 0;
@@ -183,7 +183,7 @@ pub fn add_loop_label_to_break() {
183183
#[cfg(not(any(cfail1,cfail4)))]
184184
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes")]
185185
#[rustc_clean(cfg="cfail3")]
186-
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes, optimized_mir")]
186+
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes")]
187187
#[rustc_clean(cfg="cfail6")]
188188
pub fn add_loop_label_to_break() {
189189
let mut _x = 0;
@@ -237,7 +237,7 @@ pub fn add_loop_label_to_continue() {
237237
#[cfg(not(any(cfail1,cfail4)))]
238238
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes")]
239239
#[rustc_clean(cfg="cfail3")]
240-
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes, optimized_mir")]
240+
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes")]
241241
#[rustc_clean(cfg="cfail6")]
242242
pub fn add_loop_label_to_continue() {
243243
let mut _x = 0;

0 commit comments

Comments
 (0)