Skip to content

Commit f80aec7

Browse files
committed
Test that you can't circumvent the Sized bound check
1 parent 3219993 commit f80aec7

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! This test checks that even if some associated types have
2+
//! `where Self: Sized` bounds, those without still need to be
3+
//! mentioned in trait objects.
4+
5+
trait Bop {
6+
type Bar: Default
7+
where
8+
Self: Sized;
9+
}
10+
11+
fn bop<T: Bop + ?Sized>() {
12+
let _ = <T as Bop>::Bar::default();
13+
//~^ ERROR: trait bounds were not satisfied
14+
//~| ERROR: the size for values of type `T` cannot be known at compilation time
15+
}
16+
17+
fn main() {
18+
bop::<dyn Bop>();
19+
//~^ ERROR: the size for values of type `dyn Bop` cannot be known at compilation time
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
error[E0599]: the function or associated item `default` exists for associated type `<T as Bop>::Bar`, but its trait bounds were not satisfied
2+
--> $DIR/assoc_type_bounds_sized_used.rs:12:30
3+
|
4+
LL | let _ = <T as Bop>::Bar::default();
5+
| ^^^^^^^ function or associated item cannot be called on `<T as Bop>::Bar` due to unsatisfied trait bounds
6+
|
7+
= note: the following trait bounds were not satisfied:
8+
`T: Sized`
9+
which is required by `<T as Bop>::Bar: Default`
10+
help: consider restricting the type parameter to satisfy the trait bound
11+
|
12+
LL | fn bop<T: Bop + ?Sized>() where T: Sized {
13+
| ++++++++++++++
14+
15+
error[E0277]: the size for values of type `T` cannot be known at compilation time
16+
--> $DIR/assoc_type_bounds_sized_used.rs:12:13
17+
|
18+
LL | fn bop<T: Bop + ?Sized>() {
19+
| - this type parameter needs to be `Sized`
20+
LL | let _ = <T as Bop>::Bar::default();
21+
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
22+
|
23+
note: required by a bound in `Bop::Bar`
24+
--> $DIR/assoc_type_bounds_sized_used.rs:8:15
25+
|
26+
LL | type Bar: Default
27+
| --- required by a bound in this associated type
28+
LL | where
29+
LL | Self: Sized;
30+
| ^^^^^ required by this bound in `Bop::Bar`
31+
help: consider removing the `?Sized` bound to make the type parameter `Sized`
32+
|
33+
LL - fn bop<T: Bop + ?Sized>() {
34+
LL + fn bop<T: Bop>() {
35+
|
36+
37+
error[E0277]: the size for values of type `dyn Bop` cannot be known at compilation time
38+
--> $DIR/assoc_type_bounds_sized_used.rs:18:11
39+
|
40+
LL | bop::<dyn Bop>();
41+
| ^^^^^^^ doesn't have a size known at compile-time
42+
|
43+
= help: the trait `Sized` is not implemented for `dyn Bop`
44+
note: required by a bound in `bop`
45+
--> $DIR/assoc_type_bounds_sized_used.rs:11:11
46+
|
47+
LL | fn bop<T: Bop + ?Sized>() {
48+
| ^^^ required by this bound in `bop`
49+
50+
error: aborting due to 3 previous errors
51+
52+
Some errors have detailed explanations: E0277, E0599.
53+
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)