-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #110061 - WaffleLapkin:duality_of_myself_and_this, r=cj…
…gillot Add suggestion to use closure argument instead of a capture on borrowck error Fixes #109271 r? `@compiler-errors` This should probably be refined a bit, but opening a PR so that I don't forget anything.
- Loading branch information
Showing
24 changed files
with
425 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
tests/ui/borrowck/issue-109271-pass-self-into-closure.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// run-rustfix | ||
#![allow(unused)] | ||
struct S; | ||
|
||
impl S { | ||
fn call(&mut self, f: impl FnOnce((), &mut Self)) { | ||
// change state or something ... | ||
f((), self); | ||
// change state or something ... | ||
} | ||
|
||
fn get(&self) {} | ||
fn set(&mut self) {} | ||
} | ||
|
||
fn main() { | ||
let mut v = S; | ||
|
||
v.call(|(), this: &mut S| this.get()); | ||
//~^ error: cannot borrow `v` as mutable because it is also borrowed as immutable | ||
v.call(|(), this: &mut S| this.set()); | ||
//~^ error: cannot borrow `v` as mutable more than once at a time | ||
//~| error: cannot borrow `v` as mutable more than once at a time | ||
|
||
v.call(|(), this: &mut S| { | ||
//~^ error: cannot borrow `v` as mutable more than once at a time | ||
//~| error: cannot borrow `v` as mutable more than once at a time | ||
|
||
_ = this; | ||
this.set(); | ||
this.get(); | ||
S::get(&this); | ||
|
||
use std::ops::Add; | ||
let v = 0u32; | ||
_ = v + v; | ||
_ = v.add(3); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// run-rustfix | ||
#![allow(unused)] | ||
struct S; | ||
|
||
impl S { | ||
fn call(&mut self, f: impl FnOnce((), &mut Self)) { | ||
// change state or something ... | ||
f((), self); | ||
// change state or something ... | ||
} | ||
|
||
fn get(&self) {} | ||
fn set(&mut self) {} | ||
} | ||
|
||
fn main() { | ||
let mut v = S; | ||
|
||
v.call(|(), this: &mut S| v.get()); | ||
//~^ error: cannot borrow `v` as mutable because it is also borrowed as immutable | ||
v.call(|(), this: &mut S| v.set()); | ||
//~^ error: cannot borrow `v` as mutable more than once at a time | ||
//~| error: cannot borrow `v` as mutable more than once at a time | ||
|
||
v.call(|(), this: &mut S| { | ||
//~^ error: cannot borrow `v` as mutable more than once at a time | ||
//~| error: cannot borrow `v` as mutable more than once at a time | ||
|
||
_ = v; | ||
v.set(); | ||
v.get(); | ||
S::get(&v); | ||
|
||
use std::ops::Add; | ||
let v = 0u32; | ||
_ = v + v; | ||
_ = v.add(3); | ||
}); | ||
} |
Oops, something went wrong.