-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-dyn-traitArea: trait objects, vtable layoutArea: trait objects, vtable layoutD-verboseDiagnostics: Too much output caused by a single piece of incorrect code.Diagnostics: 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.Relevant to the compiler team, which will review and decide on the PR/issue.
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
GrigorenkoPV
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-dyn-traitArea: trait objects, vtable layoutArea: trait objects, vtable layoutD-verboseDiagnostics: Too much output caused by a single piece of incorrect code.Diagnostics: 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.Relevant to the compiler team, which will review and decide on the PR/issue.