From a3ccd60b5b6afcf886ad354b5acf9a4f758dcbf0 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 20:55:33 -0800 Subject: [PATCH] Script check: a script whose only finding is unsure names is scanned once A script without includes whose declarations give names values the analysis does not read came back from its first scan with `Known::unsure` set and no names. `settle` counted that as something known and scanned the script again, which found the same and doubled the scan's steps. The proptest `any_bytes_are_checked_in_linear_work` shrank one such input to 104 bytes that took 550 steps, over its bound of 536. `settle` now keeps the first scan when it found no bound names and no File objects: the flag comes from the script's own declarations, which the first scan already read. --- crates/scriptcheck/src/lib.rs | 6 ++++-- crates/scriptcheck/src/tests.rs | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/crates/scriptcheck/src/lib.rs b/crates/scriptcheck/src/lib.rs index e74d299aaa..9c5b596a0b 100644 --- a/crates/scriptcheck/src/lib.rs +++ b/crates/scriptcheck/src/lib.rs @@ -231,7 +231,9 @@ pub fn check(name: &str, bytes: &[u8], loader: Option<&mut dyn Loader>, limits: /// that includes nothing, so a variable a function uses before the global code assigns it holds /// what it is assigned. `first` holds the scan made with nothing known, which found `known`, and /// `pre` what came before it. A scan made with what the scans before it found, and that finds -/// nothing more, is the report scan; no other scan is repeated. +/// nothing more, is the report scan; no other scan is repeated. When `known` holds no names, the +/// first scan is the report: its [`analyze::Known::unsure`] comes from the script's own +/// declarations, which that scan already read. fn settle<'l>( first: report::Acc<'l>, pre: report::Acc<'l>, @@ -240,7 +242,7 @@ fn settle<'l>( braced: &[analyze::Braced], mut known: analyze::Known, ) -> report::Acc<'l> { - if known.len() == 0 { + if known.bound.is_empty() && known.files.is_empty() { return first; } let entries = pre.limits.entries; diff --git a/crates/scriptcheck/src/tests.rs b/crates/scriptcheck/src/tests.rs index 0d65836ef6..332458838f 100644 --- a/crates/scriptcheck/src/tests.rs +++ b/crates/scriptcheck/src/tests.rs @@ -2449,3 +2449,25 @@ fn variables_that_settle_over_several_scans_stay_linear() { let r = report(&src); assert!(r.steps <= 9 * src.len() + 16, "{} steps for {} bytes", r.steps, src.len()); } + +/// A script whose declarations give names values the analysis doesn't read, and whose variables +/// hold nothing the check knows, is scanned once: a second scan would find the same. The bytes are +/// the input `any_bytes_are_checked_in_linear_work` shrank to when it found the second scan. +#[test] +fn a_script_that_is_only_unsure_is_scanned_once() { + let bytes: &[u8] = &[ + 1, 1, 32, 1, 48, 32, 33, 48, 33, 1, 33, 1, 35, 1, 35, 123, 35, 123, 32, 48, 40, 32, 35, 48, 1, 40, 32, 1, 40, 32, 32, 123, 35, 123, 32, 35, + 32, 1, 35, 48, 58, 40, 40, 48, 91, 9, 32, 92, 124, 32, 27, 33, 33, 48, 58, 124, 33, 124, 32, 48, 41, 32, 32, 41, 32, 41, 93, 32, 32, 32, 32, + 33, 32, 32, 32, 32, 27, 32, 32, 32, 32, 27, 32, 32, 32, 27, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 92, 117, 9, 1, + ]; + let limits = Limits::default(); + let mut acc = crate::report::Acc::new(&limits); + let (text, _) = decode(bytes); + let (text, braced) = analyze::unescaped(text, &mut acc); + let declared = analyze::declarations(&text, &mut acc); + let scan = analyze::scan(&text, &declared, &braced, None, std::rc::Rc::from([]), &analyze::Known::default(), &mut acc); + assert!(scan.known.unsure && scan.known.bound.is_empty() && scan.known.files.is_empty()); + let r = check("a.jsx", bytes, None, &limits); + assert_eq!(r.steps, acc.steps); + assert!(r.steps <= 5 * bytes.len() + 16, "{} steps for {} bytes", r.steps, bytes.len()); +}