Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not unsize-coercing leads to "the size for values of type dyn Trait cannot be known at compilation time" #137615

Open
theemathas opened this issue Feb 25, 2025 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@theemathas
Copy link
Contributor

theemathas commented Feb 25, 2025

Code

trait Trait {}

struct Thing;
impl Trait for Thing {}

impl<T: Trait, E> Trait for Result<T, E> {}

impl<T: Trait + ?Sized> Trait for Box<T> {}

fn foo<T>(x: &T) -> impl Trait {
    if true {
        Ok(Box::new(foo(x)))
    } else {
        Ok(Box::new(Thing) as Box<dyn Trait>)
    }
}

Current output

error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time
  --> src/lib.rs:14:9
   |
14 |         Ok(Box::new(Thing) as Box<dyn Trait>)
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `dyn Trait`

For more information about this error, try `rustc --explain E0277`.

Desired output

Rationale and extra context

I have no idea what the compiler is trying to tell me here.

Other cases

trait Trait {}

struct Thing;
impl Trait for Thing {}

impl<T: Trait + ?Sized> Trait for Box<T> {}

fn foo<T>(x: &T) -> impl Trait {
    if true {
        Box::new(foo(x))
    } else {
        Box::new(Thing) as Box<dyn Trait>
    }
}

Output:

error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time
  --> src/lib.rs:12:9
   |
12 |         Box::new(Thing) as Box<dyn Trait>
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `dyn Trait`

error: concrete type differs from previous defining opaque type use
  --> src/lib.rs:8:21
   |
8  | fn foo<T>(x: &T) -> impl Trait {
   |                     ^^^^^^^^^^ expected `dyn Trait`, got `Box<impl Trait>`
   |
note: previous use here
  --> src/lib.rs:12:9
   |
12 |         Box::new(Thing) as Box<dyn Trait>
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0277`.

Rust Version

Reproducible on playground with rust 1.87.0-nightly (2025-02-24 617aad8c2e8783f6df8e)

Anything else?

Minimized from code written by starwort on the rust community discord.

Error output using edition 2024.

@theemathas theemathas added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant