Add tests

This commit is contained in:
1000teslas 2021-02-02 03:20:56 +11:00
parent b2eed3a559
commit 8412da6829
9 changed files with 139 additions and 1 deletions

View file

@ -1,5 +1,5 @@
error[E0506]: cannot assign to `self.container_field` because it is borrowed
--> $DIR/issue-81365.rs:21:9
--> $DIR/issue-81365-1.rs:21:9
|
LL | let first = &self.target_field;
| ---- borrow of `self.container_field` occurs here

View 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.deref().target_field;
self.container_field = true; //~ ERROR E0506
first;
}
}
fn main() {}

View file

@ -0,0 +1,13 @@
error[E0506]: cannot assign to `self.container_field` because it is borrowed
--> $DIR/issue-81365-10.rs:21:9
|
LL | let first = &self.deref().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
error: aborting due to previous error
For more information about this error, try `rustc --explain E0506`.

View file

@ -0,0 +1,32 @@
use std::ops::{Deref, DerefMut};
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 DerefMut for Container {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.target
}
}
impl Container {
fn bad_borrow(&mut self) {
let first = &mut self.target_field;
self.container_field = true; //~ ERROR E0506
first;
}
}
fn main() {}

View file

@ -0,0 +1,13 @@
error[E0506]: cannot assign to `self.container_field` because it is borrowed
--> $DIR/issue-81365-11.rs:27:9
|
LL | let first = &mut 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
error: aborting due to previous error
For more information about this error, try `rustc --explain E0506`.

View file

@ -0,0 +1,15 @@
error[E0506]: cannot assign to `self.container_field` because it is borrowed
--> $DIR/issue-81365-8.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`.

View 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 = &Deref::deref(self).target_field;
self.container_field = true; //~ ERROR E0506
first;
}
}
fn main() {}

View file

@ -0,0 +1,13 @@
error[E0506]: cannot assign to `self.container_field` because it is borrowed
--> $DIR/issue-81365-9.rs:21:9
|
LL | let first = &Deref::deref(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
error: aborting due to previous error
For more information about this error, try `rustc --explain E0506`.