Skip to content

Commit fe1a7f7

Browse files
committed
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.
1 parent 9583fd1 commit fe1a7f7

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1401,8 +1401,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
14011401
)
14021402
};
14031403

1404+
// #82462: to correctly diagnose borrow errors, the block that contains
1405+
// the iter expr needs to have a span that covers the loop body.
1406+
let desugared_full_span =
1407+
self.mark_span_with_reason(DesugaringKind::ForLoop(ForLoopLoc::Head), e.span, None);
1408+
14041409
let match_expr = self.arena.alloc(self.expr_match(
1405-
desugared_span,
1410+
desugared_full_span,
14061411
into_iter_expr,
14071412
arena_vec![self; iter_arm],
14081413
hir::MatchSource::ForLoopDesugar,
@@ -1416,7 +1421,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14161421
// surrounding scope of the `match` since the `match` is not a terminating scope.
14171422
//
14181423
// Also, add the attributes to the outer returned expr node.
1419-
self.expr_drop_temps_mut(desugared_span, match_expr, attrs.into())
1424+
self.expr_drop_temps_mut(desugared_full_span, match_expr, attrs.into())
14201425
}
14211426

14221427
/// Desugar `ExprKind::Try` from: `<expr>?` into:

src/test/mir-opt/remove_storage_markers.main.RemoveStorageMarkers.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
- StorageDead(_7); // scope 3 at $DIR/remove_storage_markers.rs:8:18: 8:19
8181
- StorageDead(_6); // scope 2 at $DIR/remove_storage_markers.rs:10:5: 10:6
8282
- StorageDead(_4); // scope 1 at $DIR/remove_storage_markers.rs:10:5: 10:6
83-
- StorageDead(_2); // scope 1 at $DIR/remove_storage_markers.rs:8:18: 8:19
83+
- StorageDead(_2); // scope 1 at $DIR/remove_storage_markers.rs:10:5: 10:6
8484
- StorageDead(_1); // scope 0 at $DIR/remove_storage_markers.rs:11:1: 11:2
8585
return; // scope 0 at $DIR/remove_storage_markers.rs:11:2: 11:2
8686
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
2+
--> $DIR/issue-82462.rs:18:9
3+
|
4+
LL | for x in DroppingSlice(&*v).iter() {
5+
| ------------------
6+
| | |
7+
| | immutable borrow occurs here
8+
| a temporary with access to the immutable borrow is created here ...
9+
LL | v.push(*x);
10+
| ^ mutable borrow occurs here
11+
LL | break;
12+
LL | }
13+
| - ... and the immutable borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `DroppingSlice`
14+
|
15+
help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
16+
|
17+
LL | };
18+
| +
19+
20+
error: aborting due to previous error
21+
22+
For more information about this error, try `rustc --explain E0502`.

src/test/ui/borrowck/issue-82462.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
struct DroppingSlice<'a>(&'a [i32]);
2+
3+
impl Drop for DroppingSlice<'_> {
4+
fn drop(&mut self) {
5+
println!("hi from slice");
6+
}
7+
}
8+
9+
impl DroppingSlice<'_> {
10+
fn iter(&self) -> std::slice::Iter<'_, i32> {
11+
self.0.iter()
12+
}
13+
}
14+
15+
fn main() {
16+
let mut v = vec![1, 2, 3, 4];
17+
for x in DroppingSlice(&*v).iter() {
18+
v.push(*x); //~ERROR
19+
break;
20+
}
21+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
2+
--> $DIR/issue-82462.rs:18:9
3+
|
4+
LL | for x in DroppingSlice(&*v).iter() {
5+
| ------------------
6+
| | |
7+
| | immutable borrow occurs here
8+
| a temporary with access to the immutable borrow is created here ...
9+
LL | v.push(*x);
10+
| ^^^^^^^^^^ mutable borrow occurs here
11+
LL | break;
12+
LL | }
13+
| - ... and the immutable borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `DroppingSlice`
14+
|
15+
help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
16+
|
17+
LL | };
18+
| +
19+
20+
error: aborting due to previous error
21+
22+
For more information about this error, try `rustc --explain E0502`.

0 commit comments

Comments
 (0)