Skip to content

Verbose Error When a Function Takes a Bare Trait as Parameter #127690

Closed
@veera-sivarajan

Description

Code

// edition: 2021
trait Trait {}

fn fun(_: Trait){
    todo!()
}

fn main() {}

Current output

error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
 --> <source>:5:8
  |
5 | fn fun(_: Trait){
  |        ^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `(dyn Trait + 'static)`
help: you can use `impl Trait` as the argument type
  |
5 | fn fun(_: impl Trait){
  |           ++++
help: function arguments must have a statically known size, borrowed types always have a known size
  |
5 | fn fun(_: &dyn Trait){
  |           ++++

error[E0782]: trait objects must include the `dyn` keyword
 --> <source>:5:11
  |
5 | fn fun(_: Trait){
  |           ^^^^^
  |
help: use a new generic type parameter, constrained by `Trait`
  |
5 | fn fun<T: Trait>(_: T){
  |       ++++++++++    ~
help: you can also use an opaque type, but users won't be able to specify the type parameter when calling the `fn`, having to rely exclusively on type inference
  |
5 | fn fun(_: impl Trait){
  |           ++++
help: alternatively, use a trait object to accept any type that implements `Trait`, accessing its methods at runtime using dynamic dispatch
  |
5 | fn fun(_: &dyn Trait){
  |           ++++

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0277, E0782.
For more information about an error, try `rustc --explain E0277`.

Desired output

error[E0782]: trait objects must include the `dyn` keyword
 --> <source>:5:11
  |
5 | fn fun(_: Trait){
  |           ^^^^^
  |
help: use a new generic type parameter, constrained by `Trait`
  |
5 | fn fun<T: Trait>(_: T){
  |       ++++++++++    ~
help: you can also use an opaque type, but users won't be able to specify the type parameter when calling the `fn`, having to rely exclusively on type inference
  |
5 | fn fun(_: impl Trait){
  |           ++++
help: alternatively, use a trait object to accept any type that implements `Trait`, accessing its methods at runtime using dynamic dispatch
  |
5 | fn fun(_: &dyn Trait){
  |           ++++

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0277, E0782.
For more information about an error, try `rustc --explain E0277`.

Rationale and extra context

This return two different errors, E0277 and E0782, but they both show similar suggestions.

E0277 should be suppressed in favor of E0782.

Other cases

No response

Rust Version

rustc 1.79.0

Anything else?

No response

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-objectsArea: trait objects, vtable layoutD-verboseDiagnostics: Too much output caused by a single piece of incorrect code.T-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