Search code examples
rustcompiler-construction

Is there some sort of sequence in which the compiler operates?


I am rewriting a C++ program to Rust, and there is one thing that gives me an emotional rollercoaster. At first iteration it gives me, say 50 errors, then I solve them one by one, and just when I solve the last one, the compiler gives me 60 fresh errors, then I solve them and get another few tens of errors.

The last (yet) set of errors seems to be generated exclusively by the borrow checker. So why does this happen? Are there some layers or stages of the compiling process, and if yes what are they?

I want to know that, because I like predictability and dislike emotional rollercoasters (also I want to know when this adventure is going to end).


Solution

  • Yes, there is an order:

    1. Syntax errors, from parsing the source code
    2. Non-lifetime type errors, from the type checker
    3. Lifetime errors, from the borrow checker

    The first two are common to most typed languages. You need to build some kind of model of the type relationships before checking them, which will fail fast if the syntax is incorrect. In Rust, a subsequent step is to verify that all borrows are valid, once the basic type check has passed.

    You can read more in the blog post, Introducing MIR .