Repository navigation
Keep field reads as the value read, and forget a field written through another object - #567
CatarinaGamboa wants to merge 2 commits into
Conversation
…h another object A tracked field read named the field itself, which stands for its latest instance, so a copy of the field followed later writes. Field reads now name the current instance, and each method gives the fields without one an instance on entry. A write through another object (other.f = v, other.f++) was applied to this.f; this.f is now forgotten before (its old value is not other.f's) and after the write (other may be this). The ghost of a state tracked int field keeps the instance written. Loops now havoc the fields they write, which were named this#f instead of the qualified name, and f = !f on a field no longer throws a NullPointerException. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex review, round 1Codex ( Should be rejected (15 programs): all are rejected on this branch. On main, 9 of them passed:
Correct programs rejected on this branch:
Nothing relevant to fix from this round. |
Codex review, round 2Codex ran 36 programs against this branch and main. They covered constructors and No program that should be rejected passes on this branch. Two loops, Finding: a write to a fresh object inside a loop forgets class C { int f; void test(int n){ C other = new C(); f = 1; while (n > 0) { other.f = 2; n--; } @Refinement("_ == 1") int x = f; } }This branch rejects it; main accepts it. Same on both (not this PR):
Nothing relevant to fix from this round. |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Clean-code reviewAudit of the diff using the clean-code-principles checklist.
Re-checked after the change: |
Fixes #438, fixes #565.
Root causes
#565: a copy of a field followed later writes. A read of a field of this object (
f,this.f) was given the refinement_ == this#C.f, the field's own name. That name stands for the field's latest instance (Variable.getRefinementaddsthis#C.f == <last instance>), soint y = f; f = 5;madey == 5. Reads of locals already name the instance current at the read.#438:
o.x = 5was applied tothis.x.visitAssignmentnamed every field write withUtils.qualifyFieldName, whatever the target, so the write became the new value ofthis.x. A compound write (o.x += 1,o.x--) also started fromthis.x's value.Fix
visitCtFieldReadnames the field's current instance, as a read of a local does. Each method and constructor gives every field that has no instance yet one on entry (snapshotFields, likesnapshotWrittenParameters), so a read always names a fixed value.this.fbefore the write, sinceother.f's old value is notthis.f's, and again after it, sinceothermay bethis(forgetFieldOfOtherObject). The value before the write is shown asother.f, soother.level--now readsfound other.level@48 >= 0 && other.level == other.level@48 - 1.updateGhostField(the ghost of a state-trackedintfield, as int.n = 1) keeps the instance written, not the field's name. Otherwise a later write tothis.nwould changen(t).The first change exposed two related bugs, which are fixed here too:
changedInnamed a fieldthis#finstead ofthis#C.f, so a loop that wrote a field without calling a method did not havoc it.f = 0; while (...) { k = f /* == 0 */; f = 1; }passed on main.f = !fon a boolean field threw aNullPointerException. It happened anywhere, not only in loops (a finding from the Give a conditional expression the value of its condition, not its refinement #561 review). The same-variable branch ofgetUnaryOpRefinementslooked the field up by its simple name. Field reads now take the general path.h.v--on a field of another class also threw aNullPointerExceptionon main and now works.Tests
ErrorFieldReadBeforeWrite(new): copy then write (Soundness: a local copied from an int field follows later writes to the field (int y = f; f = 5 gives y == 5) #565), the ternaryf > 0 ? (f = -1) : 1, a loop that writes a field, andb = !b. All pass on main, or crash there.ErrorFieldOfOtherObject:other.val = 5thenval == 5(Soundness: a field write through another object of the same class (o.x = 5) is applied tothis.x#438), andval = 7; other.val -= 5againstval > 0.field_updates/ErrorFieldUpdate:t.n = -1; n = 1; t.should...()is now rejected; on main,n(t)followedthis.n.TestFoundBindersandTestCounterexamples: updated expectations. Two of them only see internal names, which now name the instance (the shown text is unchanged). The third shows the newother.level--rows.All tests pass:
mvn test, 771 tests.Other programs tried (outside the suite): each of these is rejected now.
o, through an alias ofo, and throughoin a constructor and in an inner class (M.this.fvso.f);this.x = 5; o.x = 3;thenthis.x == 5;this.lim = 5; o.lim += 1ando.lim++against_ < 7;fandthis.fbefore writes byf =,this.f =,f++,f += 2, and statics =/C.s =;if,switch,try,while,do,for, for-each, a lambda, and a loop that calls a method writing the field.These still pass: writes to
other.fof another class (h.v = 1; h.v++; h.v += 2),x = 5; o.y = 3thenx == 5, field initializers,if (f > 0)thenf > 0, andf = 3; f++thenf == 4.Several precision cases that failed on main now pass, e.g.
int v = f; f = v + 1;thenf - v == 1.Behaviour changes
other.f = vmakesthis.funknown (its declared refinement only), sinceothermay bethis.intlocals, sof = 0; while (...) { f = 1; }followed byf >= 0is rejected whenfhas no refinement. Main accepted it only because the loop did not forgetf.ifthat writes the field in one branch is no longer known to equal the field after theifwhen the other branch leaves it alone. This is the existingifjoin, which copies refinements rather than equating instances. Parameters already behaved this way. The previous "pass" came from the aliasing bug.Overlap with #393
The draft #393 also gives non-final fields a fresh instance at method entry (
forgetMutableFieldValues). Both editvisitCtMethod. If #393 lands,snapshotFieldssimply finds those fields already have an instance and skips them.🤖 Generated with Claude Code