Skip to content

Commit 65942af

Browse files
Rollup merge of #143212 - Kivooeo:tf20, r=tgross35
`tests/ui`: A New Order [20/N] > [!NOTE] > > Intermediate commits are intended to help review, but will be squashed prior to merge. Some `tests/ui/` housekeeping, to trim down number of tests directly under `tests/ui/`. Part of #133895. r? `@tgross35`
2 parents ff0a5f5 + da5c639 commit 65942af

25 files changed

+253
-223
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
7+
//@ run-rustfix
8+
9+
#[derive(Debug)]
10+
struct Foo {
11+
x: isize,
12+
}
13+
14+
impl From<Foo> for isize {
15+
fn from(val: Foo) -> isize {
16+
val.x
17+
}
18+
}
19+
20+
fn main() {
21+
let _ = isize::from(Foo { x: 1 });
22+
//~^ ERROR non-primitive cast: `Foo` as `isize` [E0605]
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
7+
//@ run-rustfix
8+
9+
#[derive(Debug)]
10+
struct Foo {
11+
x: isize,
12+
}
13+
14+
impl From<Foo> for isize {
15+
fn from(val: Foo) -> isize {
16+
val.x
17+
}
18+
}
19+
20+
fn main() {
21+
let _ = Foo { x: 1 } as isize;
22+
//~^ ERROR non-primitive cast: `Foo` as `isize` [E0605]
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0605]: non-primitive cast: `Foo` as `isize`
2+
--> $DIR/non-primitive-cast-suggestion.rs:21:13
3+
|
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
6+
|
7+
help: consider using the `From` trait instead
8+
|
9+
LL - let _ = Foo { x: 1 } as isize;
10+
LL + let _ = isize::from(Foo { x: 1 });
11+
|
12+
13+
error: aborting due to 1 previous error
14+
15+
For more information about this error, try `rustc --explain E0605`.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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.
6+
7+
//@ compile-flags: --diagnostic-width=300
8+
9+
struct NonClone(i32);
10+
11+
fn main() {
12+
let captured_value = NonClone(5);
13+
let closure = move || {
14+
let _ = captured_value.0;
15+
};
16+
17+
closure.clone();
18+
//~^ ERROR the trait bound `NonClone: Clone` is not satisfied
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,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
3+
|
4+
LL | let closure = move || {
5+
| ------- within this `{closure@$DIR/closure-clone-requires-captured-clone.rs:13:19: 13:26}`
6+
...
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`
9+
|
10+
note: required because it's used within this closure
11+
--> $DIR/closure-clone-requires-captured-clone.rs:13:19
12+
|
13+
LL | let closure = move || {
14+
| ^^^^^^^
15+
help: consider annotating `NonClone` with `#[derive(Clone)]`
16+
|
17+
LL + #[derive(Clone)]
18+
LL | struct NonClone(i32);
19+
|
20+
21+
error: aborting due to 1 previous error
22+
23+
For more information about this error, try `rustc --explain E0277`.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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.
4+
5+
fn main() {
6+
fn create_array(n: usize) {
7+
let _x = [0; n];
8+
//~^ ERROR attempt to use a non-constant value in a constant [E0435]
9+
}
10+
}

tests/ui/non-constant-expr-for-arr-len.stderr renamed to 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/noexporttypeexe.rs renamed to 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>`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/unexported-type-error-message.rs:10:20
3+
|
4+
LL | let x: isize = unexported_type_error_message::foo();
5+
| ----- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `isize`, found `Option<isize>`
6+
| |
7+
| expected due to this
8+
|
9+
= note: expected type `isize`
10+
found enum `Option<isize>`
11+
help: consider using `Option::expect` to unwrap the `Option<isize>` value, panicking if the value is an `Option::None`
12+
|
13+
LL | let x: isize = unexported_type_error_message::foo().expect("REASON");
14+
| +++++++++++++++++
15+
16+
error: aborting due to 1 previous error
17+
18+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)