Skip to content

Commit

Permalink
Add more tests on unsized locals autoderef and borrowck.
Browse files Browse the repository at this point in the history
  • Loading branch information
qnighy committed Oct 27, 2018
1 parent 1c48647 commit 2f7ea4a
Show file tree
Hide file tree
Showing 7 changed files with 368 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/test/run-pass/unsized-locals/autoderef.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#![feature(unsized_locals)]

pub trait Foo {
fn foo(self) -> String;
}

impl Foo for [char] {
fn foo(self) -> String {
self.iter().collect()
}
}

impl Foo for str {
fn foo(self) -> String {
self.to_owned()
}
}

impl Foo for dyn FnMut() -> String {
fn foo(mut self) -> String {
self()
}
}


fn main() {
let x = *(Box::new(['h', 'e', 'l', 'l', 'o']) as Box<[char]>);
assert_eq!(&x.foo() as &str, "hello");

let x = Box::new(['h', 'e', 'l', 'l', 'o']) as Box<[char]>;
assert_eq!(&x.foo() as &str, "hello");

let x = "hello".to_owned().into_boxed_str();
assert_eq!(&x.foo() as &str, "hello");

let x = *("hello".to_owned().into_boxed_str());
assert_eq!(&x.foo() as &str, "hello");

let x = "hello".to_owned().into_boxed_str();
assert_eq!(&x.foo() as &str, "hello");

let x = *(Box::new(|| "hello".to_owned()) as Box<dyn FnMut() -> String>);
assert_eq!(&x.foo() as &str, "hello");

let x = Box::new(|| "hello".to_owned()) as Box<dyn FnMut() -> String>;
assert_eq!(&x.foo() as &str, "hello");
}
51 changes: 51 additions & 0 deletions src/test/ui/unsized-locals/borrow-after-move.nll.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
error[E0382]: borrow of moved value: `x`
--> $DIR/borrow-after-move.rs:20:24
|
LL | let y = *x;
| -- value moved here
LL | drop_unsized(y);
LL | println!("{}", &x);
| ^^ value borrowed here after move

error[E0382]: borrow of moved value: `y`
--> $DIR/borrow-after-move.rs:22:24
|
LL | drop_unsized(y);
| - value moved here
...
LL | println!("{}", &y);
| ^^ value borrowed here after move
|
= note: move occurs because `y` has type `str`, which does not implement the `Copy` trait

error[E0382]: borrow of moved value: `x`
--> $DIR/borrow-after-move.rs:30:24
|
LL | let y = *x;
| -- value moved here
LL | y.foo();
LL | println!("{}", &x);
| ^^ value borrowed here after move

error[E0382]: borrow of moved value: `y`
--> $DIR/borrow-after-move.rs:32:24
|
LL | y.foo();
| - value moved here
...
LL | println!("{}", &y);
| ^^ value borrowed here after move
|
= note: move occurs because `y` has type `str`, which does not implement the `Copy` trait

error[E0382]: borrow of moved value: `x`
--> $DIR/borrow-after-move.rs:39:24
|
LL | x.foo();
| - value moved here
LL | println!("{}", &x);
| ^^ value borrowed here after move

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0382`.
42 changes: 42 additions & 0 deletions src/test/ui/unsized-locals/borrow-after-move.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#![feature(unsized_locals)]

pub trait Foo {
fn foo(self) -> String;
}

impl Foo for str {
fn foo(self) -> String {
self.to_owned()
}
}

fn drop_unsized<T: ?Sized>(_: T) {}

fn main() {
{
let x = "hello".to_owned().into_boxed_str();
let y = *x;
drop_unsized(y);
println!("{}", &x);
//~^ERROR use of moved value
println!("{}", &y);
//~^ERROR use of moved value
}

{
let x = "hello".to_owned().into_boxed_str();
let y = *x;
y.foo();
println!("{}", &x);
//~^ERROR use of moved value
println!("{}", &y);
//~^ERROR use of moved value
}

{
let x = "hello".to_owned().into_boxed_str();
x.foo();
println!("{}", &x);
//~^ERROR use of moved value
}
}
57 changes: 57 additions & 0 deletions src/test/ui/unsized-locals/borrow-after-move.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
error[E0382]: use of moved value: `x`
--> $DIR/borrow-after-move.rs:20:25
|
LL | let y = *x;
| - value moved here
LL | drop_unsized(y);
LL | println!("{}", &x);
| ^ value used here after move
|
= note: move occurs because `*x` has type `str`, which does not implement the `Copy` trait

error[E0382]: use of moved value: `y`
--> $DIR/borrow-after-move.rs:22:25
|
LL | drop_unsized(y);
| - value moved here
...
LL | println!("{}", &y);
| ^ value used here after move
|
= note: move occurs because `y` has type `str`, which does not implement the `Copy` trait

error[E0382]: use of moved value: `x`
--> $DIR/borrow-after-move.rs:30:25
|
LL | let y = *x;
| - value moved here
LL | y.foo();
LL | println!("{}", &x);
| ^ value used here after move
|
= note: move occurs because `*x` has type `str`, which does not implement the `Copy` trait

error[E0382]: use of moved value: `y`
--> $DIR/borrow-after-move.rs:32:25
|
LL | y.foo();
| - value moved here
...
LL | println!("{}", &y);
| ^ value used here after move
|
= note: move occurs because `y` has type `str`, which does not implement the `Copy` trait

error[E0382]: use of moved value: `x`
--> $DIR/borrow-after-move.rs:39:25
|
LL | x.foo();
| - value moved here
LL | println!("{}", &x);
| ^ value used here after move
|
= note: move occurs because `*x` has type `str`, which does not implement the `Copy` trait

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0382`.
55 changes: 55 additions & 0 deletions src/test/ui/unsized-locals/double-move.nll.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
error[E0382]: use of moved value: `y`
--> $DIR/double-move.rs:20:22
|
LL | drop_unsized(y);
| - value moved here
LL | drop_unsized(y); //~ERROR use of moved value
| ^ value used here after move
|
= note: move occurs because `y` has type `str`, which does not implement the `Copy` trait

error[E0382]: use of moved value: `x`
--> $DIR/double-move.rs:26:22
|
LL | let _y = *x;
| -- value moved here
LL | drop_unsized(x); //~ERROR use of moved value
| ^ value used here after move

error[E0382]: use of moved value: `*x`
--> $DIR/double-move.rs:32:18
|
LL | drop_unsized(x);
| - value moved here
LL | let _y = *x; //~ERROR use of moved value
| ^^ value used here after move

error[E0382]: use of moved value: `y`
--> $DIR/double-move.rs:39:9
|
LL | y.foo();
| - value moved here
LL | y.foo(); //~ERROR use of moved value
| ^ value used here after move
|
= note: move occurs because `y` has type `str`, which does not implement the `Copy` trait

error[E0382]: use of moved value: `*x`
--> $DIR/double-move.rs:45:9
|
LL | let _y = *x;
| -- value moved here
LL | x.foo(); //~ERROR use of moved value
| ^ value used here after move

error[E0382]: use of moved value: `*x`
--> $DIR/double-move.rs:51:18
|
LL | x.foo();
| - value moved here
LL | let _y = *x; //~ERROR use of moved value
| ^^ value used here after move

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0382`.
53 changes: 53 additions & 0 deletions src/test/ui/unsized-locals/double-move.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#![feature(unsized_locals)]

pub trait Foo {
fn foo(self) -> String;
}

impl Foo for str {
fn foo(self) -> String {
self.to_owned()
}
}

fn drop_unsized<T: ?Sized>(_: T) {}

fn main() {
{
let x = "hello".to_owned().into_boxed_str();
let y = *x;
drop_unsized(y);
drop_unsized(y); //~ERROR use of moved value
}

{
let x = "hello".to_owned().into_boxed_str();
let _y = *x;
drop_unsized(x); //~ERROR use of moved value
}

{
let x = "hello".to_owned().into_boxed_str();
drop_unsized(x);
let _y = *x; //~ERROR use of moved value
}

{
let x = "hello".to_owned().into_boxed_str();
let y = *x;
y.foo();
y.foo(); //~ERROR use of moved value
}

{
let x = "hello".to_owned().into_boxed_str();
let _y = *x;
x.foo(); //~ERROR use of moved value
}

{
let x = "hello".to_owned().into_boxed_str();
x.foo();
let _y = *x; //~ERROR use of moved value
}
}
63 changes: 63 additions & 0 deletions src/test/ui/unsized-locals/double-move.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
error[E0382]: use of moved value: `y`
--> $DIR/double-move.rs:20:22
|
LL | drop_unsized(y);
| - value moved here
LL | drop_unsized(y); //~ERROR use of moved value
| ^ value used here after move
|
= note: move occurs because `y` has type `str`, which does not implement the `Copy` trait

error[E0382]: use of moved value: `x`
--> $DIR/double-move.rs:26:22
|
LL | let _y = *x;
| -- value moved here
LL | drop_unsized(x); //~ERROR use of moved value
| ^ value used here after move
|
= note: move occurs because `*x` has type `str`, which does not implement the `Copy` trait

error[E0382]: use of moved value: `*x`
--> $DIR/double-move.rs:32:13
|
LL | drop_unsized(x);
| - value moved here
LL | let _y = *x; //~ERROR use of moved value
| ^^ value used here after move
|
= note: move occurs because `x` has type `std::boxed::Box<str>`, which does not implement the `Copy` trait

error[E0382]: use of moved value: `y`
--> $DIR/double-move.rs:39:9
|
LL | y.foo();
| - value moved here
LL | y.foo(); //~ERROR use of moved value
| ^ value used here after move
|
= note: move occurs because `y` has type `str`, which does not implement the `Copy` trait

error[E0382]: use of moved value: `*x`
--> $DIR/double-move.rs:45:9
|
LL | let _y = *x;
| -- value moved here
LL | x.foo(); //~ERROR use of moved value
| ^ value used here after move
|
= note: move occurs because `*x` has type `str`, which does not implement the `Copy` trait

error[E0382]: use of moved value: `*x`
--> $DIR/double-move.rs:51:13
|
LL | x.foo();
| - value moved here
LL | let _y = *x; //~ERROR use of moved value
| ^^ value used here after move
|
= note: move occurs because `*x` has type `str`, which does not implement the `Copy` trait

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0382`.

0 comments on commit 2f7ea4a

Please sign in to comment.