Improve diagnostic for casting T: ?Sized + Trait
to dyn Trait
#88141
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
D-confusing
Diagnostics: Confusing error or lint that should be reworked.
D-newcomer-roadblock
Diagnostics: Confusing error or lint; hard to understand for new users.
D-terse
Diagnostics: An error or lint that doesn't give enough information about the problem at hand.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Given the following code: (playpen)
The current output is:
This output is correct, however it is not quite clear in the standard mental model people have. In the standard mental model of
?Sized
, in this context people will thinkT: Trait + ?Sized
meansT
can bedyn Trait
, which should obviously be castable todyn Trait
when behind aBox
. So the error feels incorrect.The reason the error is correct is that
T
could be a different unsized type that implements the trait, for example[u8]
ordyn OtherTrait
. In such a case the cast todyn Trait
would require some kind of double-DST, which doesn't exist.I'm not quite sure how but it would be nice if this error could make this clearer.
In this specific case, if
convert
were a trait method, the solution would to have an impl forBox<T: Sized>
and one forBox<dyn Trait>
. Unsure if there's a good way to indicate that, but that's not as necessary as making the error clearer about whyT: Trait + ?Sized
can't be cast todyn Trait
The text was updated successfully, but these errors were encountered: