Skip to content

Commit da5c639

Browse files
committed
cleaned up some tests
1 parent 4feb5de commit da5c639

15 files changed

+132
-102
lines changed
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
//! Test that casting non-primitive types with `as` is rejected with a helpful suggestion.
2+
//!
3+
//! You can't use `as` to cast between non-primitive types, even if they have
4+
//! `From`/`Into` implementations. The compiler should suggest using `From::from()`
5+
//! or `.into()` instead, and rustfix should be able to apply the suggestion.
6+
17
//@ run-rustfix
28

39
#[derive(Debug)]
410
struct Foo {
5-
x: isize
11+
x: isize,
612
}
713

814
impl From<Foo> for isize {
@@ -12,5 +18,6 @@ impl From<Foo> for isize {
1218
}
1319

1420
fn main() {
15-
println!("{}", isize::from(Foo { x: 1 })); //~ ERROR non-primitive cast: `Foo` as `isize` [E0605]
21+
let _ = isize::from(Foo { x: 1 });
22+
//~^ ERROR non-primitive cast: `Foo` as `isize` [E0605]
1623
}
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
//! Test that casting non-primitive types with `as` is rejected with a helpful suggestion.
2+
//!
3+
//! You can't use `as` to cast between non-primitive types, even if they have
4+
//! `From`/`Into` implementations. The compiler should suggest using `From::from()`
5+
//! or `.into()` instead, and rustfix should be able to apply the suggestion.
6+
17
//@ run-rustfix
28

39
#[derive(Debug)]
410
struct Foo {
5-
x: isize
11+
x: isize,
612
}
713

814
impl From<Foo> for isize {
@@ -12,5 +18,6 @@ impl From<Foo> for isize {
1218
}
1319

1420
fn main() {
15-
println!("{}", Foo { x: 1 } as isize); //~ ERROR non-primitive cast: `Foo` as `isize` [E0605]
21+
let _ = Foo { x: 1 } as isize;
22+
//~^ ERROR non-primitive cast: `Foo` as `isize` [E0605]
1623
}

tests/ui/cast/non-primitive-cast-suggestion.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error[E0605]: non-primitive cast: `Foo` as `isize`
2-
--> $DIR/nonscalar-cast.rs:15:20
2+
--> $DIR/non-primitive-cast-suggestion.rs:21:13
33
|
4-
LL | println!("{}", Foo { x: 1 } as isize);
5-
| ^^^^^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
4+
LL | let _ = Foo { x: 1 } as isize;
5+
| ^^^^^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
66
|
77
help: consider using the `From` trait instead
88
|
9-
LL - println!("{}", Foo { x: 1 } as isize);
10-
LL + println!("{}", isize::from(Foo { x: 1 }));
9+
LL - let _ = Foo { x: 1 } as isize;
10+
LL + let _ = isize::from(Foo { x: 1 });
1111
|
1212

1313
error: aborting due to 1 previous error
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
//@compile-flags: --diagnostic-width=300
2-
// Check that closures do not implement `Clone` if their environment is not `Clone`.
1+
//! Test that closures only implement `Clone` if all captured values implement `Clone`.
2+
//!
3+
//! When a closure captures variables from its environment, it can only be cloned
4+
//! if all those captured variables are cloneable. This test makes sure the compiler
5+
//! properly rejects attempts to clone closures that capture non-Clone types.
36
4-
struct S(i32);
7+
//@ compile-flags: --diagnostic-width=300
8+
9+
struct NonClone(i32);
510

611
fn main() {
7-
let a = S(5);
8-
let hello = move || {
9-
println!("Hello {}", a.0);
12+
let captured_value = NonClone(5);
13+
let closure = move || {
14+
let _ = captured_value.0;
1015
};
1116

12-
let hello = hello.clone(); //~ ERROR the trait bound `S: Clone` is not satisfied
17+
closure.clone();
18+
//~^ ERROR the trait bound `NonClone: Clone` is not satisfied
1319
}

tests/ui/closures/closure-clone-requires-captured-clone.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
error[E0277]: the trait bound `S: Clone` is not satisfied in `{closure@$DIR/not-clone-closure.rs:8:17: 8:24}`
2-
--> $DIR/not-clone-closure.rs:12:23
1+
error[E0277]: the trait bound `NonClone: Clone` is not satisfied in `{closure@$DIR/closure-clone-requires-captured-clone.rs:13:19: 13:26}`
2+
--> $DIR/closure-clone-requires-captured-clone.rs:17:13
33
|
4-
LL | let hello = move || {
5-
| ------- within this `{closure@$DIR/not-clone-closure.rs:8:17: 8:24}`
4+
LL | let closure = move || {
5+
| ------- within this `{closure@$DIR/closure-clone-requires-captured-clone.rs:13:19: 13:26}`
66
...
7-
LL | let hello = hello.clone();
8-
| ^^^^^ within `{closure@$DIR/not-clone-closure.rs:8:17: 8:24}`, the trait `Clone` is not implemented for `S`
7+
LL | closure.clone();
8+
| ^^^^^ within `{closure@$DIR/closure-clone-requires-captured-clone.rs:13:19: 13:26}`, the trait `Clone` is not implemented for `NonClone`
99
|
1010
note: required because it's used within this closure
11-
--> $DIR/not-clone-closure.rs:8:17
11+
--> $DIR/closure-clone-requires-captured-clone.rs:13:19
1212
|
13-
LL | let hello = move || {
14-
| ^^^^^^^
15-
help: consider annotating `S` with `#[derive(Clone)]`
13+
LL | let closure = move || {
14+
| ^^^^^^^
15+
help: consider annotating `NonClone` with `#[derive(Clone)]`
1616
|
1717
LL + #[derive(Clone)]
18-
LL | struct S(i32);
18+
LL | struct NonClone(i32);
1919
|
2020

2121
error: aborting due to 1 previous error

tests/ui/consts/array-repeat-expr-not-const.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
// Check that non constant exprs fail for array repeat syntax
1+
//! Arrays created with `[value; length]` syntax need the length to be known at
2+
//! compile time. This test makes sure the compiler rejects runtime values like
3+
//! function parameters in the length position.
24
35
fn main() {
4-
fn bar(n: usize) {
6+
fn create_array(n: usize) {
57
let _x = [0; n];
68
//~^ ERROR attempt to use a non-constant value in a constant [E0435]
79
}

tests/ui/consts/array-repeat-expr-not-const.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0435]: attempt to use a non-constant value in a constant
2-
--> $DIR/non-constant-expr-for-arr-len.rs:5:22
2+
--> $DIR/array-repeat-expr-not-const.rs:7:22
33
|
4-
LL | fn bar(n: usize) {
5-
| - this would need to be a `const`
4+
LL | fn create_array(n: usize) {
5+
| - this would need to be a `const`
66
LL | let _x = [0; n];
77
| ^
88

tests/ui/cross-crate/unexported-type-error-message.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
//@ aux-build:noexporttypelib.rs
1+
//@ aux-build:unexported-type-error-message.rs
22

3-
extern crate noexporttypelib;
3+
extern crate unexported_type_error_message;
44

55
fn main() {
66
// Here, the type returned by foo() is not exported.
77
// This used to cause internal errors when serializing
88
// because the def_id associated with the type was
99
// not convertible to a path.
10-
let x: isize = noexporttypelib::foo();
10+
let x: isize = unexported_type_error_message::foo();
1111
//~^ ERROR mismatched types
1212
//~| NOTE expected type `isize`
1313
//~| NOTE found enum `Option<isize>`

tests/ui/cross-crate/unexported-type-error-message.stderr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
error[E0308]: mismatched types
2-
--> $DIR/noexporttypeexe.rs:10:18
2+
--> $DIR/unexported-type-error-message.rs:10:20
33
|
4-
LL | let x: isize = noexporttypelib::foo();
5-
| ----- ^^^^^^^^^^^^^^^^^^^^^^ expected `isize`, found `Option<isize>`
6-
| |
7-
| expected due to this
4+
LL | let x: isize = unexported_type_error_message::foo();
5+
| ----- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `isize`, found `Option<isize>`
6+
| |
7+
| expected due to this
88
|
99
= note: expected type `isize`
1010
found enum `Option<isize>`
1111
help: consider using `Option::expect` to unwrap the `Option<isize>` value, panicking if the value is an `Option::None`
1212
|
13-
LL | let x: isize = noexporttypelib::foo().expect("REASON");
14-
| +++++++++++++++++
13+
LL | let x: isize = unexported_type_error_message::foo().expect("REASON");
14+
| +++++++++++++++++
1515

1616
error: aborting due to 1 previous error
1717

0 commit comments

Comments
 (0)