Compiler guesses incorrect type after error occurs #76589
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
A-inference
Area: Type inference
D-verbose
Diagnostics: Too much output caused by a single piece of incorrect code.
The following code:
prints the following error messages:
The compiler appears to have inferred the type of
a
to bestr
, based on the statementlet b: &str = &a
. However, this is almost certainly the wrong thing to do. The fact that an error occurred while type-checking the initial declaration means that we cannot be sure of what type the user intended to use. In this case, unifying&str
with&a
produces an incorrect type ofa
, since a deref coercion will likely be involved in the corrected code (e.g. if the user writeslet a = String::new()
).We should be more cautious about inferring the types of locals when an error has occurred. If we are certain that no coercions can be occurring (e.g. passing
a
to a function that takes aString
parameter), then continuing on to infer the variable's type should be fine.The text was updated successfully, but these errors were encountered: