Skip to content

Too many dyn Trait is not Sized errors #105753

Closed

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

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    P-lowLow priority
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.
    on Dec 15, 2022
  2. eopb commented on Dec 23, 2022

    @eopb
    Contributor

    @rustbot claim

  3. eopb commented on Jan 9, 2023

    @eopb
    Contributor

    I found working on this issue a little too challenging. I don't think I'll be able to get any further without guidance.

    @rustbot release-assignment

  4. Ezrashaw commented on Jan 11, 2023

    @Ezrashaw
    Contributor

    Hmm, I might work on this but I only want to remove the third error. The other two make sense to me: you can't return a unsized value and you can't assign a unsized value to a local variable.

    @rustbot claim

  5. added 2 commits that reference this issue on Oct 8, 2026
    77d7f83
    41dce4c
  6. added a commit that references this issue on Oct 8, 2026
    67b04bf
  7. added a commit that references this issue on Oct 9, 2026
    998fd69
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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