RFC: type errors involving auto implicits should warn about auto implicits #6462
Labels
P-medium
We may work on this issue if we find the time
RFC accepted
RFC is waiting for a corresponding PR (external or internal)
RFC
Request for comments
Context
Currently, by default, when an unbound variable is accidentally used, the error message produced is not helpful. The exact error message produced depends on the situation, but it often involves a metavariable indicating the (could not be inferred) type of the implicitly bound variable, as well as an actual message that is completely unrelated. Nothing in the message suggests that this could be a misspelled variable, or that there is a constant with the same name but not in the current scope.
I have noticed many beginners have tripped on this issue, including myself when I was learning Lean. Without the help of someone who specifically knows about this counterintuitive (yet logical) behavior of Lean, it can be pretty challenging to find the cause of the error, especially for someone who may not know yet the precise differences between modules, namespaces and scopes in Lean.
Just to cite one example of this, consider this SO question:
This produces the following error message:
I don't know whether the person who asked the question knew about the auto implicit option, but if they didn't (which is not unlikely, given that it's opt out), it would have been pretty much impossible for them to figure out what is wrong. Especially because they seem to think that loading the
Std.Data.HashMap
implies having theHashMap
constant available in the current scope.Proposal
Have the error message suggest that this might be due to a misspelled name, or referencing a name present in an other namespace. Consider what happens, for instance, in Rust:
Both of these hints are accompanied by LSP actions that execute the suggestion.
This is a non-trivial task because, at least in my understanding, auto implicits can cause a wide variety of errors for which it is not always easy to determine that we should instead report that "the variable implicitly bound" is such a variable.
Orthogonally to this solution, one could also think of having auto implicits off by default. It is a bit of a mistery to me who would want to use such a feature, because my experience with it has been that by the time I had reached the expertise required not to be confused about what this feature does exactly, and what its gotchas are, I was already convinced that it was a bad software engineering practice to have it on, because although it makes me type less, it also makes me think more when I need to read code that relies on it, which in this case I don't think is a good tradeoff. This is of course only my opinion on the matter, and other people might think that the tradeoff is actually worth it, but:
This is especially true as, IIRC, there is a weaker version of auto implicits that only does so with names that look like parametric types, which is I think the only time this option is meant to be used anyways.
This is easy to implement. It's a breaking change, but it's not that hard to port a pre-change project: just explicitly opt in for this feature, and everything will work as before the change.
Impact
Add 👍 to issues you consider important. If others benefit from the changes in this proposal being added, please ask them to add 👍 to it.
The text was updated successfully, but these errors were encountered: