Skip to content

mem: --mem gives each var its binding line, use lines, last use and move point, and checks uses after a free - #78

Merged
vic merged 1 commit into
mainfrom
mem-polonius-vars
Nov 12, 2026
Merged

vic merged 1 commit into
mainfrom
mem-polonius-vars

Conversation

@vic

@vic vic commented Nov 12, 2026

Copy link
Copy Markdown
Owner

--mem now reports what the Polonius borrow checker reads about each variable (rust-lang/polonius, polonius-engine/src/facts.rs), as report facts, not as checks that reject a program.

  • vars[] gain defAt (var_defined_at, facts.rs:40: the line of its val, var, pattern or param, null when no binder of its def gives the name), uses (var_used_at, facts.rs:37: every line that reads it; a read inside a while counts at the loop's last line, because the loop runs again), lastUse, movedAt (path_moved_at_base, facts.rs:72: the last read of an unshared var, else null), binds (how many binders give the name in its def) and assigned.
  • New sanity check useAfterFree (Polonius errors, output/mod.rs:78: a live loan invalidated): a var that can hold a box of region r, read after r.free() in the def that handles r. It must be empty on every program (the memfail gate check).

The design table (each Polonius fact and its srs counterpart) is in the Mem design notes, section M9.

Two false positives found and scoped. Mem keys a var by its def and name, and has no kill at an assignment. So in prog/scope_shadow_inner the inner and the outer x merge (the outer one is read after the inner one's region is freed), and in prog/var_ok xs is reassigned off the freed region first. The check now claims a use after free only for a var bound once and never assigned; binds and assigned show where it claims nothing. On s2c, 1,448 var nodes merge shadowed binders: one node per binder and a kill at an assignment are filed as follow-up work.

Checks:

  • The old fields of the mem tests are unchanged; the line facts on mem/moves match the source by hand.
  • fail is empty on all 126 prog tests, the mem tests and s2c.
  • 830-output identity; fixpoint; seed updated.
  • Speed on s2c: normal compile 554,827,354 -> 554,827,328; --mem 1.952 G -> 2.035 G (+4.3%). The JSON on s2c is 10.5 MB.

Mutants (each builds):

mutant killed by
useAfterFree tests last < free mem/frees (a0) and 6 prog tests in memfail
the binds == 1 guard dropped memfail on prog/scope_shadow_inner
the !assigned guard dropped memfail on prog/var_ok
a read in a loop counts at its own line all 13 mem tests
pattern binders not bound all 13 mem tests

@vic
vic merged commit 85b9f35 into main Nov 12, 2026
3 checks passed
@vic
vic deleted the mem-polonius-vars branch November 17, 2026 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant