Skip to content

Too many dyn Trait is not Sized errors #105753

@estebank

Description

@estebank

When writing

trait T {}

fn foo() -> dyn T {
    todo!()
}

fn main() {
    let x = foo();
}

We emit three errors for dyn T: Sized not being fulfilled. We should likely only emit 1, in foo's return type, but we certainly shouldn't be emitting both one for x and one for foo's call.

error[E0746]: return type cannot have an unboxed trait object
 --> src/main.rs:3:13
  |
3 | fn foo() -> dyn T {
  |             ^^^^^ doesn't have a size known at compile-time
  |
help: use `impl T` as the return type if all return paths have the same type but you want to expose only the trait in the signature
  |
3 | fn foo() -> impl T {
  |             ~~~~~~
help: use a boxed trait object if all return paths implement trait `T`
  |
3 | fn foo() -> Box<dyn T> {
  |             ++++     +

error[E0277]: the size for values of type `dyn T` cannot be known at compilation time
 --> src/main.rs:8:9
  |
8 |     let x = foo();
  |         ^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `dyn T`
  = note: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature

error[E0277]: the size for values of type `(dyn T + 'static)` cannot be known at compilation time
 --> src/main.rs:8:13
  |
8 |     let x = foo();
  |             ^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `(dyn T + 'static)`
  = note: the return type of a function must have a statically known size

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-verboseDiagnostics: Too much output caused by a single piece of incorrect code.P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions