Skip to content

tests/ui: A New Order [20/N] #143212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tests/ui/cast/non-primitive-cast-suggestion.fixed
Original file line number Diff line number Diff line change
@@ -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<Foo> 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]
}
23 changes: 23 additions & 0 deletions tests/ui/cast/non-primitive-cast-suggestion.rs
Original file line number Diff line number Diff line change
@@ -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<Foo> 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]
}
15 changes: 15 additions & 0 deletions tests/ui/cast/non-primitive-cast-suggestion.stderr
Original file line number Diff line number Diff line change
@@ -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`.
19 changes: 19 additions & 0 deletions tests/ui/closures/closure-clone-requires-captured-clone.rs
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 23 additions & 0 deletions tests/ui/closures/closure-clone-requires-captured-clone.stderr
Original file line number Diff line number Diff line change
@@ -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`.
10 changes: 10 additions & 0 deletions tests/ui/consts/array-repeat-expr-not-const.rs
Original file line number Diff line number Diff line change
@@ -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]
}
}
Original file line number Diff line number Diff line change
@@ -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];
| ^

Expand Down
Original file line number Diff line number Diff line change
@@ -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<isize>`
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/cross-crate/unexported-type-error-message.stderr
Original file line number Diff line number Diff line change
@@ -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<isize>`
| |
| expected due to this
|
= note: expected type `isize`
found enum `Option<isize>`
help: consider using `Option::expect` to unwrap the `Option<isize>` 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`.
40 changes: 40 additions & 0 deletions tests/ui/drop/field-replace-in-struct-with-drop.rs
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/rust-lang/rust/issues/34101>.
//@ 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();
}
15 changes: 0 additions & 15 deletions tests/ui/no-core-1.rs

This file was deleted.

20 changes: 0 additions & 20 deletions tests/ui/no-core-2.rs

This file was deleted.

46 changes: 0 additions & 46 deletions tests/ui/no-warn-on-field-replace-issue-34101.rs

This file was deleted.

28 changes: 28 additions & 0 deletions tests/ui/no_std/no-core-edition2018-syntax.rs
Original file line number Diff line number Diff line change
@@ -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() {}
21 changes: 21 additions & 0 deletions tests/ui/no_std/no-core-with-explicit-std-core.rs
Original file line number Diff line number Diff line change
@@ -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();
}
18 changes: 0 additions & 18 deletions tests/ui/noexporttypeexe.stderr

This file was deleted.

8 changes: 0 additions & 8 deletions tests/ui/non-constant-expr-for-arr-len.rs

This file was deleted.

16 changes: 0 additions & 16 deletions tests/ui/nonscalar-cast.fixed

This file was deleted.

Loading
Loading