Confusing mismatched type error (part 2) #48136
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
Related to (but different from) #43608.
If you have a function whose return type uses impl trait, you can get confusing type inference errors. The following code...
...produces the following error:
In a much more complicated and less obvious instance of this issue, I spent a long time trying to figure out why the compiler thought that
impl std::fmt::Debug
implied that my return value had to be()
. It would be very helpful if the compiler could say something like "expected type()
because of this line" and point to the originalreturn
(or whatever line caused the inference).Without impl trait, the issue is less prominent because the types must match exactly, and as a result, if the two different return values have different types, at least one of them must fail to match the stated return type in the function declaration. Consider, for example, the following code:
which gives the following error:
The issue with impl trait is that both return values might implement the trait (in this case, both
()
and&'static str
implementDebug
), and so you don't get the same useful messages that you would with a concrete return type.The text was updated successfully, but these errors were encountered: