diff --git a/tests/ui/cast/non-primitive-cast-suggestion.fixed b/tests/ui/cast/non-primitive-cast-suggestion.fixed new file mode 100644 index 0000000000000..9a1a3c022c791 --- /dev/null +++ b/tests/ui/cast/non-primitive-cast-suggestion.fixed @@ -0,0 +1,23 @@ +//! Test that casting non-primitive types with `as` is rejected with a helpful suggestion. +//! +//! You can't use `as` to cast between non-primitive types, even if they have +//! `From`/`Into` implementations. The compiler should suggest using `From::from()` +//! or `.into()` instead, and rustfix should be able to apply the suggestion. + +//@ run-rustfix + +#[derive(Debug)] +struct Foo { + x: isize, +} + +impl From for isize { + fn from(val: Foo) -> isize { + val.x + } +} + +fn main() { + let _ = isize::from(Foo { x: 1 }); + //~^ ERROR non-primitive cast: `Foo` as `isize` [E0605] +} diff --git a/tests/ui/cast/non-primitive-cast-suggestion.rs b/tests/ui/cast/non-primitive-cast-suggestion.rs new file mode 100644 index 0000000000000..79006f4ba26d3 --- /dev/null +++ b/tests/ui/cast/non-primitive-cast-suggestion.rs @@ -0,0 +1,23 @@ +//! Test that casting non-primitive types with `as` is rejected with a helpful suggestion. +//! +//! You can't use `as` to cast between non-primitive types, even if they have +//! `From`/`Into` implementations. The compiler should suggest using `From::from()` +//! or `.into()` instead, and rustfix should be able to apply the suggestion. + +//@ run-rustfix + +#[derive(Debug)] +struct Foo { + x: isize, +} + +impl From for isize { + fn from(val: Foo) -> isize { + val.x + } +} + +fn main() { + let _ = Foo { x: 1 } as isize; + //~^ ERROR non-primitive cast: `Foo` as `isize` [E0605] +} diff --git a/tests/ui/cast/non-primitive-cast-suggestion.stderr b/tests/ui/cast/non-primitive-cast-suggestion.stderr new file mode 100644 index 0000000000000..bd35ded15a46c --- /dev/null +++ b/tests/ui/cast/non-primitive-cast-suggestion.stderr @@ -0,0 +1,15 @@ +error[E0605]: non-primitive cast: `Foo` as `isize` + --> $DIR/non-primitive-cast-suggestion.rs:21:13 + | +LL | let _ = Foo { x: 1 } as isize; + | ^^^^^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object + | +help: consider using the `From` trait instead + | +LL - let _ = Foo { x: 1 } as isize; +LL + let _ = isize::from(Foo { x: 1 }); + | + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0605`. diff --git a/tests/ui/closures/closure-clone-requires-captured-clone.rs b/tests/ui/closures/closure-clone-requires-captured-clone.rs new file mode 100644 index 0000000000000..80938e50b67a7 --- /dev/null +++ b/tests/ui/closures/closure-clone-requires-captured-clone.rs @@ -0,0 +1,19 @@ +//! Test that closures only implement `Clone` if all captured values implement `Clone`. +//! +//! When a closure captures variables from its environment, it can only be cloned +//! if all those captured variables are cloneable. This test makes sure the compiler +//! properly rejects attempts to clone closures that capture non-Clone types. + +//@ compile-flags: --diagnostic-width=300 + +struct NonClone(i32); + +fn main() { + let captured_value = NonClone(5); + let closure = move || { + let _ = captured_value.0; + }; + + closure.clone(); + //~^ ERROR the trait bound `NonClone: Clone` is not satisfied +} diff --git a/tests/ui/closures/closure-clone-requires-captured-clone.stderr b/tests/ui/closures/closure-clone-requires-captured-clone.stderr new file mode 100644 index 0000000000000..785cc8a3032e2 --- /dev/null +++ b/tests/ui/closures/closure-clone-requires-captured-clone.stderr @@ -0,0 +1,23 @@ +error[E0277]: the trait bound `NonClone: Clone` is not satisfied in `{closure@$DIR/closure-clone-requires-captured-clone.rs:13:19: 13:26}` + --> $DIR/closure-clone-requires-captured-clone.rs:17:13 + | +LL | let closure = move || { + | ------- within this `{closure@$DIR/closure-clone-requires-captured-clone.rs:13:19: 13:26}` +... +LL | closure.clone(); + | ^^^^^ within `{closure@$DIR/closure-clone-requires-captured-clone.rs:13:19: 13:26}`, the trait `Clone` is not implemented for `NonClone` + | +note: required because it's used within this closure + --> $DIR/closure-clone-requires-captured-clone.rs:13:19 + | +LL | let closure = move || { + | ^^^^^^^ +help: consider annotating `NonClone` with `#[derive(Clone)]` + | +LL + #[derive(Clone)] +LL | struct NonClone(i32); + | + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/consts/array-repeat-expr-not-const.rs b/tests/ui/consts/array-repeat-expr-not-const.rs new file mode 100644 index 0000000000000..55aee7336da32 --- /dev/null +++ b/tests/ui/consts/array-repeat-expr-not-const.rs @@ -0,0 +1,10 @@ +//! Arrays created with `[value; length]` syntax need the length to be known at +//! compile time. This test makes sure the compiler rejects runtime values like +//! function parameters in the length position. + +fn main() { + fn create_array(n: usize) { + let _x = [0; n]; + //~^ ERROR attempt to use a non-constant value in a constant [E0435] + } +} diff --git a/tests/ui/non-constant-expr-for-arr-len.stderr b/tests/ui/consts/array-repeat-expr-not-const.stderr similarity index 62% rename from tests/ui/non-constant-expr-for-arr-len.stderr rename to tests/ui/consts/array-repeat-expr-not-const.stderr index c9f977fbaa409..f576154525964 100644 --- a/tests/ui/non-constant-expr-for-arr-len.stderr +++ b/tests/ui/consts/array-repeat-expr-not-const.stderr @@ -1,8 +1,8 @@ error[E0435]: attempt to use a non-constant value in a constant - --> $DIR/non-constant-expr-for-arr-len.rs:5:22 + --> $DIR/array-repeat-expr-not-const.rs:7:22 | -LL | fn bar(n: usize) { - | - this would need to be a `const` +LL | fn create_array(n: usize) { + | - this would need to be a `const` LL | let _x = [0; n]; | ^ diff --git a/tests/ui/auxiliary/noexporttypelib.rs b/tests/ui/cross-crate/auxiliary/unexported-type-error-message.rs similarity index 100% rename from tests/ui/auxiliary/noexporttypelib.rs rename to tests/ui/cross-crate/auxiliary/unexported-type-error-message.rs diff --git a/tests/ui/noexporttypeexe.rs b/tests/ui/cross-crate/unexported-type-error-message.rs similarity index 73% rename from tests/ui/noexporttypeexe.rs rename to tests/ui/cross-crate/unexported-type-error-message.rs index 35257b20ccdf8..5998f0dc6e233 100644 --- a/tests/ui/noexporttypeexe.rs +++ b/tests/ui/cross-crate/unexported-type-error-message.rs @@ -1,13 +1,13 @@ -//@ aux-build:noexporttypelib.rs +//@ aux-build:unexported-type-error-message.rs -extern crate noexporttypelib; +extern crate unexported_type_error_message; fn main() { // Here, the type returned by foo() is not exported. // This used to cause internal errors when serializing // because the def_id associated with the type was // not convertible to a path. - let x: isize = noexporttypelib::foo(); + let x: isize = unexported_type_error_message::foo(); //~^ ERROR mismatched types //~| NOTE expected type `isize` //~| NOTE found enum `Option` diff --git a/tests/ui/cross-crate/unexported-type-error-message.stderr b/tests/ui/cross-crate/unexported-type-error-message.stderr new file mode 100644 index 0000000000000..b468d9839e10e --- /dev/null +++ b/tests/ui/cross-crate/unexported-type-error-message.stderr @@ -0,0 +1,18 @@ +error[E0308]: mismatched types + --> $DIR/unexported-type-error-message.rs:10:20 + | +LL | let x: isize = unexported_type_error_message::foo(); + | ----- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `isize`, found `Option` + | | + | expected due to this + | + = note: expected type `isize` + found enum `Option` +help: consider using `Option::expect` to unwrap the `Option` value, panicking if the value is an `Option::None` + | +LL | let x: isize = unexported_type_error_message::foo().expect("REASON"); + | +++++++++++++++++ + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/drop/field-replace-in-struct-with-drop.rs b/tests/ui/drop/field-replace-in-struct-with-drop.rs new file mode 100644 index 0000000000000..8c65a4b3ad1e0 --- /dev/null +++ b/tests/ui/drop/field-replace-in-struct-with-drop.rs @@ -0,0 +1,40 @@ +//! Circa 2016-06-05, `fn inline` below issued an +//! erroneous warning from the elaborate_drops pass about moving out of +//! a field in `Foo`, which has a destructor (and thus cannot have +//! content moved out of it). The reason that the warning is erroneous +//! in this case is that we are doing a *replace*, not a move, of the +//! content in question, and it is okay to replace fields within `Foo`. +//! +//! Another more subtle problem was that the elaborate_drops was +//! creating a separate drop flag for that internally replaced content, +//! even though the compiler should enforce an invariant that any drop +//! flag for such subcontent of `Foo` will always have the same value +//! as the drop flag for `Foo` itself. +//! +//! Regression test for . + +//@ check-pass + +struct Foo(String); + +impl Drop for Foo { + fn drop(&mut self) {} +} + +fn test_inline_replacement() { + // dummy variable so `f` gets assigned `var1` in MIR for both functions + let _s = (); + let mut f = Foo(String::from("foo")); + f.0 = String::from("bar"); // This should not warn +} + +fn test_outline_replacement() { + let _s = String::from("foo"); + let mut f = Foo(_s); + f.0 = String::from("bar"); // This should not warn either +} + +fn main() { + test_inline_replacement(); + test_outline_replacement(); +} diff --git a/tests/ui/no-core-1.rs b/tests/ui/no-core-1.rs deleted file mode 100644 index d6d2ba6044585..0000000000000 --- a/tests/ui/no-core-1.rs +++ /dev/null @@ -1,15 +0,0 @@ -//@ run-pass - -#![allow(stable_features)] -#![feature(no_core, core)] -#![no_core] - -extern crate std; -extern crate core; - -use std::option::Option::Some; - -fn main() { - let a = Some("foo"); - a.unwrap(); -} diff --git a/tests/ui/no-core-2.rs b/tests/ui/no-core-2.rs deleted file mode 100644 index 2f55365bdd0f3..0000000000000 --- a/tests/ui/no-core-2.rs +++ /dev/null @@ -1,20 +0,0 @@ -//@ run-pass - -#![allow(dead_code, unused_imports)] -#![feature(no_core)] -#![no_core] -//@ edition:2018 - -extern crate std; -extern crate core; -use core::{prelude::v1::*, *}; - -fn foo() { - for _ in &[()] {} -} - -fn bar() -> Option<()> { - None? -} - -fn main() {} diff --git a/tests/ui/no-warn-on-field-replace-issue-34101.rs b/tests/ui/no-warn-on-field-replace-issue-34101.rs deleted file mode 100644 index e1d5e9c5268b8..0000000000000 --- a/tests/ui/no-warn-on-field-replace-issue-34101.rs +++ /dev/null @@ -1,46 +0,0 @@ -// Issue 34101: Circa 2016-06-05, `fn inline` below issued an -// erroneous warning from the elaborate_drops pass about moving out of -// a field in `Foo`, which has a destructor (and thus cannot have -// content moved out of it). The reason that the warning is erroneous -// in this case is that we are doing a *replace*, not a move, of the -// content in question, and it is okay to replace fields within `Foo`. -// -// Another more subtle problem was that the elaborate_drops was -// creating a separate drop flag for that internally replaced content, -// even though the compiler should enforce an invariant that any drop -// flag for such subcontent of `Foo` will always have the same value -// as the drop flag for `Foo` itself. - - - - - - - - -//@ check-pass - -struct Foo(String); - -impl Drop for Foo { - fn drop(&mut self) {} -} - -fn inline() { - // (dummy variable so `f` gets assigned `var1` in MIR for both fn's) - let _s = (); - let mut f = Foo(String::from("foo")); - f.0 = String::from("bar"); -} - -fn outline() { - let _s = String::from("foo"); - let mut f = Foo(_s); - f.0 = String::from("bar"); -} - - -fn main() { - inline(); - outline(); -} diff --git a/tests/ui/no_std/no-core-edition2018-syntax.rs b/tests/ui/no_std/no-core-edition2018-syntax.rs new file mode 100644 index 0000000000000..9a327e4c8e316 --- /dev/null +++ b/tests/ui/no_std/no-core-edition2018-syntax.rs @@ -0,0 +1,28 @@ +//! Test that `#![no_core]` doesn't break modern Rust syntax in edition 2018. +//! +//! When you use `#![no_core]`, you lose the automatic prelude, but you can still +//! get everything back by manually importing `use core::{prelude::v1::*, *}`. +//! This test makes sure that after doing that, things like `for` loops and the +//! `?` operator still work as expected. + +//@ run-pass +//@ edition:2018 + +#![allow(dead_code, unused_imports)] +#![feature(no_core)] +#![no_core] + +extern crate core; +extern crate std; +use core::prelude::v1::*; +use core::*; + +fn test_for_loop() { + for _ in &[()] {} +} + +fn test_question_mark_operator() -> Option<()> { + None? +} + +fn main() {} diff --git a/tests/ui/no_std/no-core-with-explicit-std-core.rs b/tests/ui/no_std/no-core-with-explicit-std-core.rs new file mode 100644 index 0000000000000..3940bcb3aa4fb --- /dev/null +++ b/tests/ui/no_std/no-core-with-explicit-std-core.rs @@ -0,0 +1,21 @@ +//! Test that you can use `#![no_core]` and still import std and core manually. +//! +//! The `#![no_core]` attribute disables the automatic core prelude, but you should +//! still be able to explicitly import both `std` and `core` crates and use types +//! like `Option` normally. + +//@ run-pass + +#![allow(stable_features)] +#![feature(no_core, core)] +#![no_core] + +extern crate core; +extern crate std; + +use std::option::Option::Some; + +fn main() { + let a = Some("foo"); + a.unwrap(); +} diff --git a/tests/ui/noexporttypeexe.stderr b/tests/ui/noexporttypeexe.stderr deleted file mode 100644 index 59759b696c7a2..0000000000000 --- a/tests/ui/noexporttypeexe.stderr +++ /dev/null @@ -1,18 +0,0 @@ -error[E0308]: mismatched types - --> $DIR/noexporttypeexe.rs:10:18 - | -LL | let x: isize = noexporttypelib::foo(); - | ----- ^^^^^^^^^^^^^^^^^^^^^^ expected `isize`, found `Option` - | | - | expected due to this - | - = note: expected type `isize` - found enum `Option` -help: consider using `Option::expect` to unwrap the `Option` value, panicking if the value is an `Option::None` - | -LL | let x: isize = noexporttypelib::foo().expect("REASON"); - | +++++++++++++++++ - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/non-constant-expr-for-arr-len.rs b/tests/ui/non-constant-expr-for-arr-len.rs deleted file mode 100644 index 1b101d3233f14..0000000000000 --- a/tests/ui/non-constant-expr-for-arr-len.rs +++ /dev/null @@ -1,8 +0,0 @@ -// Check that non constant exprs fail for array repeat syntax - -fn main() { - fn bar(n: usize) { - let _x = [0; n]; - //~^ ERROR attempt to use a non-constant value in a constant [E0435] - } -} diff --git a/tests/ui/nonscalar-cast.fixed b/tests/ui/nonscalar-cast.fixed deleted file mode 100644 index cb5591dbb9de4..0000000000000 --- a/tests/ui/nonscalar-cast.fixed +++ /dev/null @@ -1,16 +0,0 @@ -//@ run-rustfix - -#[derive(Debug)] -struct Foo { - x: isize -} - -impl From for isize { - fn from(val: Foo) -> isize { - val.x - } -} - -fn main() { - println!("{}", isize::from(Foo { x: 1 })); //~ ERROR non-primitive cast: `Foo` as `isize` [E0605] -} diff --git a/tests/ui/nonscalar-cast.rs b/tests/ui/nonscalar-cast.rs deleted file mode 100644 index 27429b44cd086..0000000000000 --- a/tests/ui/nonscalar-cast.rs +++ /dev/null @@ -1,16 +0,0 @@ -//@ run-rustfix - -#[derive(Debug)] -struct Foo { - x: isize -} - -impl From for isize { - fn from(val: Foo) -> isize { - val.x - } -} - -fn main() { - println!("{}", Foo { x: 1 } as isize); //~ ERROR non-primitive cast: `Foo` as `isize` [E0605] -} diff --git a/tests/ui/nonscalar-cast.stderr b/tests/ui/nonscalar-cast.stderr deleted file mode 100644 index 834d4ea241c12..0000000000000 --- a/tests/ui/nonscalar-cast.stderr +++ /dev/null @@ -1,15 +0,0 @@ -error[E0605]: non-primitive cast: `Foo` as `isize` - --> $DIR/nonscalar-cast.rs:15:20 - | -LL | println!("{}", Foo { x: 1 } as isize); - | ^^^^^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object - | -help: consider using the `From` trait instead - | -LL - println!("{}", Foo { x: 1 } as isize); -LL + println!("{}", isize::from(Foo { x: 1 })); - | - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0605`. diff --git a/tests/ui/not-clone-closure.rs b/tests/ui/not-clone-closure.rs deleted file mode 100644 index 976e3b9e81c2e..0000000000000 --- a/tests/ui/not-clone-closure.rs +++ /dev/null @@ -1,13 +0,0 @@ -//@compile-flags: --diagnostic-width=300 -// Check that closures do not implement `Clone` if their environment is not `Clone`. - -struct S(i32); - -fn main() { - let a = S(5); - let hello = move || { - println!("Hello {}", a.0); - }; - - let hello = hello.clone(); //~ ERROR the trait bound `S: Clone` is not satisfied -} diff --git a/tests/ui/not-clone-closure.stderr b/tests/ui/not-clone-closure.stderr deleted file mode 100644 index 0c95a99d0c095..0000000000000 --- a/tests/ui/not-clone-closure.stderr +++ /dev/null @@ -1,23 +0,0 @@ -error[E0277]: the trait bound `S: Clone` is not satisfied in `{closure@$DIR/not-clone-closure.rs:8:17: 8:24}` - --> $DIR/not-clone-closure.rs:12:23 - | -LL | let hello = move || { - | ------- within this `{closure@$DIR/not-clone-closure.rs:8:17: 8:24}` -... -LL | let hello = hello.clone(); - | ^^^^^ within `{closure@$DIR/not-clone-closure.rs:8:17: 8:24}`, the trait `Clone` is not implemented for `S` - | -note: required because it's used within this closure - --> $DIR/not-clone-closure.rs:8:17 - | -LL | let hello = move || { - | ^^^^^^^ -help: consider annotating `S` with `#[derive(Clone)]` - | -LL + #[derive(Clone)] -LL | struct S(i32); - | - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/no-send-res-ports.rs b/tests/ui/threads-sendsync/rc-is-not-send.rs similarity index 52% rename from tests/ui/no-send-res-ports.rs rename to tests/ui/threads-sendsync/rc-is-not-send.rs index 1bac5868e73fe..dd562e9e8f34f 100644 --- a/tests/ui/no-send-res-ports.rs +++ b/tests/ui/threads-sendsync/rc-is-not-send.rs @@ -1,28 +1,28 @@ -use std::thread; +//! Test that `Rc` cannot be sent between threads. + use std::rc::Rc; +use std::thread; #[derive(Debug)] struct Port(Rc); -fn main() { - #[derive(Debug)] - struct Foo { - _x: Port<()>, - } +#[derive(Debug)] +struct Foo { + _x: Port<()>, +} - impl Drop for Foo { - fn drop(&mut self) {} - } +impl Drop for Foo { + fn drop(&mut self) {} +} - fn foo(x: Port<()>) -> Foo { - Foo { - _x: x - } - } +fn foo(x: Port<()>) -> Foo { + Foo { _x: x } +} +fn main() { let x = foo(Port(Rc::new(()))); - thread::spawn(move|| { + thread::spawn(move || { //~^ ERROR `Rc<()>` cannot be sent between threads safely let y = x; println!("{:?}", y); diff --git a/tests/ui/no-send-res-ports.stderr b/tests/ui/threads-sendsync/rc-is-not-send.stderr similarity index 58% rename from tests/ui/no-send-res-ports.stderr rename to tests/ui/threads-sendsync/rc-is-not-send.stderr index 9c30261e5cb73..a06b683f729e0 100644 --- a/tests/ui/no-send-res-ports.stderr +++ b/tests/ui/threads-sendsync/rc-is-not-send.stderr @@ -1,10 +1,10 @@ error[E0277]: `Rc<()>` cannot be sent between threads safely - --> $DIR/no-send-res-ports.rs:25:19 + --> $DIR/rc-is-not-send.rs:25:19 | -LL | thread::spawn(move|| { - | ------------- ^----- +LL | thread::spawn(move || { + | ------------- ^------ | | | - | _____|_____________within this `{closure@$DIR/no-send-res-ports.rs:25:19: 25:25}` + | _____|_____________within this `{closure@$DIR/rc-is-not-send.rs:25:19: 25:26}` | | | | | required by a bound introduced by this call LL | | @@ -13,22 +13,22 @@ LL | | println!("{:?}", y); LL | | }); | |_____^ `Rc<()>` cannot be sent between threads safely | - = help: within `{closure@$DIR/no-send-res-ports.rs:25:19: 25:25}`, the trait `Send` is not implemented for `Rc<()>` + = help: within `{closure@$DIR/rc-is-not-send.rs:25:19: 25:26}`, the trait `Send` is not implemented for `Rc<()>` note: required because it appears within the type `Port<()>` - --> $DIR/no-send-res-ports.rs:5:8 + --> $DIR/rc-is-not-send.rs:7:8 | LL | struct Port(Rc); | ^^^^ note: required because it appears within the type `Foo` - --> $DIR/no-send-res-ports.rs:9:12 + --> $DIR/rc-is-not-send.rs:10:8 | -LL | struct Foo { - | ^^^ +LL | struct Foo { + | ^^^ note: required because it's used within this closure - --> $DIR/no-send-res-ports.rs:25:19 + --> $DIR/rc-is-not-send.rs:25:19 | -LL | thread::spawn(move|| { - | ^^^^^^ +LL | thread::spawn(move || { + | ^^^^^^^ note: required by a bound in `spawn` --> $SRC_DIR/std/src/thread/mod.rs:LL:COL