From 28a645ca13d60d0031fe8e463cb998e29bd19d20 Mon Sep 17 00:00:00 2001 From: quantmind-br Date: Tue, 13 Oct 2026 08:00:40 -0300 Subject: [PATCH 1/8] Calc: a contested spill area stays with its array on full recalc and reopening (#964) When two dynamic arrays want the same cells, the one already spilling there keeps them and the newcomer is #SPILL!, as when the second is entered. recalc_all remembers the layout it clears (formulas that spilled, formulas that were #SPILL!) and lays arrays out in that order, then in reading order. The XLSX reader restores a dynamic array's saved `ref` as its spill range, because the file can only cache #VALUE! for a blocked array. Found by the command-sequence fuzz. --- ROADMAP.md | 1 + crates/calc/src/recalc.rs | 36 +++++++++++++++++++++++++++-------- crates/calc/src/tests.rs | 14 ++++++++++++++ crates/engine/src/tests.rs | 15 +++++++++++++++ crates/xlsx/src/sheet_read.rs | 8 ++++++++ 5 files changed, 66 insertions(+), 8 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 070552d07..9cd46e5fc 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -132,6 +132,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | What landed | |---|---| +| 2026-10-13 | Spills: when two dynamic arrays want the same cells, the one already spilling there keeps them after Calculate Now and after saving and reopening, as when the second was entered (#964, found by the command-sequence fuzz) | | 2026-10-12 | Rows auto-fit oversized fonts: opening a file and whole-row/column formatting grow non-custom rows to their tallest font (#833) | | 2026-10-12 | Number formats: parsing a format code is linear in its length (long runs of `[`, `,` or `/` no longer take quadratic time), and an unclosed `[` is a literal when splitting sections as well as when tokenizing (#810) | | 2026-10-12 | Number formats: a fraction denominator with 65,536 zeros no longer crashes; format codes are capped at 255 characters like Excel (longer codes display as General, `TEXT` gives `#VALUE!`) (#807) | diff --git a/crates/calc/src/recalc.rs b/crates/calc/src/recalc.rs index b391821d7..f8dc677d5 100644 --- a/crates/calc/src/recalc.rs +++ b/crates/calc/src/recalc.rs @@ -781,6 +781,17 @@ impl Host for PassHost<'_> { } } +/// What a full recalculation knows about the spill layout it clears, so a contested area goes to +/// the same formula as in an incremental recalculation. +#[derive(Default)] +struct Prior { + /// Formulas that spilled (their area, from the last calculation or the file's `ref`). + spilled: HashSet, + /// Formulas that showed #SPILL!. A cell's value moves with it on a structural edit, its spill + /// area doesn't. + blocked: HashSet, +} + impl Calc { pub fn new() -> Self { Self::default() @@ -814,16 +825,19 @@ impl Calc { self.rebuild(wb); // Spills are laid out again from the formulas where they are now (a structural edit moves // or deletes the formulas, not the values they spilled). + let mut prior = Prior::default(); for i in 0..wb.sheets.len() { if wb.sheet(i).is_some_and(|s| !s.spill_ranges.is_empty() || !s.spill.is_empty()) && let Some(sh) = wb.sheet_mut(i) { + prior.spilled.extend(sh.spill_ranges.iter().filter(|(_, r)| !r.is_single()).map(|(c, _)| (i, *c))); sh.spill.clear(); sh.spill_ranges.clear(); } } let all: Vec = self.graph.nodes.keys().copied().collect(); - self.run(wb, all, true); + prior.blocked = all.iter().copied().filter(|k| wb.sheet(k.0).is_some_and(|s| s.value(k.1) == Value::Error(CellError::Spill))).collect(); + self.run(wb, all, &prior); } /// Updates the graph for cells whose content changed and recalculates what depends on them. @@ -860,7 +874,7 @@ impl Calc { if wb.calc.mode == CalcMode::Manual { // Only the edited formulas themselves are evaluated. let own: Vec = changed.iter().copied().filter(|k| self.graph.nodes.contains_key(k)).collect(); - self.run(wb, own, false); + self.run(wb, own, &Prior::default()); return; } let mut seeds: Vec = changed.to_vec(); @@ -908,7 +922,7 @@ impl Calc { let dirty = self.dirty_closure(wb, &seeds); let t1 = prof_now(); let n = dirty.len(); - self.run(wb, dirty, false); + self.run(wb, dirty, &Prior::default()); if std::env::var_os("GRIDCRAFT_PROFILE").is_some() { eprintln!("recalc: {} seeds, closure {} cells {:.1} ms, run {:.1} ms", seeds.len(), n, t1 - t0, prof_now() - t1); } @@ -947,8 +961,9 @@ impl Calc { seen.into_iter().collect() } - /// Evaluates the dirty set and writes results (and spills) back. - fn run(&mut self, wb: &mut Workbook, mut dirty: Vec, full: bool) { + /// Evaluates the dirty set and writes results (and spills) back. `prior` is the layout a full + /// recalculation cleared (empty otherwise). + fn run(&mut self, wb: &mut Workbook, mut dirty: Vec, prior: &Prior) { let mut dynamic_again = !self.graph.dynamic.is_empty(); for _round in 0..8 { if dirty.is_empty() { @@ -997,6 +1012,14 @@ impl Calc { cell.value = v; } } + // When two arrays want the same cells, the one already spilling there keeps them and + // the newcomer is #SPILL! (as when it is entered), and one that was #SPILL! stays so: + // arrays are laid out in that order, then in reading order. Without this, a full + // recalculation or reopening the file handed the area to whichever came first. + dirty.sort_by_key(|k| { + let owns = prior.spilled.contains(k) || wb.sheet(k.0).is_some_and(|s| s.spill_ranges.contains_key(&k.1)); + (prior.blocked.contains(k), !owns) + }); for k in &dirty { let new = spills.get(k); let Some(sheet) = wb.sheet_mut(k.0) else { continue }; @@ -1062,9 +1085,6 @@ impl Calc { } } } - if full { - // Everything was already evaluated once; spill dependents need another pass. - } if std::env::var_os("GRIDCRAFT_PROFILE").is_some() { eprintln!(" write-back {:.1} ms", prof_now() - tw); } diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 196c57de7..991076d7a 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -368,6 +368,20 @@ fn spill_into_the_old_area_of_a_blocked_neighbour() { assert_eq!([t.num("C8"), t.num("D8"), t.num("C9"), t.num("D9")], [1.0, 2.0, 3.0, 4.0]); } +#[test] +fn a_full_recalc_keeps_a_spill_with_the_formula_already_spilling_there() { + // F13's array would spill over F14:G14, which C14's array already fills: F13 is #SPILL!, and + // a full recalculation (Ctrl+Alt+F9) doesn't hand the area to F13 for coming first in + // reading order. + let mut t = T::new(); + t.set("C14", "=SEQUENCE(1,7)"); + t.set("F13", "=SEQUENCE(5,2)"); + assert_eq!(t.get("F13"), Value::Error(CellError::Spill)); + t.calc.recalc_all(&mut t.wb); + assert_eq!(t.get("F13"), Value::Error(CellError::Spill)); + assert_eq!([t.num("C14"), t.num("G14"), t.num("I14")], [1.0, 5.0, 7.0]); +} + #[test] fn arrays_do_not_spill_in_tables() { // Excel: spilled array formulas aren't supported in tables. diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index ae499a189..280c1476a 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -2008,6 +2008,21 @@ fn a_spill_that_goes_away_unblocks_another() { assert_eq!((v(&s, "E10"), v(&s, "G10")), (Value::Number(1.0), Value::Number(3.0))); } +#[test] +fn a_contested_spill_area_stays_with_its_array_after_reopening() { + // F13's array wants F14:G14, which C14's array already fills, so F13 is #SPILL!. The file + // can only cache #VALUE! for it; reopening (which recalculates) used to hand the area to F13 + // for coming first in reading order. + let mut s = s(); + s.execute("cell.set", json!({"cell": "C14", "input": "=SEQUENCE(1,7)"})).unwrap(); + s.execute("cell.set", json!({"cell": "F13", "input": "=SEQUENCE(5,2)"})).unwrap(); + assert_eq!(v(&s, "F13"), Value::Error(gridcraft_core::CellError::Spill)); + let saved = s.execute("file.saveBytes", json!({"format": "xlsx"})).unwrap(); + s.execute("file.open", json!({"name": "contested.xlsx", "base64": saved["base64"]})).unwrap(); + assert_eq!(v(&s, "F13"), Value::Error(gridcraft_core::CellError::Spill)); + assert_eq!((v(&s, "C14"), v(&s, "G14"), v(&s, "I14")), (Value::Number(1.0), Value::Number(5.0), Value::Number(7.0))); +} + #[test] fn an_array_reading_another_arrays_spill_is_updated() { // G5 is evaluated before D11 in the same pass, so it first reads D11's old spill. diff --git a/crates/xlsx/src/sheet_read.rs b/crates/xlsx/src/sheet_read.rs index 0194d2268..bd4f79380 100644 --- a/crates/xlsx/src/sheet_read.rs +++ b/crates/xlsx/src/sheet_read.rs @@ -452,6 +452,14 @@ fn read_cell(cx: &mut Ctx<'_>, sheet: &mut Sheet, st: &mut RowState, c: &El, pos let dynamic = c.attr("cm").is_some(); let formula = c.child("f").and_then(|f| read_formula(cx, st, f, pos, dynamic)); let value = if formula.is_none() && st.dynamic.iter().any(|r| r.contains(pos) && r.start != pos) { Value::Empty } else { value }; + // A dynamic array keeps the area it was saved spilling over (`ref`): when two arrays want the + // same cells, recalculating gives them to this one again, as before saving. + if dynamic + && formula.as_ref().is_some_and(|f| f.array.is_none()) + && let Some(r) = st.dynamic.last().filter(|r| r.start == pos) + { + sheet.spill_ranges.insert(pos, *r); + } sheet.cells.set(pos, Cell { value, formula: formula.map(Arc::new), style }); } From cbe10f0501bcf41f25b485e34e5f9c246de63bfb Mon Sep 17 00:00:00 2001 From: quantmind-br Date: Thu, 22 Oct 2026 21:37:28 -0300 Subject: [PATCH 2/8] Recalc: clearing an array frees its whole spill area for a blocked array Clearing (or overwriting) a dynamic array's anchor empties its whole former spill area, but only arrays above and left of the anchor were re-evaluated, so an array blocked further right (F13 against C14's C14:I14) stayed #SPILL! until the next full recalculation. Reported by iluebbe on #964. --- ROADMAP.md | 2 +- crates/calc/src/recalc.rs | 7 +++++++ crates/engine/src/tests.rs | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/ROADMAP.md b/ROADMAP.md index 83e83c51e..ac6c7f201 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -158,7 +158,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | 2026-10-13 | XLSX export writes a picture used on several sheets once, as one `xl/media` part every drawing points to (it wrote a copy per drawing: a 16-sheet workbook with one 112 KB logo grew from 0.4 to 1.9 MB) (#985) | | 2026-10-13 | XLSX: `[Content_Types].xml`, `_rels/.rels`, the workbook rels and `xl/workbook.xml` are the first zip entries (document properties last), so libmagic and PHP `finfo` detect the file type even with large properties (#1090) | | 2026-10-13 | Conditional formatting: a rule gathers range statistics only when it reads them (top/bottom, above average, duplicates, scales, bars, icons) and borrows them instead of copying them per cell, so the per-frame lookup of an expression or cell-value rule no longer visits its whole range (#1098, part 1) | -| 2026-10-13 | Spills: when two dynamic arrays want the same cells, the one already spilling there keeps them after Calculate Now and after saving and reopening, as when the second was entered (#964, found by the command-sequence fuzz) | +| 2026-10-13 | Spills: when two dynamic arrays want the same cells, the one already spilling there keeps them after Calculate Now and after saving and reopening, as when the second was entered; clearing it lets the other spill at once (#964, found by the command-sequence fuzz) | | 2026-10-12 | Next/previous sheet navigation stops at the first/last visible worksheet instead of activating a hidden boundary sheet; the current selection is retained | | 2026-10-12 | Rows auto-fit oversized fonts: opening a file and whole-row/column formatting grow non-custom rows to their tallest font (#833) | | 2026-10-12 | Number formats: parsing a format code is linear in its length (long runs of `[`, `,` or `/` no longer take quadratic time), and an unclosed `[` is a literal when splitting sections as well as when tokenizing (#810) | diff --git a/crates/calc/src/recalc.rs b/crates/calc/src/recalc.rs index f2aefe676..ff3a97750 100644 --- a/crates/calc/src/recalc.rs +++ b/crates/calc/src/recalc.rs @@ -1018,6 +1018,13 @@ impl Calc { } } } + // A cleared anchor empties its whole former spill area too. + for (si, r) in &cleared { + match emptied.iter_mut().find(|x| x.0 == *si) { + Some(x) => x.1 = CellRef::new(x.1.row.max(r.end.row), x.1.col.max(r.end.col)), + None => emptied.push((*si, r.end)), + } + } // An emptied cell can unblock an anchor any distance above or to the left of it. for (si, end) in emptied { for &(s, c) in self.graph.nodes.keys() { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index a2b95fc84..728ed0d1c 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -2472,6 +2472,20 @@ fn a_spill_that_goes_away_unblocks_another() { assert_eq!((v(&s, "E10"), v(&s, "G10")), (Value::Number(1.0), Value::Number(3.0))); } +#[test] +fn clearing_the_array_that_holds_a_contested_area_lets_the_other_spill() { + // F13's array wants F14:G14, inside C14's spill C14:I14. Clearing C14 empties the whole + // area, to the right of C14 too, so F13 spills straight away, as in Excel. + let mut s = s(); + s.execute("cell.set", json!({"cell": "C14", "input": "=SEQUENCE(1,7)"})).unwrap(); + s.execute("cell.set", json!({"cell": "F13", "input": "=SEQUENCE(5,2)"})).unwrap(); + assert_eq!(v(&s, "F13"), Value::Error(gridcraft_core::CellError::Spill)); + s.execute("edit.clearContents", json!({"range": "C14"})).unwrap(); + assert_eq!((v(&s, "F13"), v(&s, "G17")), (Value::Number(1.0), Value::Number(10.0))); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!((v(&s, "F13"), v(&s, "I14")), (Value::Error(gridcraft_core::CellError::Spill), Value::Number(7.0))); +} + #[test] fn a_contested_spill_area_stays_with_its_array_after_reopening() { // F13's array wants F14:G14, which C14's array already fills, so F13 is #SPILL!. The file From 6c2ba271105e6f2ac4b490b67d9409eb04ac266a Mon Sep 17 00:00:00 2001 From: quantforge-br <218010578+quantforge-br@users.noreply.github.com> Date: Sat, 31 Oct 2026 14:41:02 -0300 Subject: [PATCH 3/8] Calc: an array that grew into another's spill area is laid out after it on full recalc --- crates/calc/src/recalc.rs | 35 +++++++++++++++++++++++++++++------ crates/calc/src/tests.rs | 17 +++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/crates/calc/src/recalc.rs b/crates/calc/src/recalc.rs index f81d54324..88c3c18ed 100644 --- a/crates/calc/src/recalc.rs +++ b/crates/calc/src/recalc.rs @@ -113,13 +113,34 @@ fn components(edges: &[Vec]) -> (Vec, Vec) { /// the same formula as in an incremental recalculation. #[derive(Default)] struct Prior { - /// Formulas that spilled (their area, from the last calculation or the file's `ref`). - spilled: HashSet, + /// Formulas that spilled, with the area they filled (from the last calculation or the file's + /// `ref`). + spilled: HashMap, /// Formulas that showed #SPILL!. A cell's value moves with it on a structural edit, its spill /// area doesn't. blocked: HashSet, } +impl Prior { + /// Formulas whose new array reaches past their own former area into another formula's: they + /// are laid out after it, as in an incremental recalculation, where that area is still filled. + fn grown_into_another(&self, dirty: &[Key], spills: &HashMap>) -> HashSet { + let mut out = HashSet::default(); + for k in dirty { + let (Some(old), Some(array)) = (self.spilled.get(k), spills.get(k)) else { continue }; + let end = CellRef::new( + k.1.row.saturating_add((array.rows as u32).saturating_sub(1)), + k.1.col.saturating_add((array.cols as u32).saturating_sub(1)), + ); + let area = RangeRef::new(k.1, end); + if !old.contains_range(&area) && self.spilled.iter().any(|(o, r)| o != k && o.0 == k.0 && r.intersects(&area)) { + out.insert(*k); + } + } + out + } +} + impl Calc { pub fn new() -> Self { Self::default() @@ -205,7 +226,7 @@ impl Calc { if wb.sheet(i).is_some_and(|s| !s.spill_ranges.is_empty() || !s.spill.is_empty()) && let Some(sh) = wb.sheet_mut(i) { - prior.spilled.extend(sh.spill_ranges.iter().filter(|(_, r)| !r.is_single()).map(|(c, _)| (i, *c))); + prior.spilled.extend(sh.spill_ranges.iter().filter(|(_, r)| !r.is_single()).map(|(c, r)| ((i, *c), *r))); sh.spill.clear(); sh.spill_ranges.clear(); } @@ -391,11 +412,13 @@ impl Calc { self.circular.extend(cycles); // When two arrays want the same cells, the one already spilling there keeps them and // the newcomer is #SPILL! (as when it is entered), and one that was #SPILL! stays so: - // arrays are laid out in that order, then in reading order. Without this, a full + // arrays are laid out in that order, then in reading order. Of two that both spilled, + // one that grew into the other's former area comes second. Without this, a full // recalculation or reopening the file handed the area to whichever came first. + let grown = if prior.spilled.is_empty() { HashSet::default() } else { prior.grown_into_another(&dirty, &spills) }; dirty.sort_by_key(|k| { - let owns = prior.spilled.contains(k) || wb.sheet(k.0).is_some_and(|s| s.spill_ranges.contains_key(&k.1)); - (prior.blocked.contains(k), !owns) + let owns = prior.spilled.contains_key(k) || wb.sheet(k.0).is_some_and(|s| s.spill_ranges.contains_key(&k.1)); + (prior.blocked.contains(k), !owns, grown.contains(k)) }); let mut spill_changes = self.write_back(wb, &dirty, results, &spills); // Formulas with references only known while evaluating (INDIRECT, OFFSET) may have read diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 1913d0343..6a8961585 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -720,6 +720,23 @@ fn a_full_recalc_keeps_a_spill_with_the_formula_already_spilling_there() { assert_eq!([t.num("C14"), t.num("G14"), t.num("I14")], [1.0, 5.0, 7.0]); } +#[test] +fn a_full_recalc_keeps_a_spill_from_an_array_that_grew_into_it() { + // Both arrays spill; in manual calculation F13 then grows over F14:G14, which C14's array + // fills. A full recalculation gives what an automatic one would: C14 keeps its cells and F13 + // is #SPILL!, although F13 comes first in reading order. + let mut t = T::new(); + t.set("J1", "1"); + t.set("C14", "=SEQUENCE(1,7)"); + t.set("F13", "=SEQUENCE(J1,2)"); + assert_eq!([t.num("F13"), t.num("G13")], [1.0, 2.0]); + t.wb.calc.mode = gridcraft_model::CalcMode::Manual; + t.set("J1", "5"); + t.calc.recalc_all(&mut t.wb); + assert_eq!(t.get("F13"), Value::Error(CellError::Spill)); + assert_eq!([t.num("C14"), t.num("F14"), t.num("G14"), t.num("I14")], [1.0, 4.0, 5.0, 7.0]); +} + #[test] fn formulas_reading_an_array_that_loses_a_spill_conflict_read_spill_errors() { // Two arrays of one recalculation want the same empty cell: the one laid out second is From f3546f25d9b1e94cb8bc14b72d44599394057565 Mon Sep 17 00:00:00 2001 From: quantforge-br <218010578+quantforge-br@users.noreply.github.com> Date: Sat, 31 Oct 2026 16:01:49 -0300 Subject: [PATCH 4/8] Calc: a full recalc keeps former spill areas taken until their formulas are laid out again; a structural edit doesn't --- crates/calc/src/recalc.rs | 60 +++++++++++++++++++++----------------- crates/calc/src/tests.rs | 30 +++++++++++++++++++ crates/engine/src/cmd.rs | 2 +- crates/engine/src/tests.rs | 13 +++++++++ 4 files changed, 77 insertions(+), 28 deletions(-) diff --git a/crates/calc/src/recalc.rs b/crates/calc/src/recalc.rs index 88c3c18ed..1abb12631 100644 --- a/crates/calc/src/recalc.rs +++ b/crates/calc/src/recalc.rs @@ -116,31 +116,14 @@ struct Prior { /// Formulas that spilled, with the area they filled (from the last calculation or the file's /// `ref`). spilled: HashMap, + /// Whether those areas are where the formulas' arrays still are. Then, until a formula is laid + /// out again, its area stays taken. A structural edit moves the formulas, not the areas. + areas_in_place: bool, /// Formulas that showed #SPILL!. A cell's value moves with it on a structural edit, its spill /// area doesn't. blocked: HashSet, } -impl Prior { - /// Formulas whose new array reaches past their own former area into another formula's: they - /// are laid out after it, as in an incremental recalculation, where that area is still filled. - fn grown_into_another(&self, dirty: &[Key], spills: &HashMap>) -> HashSet { - let mut out = HashSet::default(); - for k in dirty { - let (Some(old), Some(array)) = (self.spilled.get(k), spills.get(k)) else { continue }; - let end = CellRef::new( - k.1.row.saturating_add((array.rows as u32).saturating_sub(1)), - k.1.col.saturating_add((array.cols as u32).saturating_sub(1)), - ); - let area = RangeRef::new(k.1, end); - if !old.contains_range(&area) && self.spilled.iter().any(|(o, r)| o != k && o.0 == k.0 && r.intersects(&area)) { - out.insert(*k); - } - } - out - } -} - impl Calc { pub fn new() -> Self { Self::default() @@ -218,10 +201,20 @@ impl Calc { /// Recalculates every formula (F9 / Ctrl+Alt+F9, and after loading). pub fn recalc_all(&mut self, wb: &mut Workbook) { + self.recalc_everything(wb, true); + } + + /// Recalculates every formula after rows, columns or sheets were inserted, deleted or moved: + /// the formulas moved, the spill areas they filled are where they were. + pub fn recalc_after_structural_edit(&mut self, wb: &mut Workbook) { + self.recalc_everything(wb, false); + } + + fn recalc_everything(&mut self, wb: &mut Workbook, areas_in_place: bool) { self.rebuild(wb); // Spills are laid out again from the formulas where they are now (a structural edit moves // or deletes the formulas, not the values they spilled). - let mut prior = Prior::default(); + let mut prior = Prior { areas_in_place, ..Prior::default() }; for i in 0..wb.sheets.len() { if wb.sheet(i).is_some_and(|s| !s.spill_ranges.is_empty() || !s.spill.is_empty()) && let Some(sh) = wb.sheet_mut(i) @@ -369,6 +362,13 @@ impl Calc { fn run(&mut self, wb: &mut Workbook, mut dirty: Vec, prior: &Prior) { let mut dynamic_again = !self.graph.dynamic.is_empty(); self.last_recalc_cells = 0; + // Former areas of formulas still in the workbook (a deleted formula's area is free), when + // they are still in place. + let mut former: HashMap = if prior.areas_in_place { + prior.spilled.iter().filter(|(k, _)| self.graph.nodes.contains_key(*k)).map(|(k, r)| (*k, *r)).collect() + } else { + HashMap::default() + }; for _round in 0..8 { if dirty.is_empty() { break; @@ -412,15 +412,13 @@ impl Calc { self.circular.extend(cycles); // When two arrays want the same cells, the one already spilling there keeps them and // the newcomer is #SPILL! (as when it is entered), and one that was #SPILL! stays so: - // arrays are laid out in that order, then in reading order. Of two that both spilled, - // one that grew into the other's former area comes second. Without this, a full + // arrays are laid out in that order, then in reading order. Without this, a full // recalculation or reopening the file handed the area to whichever came first. - let grown = if prior.spilled.is_empty() { HashSet::default() } else { prior.grown_into_another(&dirty, &spills) }; dirty.sort_by_key(|k| { let owns = prior.spilled.contains_key(k) || wb.sheet(k.0).is_some_and(|s| s.spill_ranges.contains_key(&k.1)); - (prior.blocked.contains(k), !owns, grown.contains(k)) + (prior.blocked.contains(k), !owns) }); - let mut spill_changes = self.write_back(wb, &dirty, results, &spills); + let mut spill_changes = self.write_back(wb, &dirty, results, &spills, &mut former); // Formulas with references only known while evaluating (INDIRECT, OFFSET) may have read // a spill area before it was laid out: they are evaluated once more. if dynamic_again && !spills.is_empty() { @@ -446,12 +444,16 @@ impl Calc { } } + /// Writes results and lays out spills. `former` holds the areas a full recalculation cleared + /// whose formulas are not laid out yet: as in an incremental recalculation, where those cells + /// are still filled, another array can't take them. fn write_back( &mut self, wb: &mut Workbook, dirty: &[Key], results: HashMap, spills: &HashMap>, + former: &mut HashMap, ) -> Vec { let mut spill_changes = Vec::new(); let mut freed = Vec::new(); @@ -464,6 +466,7 @@ impl Calc { } } for key in dirty { + let own_former = former.remove(key); let new = spills.get(key); let Some(sheet) = wb.sheet_mut(key.0) else { continue }; let old = sheet.spill_ranges.get(&key.1).copied(); @@ -486,7 +489,10 @@ impl Calc { if let Some(array) = new { let end = CellRef::new(key.1.row.saturating_add(array.rows as u32 - 1), key.1.col.saturating_add(array.cols as u32 - 1)); let range = RangeRef::new(key.1, end); - let blocked = spill_is_blocked(sheet, key.1, range, end); + let blocked = spill_is_blocked(sheet, key.1, range, end) + || (!former.is_empty() + && own_former.is_none_or(|f| !f.contains_range(&range)) + && former.iter().any(|(other, area)| other.0 == key.0 && area.intersects(&range))); if blocked { if let Some(cell) = sheet.cells.get_mut(key.1) { cell.value = Value::Error(CellError::Spill); diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 6a8961585..42822f614 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -737,6 +737,36 @@ fn a_full_recalc_keeps_a_spill_from_an_array_that_grew_into_it() { assert_eq!([t.num("C14"), t.num("F14"), t.num("G14"), t.num("I14")], [1.0, 4.0, 5.0, 7.0]); } +#[test] +fn a_full_recalc_lays_out_arrays_as_an_automatic_one_when_one_shrinks_for_another() { + // C1 grows into C3, which A3's array fills, and A3 grows into E3, which E1's array leaves. + // Automatic recalculation lays them out in reading order with each former area still filled: + // C1 is #SPILL! and A3 takes A3:E3. A full recalculation after the same edit in manual + // calculation gives the same sheet. + let build = |manual: bool| { + let mut t = T::new(); + t.set("J1", "2"); + t.set("C1", "=SEQUENCE(J1)"); + t.set("A3", "=SEQUENCE(1,J1+1)"); + t.set("E1", "=SEQUENCE(5-J1)"); + assert_eq!([t.num("C2"), t.num("C3"), t.num("E3")], [2.0, 3.0, 3.0]); + if manual { + t.wb.calc.mode = gridcraft_model::CalcMode::Manual; + } + t.set("J1", "4"); + if manual { + t.calc.recalc_all(&mut t.wb); + } + t + }; + let (automatic, full) = (build(false), build(true)); + assert_eq!(automatic.get("C1"), Value::Error(CellError::Spill)); + assert_eq!([automatic.num("A3"), automatic.num("C3"), automatic.num("E3")], [1.0, 3.0, 5.0]); + for cell in ["A1", "C1", "C2", "C3", "C4", "E1", "E2", "A3", "B3", "C3", "D3", "E3"] { + assert_eq!(full.get(cell), automatic.get(cell), "{cell}"); + } +} + #[test] fn formulas_reading_an_array_that_loses_a_spill_conflict_read_spill_errors() { // Two arrays of one recalculation want the same empty cell: the one laid out second is diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs index be1a28f4d..7392847bd 100644 --- a/crates/engine/src/cmd.rs +++ b/crates/engine/src/cmd.rs @@ -397,7 +397,7 @@ pub(crate) fn commit(d: &mut DocState, f: impl FnOnce(&mut Ctx) -> Result) eprintln!("commit: edit {:?}, {} changed", since(t0), changed.len()); } if structural { - d.calc.recalc_all(&mut wb); + d.calc.recalc_after_structural_edit(&mut wb); } else if !changed.is_empty() { d.calc.cells_changed(&mut wb, &changed); } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 55ad67aa4..ac11a8919 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -2892,6 +2892,19 @@ fn indirect_reads_a_spill_laid_out_in_the_same_pass() { assert_eq!(v(&s, "E5"), Value::Number(9.0)); } +#[test] +fn inserting_rows_lays_out_spills_where_the_formulas_moved() { + // D1's array grows with the inserted rows to D1:D10 and C10's array moves down to C12:E14: + // the area C10:E12 it filled before the insertion blocks neither of them. + let mut s = s(); + s.execute("cell.set", json!({"cell": "D1", "input": "=SEQUENCE(ROWS(A2:A9))"})).unwrap(); + s.execute("cell.set", json!({"cell": "C8", "input": "=1"})).unwrap(); + s.execute("cell.set", json!({"cell": "C10", "input": "=SEQUENCE(3,3)"})).unwrap(); + s.execute("home.insertRows", json!({"rows": "3:4"})).unwrap(); + assert_eq!((v(&s, "D1"), v(&s, "D10")), (Value::Number(1.0), Value::Number(10.0))); + assert_eq!((v(&s, "C12"), v(&s, "D12"), v(&s, "E14")), (Value::Number(1.0), Value::Number(2.0), Value::Number(9.0))); +} + #[test] fn every_command_survives_empty_params() { let mut s = Session::new(); From 430f53df72dbb70882d5f98869aec869e16c03f5 Mon Sep 17 00:00:00 2001 From: quantforge-br <218010578+quantforge-br@users.noreply.github.com> Date: Sat, 31 Oct 2026 17:12:33 -0300 Subject: [PATCH 5/8] Calc: former spill areas are checked through their cells, not by scanning every area --- crates/calc/src/recalc.rs | 53 ++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/crates/calc/src/recalc.rs b/crates/calc/src/recalc.rs index 1abb12631..d5b54dfa5 100644 --- a/crates/calc/src/recalc.rs +++ b/crates/calc/src/recalc.rs @@ -364,11 +364,10 @@ impl Calc { self.last_recalc_cells = 0; // Former areas of formulas still in the workbook (a deleted formula's area is free), when // they are still in place. - let mut former: HashMap = if prior.areas_in_place { - prior.spilled.iter().filter(|(k, _)| self.graph.nodes.contains_key(*k)).map(|(k, r)| (*k, *r)).collect() - } else { - HashMap::default() - }; + let mut former = Former::default(); + if prior.areas_in_place { + former.areas = prior.spilled.iter().filter(|(k, _)| self.graph.nodes.contains_key(*k)).map(|(k, r)| (*k, *r)).collect(); + } for _round in 0..8 { if dirty.is_empty() { break; @@ -445,15 +444,14 @@ impl Calc { } /// Writes results and lays out spills. `former` holds the areas a full recalculation cleared - /// whose formulas are not laid out yet: as in an incremental recalculation, where those cells - /// are still filled, another array can't take them. + /// whose formulas are not laid out yet. fn write_back( &mut self, wb: &mut Workbook, dirty: &[Key], results: HashMap, spills: &HashMap>, - former: &mut HashMap, + former: &mut Former, ) -> Vec { let mut spill_changes = Vec::new(); let mut freed = Vec::new(); @@ -466,7 +464,7 @@ impl Calc { } } for key in dirty { - let own_former = former.remove(key); + let own_former = former.take(key); let new = spills.get(key); let Some(sheet) = wb.sheet_mut(key.0) else { continue }; let old = sheet.spill_ranges.get(&key.1).copied(); @@ -490,9 +488,7 @@ impl Calc { let end = CellRef::new(key.1.row.saturating_add(array.rows as u32 - 1), key.1.col.saturating_add(array.cols as u32 - 1)); let range = RangeRef::new(key.1, end); let blocked = spill_is_blocked(sheet, key.1, range, end) - || (!former.is_empty() - && own_former.is_none_or(|f| !f.contains_range(&range)) - && former.iter().any(|(other, area)| other.0 == key.0 && area.intersects(&range))); + || (own_former.is_none_or(|f| !f.contains_range(&range)) && former.reaches(key.0, range)); if blocked { if let Some(cell) = sheet.cells.get_mut(key.1) { cell.value = Value::Error(CellError::Spill); @@ -535,6 +531,39 @@ impl Calc { } } +/// Spill areas a full recalculation cleared, of formulas not laid out yet: as in an incremental +/// recalculation, where those cells are still filled, another array can't take them. +#[derive(Default)] +struct Former { + areas: HashMap, + /// The cells of `areas`, built when an array first needs more cells than its own former area + /// (so each check costs what checking the cells themselves does, however many areas there are). + cells: Option>, +} + +impl Former { + /// Frees `key`'s former area, as it is laid out now. + fn take(&mut self, key: &Key) -> Option { + let area = self.areas.remove(key)?; + if let Some(cells) = &mut self.cells { + for cell in area.iter() { + cells.remove(&(key.0, cell)); + } + } + Some(area) + } + + /// Whether `range` on `sheet` reaches a former area that is still taken. + fn reaches(&mut self, sheet: usize, range: RangeRef) -> bool { + if self.areas.is_empty() { + return false; + } + let areas = &self.areas; + let cells = self.cells.get_or_insert_with(|| areas.iter().flat_map(|(k, area)| area.iter().map(move |cell| (k.0, cell))).collect()); + range.iter().any(|cell| cells.contains(&(sheet, cell))) + } +} + fn spill_is_blocked(sheet: &gridcraft_model::Sheet, anchor: CellRef, range: RangeRef, end: CellRef) -> bool { end.row >= gridcraft_core::MAX_ROWS || end.col >= gridcraft_core::MAX_COLS From 113e312809e5adebec9bc48dfb9cf858b57ffd59 Mon Sep 17 00:00:00 2001 From: quantforge-br <218010578+quantforge-br@users.noreply.github.com> Date: Mon, 2 Nov 2026 16:12:40 -0300 Subject: [PATCH 6/8] Calc: index former spill areas by 64-cell squares, not by cell, on full recalc A crafted XLSX may declare up to 16 million dynamic-range cells per sheet, and opening it runs a full recalculation: the former-area cell set grew with every cell (841 MB peak, 2.4 s for 16 one-million-cell areas). The index now lists the areas over each 64 x 64 square they cover (12 MB, 13 ms for the same sheet). --- crates/calc/src/recalc.rs | 41 ++++++++++++++++++++++++++------------- crates/calc/src/tests.rs | 17 ++++++++++++++++ 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/crates/calc/src/recalc.rs b/crates/calc/src/recalc.rs index f0a838fe0..52de341aa 100644 --- a/crates/calc/src/recalc.rs +++ b/crates/calc/src/recalc.rs @@ -562,21 +562,27 @@ impl Calc { #[derive(Default)] struct Former { areas: HashMap, - /// The cells of `areas`, built when an array first needs more cells than its own former area - /// (so each check costs what checking the cells themselves does, however many areas there are). - cells: Option>, + /// The areas over each [`FORMER_TILE`]-cell square of a sheet, built when an array first needs + /// more cells than its own former area. A check looks only at the areas near the array, and the + /// index grows with the squares the areas cover, not with their cells: a file's `ref`s may + /// declare millions of them. + tiles: Option>>, +} + +/// The side of [`Former`]'s index squares, in cells. +const FORMER_TILE: u32 = 64; + +/// The index squares `range` overlaps, as (row, column) square numbers. +fn former_tiles(range: RangeRef) -> impl Iterator { + let cols = range.start.col / FORMER_TILE..=range.end.col / FORMER_TILE; + (range.start.row / FORMER_TILE..=range.end.row / FORMER_TILE).flat_map(move |r| cols.clone().map(move |c| (r, c))) } impl Former { - /// Frees `key`'s former area, as it is laid out now. + /// Frees `key`'s former area, as it is laid out now. The index keeps its key; checks skip + /// keys whose area is gone. fn take(&mut self, key: &Key) -> Option { - let area = self.areas.remove(key)?; - if let Some(cells) = &mut self.cells { - for cell in area.iter() { - cells.remove(&(key.0, cell)); - } - } - Some(area) + self.areas.remove(key) } /// Whether `range` on `sheet` reaches a former area that is still taken. @@ -585,8 +591,17 @@ impl Former { return false; } let areas = &self.areas; - let cells = self.cells.get_or_insert_with(|| areas.iter().flat_map(|(k, area)| area.iter().map(move |cell| (k.0, cell))).collect()); - range.iter().any(|cell| cells.contains(&(sheet, cell))) + let tiles = self.tiles.get_or_insert_with(|| { + let mut tiles: HashMap<(usize, u32, u32), Vec> = HashMap::default(); + for (key, area) in areas { + for (r, c) in former_tiles(*area) { + tiles.entry((key.0, r, c)).or_default().push(*key); + } + } + tiles + }); + former_tiles(range) + .any(|(r, c)| tiles.get(&(sheet, r, c)).is_some_and(|keys| keys.iter().any(|k| areas.get(k).is_some_and(|area| area.intersects(&range))))) } } diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 5e84c8b4e..35f3ab0cc 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -737,6 +737,23 @@ fn a_full_recalc_keeps_a_spill_from_an_array_that_grew_into_it() { assert_eq!([t.num("C14"), t.num("F14"), t.num("G14"), t.num("I14")], [1.0, 4.0, 5.0, 7.0]); } +#[test] +fn a_full_recalc_keeps_a_former_area_where_it_crosses_into_far_cells() { + // As above, with one array at BI14:BO14 and the other at BN13: the former area runs past + // column BL, and the array grows into the part beyond it (former areas are indexed by squares + // of cells, so the overlap must be found away from the area's first cells too). + let mut t = T::new(); + t.set("J1", "1"); + t.set("BI14", "=SEQUENCE(1,7)"); + t.set("BN13", "=SEQUENCE(J1,2)"); + assert_eq!([t.num("BN13"), t.num("BO13")], [1.0, 2.0]); + t.wb.calc.mode = gridcraft_model::CalcMode::Manual; + t.set("J1", "5"); + t.calc.recalc_all(&mut t.wb); + assert_eq!(t.get("BN13"), Value::Error(CellError::Spill)); + assert_eq!([t.num("BI14"), t.num("BN14"), t.num("BO14")], [1.0, 6.0, 7.0]); +} + #[test] fn a_full_recalc_lays_out_arrays_as_an_automatic_one_when_one_shrinks_for_another() { // C1 grows into C3, which A3's array fills, and A3 grows into E3, which E1's array leaves. From c47bdffcd6c99612a11e347bd7f0f6842f59975f Mon Sep 17 00:00:00 2001 From: quantforge-br <218010578+quantforge-br@users.noreply.github.com> Date: Tue, 10 Nov 2026 09:28:58 -0300 Subject: [PATCH 7/8] Calc: preserve dynamic-array kind without discarding saved spill areas (#964) --- crates/xlsx/src/tests/roundtrip.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/xlsx/src/tests/roundtrip.rs b/crates/xlsx/src/tests/roundtrip.rs index ab55c8445..4b61f55b8 100644 --- a/crates/xlsx/src/tests/roundtrip.rs +++ b/crates/xlsx/src/tests/roundtrip.rs @@ -1057,12 +1057,11 @@ fn legacy_and_dynamic_formulas_keep_their_kind() { let (xml, _) = sheet1_xml(&bytes); assert!(xml.contains("TANH(A1:A3)+A1:A3"), "{xml}"); assert!(xml.contains("SUM(A1:A3)"), "{xml}"); - // Uncalculated, the spill range is not known yet; the formula stays dynamic. - assert!(xml.contains("TANH(A1:A3)"), "{xml}"); - assert!(xml.contains("TANH(A1:A3)"), "{xml}"); let (back, _) = read_xlsx(&bytes).unwrap(); - for a in ["B2", "B3", "C2", "D2"] { + for (a, dynamic) in [("B2", false), ("B3", false), ("C2", true), ("D2", false)] { assert_eq!(text(&back, a), text(&wb, a), "{a}"); + let formula = back.sheet(0).unwrap().cell(at(a)).unwrap().formula.as_ref().unwrap(); + assert_eq!(formula.dynamic, dynamic, "{a}: dynamic-array metadata survives saving"); } } From 8a04139ef2e9b565fbb22e180d0afc298991bd81 Mon Sep 17 00:00:00 2001 From: quantforge-br <218010578+quantforge-br@users.noreply.github.com> Date: Sun, 15 Nov 2026 14:33:15 -0300 Subject: [PATCH 8/8] Calc: a spill keeps the cells it held when another array grows into them (#964) #2483 lets a spill that already exists keep its cells, but when both anchors held a spill, reading order decided: an array growing into a neighbour's spill took its cells, in automatic calculation and after a full recalculation. Excel keeps the cells the neighbour owns. Standing now also remembers the area each anchor that held a spill filled before the calculation. For each contested cell the anchor whose spill held it keeps it; otherwise #2483's rules apply as before. With the xlsx reader restoring each dynamic array's saved area, a reopened workbook keeps its layout too, so both known limits documented on Standing are gone. Tests now covered by #2483 are dropped, and its grow-and-back test asserts the blocked grower. --- ROADMAP.md | 2 +- crates/calc/src/recalc.rs | 98 +++++++++++++++++++++-------- crates/calc/src/recalc/pass_host.rs | 8 +-- crates/calc/src/tests.rs | 31 --------- crates/engine/src/tests.rs | 33 +++------- crates/xlsx/src/tests/roundtrip.rs | 3 + 6 files changed, 91 insertions(+), 84 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index ea22822d0..e8f197036 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -200,7 +200,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | 2026-10-13 | XLSX export writes a picture used on several sheets once, as one `xl/media` part every drawing points to (it wrote a copy per drawing: a 16-sheet workbook with one 112 KB logo grew from 0.4 to 1.9 MB) (#985) | | 2026-10-13 | XLSX: `[Content_Types].xml`, `_rels/.rels`, the workbook rels and `xl/workbook.xml` are the first zip entries (document properties last), so libmagic and PHP `finfo` detect the file type even with large properties (#1090) | | 2026-10-13 | Conditional formatting: a rule gathers range statistics only when it reads them (top/bottom, above average, duplicates, scales, bars, icons) and borrows them instead of copying them per cell, so the per-frame lookup of an expression or cell-value rule no longer visits its whole range (#1098, part 1) | -| 2026-10-13 | Spills: when two dynamic arrays want the same cells, the one already spilling there keeps them after Calculate Now and after saving and reopening, as when the second was entered; clearing it lets the other spill at once (#964, found by the command-sequence fuzz) | +| 2026-10-13 | Spills: an array that grows into cells another array's spill already holds is `#SPILL!`, in automatic calculation and after Calculate Now, as in Excel; a workbook reopened from XLSX keeps the spill layout it was saved with (#964, found by the command-sequence fuzz) | | 2026-10-13 | Grid: resizing several selected columns or rows previews on the dragged one and applies the size to all on release; Esc cancels (#1082) | | 2026-10-12 | Next/previous sheet navigation stops at the first/last visible worksheet instead of activating a hidden boundary sheet; the current selection is retained | | 2026-10-12 | PDF writer: objects are written straight to the output, identical pictures are stored once, style operators are emitted only when they change, `q` nesting is limited to 28 and polyline joins no longer leak into later strokes (#816) | diff --git a/crates/calc/src/recalc.rs b/crates/calc/src/recalc.rs index ab1b5bd03..f38ed8d91 100644 --- a/crates/calc/src/recalc.rs +++ b/crates/calc/src/recalc.rs @@ -59,26 +59,29 @@ pub struct Calc { /// Who gets the cells when two arrays want the same ones. Excel (measured by a reviewer on /// PR #2483) lets the spill that already exists keep its cells, whichever anchor comes first; -/// the other formula shows `#SPILL!`, and clearing the winner lets it spill. The rule here: +/// the other formula shows `#SPILL!`, and clearing the winner lets it spill. The rule here, for +/// each cell both want: /// -/// 1. an anchor that held a spill before this calculation outranks one that did not; -/// 2. otherwise the anchor first in reading order (row, then column) wins. +/// 1. the anchor whose spill held that cell before this calculation keeps it, so an array that +/// grows into another's area is `#SPILL!` even when it held a spill of its own; +/// 2. otherwise an anchor that held a spill before this calculation outranks one that did not; +/// 3. otherwise the anchor first in reading order (row, then column) wins. /// -/// Rule 2 alone decides when nothing is remembered, so the result never depends on hash order +/// Rule 3 alone decides when nothing is remembered, so the result never depends on hash order /// or evaluation order. A full recalculation remembers the layout the sheet held before it -/// started (rule 1), so it agrees with the incremental one; a workbook that is calculated for -/// the first time has no such layout (rule 2 only). -/// -/// Known limits: (a) the xlsx reader does not restore spill layouts, so a workbook opened from a -/// file is laid out by rule 2 alone, although Excel keeps the layout the file records; (b) when -/// both anchors held a spill and one grows into the other, rule 2 decides, where Excel keeps -/// the cells the other already owns. +/// started, so it agrees with the incremental one; a workbook that is calculated for the first +/// time has no such layout (rule 3 only). The xlsx reader restores the area each dynamic array +/// was saved spilling over, so a workbook opened from a file keeps the layout it was saved with. #[derive(Clone, Debug, Default)] struct Standing { /// `true`: `keys` are the anchors that held a spill (full recalculation, which lays every /// spill out afresh). `false`: `keys` are the formulas that held none (incremental). held: bool, keys: HashSet, + /// The area each anchor that held a spill filled before this calculation, for the anchors the + /// calculation lays out again (full: all of them; incremental: the formulas it recalculates). + /// Any other anchor still fills the area the sheet shows. + areas: HashMap, } impl Standing { @@ -86,7 +89,7 @@ impl Standing { self.keys.contains(&k) == self.held } - /// Whether `a` takes cells from `b` when both want them. + /// Whether `a` takes cells from `b` when both want them and neither held them (rules 2, 3). fn outranks(&self, a: Key, b: Key) -> bool { match (self.had_spill(a), self.had_spill(b)) { (true, false) => true, @@ -94,6 +97,22 @@ impl Standing { _ => (a.1.row, a.1.col) < (b.1.row, b.1.col), } } + + /// Whether `k`'s spill held `cell` before this calculation; `now` is the area the sheet + /// shows for `k`. + fn held_cell(&self, k: Key, now: Option, cell: CellRef) -> bool { + self.had_spill(k) && self.areas.get(&k).copied().or(now).is_some_and(|r| r.contains(cell)) + } + + /// Whether `a` takes `cell` from `b` when both want it (rule 1, then [`Self::outranks`]). Each + /// anchor comes with the area the sheet shows for it. + fn outranks_at(&self, (a, a_now): (Key, Option), (b, b_now): (Key, Option), cell: CellRef) -> bool { + match (self.held_cell(a, a_now, cell), self.held_cell(b, b_now, cell)) { + (true, false) => true, + (false, true) => false, + _ => self.outranks(a, b), + } + } } /// A set of cells by row, each row's columns in their own set: a window of rows and columns is @@ -313,12 +332,18 @@ impl Calc { self.rebuild(wb); // The layout the sheets hold now decides who keeps cells two arrays both want. // (A Python cell that holds a spill counts too, as it does in the incremental pass.) - let mut held = HashSet::default(); + let mut areas: HashMap = HashMap::default(); for i in 0..wb.sheets.len() { if let Some(sh) = wb.sheet(i) { - held.extend(sh.spill_ranges.keys().filter(|a| sh.cell(**a).is_some_and(|c| c.formula.is_some() || c.py.is_some())).map(|a| (i, *a))); + areas.extend( + sh.spill_ranges + .iter() + .filter(|(a, _)| sh.cell(**a).is_some_and(|c| c.formula.is_some() || c.py.is_some())) + .map(|(a, r)| ((i, *a), *r)), + ); } } + let held = areas.keys().copied().collect(); // Spills are laid out again from the formulas where they are now (a structural edit moves // or deletes the formulas, not the values they spilled). for i in 0..wb.sheets.len() { @@ -330,7 +355,7 @@ impl Calc { } } let all: Vec = self.graph.nodes.keys().copied().collect(); - self.standing = Standing { held: true, keys: held }; + self.standing = Standing { held: true, keys: held, areas }; self.run(wb, all, true); self.standing = Standing::default(); } @@ -542,8 +567,15 @@ impl Calc { if !full { // What each formula held before this calculation touched it. for &k in &dirty { - if seen.insert(k) && wb.sheet(k.0).is_some_and(|s| !s.spill_ranges.contains_key(&k.1)) { - self.standing.keys.insert(k); + if seen.insert(k) { + match wb.sheet(k.0).and_then(|s| s.spill_ranges.get(&k.1)) { + Some(r) => { + self.standing.areas.insert(k, *r); + } + None => { + self.standing.keys.insert(k); + } + } } } } @@ -664,7 +696,7 @@ impl Calc { let end = CellRef::new(key.1.row.saturating_add(array.rows as u32 - 1), key.1.col.saturating_add(array.cols as u32 - 1)); let range = RangeRef::new(key.1, end); // Two arrays that want the same cells (see [`Standing`]): a spill already laid out - // that this anchor outranks is taken over, and recalculated. + // that this anchor outranks on every cell they share is taken over, and recalculated. let standing = &self.standing; // A spill can only be in the way where it has cells besides its anchor, and an // anchor itself blocks the array. When no spilled cell lies in the range, the owners @@ -675,13 +707,18 @@ impl Calc { sheet .spill_ranges .iter() - .filter(|(o, r)| **o != key.1 && r.intersects(&range) && standing.outranks(*key, (key.0, **o))) + .filter(|(o, r)| { + **o != key.1 + && r.intersection(&range).is_some_and(|shared| { + shared.iter().all(|cell| standing.outranks_at((*key, old), ((key.0, **o), Some(**r)), cell)) + }) + }) .map(|(o, r)| (*o, *r)) .collect() } else { Vec::new() }; - let blocked = spill_is_blocked(sheet, *key, range, end, standing); + let blocked = spill_is_blocked(sheet, (*key, old), range, end, standing); if !blocked { for (o, r) in outranked { for c in r.iter() { @@ -739,11 +776,22 @@ thread_local! { pub(crate) static OWNER_SCANS: std::cell::Cell = const { std::cell::Cell::new(0) }; } -/// Whether the array at `anchor` can't be laid out over `range`. A spill already laid out that -/// `anchor` outranks does not block it: it is taken over when written back. -fn spill_is_blocked(sheet: &gridcraft_model::Sheet, anchor: Key, range: RangeRef, end: CellRef, standing: &Standing) -> bool { - let outranked = - |cell: CellRef| sheet.spill_ranges.iter().any(|(o, r)| *o != anchor.1 && r.contains(cell) && standing.outranks(anchor, (anchor.0, *o))); +/// Whether the array at `anchor` (with the area the sheet showed for it) can't be laid out over +/// `range`. A spill already laid out that `anchor` outranks does not block it: it is taken over +/// when written back. +fn spill_is_blocked( + sheet: &gridcraft_model::Sheet, + (anchor, now): (Key, Option), + range: RangeRef, + end: CellRef, + standing: &Standing, +) -> bool { + let outranked = |cell: CellRef| { + sheet + .spill_ranges + .iter() + .any(|(o, r)| *o != anchor.1 && r.contains(cell) && standing.outranks_at((anchor, now), ((anchor.0, *o), Some(*r)), cell)) + }; end.row >= gridcraft_core::MAX_ROWS || end.col >= gridcraft_core::MAX_COLS || range.iter().any(|cell| cell != anchor.1 && (sheet.cells.has(cell) || (sheet.spill.contains_key(&cell) && !outranked(cell)))) diff --git a/crates/calc/src/recalc/pass_host.rs b/crates/calc/src/recalc/pass_host.rs index 78bd5d9ef..7f42c3644 100644 --- a/crates/calc/src/recalc/pass_host.rs +++ b/crates/calc/src/recalc/pass_host.rs @@ -163,9 +163,9 @@ fn spill_hits_overlap(index: &SpillIndex, sheet: usize, hits: &[CellRef], range: } /// Whether an array result at `k` can't spill: it is in a table, or its area runs off the sheet, -/// overlaps merged cells, holds other cells, or the spill of another formula that outranks it -/// (see `Standing`; unless that formula is recalculated in this pass, and so may spill elsewhere -/// now). A spill it outranks gives way to it when written back. +/// overlaps merged cells, holds other cells, or the spill of another formula that outranks it on +/// a cell they share (see `Standing`; unless that formula is recalculated in this pass, and so +/// may spill elsewhere now). A spill it outranks gives way to it when written back. fn spill_blocked(host: &PassHost<'_>, (sheet, anchor): Key, a: &Array) -> bool { let Some(sh) = host.wb.sheet(sheet) else { return false }; let end = @@ -182,7 +182,7 @@ fn spill_blocked(host: &PassHost<'_>, (sheet, anchor): Key, a: &Array) -> bool { && r.contains(c) && !host.exprs.contains_key(&(sheet, *o)) && !host.python.contains_key(&(sheet, *o)) - && standing.outranks((sheet, *o), (sheet, anchor)) + && standing.outranks_at(((sheet, *o), Some(*r)), ((sheet, anchor), own.copied()), c) }) }; end.row >= gridcraft_core::MAX_ROWS diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index d8c424c77..b1a06db35 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -706,20 +706,6 @@ fn spill_into_the_old_area_of_a_blocked_neighbour() { assert_eq!([t.num("C8"), t.num("D8"), t.num("C9"), t.num("D9")], [1.0, 2.0, 3.0, 4.0]); } -#[test] -fn a_full_recalc_keeps_a_spill_with_the_formula_already_spilling_there() { - // F13's array would spill over F14:G14, which C14's array already fills: F13 is #SPILL!, and - // a full recalculation (Ctrl+Alt+F9) doesn't hand the area to F13 for coming first in - // reading order. - let mut t = T::new(); - t.set("C14", "=SEQUENCE(1,7)"); - t.set("F13", "=SEQUENCE(5,2)"); - assert_eq!(t.get("F13"), Value::Error(CellError::Spill)); - t.calc.recalc_all(&mut t.wb); - assert_eq!(t.get("F13"), Value::Error(CellError::Spill)); - assert_eq!([t.num("C14"), t.num("G14"), t.num("I14")], [1.0, 5.0, 7.0]); -} - #[test] fn a_full_recalc_keeps_a_spill_from_an_array_that_grew_into_it() { // Both arrays spill; in manual calculation F13 then grows over F14:G14, which C14's array @@ -737,23 +723,6 @@ fn a_full_recalc_keeps_a_spill_from_an_array_that_grew_into_it() { assert_eq!([t.num("C14"), t.num("F14"), t.num("G14"), t.num("I14")], [1.0, 4.0, 5.0, 7.0]); } -#[test] -fn a_full_recalc_keeps_a_former_area_where_it_crosses_into_far_cells() { - // As above, with one array at BI14:BO14 and the other at BN13: the former area runs past - // column BL, and the array grows into the part beyond it (former areas are indexed by squares - // of cells, so the overlap must be found away from the area's first cells too). - let mut t = T::new(); - t.set("J1", "1"); - t.set("BI14", "=SEQUENCE(1,7)"); - t.set("BN13", "=SEQUENCE(J1,2)"); - assert_eq!([t.num("BN13"), t.num("BO13")], [1.0, 2.0]); - t.wb.calc.mode = gridcraft_model::CalcMode::Manual; - t.set("J1", "5"); - t.calc.recalc_all(&mut t.wb); - assert_eq!(t.get("BN13"), Value::Error(CellError::Spill)); - assert_eq!([t.num("BI14"), t.num("BN14"), t.num("BO14")], [1.0, 6.0, 7.0]); -} - #[test] fn a_full_recalc_lays_out_arrays_as_an_automatic_one_when_one_shrinks_for_another() { // C1 grows into C3, which A3's array fills, and A3 grows into E3, which E1's array leaves. diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index a9410c232..5a3f81386 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -2993,9 +2993,10 @@ fn overlapping_spills_keep_the_one_that_exists_whichever_anchor_is_first() { #[test] fn a_spill_growing_into_another_and_back_matches_a_full_recalc_at_every_step() { use gridcraft_core::CellError::Spill; - // Both arrays already spill; F8 (first in reading order) grows down over E10's spill, takes - // its cells, shrinks back and lets it spill again, then the steps are undone, redone, edited - // inside the contested area and cleared. Every state must match a full recalc. + // Both arrays already spill; F8 (first in reading order) grows down over E10's spill, is + // #SPILL! because E10 holds those cells (as in Excel), shrinks back and spills again, then the + // steps are undone, redone, edited inside the contested area and cleared. Every state must + // match a full recalc. let mut s = s(); s.execute("cell.set", json!({"cell": "H1", "input": "1"})).unwrap(); s.execute("cell.set", json!({"cell": "F8", "input": "=SEQUENCE(H1)"})).unwrap(); @@ -3017,18 +3018,18 @@ fn a_spill_growing_into_another_and_back_matches_a_full_recalc_at_every_step() { s.execute(id, params.clone()).unwrap(); assert_matches_full_recalc(&s); if i == 0 { - // Both held a spill: reading order gives F8 the cells. - assert_eq!(v(&s, "F12"), Value::Number(5.0)); - assert_eq!(v(&s, "E10"), Value::Error(Spill)); + // Both held a spill: E10 holds F10, so it keeps it and F8 is blocked. + assert_eq!(v(&s, "F8"), Value::Error(Spill)); + assert_eq!(v(&s, "F10"), Value::Number(20.0)); } if i == 2 { assert_eq!(v(&s, "E10"), Value::Number(10.0)); assert_eq!(v(&s, "F10"), Value::Number(20.0)); } if i == 3 { - // Undone back to the grown F8. - assert_eq!(v(&s, "F12"), Value::Number(5.0)); - assert_eq!(v(&s, "E10"), Value::Error(Spill)); + // Undone back to the grown F8, blocked again. + assert_eq!(v(&s, "F8"), Value::Error(Spill)); + assert_eq!(v(&s, "F10"), Value::Number(20.0)); } if i == 8 { assert_eq!((v(&s, "F8"), v(&s, "E10"), v(&s, "F9")), (Value::Empty, Value::Empty, Value::Empty)); @@ -3270,20 +3271,6 @@ fn a_spill_that_goes_away_unblocks_another() { assert_eq!((v(&s, "E10"), v(&s, "G10")), (Value::Number(1.0), Value::Number(3.0))); } -#[test] -fn clearing_the_array_that_holds_a_contested_area_lets_the_other_spill() { - // F13's array wants F14:G14, inside C14's spill C14:I14. Clearing C14 empties the whole - // area, to the right of C14 too, so F13 spills straight away, as in Excel. - let mut s = s(); - s.execute("cell.set", json!({"cell": "C14", "input": "=SEQUENCE(1,7)"})).unwrap(); - s.execute("cell.set", json!({"cell": "F13", "input": "=SEQUENCE(5,2)"})).unwrap(); - assert_eq!(v(&s, "F13"), Value::Error(gridcraft_core::CellError::Spill)); - s.execute("edit.clearContents", json!({"range": "C14"})).unwrap(); - assert_eq!((v(&s, "F13"), v(&s, "G17")), (Value::Number(1.0), Value::Number(10.0))); - s.execute("edit.undo", json!({})).unwrap(); - assert_eq!((v(&s, "F13"), v(&s, "I14")), (Value::Error(gridcraft_core::CellError::Spill), Value::Number(7.0))); -} - #[test] fn a_contested_spill_area_stays_with_its_array_after_reopening() { // F13's array wants F14:G14, which C14's array already fills, so F13 is #SPILL!. The file diff --git a/crates/xlsx/src/tests/roundtrip.rs b/crates/xlsx/src/tests/roundtrip.rs index 2751c8224..7669f999e 100644 --- a/crates/xlsx/src/tests/roundtrip.rs +++ b/crates/xlsx/src/tests/roundtrip.rs @@ -1058,6 +1058,9 @@ fn legacy_and_dynamic_formulas_keep_their_kind() { let (xml, _) = sheet1_xml(&bytes); assert!(xml.contains("TANH(A1:A3)+A1:A3"), "{xml}"); assert!(xml.contains("SUM(A1:A3)"), "{xml}"); + // Uncalculated, the dynamic array keeps the area the file says it spilled over. + assert!(xml.contains("TANH(A1:A3)"), "{xml}"); + assert!(xml.contains("TANH(A1:A3)"), "{xml}"); let (back, _) = read_xlsx(&bytes).unwrap(); for (a, dynamic) in [("B2", false), ("B3", false), ("C2", true), ("D2", false)] { assert_eq!(text(&back, a), text(&wb, a), "{a}");