4 ms·
As someone who is not familiar with Rust at all, wow—what refreshingly helpful error messages!
by wilkystyle 8y ago
As someone who is not familiar with Rust at all, wow—what refreshingly helpful error messages!
- nnethercote 8y agoYes, the compiler's error messages are extremely good. They often give correct and useful suggestions on how to change code to make it compile. This can be invaluable when dealing with trickier corners of the language such as explicit lifetimes.
- krackers 8y agoCan someone chime in as to why C++ compilers in particular are notorious for extremely verbose yet unhelpful error messages? Is it something endemic to parsing the language that there can't be errors as helpful as Rust's?
- oddity 8y agoThe short answer, devoid of some interesting nuance, is that C++’s template system is similar to an untyped functional programming language, so the errors you get are the compiler’s attempt to make sense of the large tree of nonsense it couldn’t reduce. C++ also suffers from years of backwards-compatibility twister creating abundant cases of ambiguity if you do something wrong. The compiler will do it’s best, but if your code suddenly parses wildly differently because your semicolon is missing, oh well. Rust is far simpler in that regard, so there are fewer instances where your code might accidentally make sense to the compiler if you have an error.
- bluejekyll 8y ago> Rust is far simpler in that regard, so there are fewer instances where your code might accidentally make sense to the compiler if you have an error. I say this from a place of love for Rust, but the first time you run into a massive generic type tree error thats root cause is a type parameter three levels deep not implementing Send (or some other trait) it takes your breath away. There are some great jokes about this in Deisel and Futures for example. It makes Java generics look like a baby just starting to craw. The error messages are good, you just need to go grab a coffee while you read through the book on type theory that’s been dumped to your screen.
- marcosdumay 8y agoSimilar stuff happens on Haskell too. I guess that since the type system is an unityped logic programming language, it is an even worse place than C++ templates. It is a smaller problem on practice only because the type declarations are much shorter programs than C++ templates.
- fmap 8y agoLet me just stress the first point, because that's exactly where Rust and C++ differ: C++ templates really are untyped. There is no way to check whether a template definition is correct. A C++ compiler has to first expand the templates ("complete monomorphisation") and then perform type checking. Any errors you get will be in terms of the template instantiations, not your original code. Unlike C++, Rust has a type system for the full language. In particular, traits are type checked once and you get errors in terms of the code you wrote yourself. There are other compilers that use complete monomorphisation, such as the MLton compiler for Standard ML, where you don't hear horror stories about terrifying error messages because your code was checked before being specialized. I want to stress that this is a terrible design decision in terms of usability, because it is incredibly attractive from an implementation perspective. Parametric types are a delicate issue in an imperative language and always end up rejecting some perfectly fine programs. Monomorphisation both eliminates this issue and potentially leads to more efficient code, but your type errors will suffer. I could say more about this - there are more trade-offs involved - but actually working with template heave C++ code is a better argument than anything I could say. ...That said, the examples in the blog post actually don't use any complicated machinery, and the quality of the compiler errors just comes down to very good engineering on the part of the Rust development team. :)
- nnethercote 8y agoAs well as templates, C++ has function overloading, so when a function is named in an error message it has to show all the arguments as well. And if it can't find a function variant that matches a call, it'll print out a "did you mean this one?" message for all of them.
- masklinn 8y agoAnd of course C++ harks back to an era where error messages were not considered much of a concern. It took the Clang project for C compilation errors to start improving (including kicking gcc's ass), and C is not the most difficult language to parse/validate.
- twic 8y agoI suspect it's the inverse: the Rust compiler team has specifically put a lot of effort into good error messages, whereas for C++ compiler developers it's been a low priority. Some of the inspiration came from Elm [1], which has famously good error messages. [1] https://blog.rust-lang.org/2016/08/10/Shape-of-errors-to-come.html https://blog.rust-lang.org/2016/08/10/Shape-of-errors-to-com...
- abacate 8y agoC++ template system is too generic (Turing complete), and it is usually the one to blame for the verboseness of error messages. Something like Concepts would probably make it more like a proper "generics" mechanism and allow better error messages - ie, "this type does not implement EqualityTrait" instead of "signature mismatch <detailed and very verbose function declaration 1> and <detailed and very verbose function declaration 2>". Despite this, newer g++ versions have better error messages overall - but sometimes you are stuck with an older version of the compiler as well.