Functions/closures do not coerce to trait objects and give confusing diagnostics #87093
Labels
A-coercions
Area: implicit and explicit `expr as Type` coercions
A-diagnostics
Area: Messages for errors, warnings, and lints
A-trait-objects
Area: trait objects, vtable layout
D-confusing
Diagnostics: Confusing error or lint that should be reworked.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Bad case:
Worse case:
In all cases, the workaround is to cast to the function type, for example
&(||{} as fn())
. But this is not obvious, not hinted by the diagnostic, and it is surprising that it is required. Moreover, it can in one case produce an even worse diagnostic:It would be most convenient if no casting would be needed. If casting is unavoidable however, the diagnostic could at least hint this.
These are minimal cases with minimally complicated types. In practice the problem will be less clear. Consider for example that the type signature shown in the diagnostic may include implied lifetime details. It is then easy to mistakenly assume that the lifetimes are the problem.
A practical use of this coercion is making type-safe arrays of pointers to functions with diverse signatures. In such a context it can be especially annoying to have to write each signature twice (in the function/closure definition and in the cast).
The text was updated successfully, but these errors were encountered: