about summary refs log tree commit diff
path: root/src/test/ui/borrowck
diff options
context:
space:
mode:
authorfee1-dead <ent3rm4n@gmail.com>2022-09-25 22:06:37 +0800
committerGitHub <noreply@github.com>2022-09-25 22:06:37 +0800
commit084029f39d0aba9d34188d512760d7ba324081b7 (patch)
tree98118d2942ead8151b4e2a531e8f042692ab70f5 /src/test/ui/borrowck
parent033f93fbb98c3fb9bd09858f5b822789a4339de8 (diff)
parent7e226e6d3f5dc5c39a222091f7a234c1480e1cbd (diff)
downloadrust-084029f39d0aba9d34188d512760d7ba324081b7.tar.gz
rust-084029f39d0aba9d34188d512760d7ba324081b7.zip
Rollup merge of #101431 - compiler-errors:move-place-ty-for-move-place-sugg, r=cjgillot
Look at move place's type when suggesting mutable reborrow

Not sure why we are looking at the use site's ty instead of the move site's ty in order to suggest reborrowing the move site, but it was suppressing a perfectly valid reborrow suggestion.

r? `@estebank` who i think touched this last in 520461f1fb2730f8edb17922f3bcc74fccdc52d3, though that was quite a while ago so feel free to reassign.
Diffstat (limited to 'src/test/ui/borrowck')
-rw-r--r--src/test/ui/borrowck/reborrow-sugg-move-then-borrow.rs26
-rw-r--r--src/test/ui/borrowck/reborrow-sugg-move-then-borrow.stderr24
2 files changed, 50 insertions, 0 deletions
diff --git a/src/test/ui/borrowck/reborrow-sugg-move-then-borrow.rs b/src/test/ui/borrowck/reborrow-sugg-move-then-borrow.rs
new file mode 100644
index 00000000000..31eba074008
--- /dev/null
+++ b/src/test/ui/borrowck/reborrow-sugg-move-then-borrow.rs
@@ -0,0 +1,26 @@
+// Tests the suggestion to reborrow the first move site
+// when we move then borrow a `&mut` ref.
+
+struct State;
+
+impl IntoIterator for &mut State {
+    type IntoIter = std::vec::IntoIter<()>;
+    type Item = ();
+
+    fn into_iter(self) -> Self::IntoIter {
+        vec![].into_iter()
+    }
+}
+
+fn once(f: impl FnOnce()) {}
+
+fn fill_memory_blocks_mt(state: &mut State) {
+    for _ in state {}
+    //~^ HELP consider creating a fresh reborrow of `state` here
+    fill_segment(state);
+    //~^ ERROR borrow of moved value: `state`
+}
+
+fn fill_segment(state: &mut State) {}
+
+fn main() {}
diff --git a/src/test/ui/borrowck/reborrow-sugg-move-then-borrow.stderr b/src/test/ui/borrowck/reborrow-sugg-move-then-borrow.stderr
new file mode 100644
index 00000000000..13a2005e2ef
--- /dev/null
+++ b/src/test/ui/borrowck/reborrow-sugg-move-then-borrow.stderr
@@ -0,0 +1,24 @@
+error[E0382]: borrow of moved value: `state`
+  --> $DIR/reborrow-sugg-move-then-borrow.rs:20:18
+   |
+LL | fn fill_memory_blocks_mt(state: &mut State) {
+   |                          ----- move occurs because `state` has type `&mut State`, which does not implement the `Copy` trait
+LL |     for _ in state {}
+   |              ----- `state` moved due to this implicit call to `.into_iter()`
+LL |
+LL |     fill_segment(state);
+   |                  ^^^^^ value borrowed here after move
+   |
+note: this function takes ownership of the receiver `self`, which moves `state`
+  --> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
+   |
+LL |     fn into_iter(self) -> Self::IntoIter;
+   |                  ^^^^
+help: consider creating a fresh reborrow of `state` here
+   |
+LL |     for _ in &mut *state {}
+   |              ++++++
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0382`.