From df34dac2d1da73ab8876e19496a5dc419fb0ba25 Mon Sep 17 00:00:00 2001 From: Harrison Booth Date: Thu, 15 Oct 2026 02:29:30 -0500 Subject: [PATCH] Recalc: array-entering over a selection updates readers of the cells it clears cell.set with array:true over a multi-cell selection cleared the other cells of the range but marked only the first cell changed, so formulas reading the cleared cells kept stale values until a full recalc (array formulas hid this because their results refresh the range; a constant input didn't). The clearing loop also stopped after a million addresses, so a whole-column array left content further down in place. The array branch now walks the cells the sheet holds in the range and marks each one it clears. Clear (Delete and the Clear commands) had the same cap on the addresses it marked: readers of cells past the first million of a large range stayed stale. It now marks the cells it clears. Found by the command-sequence fuzzer (seeds 4 and 1213). Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/edit.rs | 112 +++++++++++++++++++++++++++++++--- 1 file changed, 103 insertions(+), 9 deletions(-) diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 82dbe0be7..0cb6382e5 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -246,15 +246,23 @@ fn cell_set(s: &mut Session, p: &Json) -> Result { std::sync::Arc::make_mut(f).array = Some(range); } let sh = cx.sheet_mut(sheet)?; - // The array takes the whole range: other cells in it lose their contents. - for c in range.iter().skip(1).take(1_000_000) { - if let Some(old) = sh.cells.get(c).filter(|x| !x.value.is_empty() || x.formula.is_some()) { - let style = old.style; - sh.set_cell(c, Cell { style, ..Cell::default() }); - } + // The array takes the whole range: other cells in it lose their contents. Walk the + // cells the sheet holds rather than every address, so a whole-column array clears + // all of its area, and mark each cleared cell so formulas reading it recalculate. + let cleared: Vec<(CellRef, StyleId)> = sh + .cells + .iter_range(range) + .filter(|(c, x)| *c != range.start && (!x.value.is_empty() || x.formula.is_some())) + .map(|(c, x)| (c, x.style)) + .collect(); + for &(c, style) in &cleared { + sh.set_cell(c, Cell { style, ..Cell::default() }); } sh.set_cell(range.start, cell); cx.touch(sheet, range.start); + for (c, _) in cleared { + cx.touch(sheet, c); + } return Ok(Json::Null); } let cell = input_to_cell(&input, sheet, at, &mut cx.wb)?; @@ -1261,6 +1269,9 @@ fn clear(s: &mut Session, p: &Json, what: &str) -> Result { } } } + // Mark the cells the sheet held, not every address of the range: a whole-column range + // holds millions of addresses, and a cap on them left readers of cells past it stale. + cx.changed.extend(cells.iter().map(|(c, _)| (sheet, *c))); let sh = cx.sheet_mut(sheet)?; for (c, mut cell) in cells { match what { @@ -1286,9 +1297,6 @@ fn clear(s: &mut Session, p: &Json, what: &str) -> Result { if matches!(what, "all" | "formats") { sh.merges.retain(|m| !r.contains_range(m)); } - for c in r.iter().take(1_000_000) { - cx.changed.push((sheet, c)); - } } Ok(Json::Null) }) @@ -1636,6 +1644,92 @@ mod tests { assert_eq!(sh.value(CellRef::parse("K20").unwrap()), Value::text("keep")); } + /// The value a full recalculation from scratch gives `a1` on the active sheet. + fn fully_recalculated(s: &Session, a1: &str) -> Value { + let mut fresh = (*s.doc().unwrap().wb).clone(); + gridcraft_calc::Calc::new().recalc_all(&mut fresh); + fresh.active().unwrap().value(CellRef::parse(a1).unwrap()) + } + + #[test] + fn array_entering_a_constant_recalculates_readers_of_the_cleared_cells() { + // Found by the command-sequence fuzzer (seeds 4, 1213): only the array's first cell was + // marked changed, so formulas reading the cells it cleared kept their old values. + let mut s = Session::new(); + s.new_workbook(); + s.execute("range.setValues", json!({"range": "A1", "values": [[5, 6, "=B1+1"]]})).unwrap(); + s.execute("selection.set", json!({"active": "A1", "range": "A1:B1"})).unwrap(); + s.execute("cell.set", json!({"cell": "A1", "input": "7", "array": true})).unwrap(); + let sh = s.doc().unwrap().wb.active().unwrap(); + assert_eq!(sh.value(CellRef::parse("B1").unwrap()), Value::Empty); + assert_eq!(sh.value(CellRef::parse("C1").unwrap()), Value::number(1.0)); + + // The fuzzer's minimal repro on the budget sample. + let mut s = Session::new(); + s.execute("file.new", json!({"sample": "budget"})).unwrap(); + s.execute("selection.set", json!({"active": "C5", "range": "C5:D5"})).unwrap(); + s.execute("cell.set", json!({"array": true, "cell": "C5", "input": "7"})).unwrap(); + let in_memory = s.doc().unwrap().wb.active().unwrap().value(CellRef::parse("D15").unwrap()); + assert_eq!(in_memory, fully_recalculated(&s, "D15")); + } + + #[test] + fn a_whole_column_array_clears_its_whole_area() { + // The clearing loop stopped after a million addresses, so content further down the + // column survived inside the array (and its readers weren't recalculated). + let mut s = Session::new(); + s.new_workbook(); + s.execute("cell.set", json!({"cell": "D1048000", "input": "41"})).unwrap(); + s.execute("cell.set", json!({"cell": "F1", "input": "=D1048000+1"})).unwrap(); + s.execute("selection.set", json!({"active": "D1", "range": "D:D"})).unwrap(); + s.execute("cell.set", json!({"cell": "D1", "input": "1", "array": true})).unwrap(); + let sh = s.doc().unwrap().wb.active().unwrap(); + assert_eq!(sh.value(CellRef::parse("D1048000").unwrap()), Value::Empty); + assert_eq!(sh.value(CellRef::parse("F1").unwrap()), Value::number(1.0)); + assert_eq!(fully_recalculated(&s, "F1"), Value::number(1.0)); + } + + #[test] + fn clearing_more_than_a_million_cells_recalculates_readers_of_all_of_them() { + // Clear marked only the first million addresses of the range as changed, so formulas + // reading a cleared cell further down kept its old value. + let mut s = Session::new(); + s.new_workbook(); + s.execute("cell.set", json!({"cell": "D1", "input": "1"})).unwrap(); + s.execute("cell.set", json!({"cell": "D1048000", "input": "41"})).unwrap(); + s.execute("cell.set", json!({"cell": "F1", "input": "=D1048000+1"})).unwrap(); + s.execute("edit.clearContents", json!({"range": "D:D"})).unwrap(); + let sh = s.doc().unwrap().wb.active().unwrap(); + assert_eq!(sh.value(CellRef::parse("D1048000").unwrap()), Value::Empty); + assert_eq!(sh.value(CellRef::parse("F1").unwrap()), Value::number(1.0)); + } + + #[test] + fn clearing_a_spill_recalculates_readers_of_its_spilled_cells() { + // Clear marks only the cells the sheet holds; a spill's other cells go with its anchor. + let mut s = Session::new(); + s.new_workbook(); + s.execute("cell.set", json!({"cell": "A1", "input": "=SEQUENCE(5)"})).unwrap(); + s.execute("cell.set", json!({"cell": "C1", "input": "=A3+1"})).unwrap(); + assert_eq!(s.doc().unwrap().wb.active().unwrap().value(CellRef::parse("C1").unwrap()), Value::number(4.0)); + s.execute("edit.clearContents", json!({"range": "A1:A5"})).unwrap(); + assert_eq!(s.doc().unwrap().wb.active().unwrap().value(CellRef::parse("C1").unwrap()), Value::number(1.0)); + assert_eq!(fully_recalculated(&s, "C1"), Value::number(1.0)); + } + + #[test] + fn readers_of_an_array_formulas_cells_see_its_results() { + let mut s = Session::new(); + s.new_workbook(); + s.execute("range.setValues", json!({"range": "B2", "values": [[1], [2]]})).unwrap(); + s.execute("cell.set", json!({"cell": "C3", "input": "100"})).unwrap(); + s.execute("cell.set", json!({"cell": "E1", "input": "=C3+C4+1"})).unwrap(); + s.execute("selection.set", json!({"range": "C2:C4"})).unwrap(); + s.execute("cell.set", json!({"cell": "C2", "input": "=B2:B3*2", "array": true})).unwrap(); + let sh = s.doc().unwrap().wb.active().unwrap(); + assert_eq!(sh.value(CellRef::parse("E1").unwrap()), fully_recalculated(&s, "E1")); + } + #[test] fn wildcards() { assert!(wildcard("hello", "h*o"));