Skip to content

Commit 545d06b

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

15 files changed

+126
-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

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
1-
// Issue 34101: Circa 2016-06-05, `fn inline` below issued an
2-
// erroneous warning from the elaborate_drops pass about moving out of
3-
// a field in `Foo`, which has a destructor (and thus cannot have
4-
// content moved out of it). The reason that the warning is erroneous
5-
// in this case is that we are doing a *replace*, not a move, of the
6-
// content in question, and it is okay to replace fields within `Foo`.
7-
//
8-
// Another more subtle problem was that the elaborate_drops was
9-
// creating a separate drop flag for that internally replaced content,
10-
// even though the compiler should enforce an invariant that any drop
11-
// flag for such subcontent of `Foo` will always have the same value
12-
// as the drop flag for `Foo` itself.
13-
14-
15-
16-
17-
18-
19-
1+
//! Test that field replacement in structs with destructors doesn't trigger warnings.
2+
//!
3+
//! Back in 2016, the compiler would incorrectly warn about "moving out of type with dtor"
4+
//! when you assigned to a field of a struct that has a Drop impl. But this is perfectly
5+
//! fine - we're replacing the field, not moving out of it. The old value gets dropped
6+
//! and the new value takes its place.
7+
//!
8+
//! Regression test for <https://github.com/rust-lang/rust/issues/34101>.
209
2110
//@ check-pass
2211

@@ -26,21 +15,20 @@ impl Drop for Foo {
2615
fn drop(&mut self) {}
2716
}
2817

29-
fn inline() {
30-
// (dummy variable so `f` gets assigned `var1` in MIR for both fn's)
18+
fn test_inline_replacement() {
19+
// dummy variable so `f` gets assigned `var1` in MIR for both functions
3120
let _s = ();
3221
let mut f = Foo(String::from("foo"));
33-
f.0 = String::from("bar");
22+
f.0 = String::from("bar"); // This should not warn
3423
}
3524

36-
fn outline() {
25+
fn test_outline_replacement() {
3726
let _s = String::from("foo");
3827
let mut f = Foo(_s);
39-
f.0 = String::from("bar");
28+
f.0 = String::from("bar"); // This should not warn either
4029
}
4130

42-
4331
fn main() {
44-
inline();
45-
outline();
32+
test_inline_replacement();
33+
test_outline_replacement();
4634
}

tests/ui/no_std/no-core-edition2018-syntax.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1+
//! Test that `#![no_core]` doesn't break modern Rust syntax in edition 2018.
2+
//!
3+
//! When you use `#![no_core]`, you lose the automatic prelude, but you can still
4+
//! get everything back by manually importing `use core::{prelude::v1::*, *}`.
5+
//! This test makes sure that after doing that, things like `for` loops and the
6+
//! `?` operator still work as expected.
7+
18
//@ run-pass
9+
//@ edition:2018
210

311
#![allow(dead_code, unused_imports)]
412
#![feature(no_core)]
513
#![no_core]
6-
//@ edition:2018
714

8-
extern crate std;
915
extern crate core;
10-
use core::{prelude::v1::*, *};
16+
extern crate std;
17+
use core::prelude::v1::*;
18+
use core::*;
1119

12-
fn foo() {
20+
fn test_for_loop() {
1321
for _ in &[()] {}
1422
}
1523

16-
fn bar() -> Option<()> {
24+
fn test_question_mark_operator() -> Option<()> {
1725
None?
1826
}
1927

tests/ui/no_std/no-core-with-explicit-std-core.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
//! Test that you can use `#![no_core]` and still import std and core manually.
2+
//!
3+
//! The `#![no_core]` attribute disables the automatic core prelude, but you should
4+
//! still be able to explicitly import both `std` and `core` crates and use types
5+
//! like `Option` normally.
6+
17
//@ run-pass
28

39
#![allow(stable_features)]
410
#![feature(no_core, core)]
511
#![no_core]
612

7-
extern crate std;
813
extern crate core;
14+
extern crate std;
915

1016
use std::option::Option::Some;
1117

tests/ui/threads-sendsync/rc-is-not-send.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
use std::thread;
1+
//! Test that `Rc<T>` cannot be sent between threads.
2+
23
use std::rc::Rc;
4+
use std::thread;
35

46
#[derive(Debug)]
57
struct Port<T>(Rc<T>);
68

7-
fn main() {
8-
#[derive(Debug)]
9-
struct Foo {
10-
_x: Port<()>,
11-
}
9+
#[derive(Debug)]
10+
struct Foo {
11+
_x: Port<()>,
12+
}
1213

13-
impl Drop for Foo {
14-
fn drop(&mut self) {}
15-
}
14+
impl Drop for Foo {
15+
fn drop(&mut self) {}
16+
}
1617

17-
fn foo(x: Port<()>) -> Foo {
18-
Foo {
19-
_x: x
20-
}
21-
}
18+
fn foo(x: Port<()>) -> Foo {
19+
Foo { _x: x }
20+
}
2221

22+
fn main() {
2323
let x = foo(Port(Rc::new(())));
2424

25-
thread::spawn(move|| {
25+
thread::spawn(move || {
2626
//~^ ERROR `Rc<()>` cannot be sent between threads safely
2727
let y = x;
2828
println!("{:?}", y);

tests/ui/threads-sendsync/rc-is-not-send.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error[E0277]: `Rc<()>` cannot be sent between threads safely
2-
--> $DIR/no-send-res-ports.rs:25:19
2+
--> $DIR/rc-is-not-send.rs:25:19
33
|
4-
LL | thread::spawn(move|| {
5-
| ------------- ^-----
4+
LL | thread::spawn(move || {
5+
| ------------- ^------
66
| | |
7-
| _____|_____________within this `{closure@$DIR/no-send-res-ports.rs:25:19: 25:25}`
7+
| _____|_____________within this `{closure@$DIR/rc-is-not-send.rs:25:19: 25:26}`
88
| | |
99
| | required by a bound introduced by this call
1010
LL | |
@@ -13,22 +13,22 @@ LL | | println!("{:?}", y);
1313
LL | | });
1414
| |_____^ `Rc<()>` cannot be sent between threads safely
1515
|
16-
= help: within `{closure@$DIR/no-send-res-ports.rs:25:19: 25:25}`, the trait `Send` is not implemented for `Rc<()>`
16+
= help: within `{closure@$DIR/rc-is-not-send.rs:25:19: 25:26}`, the trait `Send` is not implemented for `Rc<()>`
1717
note: required because it appears within the type `Port<()>`
18-
--> $DIR/no-send-res-ports.rs:5:8
18+
--> $DIR/rc-is-not-send.rs:7:8
1919
|
2020
LL | struct Port<T>(Rc<T>);
2121
| ^^^^
2222
note: required because it appears within the type `Foo`
23-
--> $DIR/no-send-res-ports.rs:9:12
23+
--> $DIR/rc-is-not-send.rs:10:8
2424
|
25-
LL | struct Foo {
26-
| ^^^
25+
LL | struct Foo {
26+
| ^^^
2727
note: required because it's used within this closure
28-
--> $DIR/no-send-res-ports.rs:25:19
28+
--> $DIR/rc-is-not-send.rs:25:19
2929
|
30-
LL | thread::spawn(move|| {
31-
| ^^^^^^
30+
LL | thread::spawn(move || {
31+
| ^^^^^^^
3232
note: required by a bound in `spawn`
3333
--> $SRC_DIR/std/src/thread/mod.rs:LL:COL
3434

0 commit comments

Comments
 (0)