-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Open
Labels
A-DSTsArea: Dynamically-sized types (DSTs)Area: Dynamically-sized types (DSTs)A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.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
Given
fn foo(_: &dyn std::error::Error) {}
enum E {
A(Box<dyn std::error::Error>),
}
fn bar(e: &E) {
match e {
E::A(err) => {
foo(err);
}
}
}
error[E0277]: the size for values of type `(dyn std::error::Error + 'static)` cannot be known at compilation time
--> src/main.rs:9:17
|
9 | foo(err);
| ^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn std::error::Error + 'static)`
= note: required because of the requirements on the impl of `std::error::Error` for `Box<(dyn std::error::Error + 'static)>`
= note: required for the cast to the object type `dyn std::error::Error`
Trying the usual go-to fixes (foo(&err)
, foo(&*err)
) produce further errors that aren't helpful either.
The solution to the code is to write foo(&**err)
or foo(err.as_ref())
. We already provide some suggestions for foo(&err)
, foo(*err)
and foo(&*err)
when appropriate and this causes general confusion to enough people that it would be good to extend that machinery to also handle this case, as well as improving the foo(&err)
and foo(&*err)
cases.
diondokter, syvb, slerpyyy, jonhoo, h3ssto and 6 more
Metadata
Metadata
Assignees
Labels
A-DSTsArea: Dynamically-sized types (DSTs)Area: Dynamically-sized types (DSTs)A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.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.