Skip to content

Commit 43a6684

Browse files
committed
Add test for calling fn with unsized return type
1 parent 33a281c commit 43a6684

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

tests/ui/sized/unsized-return.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
trait T {}
2+
3+
fn foo() -> dyn T { //~ E0746
4+
todo!()
5+
}
6+
7+
fn main() {
8+
let x = foo(); //~ ERROR E0277
9+
//~^ ERROR E0277
10+
let x: dyn T = foo(); //~ ERROR E0277
11+
}

tests/ui/sized/unsized-return.stderr

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
error[E0746]: return type cannot have an unboxed trait object
2+
--> $DIR/unsized-return.rs:3:13
3+
|
4+
LL | fn foo() -> dyn T {
5+
| ^^^^^ doesn't have a size known at compile-time
6+
|
7+
= help: if the returned value came from a borrowed argument that implements the trait, then you could return `&dyn Trait`
8+
help: return an `impl Trait` instead of a `dyn Trait`
9+
|
10+
LL | fn foo() -> impl T {
11+
| ~~~~
12+
help: alternatively, box the return type to make a boxed trait object, and wrap all of the returned values in `Box::new`
13+
|
14+
LL ~ fn foo() -> Box<dyn T> {
15+
LL ~ Box::new(todo!())
16+
|
17+
18+
error[E0277]: the size for values of type `dyn T` cannot be known at compilation time
19+
--> $DIR/unsized-return.rs:10:12
20+
|
21+
LL | let x: dyn T = foo();
22+
| ^^^^^ doesn't have a size known at compile-time
23+
|
24+
= help: the trait `Sized` is not implemented for `dyn T`
25+
= note: all local variables must have a statically known size
26+
= help: unsized locals are gated as an unstable feature
27+
help: consider borrowing here
28+
|
29+
LL | let x: &dyn T = foo();
30+
| +
31+
32+
error[E0277]: the size for values of type `dyn T` cannot be known at compilation time
33+
--> $DIR/unsized-return.rs:8:13
34+
|
35+
LL | let x = foo();
36+
| ^^^^^ doesn't have a size known at compile-time
37+
|
38+
= help: the trait `Sized` is not implemented for `dyn T`
39+
= note: all local variables must have a statically known size
40+
= help: unsized locals are gated as an unstable feature
41+
42+
error[E0277]: the size for values of type `(dyn T + 'static)` cannot be known at compilation time
43+
--> $DIR/unsized-return.rs:8:13
44+
|
45+
LL | let x = foo();
46+
| ^^^^^ doesn't have a size known at compile-time
47+
|
48+
= help: the trait `Sized` is not implemented for `(dyn T + 'static)`
49+
= note: the return type of a function must have a statically known size
50+
51+
error: aborting due to 4 previous errors
52+
53+
Some errors have detailed explanations: E0277, E0746.
54+
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)