From f80aec7429e43b7ef5f508e3c41819b2552f6f26 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Fri, 16 Jun 2023 15:21:53 +0000 Subject: [PATCH] Test that you can't circumvent the `Sized` bound check --- .../assoc_type_bounds_sized_used.rs | 20 +++++++ .../assoc_type_bounds_sized_used.stderr | 53 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 tests/ui/object-safety/assoc_type_bounds_sized_used.rs create mode 100644 tests/ui/object-safety/assoc_type_bounds_sized_used.stderr diff --git a/tests/ui/object-safety/assoc_type_bounds_sized_used.rs b/tests/ui/object-safety/assoc_type_bounds_sized_used.rs new file mode 100644 index 0000000000000..cf5345b1c1dca --- /dev/null +++ b/tests/ui/object-safety/assoc_type_bounds_sized_used.rs @@ -0,0 +1,20 @@ +//! This test checks that even if some associated types have +//! `where Self: Sized` bounds, those without still need to be +//! mentioned in trait objects. + +trait Bop { + type Bar: Default + where + Self: Sized; +} + +fn bop() { + let _ = ::Bar::default(); + //~^ ERROR: trait bounds were not satisfied + //~| ERROR: the size for values of type `T` cannot be known at compilation time +} + +fn main() { + bop::(); + //~^ ERROR: the size for values of type `dyn Bop` cannot be known at compilation time +} diff --git a/tests/ui/object-safety/assoc_type_bounds_sized_used.stderr b/tests/ui/object-safety/assoc_type_bounds_sized_used.stderr new file mode 100644 index 0000000000000..f8488d842e2f1 --- /dev/null +++ b/tests/ui/object-safety/assoc_type_bounds_sized_used.stderr @@ -0,0 +1,53 @@ +error[E0599]: the function or associated item `default` exists for associated type `::Bar`, but its trait bounds were not satisfied + --> $DIR/assoc_type_bounds_sized_used.rs:12:30 + | +LL | let _ = ::Bar::default(); + | ^^^^^^^ function or associated item cannot be called on `::Bar` due to unsatisfied trait bounds + | + = note: the following trait bounds were not satisfied: + `T: Sized` + which is required by `::Bar: Default` +help: consider restricting the type parameter to satisfy the trait bound + | +LL | fn bop() where T: Sized { + | ++++++++++++++ + +error[E0277]: the size for values of type `T` cannot be known at compilation time + --> $DIR/assoc_type_bounds_sized_used.rs:12:13 + | +LL | fn bop() { + | - this type parameter needs to be `Sized` +LL | let _ = ::Bar::default(); + | ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | +note: required by a bound in `Bop::Bar` + --> $DIR/assoc_type_bounds_sized_used.rs:8:15 + | +LL | type Bar: Default + | --- required by a bound in this associated type +LL | where +LL | Self: Sized; + | ^^^^^ required by this bound in `Bop::Bar` +help: consider removing the `?Sized` bound to make the type parameter `Sized` + | +LL - fn bop() { +LL + fn bop() { + | + +error[E0277]: the size for values of type `dyn Bop` cannot be known at compilation time + --> $DIR/assoc_type_bounds_sized_used.rs:18:11 + | +LL | bop::(); + | ^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `dyn Bop` +note: required by a bound in `bop` + --> $DIR/assoc_type_bounds_sized_used.rs:11:11 + | +LL | fn bop() { + | ^^^ required by this bound in `bop` + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0277, E0599. +For more information about an error, try `rustc --explain E0277`.