Skip to content

Commit fb6acfa

Browse files
committed
Introduce basic Reborrow tests
1 parent 3df92ea commit fb6acfa

12 files changed

+215
-0
lines changed

tests/ui/reborrow/custom_mut.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(reborrow)]
2+
use std::marker::Reborrow;
3+
4+
struct CustomMut<'a, T>(&'a mut T);
5+
impl<'a, T> Reborrow for CustomMut<'a, T> {}
6+
7+
fn method(a: CustomMut<'_, ()>) {}
8+
9+
fn main() {
10+
let a = CustomMut(&mut ());
11+
let _ = method(a);
12+
let _ = method(a); //~ERROR use of moved value: `a`
13+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error[E0382]: use of moved value: `a`
2+
--> $DIR/custom_mut.rs:12:20
3+
|
4+
LL | let a = CustomMut(&mut ());
5+
| - move occurs because `a` has type `CustomMut<'_, ()>`, which does not implement the `Copy` trait
6+
LL | let _ = method(a);
7+
| - value moved here
8+
LL | let _ = method(a);
9+
| ^ value used here after move
10+
|
11+
note: consider changing this parameter type in function `method` to borrow instead if owning the value isn't necessary
12+
--> $DIR/custom_mut.rs:7:14
13+
|
14+
LL | fn method(a: CustomMut<'_, ()>) {}
15+
| ------ ^^^^^^^^^^^^^^^^^ this parameter takes ownership of the value
16+
| |
17+
| in this function
18+
note: if `CustomMut<'_, ()>` implemented `Clone`, you could clone the value
19+
--> $DIR/custom_mut.rs:4:1
20+
|
21+
LL | struct CustomMut<'a, T>(&'a mut T);
22+
| ^^^^^^^^^^^^^^^^^^^^^^^ consider implementing `Clone` for this type
23+
...
24+
LL | let _ = method(a);
25+
| - you could clone this value
26+
27+
error: aborting due to 1 previous error
28+
29+
For more information about this error, try `rustc --explain E0382`.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![feature(reborrow)]
2+
use std::marker::{Reborrow, CoerceShared};
3+
4+
struct CustomMut<'a, T>(&'a mut T);
5+
impl<'a, T> Reborrow for CustomMut<'a, T> {}
6+
impl<'a, T> CoerceShared for CustomMut<'a, T> {
7+
type Target = CustomRef<'a, T>;
8+
}
9+
10+
struct CustomRef<'a, T>(&'a T);
11+
12+
impl<'a, T> Clone for CustomRef<'a, T> {
13+
fn clone(&self) -> Self {
14+
Self(self.0)
15+
}
16+
}
17+
impl<'a, T> Copy for CustomRef<'a, T> {}
18+
19+
fn method(a: CustomRef<'_, ()>) {} //~NOTE function defined here
20+
21+
fn main() {
22+
let a = CustomMut(&mut ());
23+
method(a);
24+
//~^ ERROR mismatched types
25+
//~| NOTE expected `CustomRef<'_, ()>`, found `CustomMut<'_, ()>`
26+
//~| NOTE arguments to this function are incorrect
27+
//~| NOTE expected struct `CustomRef<'_, ()>`
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/custom_mut_coerce_shared.rs:23:12
3+
|
4+
LL | method(a);
5+
| ------ ^ expected `CustomRef<'_, ()>`, found `CustomMut<'_, ()>`
6+
| |
7+
| arguments to this function are incorrect
8+
|
9+
= note: expected struct `CustomRef<'_, ()>`
10+
found struct `CustomMut<'_, ()>`
11+
note: function defined here
12+
--> $DIR/custom_mut_coerce_shared.rs:19:4
13+
|
14+
LL | fn method(a: CustomRef<'_, ()>) {}
15+
| ^^^^^^ --------------------
16+
17+
error: aborting due to 1 previous error
18+
19+
For more information about this error, try `rustc --explain E0308`.

tests/ui/reborrow/option_mut.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn method(a: Option<& mut ()>) {}
2+
3+
fn main() {
4+
let a = Some(&mut ());
5+
let _ = method(a);
6+
let _ = method(a); //~ERROR use of moved value: `a`
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0382]: use of moved value: `a`
2+
--> $DIR/option_mut.rs:6:20
3+
|
4+
LL | let a = Some(&mut ());
5+
| - move occurs because `a` has type `Option<&mut ()>`, which does not implement the `Copy` trait
6+
LL | let _ = method(a);
7+
| - value moved here
8+
LL | let _ = method(a);
9+
| ^ value used here after move
10+
|
11+
note: consider changing this parameter type in function `method` to borrow instead if owning the value isn't necessary
12+
--> $DIR/option_mut.rs:1:14
13+
|
14+
LL | fn method(a: Option<& mut ()>) {}
15+
| ------ ^^^^^^^^^^^^^^^^ this parameter takes ownership of the value
16+
| |
17+
| in this function
18+
19+
error: aborting due to 1 previous error
20+
21+
For more information about this error, try `rustc --explain E0382`.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn method(a: Option<&()>) {} //~NOTE function defined here
2+
3+
fn main() {
4+
let a = Some(&mut ());
5+
method(a);
6+
//~^ ERROR mismatched types
7+
//~| NOTE arguments to this function are incorrect
8+
//~| NOTE types differ in mutability
9+
//~| NOTE expected enum `Option<&()>`
10+
//~| NOTE found enum `Option<&mut ()>`
11+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/option_mut_coerce_shared.rs:5:12
3+
|
4+
LL | method(a);
5+
| ------ ^ types differ in mutability
6+
| |
7+
| arguments to this function are incorrect
8+
|
9+
= note: expected enum `Option<&()>`
10+
found enum `Option<&mut ()>`
11+
note: function defined here
12+
--> $DIR/option_mut_coerce_shared.rs:1:4
13+
|
14+
LL | fn method(a: Option<&()>) {}
15+
| ^^^^^^ --------------
16+
help: try using `.as_deref()` to convert `Option<&mut ()>` to `Option<&()>`
17+
|
18+
LL | method(a.as_deref());
19+
| +++++++++++
20+
21+
error: aborting due to 1 previous error
22+
23+
For more information about this error, try `rustc --explain E0308`.

tests/ui/reborrow/pin_mut.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use std::pin::Pin;
2+
3+
fn method(a: Pin<& mut ()>) {}
4+
5+
fn main() {
6+
let a = &mut ();
7+
let a = Pin::new(a);
8+
let _ = method(a);
9+
let _ = method(a); //~ERROR use of moved value: `a`
10+
}

tests/ui/reborrow/pin_mut.stderr

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0382]: use of moved value: `a`
2+
--> $DIR/pin_mut.rs:9:20
3+
|
4+
LL | let a = Pin::new(a);
5+
| - move occurs because `a` has type `Pin<&mut ()>`, which does not implement the `Copy` trait
6+
LL | let _ = method(a);
7+
| - value moved here
8+
LL | let _ = method(a);
9+
| ^ value used here after move
10+
|
11+
note: consider changing this parameter type in function `method` to borrow instead if owning the value isn't necessary
12+
--> $DIR/pin_mut.rs:3:14
13+
|
14+
LL | fn method(a: Pin<& mut ()>) {}
15+
| ------ ^^^^^^^^^^^^^ this parameter takes ownership of the value
16+
| |
17+
| in this function
18+
19+
error: aborting due to 1 previous error
20+
21+
For more information about this error, try `rustc --explain E0382`.

0 commit comments

Comments
 (0)