about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-03-06 16:41:57 +0100
committerGitHub <noreply@github.com>2023-03-06 16:41:57 +0100
commit1866ea136ce36ee780e1d59bcba9b34f7fd6f13d (patch)
treef8344df36fcc0f63b20a875a4f83a822f83e17cf
parent6240b5496602ba10f28a974d26b7421d4178627f (diff)
parentbeebd3a4c6ab38a71d4ff7cafcd1bf37def5b721 (diff)
downloadrust-1866ea136ce36ee780e1d59bcba9b34f7fd6f13d.tar.gz
rust-1866ea136ce36ee780e1d59bcba9b34f7fd6f13d.zip
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``
-rw-r--r--tests/ui/borrowck/drop-in-loop.rs24
-rw-r--r--tests/ui/borrowck/drop-in-loop.stderr14
-rw-r--r--tests/ui/borrowck/issue-70919-drop-in-loop.rs25
3 files changed, 63 insertions, 0 deletions
diff --git a/tests/ui/borrowck/drop-in-loop.rs b/tests/ui/borrowck/drop-in-loop.rs
new file mode 100644
index 00000000000..866c27ef203
--- /dev/null
+++ b/tests/ui/borrowck/drop-in-loop.rs
@@ -0,0 +1,24 @@
+// A version of `issue-70919-drop-in-loop`, but without
+// the necessary `drop` call.
+//
+// This should fail to compile, since the `Drop` impl
+// for `WrapperWithDrop` could observe the changed
+// `base` value.
+
+struct WrapperWithDrop<'a>(&'a mut bool);
+impl<'a> Drop for WrapperWithDrop<'a> {
+    fn drop(&mut self) {
+    }
+}
+
+fn drop_in_loop() {
+    let mut base = true;
+    let mut wrapper = WrapperWithDrop(&mut base);
+    loop {
+        base = false; //~ ERROR: cannot assign to `base`
+        wrapper = WrapperWithDrop(&mut base);
+    }
+}
+
+fn main() {
+}
diff --git a/tests/ui/borrowck/drop-in-loop.stderr b/tests/ui/borrowck/drop-in-loop.stderr
new file mode 100644
index 00000000000..d5734e7ec97
--- /dev/null
+++ b/tests/ui/borrowck/drop-in-loop.stderr
@@ -0,0 +1,14 @@
+error[E0506]: cannot assign to `base` because it is borrowed
+  --> $DIR/drop-in-loop.rs:18:9
+   |
+LL |     let mut wrapper = WrapperWithDrop(&mut base);
+   |                                       --------- `base` is borrowed here
+LL |     loop {
+LL |         base = false;
+   |         ^^^^^^^^^^^^ `base` is assigned to here but it was already borrowed
+LL |         wrapper = WrapperWithDrop(&mut base);
+   |         ------- borrow might be used here, when `wrapper` is dropped and runs the `Drop` code for type `WrapperWithDrop`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0506`.
diff --git a/tests/ui/borrowck/issue-70919-drop-in-loop.rs b/tests/ui/borrowck/issue-70919-drop-in-loop.rs
new file mode 100644
index 00000000000..a8d5849a31c
--- /dev/null
+++ b/tests/ui/borrowck/issue-70919-drop-in-loop.rs
@@ -0,0 +1,25 @@
+// Regression test for issue #70919
+// Tests that we don't emit a spurious "borrow might be used" error
+// when we have an explicit `drop` in a loop
+
+// check-pass
+
+struct WrapperWithDrop<'a>(&'a mut bool);
+impl<'a> Drop for WrapperWithDrop<'a> {
+    fn drop(&mut self) {
+    }
+}
+
+fn drop_in_loop() {
+    let mut base = true;
+    let mut wrapper = WrapperWithDrop(&mut base);
+    loop {
+        drop(wrapper);
+
+        base = false;
+        wrapper = WrapperWithDrop(&mut base);
+    }
+}
+
+fn main() {
+}