Auto merge of #88214 - notriddle:notriddle/for-loop-span-drop-temps-mut, r=nagisa

rustc: use more correct span data in for loop desugaring

Fixes #82462

Before:

      help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
         |
      LL |     for x in DroppingSlice(&*v).iter(); {
         |                                       +

After:

      help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
         |
      LL |     };
         |      +

This seems like a reasonable fix: since the desugared "expr_drop_temps_mut" contains the entire desugared loop construct, its span should contain the entire loop construct as well.
This commit is contained in:
bors 2021-09-11 07:11:01 +00:00
commit 4e880f8cbc
9 changed files with 82 additions and 13 deletions

View file

@ -1450,8 +1450,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
)
};
// #82462: to correctly diagnose borrow errors, the block that contains
// the iter expr needs to have a span that covers the loop body.
let desugared_full_span =
self.mark_span_with_reason(DesugaringKind::ForLoop(ForLoopLoc::Head), e.span, None);
let match_expr = self.arena.alloc(self.expr_match(
desugared_span,
desugared_full_span,
into_iter_expr,
arena_vec![self; iter_arm],
hir::MatchSource::ForLoopDesugar,
@ -1465,7 +1470,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// surrounding scope of the `match` since the `match` is not a terminating scope.
//
// Also, add the attributes to the outer returned expr node.
self.expr_drop_temps_mut(desugared_span, match_expr, attrs.into())
self.expr_drop_temps_mut(desugared_full_span, match_expr, attrs.into())
}
/// Desugar `ExprKind::Try` from: `<expr>?` into: