Skip to content

Recalc: an anchor's new formula no longer reads its old spill area - #1427

Merged
aiongg merged 1 commit into
storytold:mainfrom
nihilist-narwhal:fix/spill-anchor-stale-self-read
Oct 22, 2026
Merged

aiongg merged 1 commit into
storytold:mainfrom
nihilist-narwhal:fix/spill-anchor-stale-self-read

Conversation

@nihilist-narwhal

@nihilist-narwhal nihilist-narwhal commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

What happened

When a dynamic array's anchor gets a new formula that reads the cells its old array spilled into, the new formula keeps the old values:

  • A1 =SEQUENCE(3), then A1 =SUM(A2:A3)+1: A2:A3 are empty now, but A1 shows 6 (2 + 3 + 1) instead of 1.
  • A1 =SEQUENCE(3), then A1 =SEQUENCE(1,A2+1): it reads A2 as 2 and spills over A1:C1 instead of showing 1.
  • A1 =SEQUENCE(3), then A1 =SEQUENCE(MAX(1,A3-1)): it reads A3 as 3 and spills 1, 2 over A1:A2 instead of showing 1.

Recalculating from scratch (Calculate Now) gives the right values.

Why

The new formula is evaluated while the old array is still laid out, because spills are only cleared and laid out again at write-back. So it reads the old spilled values. Write-back then sends the dependents of the changed spill area to another round, but it leaves the anchor itself out so that an array never reads its own array. Once the old array was gone, nothing evaluated the anchor again.

Fix

At write-back, the anchor goes to the next round with the other dependents of its spill area when its old array is gone or only partly covered by the new one. It is still left out when the new array covers the old one, because there reading the area would mean reading its own array.

Tests

…l area sees it empty

When a dynamic array's anchor gets a new formula, the formula is evaluated
while the old array is still laid out, so it can read the old spilled
values. The anchor was then never evaluated again: it is left out of its
own spill area's dependents, so as not to read its own array.

- `A1` was `=SEQUENCE(3)` and becomes `=SUM(A2:A3)+1`: it showed 6 (2 + 3
  + 1) with A2:A3 empty; recalculating from scratch gives 1.
- `A1` was `=SEQUENCE(3)` and becomes `=SEQUENCE(1,A2+1)`: it read A2 as 2
  and spilled over A1:C1 instead of showing 1.

The anchor is now evaluated again when its old array is gone or partly
freed. It is still left out when its new array covers the old one, where
it would read its own array.

Regression test in calc
(`an_anchor_given_a_formula_that_reads_its_old_spill_area_sees_it_empty`:
the new formula stops spilling, spills elsewhere, or spills over part of
the old area); it fails without the fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Hamza0101

Copy link
Copy Markdown
Contributor

Not a maintainer, but I have a PR touching the same function so I built this branch and ran your three examples against origin/main. One result that may be worth a line in the description, and a note on how it sits with the other spill PRs.

Your first example reproduces and is fixed:

A1 =SEQUENCE(3) then A1 =SUM(A2:A3)+1     main: 6      this branch: 1

The other two already give 1 on main for me:

A1 =SEQUENCE(3) then A1 =SEQUENCE(1,A2+1)       main: 1
A1 =SEQUENCE(3) then A1 =SEQUENCE(MAX(1,A3-1))  main: 1

I would rather assume my reproduction is the problem than your report. I drove it through the gridcraft-calc test harness — T::set for each formula in turn, which goes through cells_changed — so if you saw those two in the UI or after a different sequence, the harness is simply not reaching the same state. Worth saying how you drove them, since the difference between the first example and the other two is interesting in itself: all three read the old spill area, but only the one whose new formula is not an array reproduces here.

For what it is worth I checked whether main had moved under you: one commit since your merge base, and it does not touch crates/calc/src/recalc.rs, so the spill code you based on is the current one. That rules out the easy explanation.

On overlapping with the other two spill PRs (I mention it because all three of us edit recalc.rs and tests.rs): your branch, #1428 and my #1510 all merge together cleanly, and the gridcraft-calc suite passes with all three applied (62 tests). The only conflict is ROADMAP.md, where each of us appends a line. Nothing for you to do — just so nobody assumes the three need sequencing.

@aiongg
aiongg merged commit 1f1be40 into storytold:main Oct 22, 2026
1 check passed
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.

3 participants