Not unsize-coercing leads to "the size for values of type `dyn Trait` cannot be known at compilation time"
Open
Nobody has claimed this yet.
A-diagnostics
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the report with the supplied example on the referenced Rust 1.87.0-nightly version, using the src/lib.rs locations shown in the diagnostics. Read the compiler diagnostic tests and related handling for E0277 and opaque type errors; done means the example produces an explanation that identifies the unsize-coercion problem clearly without the misleading dyn Trait size message.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100