Skip to content

Commit 1866ea1

Browse files
authored
Rollup merge of #108780 - Zeegomo:close-70919, r=WaffleLapkin
Add regression tests for issue 70919 Desugaring DropAndReplace at MIR build (#107844) fixed #70919. Add regressions tests, borrowed from #102078, to ensure we check for this in the future. cc ``@Aaron1011``
2 parents 6240b54 + beebd3a commit 1866ea1

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

tests/ui/borrowck/drop-in-loop.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// A version of `issue-70919-drop-in-loop`, but without
2+
// the necessary `drop` call.
3+
//
4+
// This should fail to compile, since the `Drop` impl
5+
// for `WrapperWithDrop` could observe the changed
6+
// `base` value.
7+
8+
struct WrapperWithDrop<'a>(&'a mut bool);
9+
impl<'a> Drop for WrapperWithDrop<'a> {
10+
fn drop(&mut self) {
11+
}
12+
}
13+
14+
fn drop_in_loop() {
15+
let mut base = true;
16+
let mut wrapper = WrapperWithDrop(&mut base);
17+
loop {
18+
base = false; //~ ERROR: cannot assign to `base`
19+
wrapper = WrapperWithDrop(&mut base);
20+
}
21+
}
22+
23+
fn main() {
24+
}

tests/ui/borrowck/drop-in-loop.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0506]: cannot assign to `base` because it is borrowed
2+
--> $DIR/drop-in-loop.rs:18:9
3+
|
4+
LL | let mut wrapper = WrapperWithDrop(&mut base);
5+
| --------- `base` is borrowed here
6+
LL | loop {
7+
LL | base = false;
8+
| ^^^^^^^^^^^^ `base` is assigned to here but it was already borrowed
9+
LL | wrapper = WrapperWithDrop(&mut base);
10+
| ------- borrow might be used here, when `wrapper` is dropped and runs the `Drop` code for type `WrapperWithDrop`
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0506`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Regression test for issue #70919
2+
// Tests that we don't emit a spurious "borrow might be used" error
3+
// when we have an explicit `drop` in a loop
4+
5+
// check-pass
6+
7+
struct WrapperWithDrop<'a>(&'a mut bool);
8+
impl<'a> Drop for WrapperWithDrop<'a> {
9+
fn drop(&mut self) {
10+
}
11+
}
12+
13+
fn drop_in_loop() {
14+
let mut base = true;
15+
let mut wrapper = WrapperWithDrop(&mut base);
16+
loop {
17+
drop(wrapper);
18+
19+
base = false;
20+
wrapper = WrapperWithDrop(&mut base);
21+
}
22+
}
23+
24+
fn main() {
25+
}

0 commit comments

Comments
 (0)