Point out implicit deref coercions in borrow
Clean up code
This commit is contained in:
parent
0e63af5da3
commit
b2eed3a559
16 changed files with 375 additions and 4 deletions
30
src/test/ui/borrowck/issue-81365-2.rs
Normal file
30
src/test/ui/borrowck/issue-81365-2.rs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
use std::ops::Deref;
|
||||
|
||||
struct DerefTarget {
|
||||
target_field: bool,
|
||||
}
|
||||
struct Container {
|
||||
target: DerefTarget,
|
||||
container_field: bool,
|
||||
}
|
||||
|
||||
impl Deref for Container {
|
||||
type Target = DerefTarget;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.target
|
||||
}
|
||||
}
|
||||
|
||||
struct Outer {
|
||||
container: Container,
|
||||
}
|
||||
|
||||
impl Outer {
|
||||
fn bad_borrow(&mut self) {
|
||||
let first = &self.container.target_field;
|
||||
self.container.container_field = true; //~ ERROR E0506
|
||||
first;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
15
src/test/ui/borrowck/issue-81365-2.stderr
Normal file
15
src/test/ui/borrowck/issue-81365-2.stderr
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
error[E0506]: cannot assign to `self.container.container_field` because it is borrowed
|
||||
--> $DIR/issue-81365-2.rs:25:9
|
||||
|
|
||||
LL | let first = &self.container.target_field;
|
||||
| -------------- borrow of `self.container.container_field` occurs here
|
||||
LL | self.container.container_field = true;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `self.container.container_field` occurs here
|
||||
LL | first;
|
||||
| ----- borrow later used here
|
||||
|
|
||||
= note: borrow occurs due to deref coercion to `DerefTarget`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0506`.
|
||||
37
src/test/ui/borrowck/issue-81365-3.rs
Normal file
37
src/test/ui/borrowck/issue-81365-3.rs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
use std::ops::Deref;
|
||||
|
||||
struct DerefTarget {
|
||||
target_field: bool,
|
||||
}
|
||||
struct Container {
|
||||
target: DerefTarget,
|
||||
container_field: bool,
|
||||
}
|
||||
|
||||
impl Deref for Container {
|
||||
type Target = DerefTarget;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.target
|
||||
}
|
||||
}
|
||||
|
||||
struct Outer {
|
||||
container: Container,
|
||||
}
|
||||
|
||||
impl Deref for Outer {
|
||||
type Target = Container;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.container
|
||||
}
|
||||
}
|
||||
|
||||
impl Outer {
|
||||
fn bad_borrow(&mut self) {
|
||||
let first = &self.target_field;
|
||||
self.container.container_field = true; //~ ERROR E0506
|
||||
first;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
15
src/test/ui/borrowck/issue-81365-3.stderr
Normal file
15
src/test/ui/borrowck/issue-81365-3.stderr
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
error[E0506]: cannot assign to `self.container.container_field` because it is borrowed
|
||||
--> $DIR/issue-81365-3.rs:32:9
|
||||
|
|
||||
LL | let first = &self.target_field;
|
||||
| ---- borrow of `self.container.container_field` occurs here
|
||||
LL | self.container.container_field = true;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `self.container.container_field` occurs here
|
||||
LL | first;
|
||||
| ----- borrow later used here
|
||||
|
|
||||
= note: borrow occurs due to deref coercion to `Container`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0506`.
|
||||
38
src/test/ui/borrowck/issue-81365-4.rs
Normal file
38
src/test/ui/borrowck/issue-81365-4.rs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
use std::ops::Deref;
|
||||
|
||||
struct DerefTarget {
|
||||
target_field: bool,
|
||||
}
|
||||
struct Container {
|
||||
target: DerefTarget,
|
||||
container_field: bool,
|
||||
}
|
||||
|
||||
impl Deref for Container {
|
||||
type Target = DerefTarget;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.target
|
||||
}
|
||||
}
|
||||
|
||||
struct Outer {
|
||||
container: Container,
|
||||
outer_field: bool,
|
||||
}
|
||||
|
||||
impl Deref for Outer {
|
||||
type Target = Container;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.container
|
||||
}
|
||||
}
|
||||
|
||||
impl Outer {
|
||||
fn bad_borrow(&mut self) {
|
||||
let first = &self.target_field;
|
||||
self.outer_field = true; //~ ERROR E0506
|
||||
first;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
15
src/test/ui/borrowck/issue-81365-4.stderr
Normal file
15
src/test/ui/borrowck/issue-81365-4.stderr
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
error[E0506]: cannot assign to `self.outer_field` because it is borrowed
|
||||
--> $DIR/issue-81365-4.rs:33:9
|
||||
|
|
||||
LL | let first = &self.target_field;
|
||||
| ---- borrow of `self.outer_field` occurs here
|
||||
LL | self.outer_field = true;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `self.outer_field` occurs here
|
||||
LL | first;
|
||||
| ----- borrow later used here
|
||||
|
|
||||
= note: borrow occurs due to deref coercion to `Container`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0506`.
|
||||
33
src/test/ui/borrowck/issue-81365-5.rs
Normal file
33
src/test/ui/borrowck/issue-81365-5.rs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
use std::ops::Deref;
|
||||
|
||||
struct DerefTarget {
|
||||
target_field: bool,
|
||||
}
|
||||
|
||||
impl DerefTarget {
|
||||
fn get(&self) -> &bool {
|
||||
&self.target_field
|
||||
}
|
||||
}
|
||||
|
||||
struct Container {
|
||||
target: DerefTarget,
|
||||
container_field: bool,
|
||||
}
|
||||
|
||||
impl Deref for Container {
|
||||
type Target = DerefTarget;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.target
|
||||
}
|
||||
}
|
||||
|
||||
impl Container {
|
||||
fn bad_borrow(&mut self) {
|
||||
let first = self.get();
|
||||
self.container_field = true; //~ ERROR E0506
|
||||
first;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
15
src/test/ui/borrowck/issue-81365-5.stderr
Normal file
15
src/test/ui/borrowck/issue-81365-5.stderr
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
error[E0506]: cannot assign to `self.container_field` because it is borrowed
|
||||
--> $DIR/issue-81365-5.rs:28:9
|
||||
|
|
||||
LL | let first = self.get();
|
||||
| ---- borrow of `self.container_field` occurs here
|
||||
LL | self.container_field = true;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `self.container_field` occurs here
|
||||
LL | first;
|
||||
| ----- borrow later used here
|
||||
|
|
||||
= note: borrow occurs due to deref coercion to `DerefTarget`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0506`.
|
||||
23
src/test/ui/borrowck/issue-81365-6.rs
Normal file
23
src/test/ui/borrowck/issue-81365-6.rs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
use std::ops::Deref;
|
||||
|
||||
struct Container {
|
||||
target: Vec<()>,
|
||||
container_field: bool,
|
||||
}
|
||||
|
||||
impl Deref for Container {
|
||||
type Target = [()];
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.target
|
||||
}
|
||||
}
|
||||
|
||||
impl Container {
|
||||
fn bad_borrow(&mut self) {
|
||||
let first = &self[0];
|
||||
self.container_field = true; //~ ERROR E0506
|
||||
first;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
15
src/test/ui/borrowck/issue-81365-6.stderr
Normal file
15
src/test/ui/borrowck/issue-81365-6.stderr
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
error[E0506]: cannot assign to `self.container_field` because it is borrowed
|
||||
--> $DIR/issue-81365-6.rs:18:9
|
||||
|
|
||||
LL | let first = &self[0];
|
||||
| ---- borrow of `self.container_field` occurs here
|
||||
LL | self.container_field = true;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `self.container_field` occurs here
|
||||
LL | first;
|
||||
| ----- borrow later used here
|
||||
|
|
||||
= note: borrow occurs due to deref coercion to `[()]`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0506`.
|
||||
24
src/test/ui/borrowck/issue-81365-7.rs
Normal file
24
src/test/ui/borrowck/issue-81365-7.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use std::ops::Deref;
|
||||
|
||||
struct DerefTarget {
|
||||
target_field: bool,
|
||||
}
|
||||
struct Container {
|
||||
target: DerefTarget,
|
||||
container_field: bool,
|
||||
}
|
||||
|
||||
impl Deref for Container {
|
||||
type Target = DerefTarget;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.target
|
||||
}
|
||||
}
|
||||
|
||||
fn bad_borrow(c: &mut Container) {
|
||||
let first = &c.target_field;
|
||||
c.container_field = true; //~ ERROR E0506
|
||||
first;
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
15
src/test/ui/borrowck/issue-81365-7.stderr
Normal file
15
src/test/ui/borrowck/issue-81365-7.stderr
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
error[E0506]: cannot assign to `c.container_field` because it is borrowed
|
||||
--> $DIR/issue-81365-7.rs:20:5
|
||||
|
|
||||
LL | let first = &c.target_field;
|
||||
| - borrow of `c.container_field` occurs here
|
||||
LL | c.container_field = true;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `c.container_field` occurs here
|
||||
LL | first;
|
||||
| ----- borrow later used here
|
||||
|
|
||||
= note: borrow occurs due to deref coercion to `DerefTarget`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0506`.
|
||||
26
src/test/ui/borrowck/issue-81365-8.rs
Normal file
26
src/test/ui/borrowck/issue-81365-8.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
use std::ops::Deref;
|
||||
|
||||
struct DerefTarget {
|
||||
target_field: bool,
|
||||
}
|
||||
struct Container {
|
||||
target: DerefTarget,
|
||||
container_field: bool,
|
||||
}
|
||||
|
||||
impl Deref for Container {
|
||||
type Target = DerefTarget;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.target
|
||||
}
|
||||
}
|
||||
|
||||
impl Container {
|
||||
fn bad_borrow(&mut self) {
|
||||
let first = &(*self).target_field;
|
||||
self.container_field = true; //~ ERROR E0506
|
||||
first;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
26
src/test/ui/borrowck/issue-81365.rs
Normal file
26
src/test/ui/borrowck/issue-81365.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
use std::ops::Deref;
|
||||
|
||||
struct DerefTarget {
|
||||
target_field: bool,
|
||||
}
|
||||
struct Container {
|
||||
target: DerefTarget,
|
||||
container_field: bool,
|
||||
}
|
||||
|
||||
impl Deref for Container {
|
||||
type Target = DerefTarget;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.target
|
||||
}
|
||||
}
|
||||
|
||||
impl Container {
|
||||
fn bad_borrow(&mut self) {
|
||||
let first = &self.target_field;
|
||||
self.container_field = true; //~ ERROR E0506
|
||||
first;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
15
src/test/ui/borrowck/issue-81365.stderr
Normal file
15
src/test/ui/borrowck/issue-81365.stderr
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
error[E0506]: cannot assign to `self.container_field` because it is borrowed
|
||||
--> $DIR/issue-81365.rs:21:9
|
||||
|
|
||||
LL | let first = &self.target_field;
|
||||
| ---- borrow of `self.container_field` occurs here
|
||||
LL | self.container_field = true;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `self.container_field` occurs here
|
||||
LL | first;
|
||||
| ----- borrow later used here
|
||||
|
|
||||
= note: borrow occurs due to deref coercion to `DerefTarget`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0506`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue