From f5f298dad4f8ed383cac22d90659b55a820f7ba0 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 28 May 2024 15:38:18 +0000 Subject: [PATCH] Add more tests --- .../impl-trait/unsized_coercion.next.stderr | 26 +++++++++++++++++++ tests/ui/impl-trait/unsized_coercion.rs | 21 +++++++++++++++ .../impl-trait/unsized_coercion2.old.stderr | 12 +++++++++ tests/ui/impl-trait/unsized_coercion2.rs | 21 +++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 tests/ui/impl-trait/unsized_coercion.next.stderr create mode 100644 tests/ui/impl-trait/unsized_coercion.rs create mode 100644 tests/ui/impl-trait/unsized_coercion2.old.stderr create mode 100644 tests/ui/impl-trait/unsized_coercion2.rs diff --git a/tests/ui/impl-trait/unsized_coercion.next.stderr b/tests/ui/impl-trait/unsized_coercion.next.stderr new file mode 100644 index 00000000000..49ac3f1845f --- /dev/null +++ b/tests/ui/impl-trait/unsized_coercion.next.stderr @@ -0,0 +1,26 @@ +error[E0271]: type mismatch resolving `impl Trait <: dyn Trait` + --> $DIR/unsized_coercion.rs:14:17 + | +LL | let x = hello(); + | ^^^^^^^ types differ + +error[E0308]: mismatched types + --> $DIR/unsized_coercion.rs:18:14 + | +LL | fn hello() -> Box { + | ---------- the expected opaque type +... +LL | Box::new(1u32) + | -------- ^^^^ types differ + | | + | arguments to this function are incorrect + | + = note: expected opaque type `impl Trait` + found type `u32` +note: associated function defined here + --> $SRC_DIR/alloc/src/boxed.rs:LL:COL + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0271, E0308. +For more information about an error, try `rustc --explain E0271`. diff --git a/tests/ui/impl-trait/unsized_coercion.rs b/tests/ui/impl-trait/unsized_coercion.rs new file mode 100644 index 00000000000..46e040c1428 --- /dev/null +++ b/tests/ui/impl-trait/unsized_coercion.rs @@ -0,0 +1,21 @@ +//! This test checks that opaque types get unsized instead of +//! constraining their hidden type to a trait object. + +//@ revisions: next old +//@[next] compile-flags: -Znext-solver +//@[old] check-pass + +trait Trait {} + +impl Trait for u32 {} + +fn hello() -> Box { + if true { + let x = hello(); + //[next]~^ ERROR: type mismatch resolving `impl Trait <: dyn Trait` + let y: Box = x; + } + Box::new(1u32) //[next]~ ERROR: mismatched types +} + +fn main() {} diff --git a/tests/ui/impl-trait/unsized_coercion2.old.stderr b/tests/ui/impl-trait/unsized_coercion2.old.stderr new file mode 100644 index 00000000000..a89d40f1130 --- /dev/null +++ b/tests/ui/impl-trait/unsized_coercion2.old.stderr @@ -0,0 +1,12 @@ +error[E0277]: the size for values of type `impl Trait + ?Sized` cannot be known at compilation time + --> $DIR/unsized_coercion2.rs:15:33 + | +LL | let y: Box = x; + | ^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `impl Trait + ?Sized` + = note: required for the cast from `Box` to `Box` + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/impl-trait/unsized_coercion2.rs b/tests/ui/impl-trait/unsized_coercion2.rs new file mode 100644 index 00000000000..7368d47dbe2 --- /dev/null +++ b/tests/ui/impl-trait/unsized_coercion2.rs @@ -0,0 +1,21 @@ +//! This test checks that opaque types get unsized instead of +//! constraining their hidden type to a trait object. + +//@ revisions: next old +//@[next] compile-flags: -Znext-solver +//@[next] check-pass + +trait Trait {} + +impl Trait for u32 {} + +fn hello() -> Box { + if true { + let x = hello(); + let y: Box = x; + //[old]~^ ERROR: the size for values of type `impl Trait + ?Sized` cannot be know + } + Box::new(1u32) +} + +fn main() {}