rustc: use more correct span data in for loop desugaring

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:
Michael Howell 2021-08-21 12:51:34 -07:00
parent 9583fd1bdd
commit fe1a7f71fb
5 changed files with 73 additions and 3 deletions

View File

@ -1401,8 +1401,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,
@ -1416,7 +1421,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:

View File

@ -80,7 +80,7 @@
- StorageDead(_7); // scope 3 at $DIR/remove_storage_markers.rs:8:18: 8:19
- StorageDead(_6); // scope 2 at $DIR/remove_storage_markers.rs:10:5: 10:6
- StorageDead(_4); // scope 1 at $DIR/remove_storage_markers.rs:10:5: 10:6
- StorageDead(_2); // scope 1 at $DIR/remove_storage_markers.rs:8:18: 8:19
- StorageDead(_2); // scope 1 at $DIR/remove_storage_markers.rs:10:5: 10:6
- StorageDead(_1); // scope 0 at $DIR/remove_storage_markers.rs:11:1: 11:2
return; // scope 0 at $DIR/remove_storage_markers.rs:11:2: 11:2
}

View File

@ -0,0 +1,22 @@
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
--> $DIR/issue-82462.rs:18:9
|
LL | for x in DroppingSlice(&*v).iter() {
| ------------------
| | |
| | immutable borrow occurs here
| a temporary with access to the immutable borrow is created here ...
LL | v.push(*x);
| ^ mutable borrow occurs here
LL | break;
LL | }
| - ... and the immutable borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `DroppingSlice`
|
help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
|
LL | };
| +
error: aborting due to previous error
For more information about this error, try `rustc --explain E0502`.

View File

@ -0,0 +1,21 @@
struct DroppingSlice<'a>(&'a [i32]);
impl Drop for DroppingSlice<'_> {
fn drop(&mut self) {
println!("hi from slice");
}
}
impl DroppingSlice<'_> {
fn iter(&self) -> std::slice::Iter<'_, i32> {
self.0.iter()
}
}
fn main() {
let mut v = vec![1, 2, 3, 4];
for x in DroppingSlice(&*v).iter() {
v.push(*x); //~ERROR
break;
}
}

View File

@ -0,0 +1,22 @@
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
--> $DIR/issue-82462.rs:18:9
|
LL | for x in DroppingSlice(&*v).iter() {
| ------------------
| | |
| | immutable borrow occurs here
| a temporary with access to the immutable borrow is created here ...
LL | v.push(*x);
| ^^^^^^^^^^ mutable borrow occurs here
LL | break;
LL | }
| - ... and the immutable borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `DroppingSlice`
|
help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
|
LL | };
| +
error: aborting due to previous error
For more information about this error, try `rustc --explain E0502`.