What kind of borrow checker does rustc currently use?

i try to fingure out how does rustc borrow checker work exactly. and i find some resourecs.

in the above blog, there seems to exist a stack based borrow checker implementation.

but according to the above tutorial, it seems that recent borrow checker based on region inference.

does the stack based borrow checker is deprecated?

1 Like

There are two different things here:

  1. The rules that all pointers must follow, whether or not they are references checked by the borrow checker, or raw pointers managed by unsafe code.
  2. The way the borrow checker works. Including how the borrow checker ensures that you cannot break the rules in point 1 in safe code.

Stacked borrows is a proposed answer to point 1. Your second link is the current answer to point 2.

So, to be clear, stacked borrows is not really related to the borrow checker. (Except that the stacked borrow rules have been designed so that you can implement a borrow checker that enforces something at least as restrictive as the stacked borrows rules.)


thanks for your reply. i forget that borrow checker should only deal with safe code. so basically the current implementation should based on this RFC, 2094-nll - The Rust RFC Book, but i find i don't really understand it even though i have read it couple times. do you know where i can find more thorough description of the underlying implementation?

it seems that the rules in point 1 is a superset of the rules in point 2. so why not just implement the stack based borrow checker? i mean there must be some implementations in current rustc to guarantee the rules that all pointers should follow (include reference and raw pointers).

Stacked borrows is an operational model. For a given execution it is possible to determine if the rules were violated. For example miri does this when interpreting your program. The borrow checker however checks if for every possible execution path the rules are followed rather than just a single one. Due to the halting problem it is necessary to over approximate the restrictions.

For example

if rand_bool() {
    let a;
        let b = 0;
        a = &b; // dangling outside of this block

doesn't have UB if rand_bool() returns false, so miri would be fine with it in that case, but the borrow checker has to unconditionally reject it because if rand_bool() returns true there would be a dereference of a dangling pointer, which is UB.


They are fundamentally two different types of rule sets. Stacked borrows is not a borrow checker.

The purpose of the point 1 rules is to be something that the compiler authors can assume to be true when reasoning about whether the optimizations they are writing are correct. If the compiler breaks code that follows the rules, then it's a bug in the compiler. If the program doesn't follow the rules, then it's not a bug in the compiler. The goals of the rule set are:

  1. Make it possible for the compiler to optimize your code effectively.
  2. Be understandable to people who are writing unsafe Rust code.

Whereas the point 2 rules have these goals:

  1. Be something that can be checked at compile time, even if this makes the rules stricter than the point 1 rules.
  2. Don't be so strict that you can't write useful safe programs while following the rules.

No, there is no such checker in rustc. And it is not a goal for there to be such a checker.

(There is miri, but it's an interpreter that runs a program and detects whether you break the rules at runtime. It does not catch errors at compile time.)


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.