From df34dac2d1da73ab8876e19496a5dc419fb0ba25 Mon Sep 17 00:00:00 2001 From: Harrison Booth Date: Thu, 15 Oct 2026 02:29:30 -0500 Subject: [PATCH 1/3] 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 82dbe0be72..0cb6382e5c 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")); From c708afad34b91d08e51dc415e34f8a4e68d8e6b5 Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Mon, 26 Oct 2026 10:10:19 -0500 Subject: [PATCH 2/3] Array entry of a constant fills every cell and forms no array Ctrl+Shift+Enter over a multi-cell selection with a plain value puts that value in every cell, as Excel does; formulas still become arrays. --- crates/engine/src/cmd/edit.rs | 60 +++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 4da5cf2f75..99c8f41ecf 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -244,11 +244,29 @@ fn cell_set(s: &mut Session, p: &Json) -> Result { // the cell; a cell elsewhere (or on another sheet) gets a one-cell array. let selected = cx.sel.current(); let range = if sheet == active_sheet && selected.contains(at) { selected } else { RangeRef::cell(at) }; + let mut cell = input_to_cell(&input, sheet, range.start, &mut cx.wb)?.unwrap_or_default(); + // Excel forms no array from a constant: every cell of the selection takes the value. + if cell.formula.is_none() && !range.is_single() { + if range.count() > crate::fill::MAX_FILL_CELLS { + return Err(EngineError::Other("That selection is too large to fill.".into())); + } + for c in range.iter() { + let filled = input_to_cell(&input, sheet, c, &mut cx.wb)?; + let sh = cx.sheet_mut(sheet)?; + match filled { + Some(f) => sh.set_cell(c, f), + None => { + sh.remove_cell(c); + } + } + cx.touch(sheet, c); + } + return Ok(Json::Null); + } // A table header can't hold an array's value, and a table resizes under it. if !range.is_single() && cx.wb.sheet(sheet).is_some_and(|sh| sh.tables.iter().any(|t| t.range.intersects(&range))) { return Err(EngineError::Other("Multi-cell array formulas aren't allowed in tables.".into())); } - let mut cell = input_to_cell(&input, sheet, range.start, &mut cx.wb)?.unwrap_or_default(); if let Some(f) = cell.formula.as_mut() { std::sync::Arc::make_mut(f).array = Some(range); } @@ -2153,7 +2171,7 @@ mod tests { } #[test] - fn array_entering_a_constant_recalculates_readers_of_the_cleared_cells() { + fn array_entering_a_constant_recalculates_readers_of_the_cells_it_fills() { // 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(); @@ -2162,8 +2180,13 @@ mod tests { 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)); + // Excel fills both cells with the constant and forms no array. + for a in ["A1", "B1"] { + let c = sh.cell(CellRef::parse(a).unwrap()).unwrap(); + assert_eq!(c.value, Value::number(7.0)); + assert!(c.formula.is_none()); + } + assert_eq!(sh.value(CellRef::parse("C1").unwrap()), Value::number(8.0)); // The fuzzer's minimal repro on the budget sample. let mut s = Session::new(); @@ -2175,9 +2198,26 @@ mod tests { } #[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). + fn array_entering_a_formula_fills_every_cell_as_an_array() { + let mut s = Session::new(); + s.new_workbook(); + s.execute("cell.set", json!({"cell": "A2", "input": "5"})).unwrap(); + for (input, want) in [("=1", 1.0), ("=A2", 5.0)] { + s.execute("selection.set", json!({"active": "A1", "range": "A1:B1"})).unwrap(); + s.execute("cell.set", json!({"cell": "A1", "input": input, "array": true})).unwrap(); + let sh = s.doc().unwrap().wb.active().unwrap(); + for a in ["A1", "B1"] { + assert_eq!(sh.value(CellRef::parse(a).unwrap()), Value::number(want), "{input} {a}"); + } + let f = sh.cell(CellRef::parse("A1").unwrap()).and_then(|c| c.formula.clone()).unwrap(); + assert_eq!(f.array, Some(RangeRef::parse("A1:B1").unwrap())); + } + } + + #[test] + fn a_whole_column_constant_fills_its_whole_area() { + // The loop stopped after a million addresses, so cells further down the column kept + // their old content (and their readers weren't recalculated). let mut s = Session::new(); s.new_workbook(); s.execute("cell.set", json!({"cell": "D1048000", "input": "41"})).unwrap(); @@ -2185,9 +2225,9 @@ mod tests { 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)); + assert_eq!(sh.value(CellRef::parse("D1048000").unwrap()), Value::number(1.0)); + assert_eq!(sh.value(CellRef::parse("F1").unwrap()), Value::number(2.0)); + assert_eq!(fully_recalculated(&s, "F1"), Value::number(2.0)); } #[test] From 7a8b2261f8cbdaf7d23872b823f7b6f3c1faa308 Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Mon, 26 Oct 2026 10:20:01 -0500 Subject: [PATCH 3/3] Array entry of a formula marks every result cell changed, stored or not --- crates/engine/src/cmd/edit.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 99c8f41ecf..ac5ba49e28 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -285,8 +285,16 @@ fn cell_set(s: &mut Session, p: &Json) -> Result { } sh.set_cell(range.start, cell); cx.touch(sheet, range.start); - for (c, _) in cleared { - cx.touch(sheet, c); + // The array's results land in every cell of the range, stored or not, so readers of + // an empty result cell need the touch too. A huge range marks only the cells it held. + if range.count() <= 1_000_000 { + for c in range.iter() { + cx.touch(sheet, c); + } + } else { + for (c, _) in cleared { + cx.touch(sheet, c); + } } return Ok(Json::Null); } @@ -2214,6 +2222,17 @@ mod tests { } } + #[test] + fn readers_of_an_empty_array_result_cell_see_its_result() { + let mut s = Session::new(); + s.new_workbook(); + s.execute("cell.set", json!({"cell": "D1", "input": "=B1+1"})).unwrap(); + s.execute("selection.set", json!({"active": "A1", "range": "A1:B1"})).unwrap(); + s.execute("cell.set", json!({"cell": "A1", "input": "=5", "array": true})).unwrap(); + assert_eq!(s.doc().unwrap().wb.active().unwrap().value(CellRef::parse("D1").unwrap()), Value::number(6.0)); + assert_eq!(fully_recalculated(&s, "D1"), Value::number(6.0)); + } + #[test] fn a_whole_column_constant_fills_its_whole_area() { // The loop stopped after a million addresses, so cells further down the column kept