-
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-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.Diagnostics: A diagnostic that is giving misleading or incorrect information.D-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[E0746]: return type cannot have an unboxed trait object
--> <source>:5:13
|
5 | fn fun() -> Trait {
| ^^^^^ doesn't have a size known at compile-time
|
help: box the return type, and wrap all of the returned values in `Box::new`
|
5 ~ fn fun() -> Box<Trait> {
6 ~ Box::new(todo!())
|
error[E0782]: trait objects must include the `dyn` keyword
--> <source>:5:13
|
5 | fn fun() -> Trait {
| ^^^^^
|
help: use `impl Trait` to return an opaque type, as long as you return a single underlying type
|
5 | fn fun() -> impl Trait {
| ++++
help: alternatively, you can return an owned trait object
|
5 | fn fun() -> Box<dyn Trait> {
| +++++++ +
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0746, E0782.
For more information about an error, try `rustc --explain E0746`.
Desired output
error[E0782]: trait objects must include the `dyn` keyword
--> <source>:5:13
|
5 | fn fun() -> Trait {
| ^^^^^
|
help: use `impl Trait` to return an opaque type, as long as you return a single underlying type
|
5 | fn fun() -> impl Trait {
| ++++
help: alternatively, you can return an owned trait object
|
5 | fn fun() -> Box<dyn Trait> {
| +++++++ +
Rationale and extra context
This error report has two issues:
- E0746 incorrectly suggests to return a
Box<Trait>
. - Both the errors say the same thing and E0746 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-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.Diagnostics: A diagnostic that is giving misleading or incorrect information.D-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.