hn.today

A new way to think about borrow checking (in reverse)

verdagon.dev75 points7 comments
Screenshot of A new way to think about borrow checking (in reverse)

Valen introduces a borrow checker that performs its safety checks by walking a function backward from its end to its start. Compilation has four phases: parsing, static typing, static grouping (where each reference is resolved to the precise variable or path it points to), and a backwards safety-check pass. The safety pass maintains a pending set of values that are used later: when encountering a USE it adds the referenced group to pending, when encountering a DECLARE it removes it, and when encountering a MUTATE it verifies that nothing in pending is invalidated. That backwards scan directly discovers use-after-free cases by detecting mutations to groups that are still pending for later use, and it treats reference-target information from static grouping as part of the type to determine invalidation relationships.

The reverse approach simplifies locating last uses, handles loops more naturally, and can be faster by avoiding forward liveness inference. Examples show a definite use-after-free: taking a reference into e.buffs, calling e.add_buff() (which mutates e), then using the reference - the backwards pass sees the later USE, adds the buff reference to pending, then finds the MUTATE to e and flags the conflict. A non-error reordering demonstrates pending being cleared by a DECLARE before mutation. This backward design is unusual compared to typical forward-region constraint solving and forward safety passes, and aims to make memory-safety checking more direct and efficient.

Read on verdagon.dev7 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

Linux 7.3 Released

Linux 7.3 Released

Linux kernel 7.3 has been released, featuring 18,765 commits from over 3,000 developers, including many first-timers. Major updates include new filesystem features, performance improvements, and system call enhancements. (lwn.net)

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.