Compiler reminders

37 points by jfmengels


ekuber

Even though we don’t call it that, the Rust compiler errors are written in this way. If we notice or get a report of a common incorrect code transformation or subtle mistake (like a typo of :: or ; to :, or writing an enum variant where a type was expected), we look at what the steps necessary to get to the intended effect (when one or two are obvious), and provide suggestions in that direction. When the compiler stage architecture is amenable, we skip intermediary code transformation steps and suggest the target. But if the compiler doesn’t yet have enough information (like needing type information during parsing), and changing the error to be emitted later is infeasible for the benefit (like the turbo fish recovery, it is purely syntactical to avoid implementation complexity), we make it so that the suggested code results in an error that does suggest you the right code, in no more than 3 steps. So you end up having to implement the same or similar suggestion in 2 or 3 places, but in the end it means that no matter what the user tried to write, we have several chances of “catching” the user straying from the right path and nudging then in the right direction. The other benefit of this is that when the transformation is of high confidence, then the compiler can recover and continue emitting errors from later stages that are actually useful. When they aren’t, we try to poison the smallest possible element and mark it as “don’t emit more errors from this”. So errors should always be relevant. Periodic reminder that if they aren’t accurate, relevant or useful, tickets are welcome.

typesanitizer

This is why we often advise Elm developers to list out all the branches in case expressions rather than using a wildcard. Even if it sometimes feels tedious, it increases the number of cases where making a change leads to getting compiler reminders.

I find it unfortunate that this kind of recommendation keeps going around – this is really a tooling failure that ought to be fixable.

E.g. imagine if you could annotate the new case with something (be it a magic comment or something else) indicating that it is new. Then a compiler or linter with access to pattern-matching information could tell you all places where the new case is being matched against using a wildcard pattern. You could then go audit them one-by-one.

At the same time, it’s understandable that this recommendation is made to users, because most users don’t write or contribute directly to tools…

lobo

very nice post! small problem though, the link in the last paragraph gets to a non-existent page due to it linking to https://jfmengels.net/compiler-reminders/constraints-and-guarantees instead of https://jfmengels.net/constraints-and-guarantees