Skip to content

Improve error message for attempt to statically allocate trait object #57186

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

Closed
gabcbrown opened this issue Dec 29, 2018 · 3 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@gabcbrown
Copy link

gabcbrown commented Dec 29, 2018

Simple example:

trait T {}

struct S;

impl T for S {}

fn foo<X: T>(s: X) -> X {
    s
}

fn main() {
    let x: T = foo(S);
}

This gives the error message:

error[E0277]: the size for values of type `dyn T` cannot be known at compilation time
  --> src/main.rs:12:9
   |
12 |     let x: T = foo(S);
   |         ^ doesn't have a size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `dyn T`
   = note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
   = note: all local variables must have a statically known size
   = help: unsized locals are gated as an unstable feature

The following code works, so perhaps it would be useful for the compiler error to suggest using Box:

trait T {}

struct S;

impl T for S {}

fn foo<X: T>(s: X) -> Box<X> {
    Box::new(s)
}

fn main() {
    let x: Box<T> = foo(S);
}
@estebank estebank added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints labels Dec 29, 2018
@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 10, 2019
@estebank
Copy link
Contributor

It should probably also suggest trait objects and link to https://doc.rust-lang.org/book/ch17-02-trait-objects.html

@estebank
Copy link
Contributor

After #106223 the output will be

~/workspace/rust% rustc +dev f57.rs
error[E0308]: mismatched types
  --> f57.rs:11:20
   |
11 |     let x: dyn T = foo(S);
   |            -----   ^^^^^^ expected trait object `dyn T`, found struct `S`
   |            |
   |            expected due to this
   |
   = note: expected trait object `dyn T`
                    found struct `S`

error[E0277]: the size for values of type `dyn T` cannot be known at compilation time
  --> f57.rs:11:9
   |
11 |     let x: dyn T = foo(S);
   |         ^ 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
help: consider borrowing here
   |
11 |     let x: &dyn T = foo(S);
   |            +

We probably should mention Box<dyn T> as well.

@estebank
Copy link
Contributor

Current output:

error[E0308]: mismatched types
  --> src/main.rs:12:20
   |
12 |     let x: dyn T = foo(S);
   |            -----   ^^^^^^ expected `dyn T`, found `S`
   |            |
   |            expected due to this
   |
   = note: expected trait object `dyn T`
                    found struct `S`
   = help: `S` implements `T` so you could box the found value and coerce it to the trait object `Box<dyn T>`, you will have to change the expected type as well

error[E0277]: the size for values of type `dyn T` cannot be known at compilation time
  --> src/main.rs:12:9
   |
12 |     let x: dyn T = foo(S);
   |         ^ 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
help: consider borrowing here
   |
12 |     let x: &dyn T = foo(S);
   |            +

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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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

2 participants