diff --git a/Cargo.lock b/Cargo.lock index 1605548e0..edcff8568 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2298,6 +2298,9 @@ dependencies = [ [[package]] name = "craft-media-testkit" version = "0.1.0" +dependencies = [ + "craft-testkit", +] [[package]] name = "craft-menu" @@ -2978,6 +2981,10 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-testkit" +version = "0.1.0" + [[package]] name = "craft-text" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index c0e4f0ed5..9106920a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -164,6 +164,7 @@ craft-stencilcraft-format = { path = "crates/stencilcraft-format" } craft-stencilcraft-document = { path = "crates/stencilcraft-document" } craft-svg-paths = { path = "crates/svg-paths" } craft-stream-codecs = { path = "crates/stream-codecs" } +craft-testkit = { path = "crates/testkit" } craft-text = { path = "crates/text" } craft-tcraft = { path = "crates/tcraft" } craft-texture = { path = "crates/texture" } diff --git a/README.md b/README.md index 88c15eb62..020b2b121 100644 --- a/README.md +++ b/README.md @@ -367,6 +367,13 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit check for drawing a tree with resvg. Apps take usvg from here, not as their own dependency. - [`craft-svg-paths`](crates/svg-paths/src/lib.rs): the former name of `craft-svg`'s `paths`, `color` and `write`; re-exports them for existing users. +- [`craft-testkit`](crates/testkit/README.md): test support for the apps' own tests. Its `guards` module + reports source patterns that broke platforms before: muda's predefined Quit item, WGSL float literals + out of range, and `std::time::Instant` in code the web build runs. Its `deadline` module fails a test + whose body runs past a limit, or whose condition does not hold in time; a variable the caller names + replaces each limit. Its `hostile` module runs a parser on every truncation and on seeded damaged + copies of a valid input, and fails the test with the case that panicked or did not finish. Not + published. - [`craft-text`](crates/text/README.md): text processing below the composer. Its `hyphen` module finds hyphenation points, with a bundled US English dictionary and patterns and optional hypher patterns for 47 more languages (one cargo feature each). diff --git a/crates/media-testkit/Cargo.toml b/crates/media-testkit/Cargo.toml index 827d2ae6b..d69a88bf1 100644 --- a/crates/media-testkit/Cargo.toml +++ b/crates/media-testkit/Cargo.toml @@ -8,5 +8,8 @@ rust-version.workspace = true repository.workspace = true publish = false +[dependencies] +craft-testkit = { workspace = true } + [lints] workspace = true diff --git a/crates/media-testkit/src/hostile.rs b/crates/media-testkit/src/hostile.rs index 1051d0405..f40021986 100644 --- a/crates/media-testkit/src/hostile.rs +++ b/crates/media-testkit/src/hostile.rs @@ -1,89 +1,26 @@ -//! Deterministic hostile-input hammer: truncations, bit flips and extreme-value overwrites of a -//! valid seed, each run under `catch_unwind` with a time budget. No dependencies, no `unsafe`. +//! Deterministic hostile-input hammer, from [`craft_testkit::hostile`]: [`hammer`] runs a parser on +//! a valid seed, every truncation of it and seeded damage of it, one case after another on a worker +//! thread with a time limit for each case. [`Rng`], [`mutations`] and the other names here are +//! `craft_testkit`'s. -use std::fmt::Write as _; -use std::panic::{AssertUnwindSafe, catch_unwind}; -use std::time::{Duration, Instant}; +#[cfg(not(target_arch = "wasm32"))] +pub use craft_testkit::hostile::{HammerConfig, HammerError}; +pub use craft_testkit::hostile::{MAX_CASES, Mutation, Mutations, Rng, mutations}; -/// xorshift64* PRNG. -pub struct Rng(pub u64); +/// The variable that multiplies [`hammer`]'s iterations. +#[cfg(not(target_arch = "wasm32"))] +const MULTIPLIER_VARIABLE: &str = "CRAFT_MEDIA_HOSTILE_MULT"; -impl Rng { - /// The next value. - pub fn next_u64(&mut self) -> u64 { - self.0 ^= self.0 >> 12; - self.0 ^= self.0 << 25; - self.0 ^= self.0 >> 27; - self.0.wrapping_mul(0x2545_F491_4F6C_DD1D) - } - /// A value below `n` (0 for `n` = 0). - pub fn below(&mut self, n: usize) -> usize { - (self.next_u64() % n.max(1) as u64) as usize - } -} - -const EXTREMES: [u64; 8] = [0, 1, 0x7F, 0x80, 0xFF, 0xFFFF, 0x8000_0000, u64::MAX]; - -fn run_one(name: &str, what: &str, data: &[u8], run: &dyn Fn(&[u8])) { - let t = Instant::now(); - let r = catch_unwind(AssertUnwindSafe(|| run(data))); - let el = t.elapsed(); - assert!(r.is_ok(), "{name}: PANIC on {what} (len {})", data.len()); - assert!(el < Duration::from_secs(5), "{name}: HANG/slow ({el:?}) on {what} (len {})", data.len()); -} - -/// Run `run` over every truncation of `seed` (stepped beyond 4096 lengths), then `iters` random -/// mutations (bit flips, random bytes, 1/2/4/8-byte extreme-value overwrites in both -/// endiannesses, deletions, truncation). Panics, naming the mutation, on the first panic or on -/// a run slower than 5 s. -pub fn hammer(name: &str, seed: &[u8], iters: usize, run: impl Fn(&[u8])) { - run_one(name, "the unmodified seed", seed, &run); - let step = (seed.len() / 4096).max(1); - for n in (0..seed.len()).step_by(step) { - run_one(name, &format!("truncation to {n}"), &seed[..n], &run); - } - let mut rng = Rng((0x5EED_F00D ^ seed.len() as u64) | 1); - // CRAFT_MEDIA_HOSTILE_MULT=10 runs ten times the iterations (soak runs). - let mult = std::env::var("CRAFT_MEDIA_HOSTILE_MULT").ok().and_then(|v| v.parse::().ok()).unwrap_or(1); - for _ in 0..iters.saturating_mul(mult) { - let mut d = seed.to_vec(); - let mut what = String::new(); - for _ in 0..1 + rng.below(4) { - if d.is_empty() { - break; - } - let pos = rng.below(d.len()); - match rng.below(7) { - 0 => { - let b = rng.below(8); - d[pos] ^= 1 << b; - let _ = write!(what, "flip[{pos}].{b} "); - } - 1 => { - d[pos] = rng.next_u64() as u8; - let _ = write!(what, "set[{pos}]={:#x} ", d[pos]); - } - 2..=4 => { - let w = [1usize, 2, 4, 8][rng.below(4)]; - let v = EXTREMES[rng.below(EXTREMES.len())]; - let be = rng.below(2) == 0; - let bytes = if be { v.to_be_bytes()[8 - w..].to_vec() } else { v.to_le_bytes()[..w].to_vec() }; - let end = (pos + w).min(d.len()); - d[pos..end].copy_from_slice(&bytes[..end - pos]); - let _ = write!(what, "put{w}{}[{pos}]={v:#x} ", if be { "be" } else { "le" }); - } - 5 => { - let n = 1 + rng.below(16); - let end = (pos + n).min(d.len()); - d.drain(pos..end); - let _ = write!(what, "del[{pos}..{end}] "); - } - _ => { - d.truncate(pos); - let _ = write!(what, "trunc{pos} "); - } - } - } - run_one(name, &what, &d, &run); - } +/// Run `run` over `seed`, every truncation of it, then `iters` random mutations (bit flips, random +/// bytes, 1/2/4/8-byte extreme-value overwrites in both endiannesses, deletions, truncation). The +/// cases run on a worker thread with a limit of 5 s each, which `CRAFT_TEST_TIMEOUT_SECS` replaces. +/// Panics on the first case that panics or does not finish, naming `name`, the case, its length and +/// the panic's message. +/// +/// `CRAFT_MEDIA_HOSTILE_MULT=10` runs ten times the iterations (soak runs). A value that is not a +/// whole number above zero panics, naming the variable, before `run` is called. +#[cfg(not(target_arch = "wasm32"))] +#[track_caller] +pub fn hammer(name: &str, seed: &[u8], iters: usize, run: impl Fn(&[u8]) + Send + Sync + 'static) { + craft_testkit::hostile::hammer(&HammerConfig::new(name, iters).with_multiplier_variable(MULTIPLIER_VARIABLE), seed, run); } diff --git a/crates/media-testkit/src/lib.rs b/crates/media-testkit/src/lib.rs index 6d3a75525..4eba224e0 100644 --- a/crates/media-testkit/src/lib.rs +++ b/crates/media-testkit/src/lib.rs @@ -11,8 +11,9 @@ //! Fixtures are generated into `$CRAFT_MEDIA_FIXTURES_DIR` or `/target/fixtures`, one //! directory per crate or format, and never committed. //! -//! [`hostile::hammer`] runs a parser over truncations and mutations of a valid input and fails on -//! a panic or a slow run; `CRAFT_MEDIA_HOSTILE_MULT=10` runs ten times the iterations. +//! [`hostile::hammer`] runs a parser over every truncation and mutations of a valid input and fails +//! on a panic or a case that does not finish in time; `CRAFT_MEDIA_HOSTILE_MULT=10` runs ten times +//! the iterations. It and [`with_deadline`] call `craft_testkit`. //! //! Failing loudly is what a test helper is for: a wrong `CRAFT_MEDIA_*` setting or an unwritable //! fixture directory panics, which fails the test that asked. @@ -216,35 +217,19 @@ pub fn set_progress(label: &str) { eprintln!("PROGRESS: {label}"); } -/// Run closure with an execution deadline, terminating with a panic if the limit is exceeded. -#[allow(clippy::panic)] +/// Run closure with an execution deadline of `secs` seconds, which `CRAFT_TEST_TIMEOUT_SECS` +/// replaces, through [`craft_testkit::with_deadline`]. Panics when the closure does not finish in +/// time, when `secs` is zero, or when the variable is not a whole number of seconds above zero. +#[cfg(not(target_arch = "wasm32"))] +#[track_caller] pub fn with_deadline(secs: u64, f: F) { - let timeout_secs = std::env::var("CRAFT_TEST_TIMEOUT_SECS").ok().and_then(|s| s.parse().ok()).unwrap_or(secs); - with_timeout(timeout_secs, f); + with_limit(craft_testkit::DeadlineLimit::new(std::time::Duration::from_secs(secs)), f); } -#[allow(clippy::panic)] -fn with_timeout(timeout_secs: u64, f: F) { - let (tx, rx) = std::sync::mpsc::channel(); - let handle = std::thread::spawn(move || { - f(); - let _ = tx.send(()); - }); - match rx.recv_timeout(std::time::Duration::from_secs(timeout_secs)) { - Ok(()) => { - if let Err(payload) = handle.join() { - std::panic::resume_unwind(payload); - } - } - Err(std::sync::mpsc::RecvTimeoutError::Timeout) => { - panic!("test exceeded execution deadline of {timeout_secs} seconds"); - } - Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => { - if let Err(payload) = handle.join() { - std::panic::resume_unwind(payload); - } - } - } +#[cfg(not(target_arch = "wasm32"))] +#[track_caller] +fn with_limit(limit: craft_testkit::DeadlineLimit<'_>, f: F) { + craft_testkit::with_deadline(limit, "the test body", f); } /// Return fixture directory for a crate/category, creating it if needed. @@ -339,10 +324,31 @@ Encoders: } #[test] - #[should_panic(expected = "test exceeded execution deadline")] + #[should_panic(expected = "the test body did not finish within 1s")] fn with_deadline_times_out() { - with_timeout(1, || { - std::thread::sleep(std::time::Duration::from_secs(3)); + let (_keep, rx) = std::sync::mpsc::channel::<()>(); + let limit = craft_testkit::DeadlineLimit::new(std::time::Duration::from_secs(1)).with_variable("CRAFT_MEDIA_TESTKIT_UNSET_IN_TESTS"); + with_limit(limit, move || { + let _ = rx.recv(); }); } + + #[test] + #[should_panic(expected = "the default limit is zero")] + fn with_deadline_of_zero_fails() { + with_deadline(0, || {}); + } + + #[test] + fn with_deadline_names_a_bad_timeout_variable() { + let out = Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "tests::with_deadline_completes", "--nocapture", "--test-threads=1"]) + .env("CRAFT_TEST_TIMEOUT_SECS", "soon") + .output() + .unwrap(); + let printed = format!("{}{}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr)); + assert!(printed.contains("running 1 test"), "the child did not run the test:\n{printed}"); + assert!(!out.status.success(), "the child passed:\n{printed}"); + assert!(printed.contains(r#"CRAFT_TEST_TIMEOUT_SECS="soon" is not a whole number of seconds"#), "{printed}"); + } } diff --git a/crates/media-testkit/tests/hostile.rs b/crates/media-testkit/tests/hostile.rs new file mode 100644 index 000000000..8e34cd5af --- /dev/null +++ b/crates/media-testkit/tests/hostile.rs @@ -0,0 +1,93 @@ +//! `hostile::hammer` as the media crates' tests call it. + +use std::collections::BTreeSet; +use std::process::Command; +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +use craft_media_testkit::hostile::hammer; + +/// Set only in the child processes of `run_child`: the value names what `child_hammer` does. +const CHILD_VARIABLE: &str = "CRAFT_MEDIA_TESTKIT_HOSTILE_CHILD"; + +/// Overrides the two-minute limit on each child process of `run_child`. The limit does not read +/// `CRAFT_TEST_TIMEOUT_SECS`, which `a_hang_fails_at_the_deadline` sets for the child. +const WATCHDOG_VARIABLE: &str = "CRAFT_MEDIA_TESTKIT_CHILD_WATCHDOG_SECS"; + +/// Runs `child_hammer` in a child process with `vars` set, and returns what it printed. The child +/// must fail. A child that never returns fails the test after two minutes. +fn run_child(vars: &'static [(&'static str, &'static str)]) -> String { + let limit = craft_testkit::DeadlineLimit::new(Duration::from_secs(120)).with_variable(WATCHDOG_VARIABLE); + craft_testkit::with_deadline(limit, "the child test", move || { + let out = Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "child_hammer", "--nocapture", "--test-threads=1"]) + .envs(vars.iter().copied()) + .output() + .unwrap(); + let printed = format!("{}{}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr)); + assert!(printed.contains("running 1 test"), "the child did not run the test:\n{printed}"); + assert!(!out.status.success(), "the child passed:\n{printed}"); + printed + }) +} + +/// Runs in the child processes of `run_child`. Without `CHILD_VARIABLE` it hammers a small seed and +/// passes; with `hang`, its parser never returns. +#[test] +fn child_hammer() { + if std::env::var_os(CHILD_VARIABLE).is_some_and(|v| v == "hang") { + hammer("hang", &[1, 2, 3], 0, |_| { + loop { + std::thread::park(); + } + }); + } else { + hammer("small", &[1, 2, 3], 10, |_| {}); + } +} + +#[test] +fn a_hang_fails_at_the_deadline() { + let printed = run_child(&[(CHILD_VARIABLE, "hang"), ("CRAFT_TEST_TIMEOUT_SECS", "1")]); + assert!(printed.contains("hang: the unmodified seed (len 3) did not finish within 1s, set by CRAFT_TEST_TIMEOUT_SECS"), "{printed}"); +} + +#[test] +fn a_bad_multiplier_names_the_variable() { + let printed = run_child(&[("CRAFT_MEDIA_HOSTILE_MULT", "ten")]); + assert!(printed.contains(r#"small: CRAFT_MEDIA_HOSTILE_MULT="ten" is not a whole number written in digits"#), "{printed}"); +} + +#[test] +fn a_panic_keeps_its_message() { + let payload = std::panic::catch_unwind(|| hammer("demo", &[9, 8, 7, 6], 0, |d| assert!(d.len() != 2, "boom at {}", d.len()))).unwrap_err(); + let message = payload.downcast_ref::().cloned().unwrap_or_default(); + assert!(message.starts_with("demo: truncation to 2 (len 2): the parser panicked: "), "{message}"); + assert!(message.contains("boom at 2"), "{message}"); +} + +#[test] +fn every_truncation_of_a_large_seed_runs() { + let seed: Vec = (0..8_192u32).map(|i| i as u8).collect(); + let lengths = Arc::new(Mutex::new(BTreeSet::new())); + let record = Arc::clone(&lengths); + hammer("large", &seed, 0, move |d| { + record.lock().unwrap().insert(d.len()); + }); + // The seed and its 8,192 truncations. + assert_eq!(lengths.lock().unwrap().len(), 8_193); +} + +#[test] +fn the_child_timeout_variable_does_not_set_the_watchdog() { + // `a_hang_fails_at_the_deadline` sets `CRAFT_TEST_TIMEOUT_SECS` for its child. Run it with a value + // that `with_deadline` rejects; it passes only if `run_child`'s watchdog reads another variable. + let out = Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "a_hang_fails_at_the_deadline", "--nocapture", "--test-threads=1"]) + .env("CRAFT_TEST_TIMEOUT_SECS", "not a number") + .output() + .unwrap(); + let printed = format!("{}{}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr)); + assert!(printed.contains("running 1 test"), "the test did not run:\n{printed}"); + assert!(out.status.success(), "{printed}"); +} diff --git a/crates/media/src/mpegts/tests/hostile.rs b/crates/media/src/mpegts/tests/hostile.rs index ff1784fc8..97bba47d0 100644 --- a/crates/media/src/mpegts/tests/hostile.rs +++ b/crates/media/src/mpegts/tests/hostile.rs @@ -89,7 +89,8 @@ fn elementary_stream_frame_parsers_never_panic() { ac3.extend([1u8; 60]); let seeds = [adts_frame(3), mp2_frame(1), ac3, mpeg2_frame(1, 0, false, 64), vec![0x56, 0xE0, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80]]; for (i, s) in seeds.iter().enumerate() { - craft_media_testkit::hostile::hammer(&format!("es seed {i}"), s, 40_000, |d| { + let codecs = codecs.clone(); + craft_media_testkit::hostile::hammer(&format!("es seed {i}"), s, 40_000, move |d| { for c in &codecs { let _ = frame_bytes(c, d); let _ = frame_info(c, d); diff --git a/crates/testkit/Cargo.toml b/crates/testkit/Cargo.toml new file mode 100644 index 000000000..d32ed735f --- /dev/null +++ b/crates/testkit/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "craft-testkit" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true +description = "Source-level platform regression guards and verification utilities for the Craft application suite" +publish = false + +[dependencies] + +[lints] +workspace = true diff --git a/crates/testkit/README.md b/crates/testkit/README.md new file mode 100644 index 000000000..8b8e9b208 --- /dev/null +++ b/crates/testkit/README.md @@ -0,0 +1,268 @@ +# craft-testkit + +Source-level platform regression guards and verification utilities for the Craft application suite. + +## Overview + +`craft-testkit` provides lightweight, non-panicking source guards that prevent applications in the Craft ecosystem from repeating platform defects that were previously diagnosed and resolved in peer applications: + +1. **`predefined_quit`**: Flags direct calls to `muda`'s `PredefinedMenuItem::quit` outside comments, strings and character literals, also with whitespace or a line break around `::`, in the qualified form `::quit`, and through a `use muda::PredefinedMenuItem as ...` rename. A type of the same name reached through another crate or module, such as `other::PredefinedMenuItem::quit`, is not flagged. On macOS, this predefined menu item routes directly to `NSApplication terminate:`, completely bypassing application dirty document checks and unsaved changes save prompts. Applications should instead wire Quit through `craft-quit` lifecycle interceptors. +2. **`wgsl_float_literals` / `wgsl_float_literals_in`**: Scans WGSL shader sources and flags decimal and hexadecimal float literals whose value, rounded to `f64`, exceeds `f32::MAX` (such as `3.40282347e38` or `0x1p+500f`), or 65504, the largest finite `f16`, for a literal with an `h` suffix. A literal that exceeds the maximum by less than `f64` precision rounds to the maximum and is not reported. Integer literals are not checked. Some native compilers round such literals down, but browser WebGPU compilers reject them with pipeline compilation errors, forcing applications into CPU fallback rendering. `f32::MAX` can be written as the hex float `0x1.fffffep+127f`, which equals it, or as the decimal `3.4028234663852886e38`, which rounds to it. +3. **`std_instant`**: Flags `std::time::Instant` outside comments and strings: the full path, `::Instant` where a `use` binds `m` to `std::time`, and each `use` that imports `std::time::Instant` or `std::time::*`, under any name. The parts of a path may be split by whitespace, comments or line breaks. Other crates' `Instant` (such as `web_time::Instant` or `tokio::time::Instant`) is not flagged, and neither is an item whose `cfg` attribute is false on `wasm32-unknown-unknown`; code before that attribute or after its item on the same line is still checked. A `mod name;` inside such an item also skips the module's file (`name.rs` or `name/mod.rs`, or the file a `#[path]` attribute names) and the files of the modules declared in it, when they are among the files passed in and no declaration outside such items also reaches them. On that target, `std::time::Instant::now()` panics at runtime with `"time not implemented on this platform"`. Code targeting the web must use `web_time::Instant`. +4. **`rust_sources`**: Recursively discovers a crate's `.rs` source files while excluding integration tests (`tests/`), test modules (`tests.rs`, `tests_*.rs`, `*_tests.rs`, `*_test.rs`), build artifacts (`target/`, any folder holding a `CACHEDIR.TAG` file that starts with the line `Signature: 8a477f597d28d172789f06886806bc55`, `.git/` and other folders whose name starts with `.`) and symbolic links, which it does not follow. A file over 8 MiB, more than 256 MiB in all or more than 50,000 files returns an error; `rust_sources_with_limits` takes other limits. It is not built for `wasm32`. + +## Epistemic Scope: Syntactic Guards vs Runtime Validation + +Source-level guards provide deterministic syntactic evidence about codebase structure: + +### What They Establish +- Checked source files do not contain forbidden symbol invocations, known out-of-range literal representations, or platform-hazardous imports. +- Reusable, deterministic checks that run in milliseconds as regular Cargo tests without external runners. + +### What They Cannot Establish +- **Runtime execution safety**: Syntactic checks do not verify whether code branches actually execute at runtime. +- **Semantic type resolution**: The scanners follow `use` renames within one file only. They do not resolve paths through other modules or re-exports, a rename of `std` itself, or expand macros. +- **Complex target predicates**: `std_instant` evaluates `cfg` predicates (`all`, `any`, `not`, `unix`, `windows` and the `target_*` keys) for `wasm32-unknown-unknown`, on outer attributes for the item that follows and on inner attributes (`#![cfg(...)]`) for the rest of the module, and on `mod name;` declarations for the module's file and its descendants. It compares file paths as text, so the files must come from one `rust_sources` call or be named the same way. A predicate that depends on a feature or on `test` is not known from the source, so its item is checked. Applications should scope `std_instant` tests to the specific crates that compile into their web distribution. +- **Driver and host OS behavior**: Syntactic checks do not verify WebGPU driver compatibility or host window manager event dispatch. + +Source guards serve as fast, zero-dependency regression gates in application test suites, complementing platform integration tests and CI workflows. + +## Consumer Adoption Examples + +### 1. PhotoCraft: Adopt `wgsl_float_literals` and `predefined_quit` + +In `photocraft/crates/gpu/src/lib.rs`, replace the in-tree `decimal_float_literals` helper with `craft_testkit::wgsl_float_literals`: + +```rust +#[test] +fn shader_float_literals_fit_f32() { + let bad = craft_testkit::wgsl_float_literals(SHADER); + assert!( + bad.is_empty(), + "float literals outside f32 finite range reject on WebGPU (Tint):\n{}", + bad.iter().map(|f| f.to_string()).collect::>().join("\n") + ); +} +``` + +In `photocraft/apps/photocraft/tests/native_menu_tests.rs`: + +```rust +#[test] +fn no_unintercepted_predefined_quit_in_menu() { + let sources = craft_testkit::rust_sources("src").expect("scan sources"); + let findings = craft_testkit::predefined_quit(&sources); + assert!( + findings.is_empty(), + "predefined muda Quit bypasses dirty document save prompts:\n{}", + findings.iter().map(|f| f.to_string()).collect::>().join("\n") + ); +} +``` + +### 2. FilmCraft: Adopt `std_instant` and `wgsl_float_literals` + +In `filmcraft/crates/engine/src/wasm_clock_tests.rs`, replace in-tree textual search with `craft_testkit::std_instant`: + +```rust +#[test] +fn no_std_instant_in_code_the_web_build_runs() { + let sources = craft_testkit::rust_sources(Path::new(env!("CARGO_MANIFEST_DIR")).join("src")) + .expect("scan engine sources"); + let findings = craft_testkit::std_instant(&sources); + assert!( + findings.is_empty(), + "std::time::Instant panics on wasm32; use web_time::Instant:\n{}", + findings.iter().map(|f| f.to_string()).collect::>().join("\n") + ); +} +``` + +## deadline + +The `deadline` module is not built for `wasm32`. + +`with_deadline(limit, label, f)` runs `f` on its own thread and returns its value. When `f` does not +finish within the limit, the test fails with a message that names `label`, the limit and the variable +that sets it. A panic in `f` is raised again in the test with its original payload. The limit counts +from the call, including the time spent starting the thread. When `f` returns or panics after the +limit has passed, the test fails with the label and the limit, and with the panic's message if it has +one. + +`wait_until(limit, what, cond)` calls `cond` on the test's thread until it returns `true`. It calls +`cond` at once, then after pauses that start at 1 ms and double up to 50 ms, and does not call it +again once the limit has passed. When the limit passes first, or `cond` returns `true` only after the +limit has passed, the test fails with a message that names `what`. + +```rust +use std::time::Duration; +use craft_testkit::deadline::{DeadlineLimit, wait_until, with_deadline}; + +// The default of 30 s; CRAFT_TEST_TIMEOUT_SECS replaces it. +let pixels = with_deadline(Duration::from_secs(30), "decode seed 7", move || decode(&bytes)); + +// The app's own variable replaces the default instead. +let limit = DeadlineLimit::new(Duration::from_secs(30)).with_variable("FILMCRAFT_HOSTILE_TIMEOUT_SECS"); +with_deadline(limit, "hostile input 12", move || parse(&input)); + +wait_until(Duration::from_secs(10), "the export to finish", || job.is_done()); +``` + +### The limit variable + +A `Duration` reads `CRAFT_TEST_TIMEOUT_SECS`. `DeadlineLimit::with_variable` names another variable. +The variable is read on every call; neither function sets or caches it. + +| Value | Result | +|---|---| +| Not set | The default limit applies. | +| Decimal digits above zero, such as `30` or `007` | That many seconds replace the default. | +| Empty, signed (`+5`, `-5`), fractional (`1.5`), with spaces or units (`5s`), zero, or larger than a `u64` | The test fails with the variable's name and value, before `f` runs or `cond` is called. | +| Not Unicode | The test fails with the variable's name, before `f` runs or `cond` is called. | + +A zero default, a variable name that is empty or holds `=` or a NUL character, and a limit that ends past +the latest time the platform's clock can represent also fail the test before any work starts. + +FilmCraft's `filmcraft-testkit` uses the default when the variable holds something other than a +number or holds zero. This module fails the test instead. `craft-media-testkit`'s `with_deadline` +calls this module's. + +### Limits + +- A body that runs past its limit fails the test, but its thread is left running until the test + process exits. A body that never returns can keep the test binary alive after its tests finish. +- `wait_until` checks its limit only between calls of `cond`. A `cond` that blocks is not interrupted, + so it must return promptly. +- Neither function returns elapsed time. Tests of this crate use channel handshakes, and the only time + bounds in them are the limits under test. + +## hostile + +`mutations(seed, rounds)` yields `rounds` damaged copies of a valid seed. Each round makes one to four +edits: a bit flip, a random byte, a 1, 2, 4 or 8-byte overwrite with an extreme value +(0, 1, 0x7F, 0x80, 0xFF, 0xFFFF, 0x8000_0000 or `u64::MAX`) in either byte order, a deletion of up to +16 bytes, or a truncation. Each copy carries a label that lists its edits, such as +`flip[3].5 del[7..9]`. The xorshift64* generator behind it is seeded from the seed's length, so the +same seed length and round count give the same edits on every run and every platform. An empty seed +yields no copies. `mutations` and `Rng` build for `wasm32`. + +`hammer(&config, seed, parse)` runs `parse` on the unmodified seed, on every truncation of it +(lengths 0 to one less than its length), and on `mutations(seed, rounds)`. The cases run one after +another on one worker thread, and the test's thread fails the test when a case runs past its limit. +A case that finishes after its limit also fails the test. The test fails on the first case that +panics or does not finish, with a message that names the config's label, the case +(`the unmodified seed`, `truncation to N` or the edits), the input's length and the panic's message. +`parse` must be `Fn(&[u8]) + Send + Sync + 'static`, since a case that does not finish keeps running +on the worker thread after the test fails. `hammer` is not built for `wasm32`. + +```rust +use std::time::Duration; +use craft_testkit::DeadlineLimit; +use craft_testkit::hostile::{HammerConfig, hammer}; + +// 2,000 rounds times CRAFT_HOSTILE_MULT; 5 s per case, or CRAFT_TEST_TIMEOUT_SECS. +hammer(&HammerConfig::new("png seed 3", 2_000), &seed, |data| { + let _ = decode(data); +}); + +// The app's own variables instead. +let config = HammerConfig::new("mxf primer", 5_000) + .with_multiplier_variable("CRAFT_MEDIA_HOSTILE_MULT") + .with_timeout(DeadlineLimit::new(Duration::from_secs(5)).with_variable("FILMCRAFT_HOSTILE_TIMEOUT_SECS")); +hammer(&config, &primer, |data| { + let _ = parse_primer(data); +}); +``` + +### The rounds + +The rounds are the config's iterations times the multiplier variable, `CRAFT_HOSTILE_MULT` unless +`with_multiplier_variable` names another. The limit for each case is 5 s, replaced by +`CRAFT_TEST_TIMEOUT_SECS` or the variable that `with_timeout` names, as in `deadline`. Both variables +are read once, before any case runs. `HammerConfig::rounds` returns the rounds or the error that `hammer` would fail with. + +| Multiplier value | Result | +|---|---| +| Not set | The multiplier is 1. | +| Decimal digits above zero, such as `10` | The rounds are multiplied by that number. | +| Empty, signed, fractional, with spaces, zero, or larger than a `usize` | The test fails with the variable's name and value before `parse` is called. | +| Not Unicode | The test fails with the variable's name before `parse` is called. | + +A product of the iterations and the multiplier that does not fit in a `usize`, a call that would run +more than `MAX_CASES` (10,000,000) cases in all, and a limit variable that `deadline` would reject +also fail the test before `parse` is called. + +Changes from `craft-media-testkit`'s `hammer`: + +- A case that does not finish fails the test after its limit with the case's label. Before, a case + was timed after it returned and failed only when it took longer than 5 s. +- A panic's message is part of the failure. +- Every truncation runs. Before, a seed of 8,192 bytes or more ran every `len / 4096`th truncation. +- The caller names the multiplier variable. Before, it was always `CRAFT_MEDIA_HOSTILE_MULT`, and a + value that was not a number counted as 1. +- An empty seed runs once. Before, it ran once more for each round. +- A mutation's label joins its edits with single spaces. Before, every edit ended with a space, so + each label ended with one. +- `parse` is owned and runs on a worker thread. A closure that borrows from the test needs its data + moved or shared with an `Arc`. + +## Origin + +### guards + +| Module | Source | Path | Commit | +|---|---|---|---| +| `guards` | storytold/craft-libs#163 (zachshallbetter), with the rules of PhotoCraft's shader float test and FilmCraft's web clock test | `crates/testkit/src/guards.rs` | `e92825e427` | + +### deadline + +| Code | Repository | Path | SHA | +|---|---|---|---| +| `with_deadline`, its panic and timeout tests (`a_slow_body_passes`, `a_panic_in_the_body_is_re_raised`, `a_hang_fails_and_names_the_input`) | storytold/filmcraft | `crates/testkit/src/deadline.rs` | `cff7e144` | +| `with_deadline` under `CRAFT_TEST_TIMEOUT_SECS`, tests `with_deadline_completes` and `with_deadline_times_out` | storytold/craft-libs | `crates/media-testkit/src/lib.rs` | `75a654f4` | +| `within`, a deadline that returns the body's value | storytold/lightcraft | `crates/engine/src/demo/tests.rs` | `487833ad` | +| `within`, a deadline that returns the body's value | storytold/gridcraft | `crates/math/src/lib.rs` | `c2aba7a3` | +| `wait_until` | storytold/photocraft | `crates/ui-egui/src/canvas/paint_thread_tests.rs` | `0d3b1e85` | +| `wait_until` | storytold/lightcraft | `crates/engine/src/availability.rs` | `7ebbb17a` | +| `wait_until` | storytold/effectcraft | `crates/engine/src/footage/footage_reload.rs` | `53576d22` | +| `wait_until` | storytold/filmcraft | `crates/ui-egui/tests/it/export_ui.rs` | `cff7e144` | + +### hostile + +| Code | Repository | Path | SHA | +|---|---|---|---| +| `Rng`, the edits of `mutations` and `hammer` | storytold/craft-libs | `crates/media-testkit/src/hostile.rs` | `75a654f4` | + +## Migration + +### guards + +| Consumer | Guard | Replaces | State | +|---|---|---|---| +| PhotoCraft | `wgsl_float_literals` | `crates/gpu/src/tests.rs` scanner | Not started | +| FilmCraft | `std_instant` | `crates/engine/src/wasm_clock_tests.rs` search | Not started | + +### deadline + +| Consumer | Code it replaces | State | +|---|---|---| +| FilmCraft | `filmcraft-testkit`'s `with_deadline` (`FILMCRAFT_HOSTILE_TIMEOUT_SECS`) and the export UI tests' `wait_until` | Pending | +| LightCraft | `within` in the demo tests and `wait_until` in the availability tests | Pending | +| GridCraft | `within` in the tests of its `crates/math` | Pending | +| PhotoCraft | `wait_until` in the paint thread tests | Pending | +| EffectCraft | `wait_until` in the footage reload tests | Pending | +| `craft-media-testkit` | `with_deadline` (`CRAFT_TEST_TIMEOUT_SECS`) | Done | + +An adoption keeps the variable name the app uses today. + +### hostile + +| Consumer | Code it replaces | State | +|---|---|---| +| `craft-media-testkit` | `hostile::hammer` (`CRAFT_MEDIA_HOSTILE_MULT`), called by the ISO BMFF, AVI, Matroska, MXF and MPEG TS hostile tests | Done | +| `craft-cfb` | its `hostile_input_never_panics` test, through `craft-media-testkit` | Done | +| CADCraft | the mutation loop of `hostile_files_never_panic` in `crates/fonts/src/shx/tests.rs` | Pending | +| TextureCraft | the `hammer` of `malformed_meshes_are_errors` in `crates/formats/src/mesh_import/tests.rs` | Pending | + +CADCraft's and TextureCraft's loops make other edits than `mutations`. Their adoptions say which +cases change, and keep the test names. diff --git a/crates/testkit/src/deadline.rs b/crates/testkit/src/deadline.rs new file mode 100644 index 000000000..918719e6b --- /dev/null +++ b/crates/testkit/src/deadline.rs @@ -0,0 +1,788 @@ +//! Limits on how long a test waits. +//! +//! [`with_deadline`] runs a test body on its own thread and returns its value. When the body does not +//! finish within the limit, the test fails with the call's label, the limit and the variable that sets +//! it. A panic in the body is raised again in the test with its original payload. +//! +//! [`wait_until`] calls a condition on the test's thread until it returns `true`, pausing between +//! calls, and fails the test with its description when the limit passes first. +//! +//! A limit is a default [`Duration`] and the name of a variable that replaces it. A plain `Duration` +//! reads [`DEFAULT_VARIABLE`]; [`DeadlineLimit::with_variable`] names another one. The variable holds +//! whole seconds as decimal digits and is read on every call. A value that is empty, signed, +//! fractional, not Unicode, zero, or larger than a `u64` fails the test that reads it, as does a zero +//! default and a limit past the latest time the platform's clock can represent. +//! +//! Neither function can stop code that does not return. A body that runs past its limit fails the +//! test, and its thread is left running until the test process exits. A condition that blocks is +//! not interrupted: [`wait_until`] checks its limit only between calls, so a condition must return +//! promptly. + +use std::any::Any; +use std::ffi::OsString; +use std::fmt; +use std::io; +use std::panic::{AssertUnwindSafe, catch_unwind, resume_unwind}; +use std::sync::mpsc::{self, RecvTimeoutError, Sender}; +use std::sync::{Arc, Mutex, PoisonError}; +use std::thread; +use std::time::{Duration, Instant}; + +/// The variable a limit reads unless [`DeadlineLimit::with_variable`] names another. +pub const DEFAULT_VARIABLE: &str = "CRAFT_TEST_TIMEOUT_SECS"; + +/// The first pause of [`wait_until`] between two calls of its condition. +const FIRST_PAUSE: Duration = Duration::from_millis(1); + +/// The longest pause of [`wait_until`]: each pause doubles up to this. +const MAX_PAUSE: Duration = Duration::from_millis(50); + +/// The name of the thread [`with_deadline`] runs a body on. +const WORKER_NAME: &str = "craft-testkit deadline"; + +/// A limit for [`with_deadline`] and [`wait_until`]: a default duration and the name of the +/// variable that replaces it. +/// +/// ``` +/// use std::time::Duration; +/// use craft_testkit::deadline::DeadlineLimit; +/// +/// // Reads CRAFT_TEST_TIMEOUT_SECS. +/// let limit: DeadlineLimit = Duration::from_secs(30).into(); +/// assert_eq!(limit.variable(), "CRAFT_TEST_TIMEOUT_SECS"); +/// +/// // Reads the app's own variable instead. +/// let limit = DeadlineLimit::new(Duration::from_secs(30)).with_variable("FILMCRAFT_HOSTILE_TIMEOUT_SECS"); +/// assert_eq!(limit.default_limit(), Duration::from_secs(30)); +/// ``` +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct DeadlineLimit<'a> { + default: Duration, + variable: &'a str, +} + +impl DeadlineLimit<'static> { + /// A limit of `default` that [`DEFAULT_VARIABLE`] replaces. + pub fn new(default: Duration) -> Self { + Self { default, variable: DEFAULT_VARIABLE } + } +} + +impl<'a> DeadlineLimit<'a> { + /// The same default, replaced by the variable `name` instead. + pub fn with_variable<'b>(self, name: &'b str) -> DeadlineLimit<'b> { + DeadlineLimit { default: self.default, variable: name } + } + + /// The limit that applies when the variable is not set. + pub fn default_limit(&self) -> Duration { + self.default + } + + /// The name of the variable that replaces the default. + pub fn variable(&self) -> &'a str { + self.variable + } +} + +impl From for DeadlineLimit<'static> { + fn from(default: Duration) -> Self { + Self::new(default) + } +} + +/// Runs `f` on its own thread and returns its value, failing the calling test when `f` does not +/// finish within `limit`. +/// +/// The failure names `label`, the limit and the variable that sets it. A panic in `f` is raised +/// again here with its original payload. The limit is read and checked before `f` starts, and a +/// limit that is not valid fails the test without running `f`. The limit counts from the call, so +/// the time spent starting the thread counts against it. When `f` returns or panics after the limit +/// has passed, the test fails with the label and the limit, and with the panic's message if it has +/// one. When the limit passes, the thread running `f` is left running. +/// +/// ``` +/// use std::time::Duration; +/// use craft_testkit::deadline::with_deadline; +/// +/// let sum = with_deadline(Duration::from_secs(30), "sum of 1 to 10", || (1..=10).sum::()); +/// assert_eq!(sum, 55); +/// ``` +// Failing the calling test is what this function is for. +#[allow(clippy::panic)] +#[track_caller] +pub fn with_deadline<'a, T: Send + 'static>(limit: impl Into>, label: &str, f: impl FnOnce() -> T + Send + 'static) -> T { + let start = Instant::now(); + match run_with_deadline(limit.into(), label, f, read_variable, spawn_worker, Arc::new(move || start.elapsed())) { + Ok(value) => value, + Err(Failure::Panic(payload)) => resume_unwind(payload), + Err(Failure::Message(message)) => panic!("{message}"), + } +} + +/// Calls `cond` on the calling thread until it returns `true`, failing the calling test when the +/// limit passes first. +/// +/// `cond` is called at once, and then after pauses that grow from 1 ms to 50 ms, but not again once +/// the limit has passed. The failure names `what`, the limit and the variable that sets it. A `true` +/// that `cond` returns after the limit has passed also fails the test. A panic in `cond` reaches the +/// caller unchanged. `cond` runs on the calling thread and is not interrupted, so it must return +/// promptly. +/// +/// ``` +/// use std::sync::atomic::{AtomicBool, Ordering}; +/// use std::time::Duration; +/// use craft_testkit::deadline::wait_until; +/// +/// let ready = AtomicBool::new(false); +/// std::thread::scope(|s| { +/// s.spawn(|| ready.store(true, Ordering::SeqCst)); +/// wait_until(Duration::from_secs(30), "the worker to start", || ready.load(Ordering::SeqCst)); +/// }); +/// ``` +// Failing the calling test is what this function is for. +#[allow(clippy::panic)] +#[track_caller] +pub fn wait_until<'a>(limit: impl Into>, what: &str, mut cond: impl FnMut() -> bool) { + let limit = limit.into(); + let start = Instant::now(); + let resolved = match resolve(&limit, read_variable, start) { + Ok(resolved) => resolved, + Err(error) => panic!("waiting for {what}: {error}"), + }; + let mut elapsed = || start.elapsed(); + if let Err(message) = poll_until(&resolved, what, &mut cond, &mut elapsed, &mut thread::sleep) { + panic!("{message}"); + } +} + +/// A limit after reading its variable. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct Resolved<'a> { + pub(crate) limit: Duration, + variable: &'a str, + from_variable: bool, +} + +impl fmt::Display for Resolved<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.from_variable { + write!(f, "{:?}, set by {}", self.limit, self.variable) + } else { + write!(f, "{:?}, the default; {} replaces it in whole seconds", self.limit, self.variable) + } + } +} + +/// Why a limit cannot be used. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) enum LimitError<'a> { + /// The variable name is empty or holds `=` or a NUL character. + BadName { variable: &'a str }, + /// The variable's value is not Unicode. + NotUnicode { variable: &'a str }, + /// The variable's value is not a whole number of seconds above zero. + BadValue { variable: &'a str, value: String, reason: &'static str }, + /// The default limit is zero. + ZeroDefault { variable: &'a str }, + /// The limit ends past the latest time the clock can represent. + TooLarge { resolved: Resolved<'a> }, +} + +impl fmt::Display for LimitError<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::BadName { variable } => write!(f, "{variable:?} cannot name a variable: it is empty or holds `=` or a NUL character"), + Self::NotUnicode { variable } => write!(f, "{variable} is not valid Unicode; set it to a whole number of seconds above zero"), + Self::BadValue { variable, value, reason } => { + write!(f, "{variable}={value:?} {reason}; set it to a whole number of seconds above zero") + } + Self::ZeroDefault { variable } => write!(f, "the default limit is zero; pass a default above zero ({variable} replaces it)"), + Self::TooLarge { resolved } => write!(f, "the limit of {resolved} ends past the latest time this platform's clock can represent"), + } + } +} + +/// Reads and checks `limit`: the variable's value when `lookup` finds one, otherwise the default. +/// `now` is the time the limit starts from. +pub(crate) fn resolve<'a>( + limit: &DeadlineLimit<'a>, + lookup: impl FnOnce(&str) -> Option, + now: Instant, +) -> Result, LimitError<'a>> { + let variable = limit.variable; + if variable.is_empty() || variable.contains(['=', '\0']) { + return Err(LimitError::BadName { variable }); + } + if limit.default.is_zero() { + return Err(LimitError::ZeroDefault { variable }); + } + let resolved = match lookup(variable) { + None => Resolved { limit: limit.default, variable, from_variable: false }, + Some(raw) => { + let value = raw.into_string().map_err(|_| LimitError::NotUnicode { variable })?; + let secs = parse_secs(&value).map_err(|reason| LimitError::BadValue { variable, value: value.clone(), reason })?; + Resolved { limit: Duration::from_secs(secs), variable, from_variable: true } + } + }; + if now.checked_add(resolved.limit).is_none() { + return Err(LimitError::TooLarge { resolved }); + } + Ok(resolved) +} + +/// Parses a whole number of seconds above zero, written as decimal digits only. +fn parse_secs(value: &str) -> Result { + if value.is_empty() { + return Err("is empty"); + } + if !value.bytes().all(|b| b.is_ascii_digit()) { + return Err("is not a whole number of seconds written in digits"); + } + match value.parse::() { + Ok(0) => Err("is zero"), + Ok(secs) => Ok(secs), + Err(_) => Err("is more seconds than a u64 holds"), + } +} + +/// Reads the variable `name` from the process environment. +fn read_variable(name: &str) -> Option { + std::env::var_os(name) +} + +/// A body boxed for the worker thread. +type Job = Box; + +/// Starts `job` on a named thread and detaches it. The thread sends on `done` after `job` returns. +fn spawn_worker(job: Job, done: Sender<()>) -> io::Result<()> { + thread::Builder::new() + .name(WORKER_NAME.to_owned()) + .spawn(move || { + job(); + let _ = done.send(()); + }) + .map(drop) +} + +/// The body's result and the clock reading when it finished, filled in by the worker. +type Slot = Arc, Duration)>>>; + +/// How a [`with_deadline`] call failed. +enum Failure { + /// The body panicked with this payload. + Panic(Box), + /// The call failed for this reason. + Message(String), +} + +/// A clock that returns the time since a [`with_deadline`] call started. +type Clock = Arc Duration + Send + Sync>; + +/// The text of a panic payload that holds a message. +fn panic_text(payload: &(dyn Any + Send)) -> Option<&str> { + payload.downcast_ref::().map(String::as_str).or_else(|| payload.downcast_ref::<&str>().copied()) +} + +/// [`with_deadline`] with the variable lookup, the thread start and the clock passed in. Starting +/// the worker counts against the limit. The worker stores the body's result with the clock reading +/// when it finished, under a lock, and then sends on the channel `spawn` receives. When the wait +/// times out, the stored result is still used if it is there, so a body that finished in time passes +/// even when its thread sends late. +fn run_with_deadline( + limit: DeadlineLimit<'_>, + label: &str, + f: impl FnOnce() -> T + Send + 'static, + lookup: impl FnOnce(&str) -> Option, + spawn: impl FnOnce(Job, Sender<()>) -> io::Result<()>, + clock: Clock, +) -> Result { + let resolved = resolve(&limit, lookup, Instant::now()).map_err(|error| Failure::Message(format!("{label}: {error}")))?; + let (tx, rx) = mpsc::channel(); + let slot: Slot = Arc::new(Mutex::new(None)); + let worker_slot = Arc::clone(&slot); + let worker_clock = Arc::clone(&clock); + let job: Job = Box::new(move || { + let result = catch_unwind(AssertUnwindSafe(f)); + // The clock is read under the lock: once the caller finds the slot empty after its limit, + // any later reading is past the limit too. + let mut stored = worker_slot.lock().unwrap_or_else(PoisonError::into_inner); + *stored = Some((result, worker_clock())); + }); + spawn(job, tx).map_err(|error| Failure::Message(format!("{label}: could not start the worker thread: {error}")))?; + let waited = rx.recv_timeout(resolved.limit.saturating_sub(clock())); + let stored = slot.lock().unwrap_or_else(PoisonError::into_inner).take(); + match (stored, waited) { + (Some((result, finished)), _) if finished > resolved.limit => { + let panicked = match &result { + Ok(_) => String::new(), + Err(payload) => format!(" (it panicked: {})", panic_text(payload.as_ref()).unwrap_or("a payload that is not a message")), + }; + Err(Failure::Message(format!("{label} finished only after its limit of {resolved}{panicked}"))) + } + (Some((Ok(value), _)), _) => Ok(value), + (Some((Err(payload), _)), _) => Err(Failure::Panic(payload)), + (None, Err(RecvTimeoutError::Timeout)) => { + Err(Failure::Message(format!("{label} did not finish within {resolved}; its thread is left running"))) + } + (None, _) => Err(Failure::Message(format!("{label}: the worker thread stopped without a result"))), + } +} + +/// The loop of [`wait_until`], with the clock and the pause passed in. `elapsed` returns the time +/// since the wait started. +fn poll_until( + resolved: &Resolved<'_>, + what: &str, + cond: &mut dyn FnMut() -> bool, + elapsed: &mut dyn FnMut() -> Duration, + pause: &mut dyn FnMut(Duration), +) -> Result<(), String> { + let mut next_pause = FIRST_PAUSE; + let mut first = true; + loop { + // After the first call, the condition is called only while the limit has not passed. + if !first && elapsed() >= resolved.limit { + return Err(format!("timed out waiting for {what} within {resolved}")); + } + first = false; + let held = cond(); + let spent = elapsed(); + if held && spent <= resolved.limit { + return Ok(()); + } + if held { + return Err(format!("timed out waiting for {what} within {resolved} (it held only after the limit had passed)")); + } + if spent < resolved.limit { + pause(next_pause.min(resolved.limit - spent)); + } + next_pause = next_pause.saturating_mul(2).min(MAX_PAUSE); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::ffi::OsStr; + use std::process::Command; + use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; + + /// A variable no test sets, so the environment cannot lengthen a test's short limit. + const UNSET: &str = "CRAFT_TESTKIT_DEADLINE_UNSET_IN_TESTS"; + + /// The variable `run_child` sets for the child process. + const CHILD_VARIABLE: &str = "CRAFT_TESTKIT_DEADLINE_CHILD_SECS"; + + fn short() -> DeadlineLimit<'static> { + DeadlineLimit::new(Duration::from_millis(50)).with_variable(UNSET) + } + + fn long() -> DeadlineLimit<'static> { + DeadlineLimit::new(Duration::from_secs(600)).with_variable(UNSET) + } + + fn message(payload: Box) -> String { + match payload.downcast::() { + Ok(text) => *text, + Err(payload) => match payload.downcast::<&str>() { + Ok(text) => (*text).to_owned(), + Err(_) => panic!("the panic payload is not a message"), + }, + } + } + + fn failure_message(result: Result) -> String { + match result { + Ok(_) => panic!("the call did not fail"), + Err(Failure::Panic(payload)) => message(payload), + Err(Failure::Message(text)) => text, + } + } + + /// Runs `child_reads_its_variable` in a child process with `CHILD_VARIABLE` set to `value`, and + /// returns what the child printed. The child must fail. + fn run_child(value: &OsStr) -> String { + let out = Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "deadline::tests::child_reads_its_variable", "--nocapture", "--test-threads=1"]) + .env(CHILD_VARIABLE, value) + .output() + .unwrap(); + let printed = format!("{}{}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr)); + assert!(!out.status.success(), "the child passed:\n{printed}"); + assert!(printed.contains("running 1 test"), "the child did not run the test:\n{printed}"); + printed + } + + /// Runs in the child processes of `run_child`. Without the variable it takes its default and + /// passes; with it, the body blocks until the limit passes. + #[test] + fn child_reads_its_variable() { + let blocks = std::env::var_os(CHILD_VARIABLE).is_some(); + let (_keep, rx) = mpsc::channel::<()>(); + with_deadline(DeadlineLimit::new(Duration::from_secs(2)).with_variable(CHILD_VARIABLE), "child body", move || { + if blocks { + let _ = rx.recv(); + } + }); + } + + #[test] + fn a_slow_body_passes() { + let (tx, rx) = mpsc::channel(); + let sender = thread::spawn(move || tx.send(()).unwrap()); + with_deadline(long(), "slow body", move || rx.recv().unwrap()); + sender.join().unwrap(); + } + + #[test] + #[should_panic(expected = "boom")] + fn a_panic_in_the_body_is_re_raised() { + with_deadline(long(), "panicking body", || panic!("boom")); + } + + #[test] + #[should_panic(expected = "seed 7 byte 3 did not finish within 50ms")] + fn a_hang_fails_and_names_the_input() { + let (gate, gate_rx) = mpsc::channel::<()>(); + let (done_tx, done) = mpsc::channel(); + let result = catch_unwind(AssertUnwindSafe(|| { + with_deadline(short(), "seed 7 byte 3", move || { + let _ = gate_rx.recv(); + done_tx.send(()).unwrap(); + }) + })); + drop(gate); + done.recv().unwrap(); + if let Err(payload) = result { + resume_unwind(payload); + } + } + + #[test] + fn with_deadline_completes() { + with_deadline(Duration::from_secs(5), "fast body", || { + // fast operation + }); + } + + #[test] + #[should_panic(expected = "did not finish within")] + fn with_deadline_times_out() { + let (gate, gate_rx) = mpsc::channel::<()>(); + let result = catch_unwind(AssertUnwindSafe(|| with_deadline(short(), "blocked body", move || gate_rx.recv().unwrap_or_default()))); + drop(gate); + if let Err(payload) = result { + resume_unwind(payload); + } + } + + #[test] + fn returns_worker_value() { + assert_eq!(with_deadline(long(), "value", || vec![1u8, 2, 3]), vec![1, 2, 3]); + } + + #[test] + fn worker_panic_keeps_original_payload() { + #[derive(Debug, PartialEq)] + struct Marker(u32); + let payload = catch_unwind(|| with_deadline(long(), "typed panic", || std::panic::panic_any(Marker(7)))).unwrap_err(); + assert_eq!(payload.downcast_ref::(), Some(&Marker(7))); + } + + #[test] + fn timeout_names_this_call_only() { + let call = |label: &'static str| { + thread::spawn(move || { + let (gate, gate_rx) = mpsc::channel::<()>(); + let result = catch_unwind(AssertUnwindSafe(|| with_deadline(short(), label, move || gate_rx.recv().unwrap_or_default()))); + drop(gate); + message(result.unwrap_err()) + }) + }; + let first = call("first call"); + let second = call("second call"); + let first = first.join().unwrap(); + let second = second.join().unwrap(); + assert!(first.contains("first call") && !first.contains("second call"), "{first}"); + assert!(second.contains("second call") && !second.contains("first call"), "{second}"); + } + + #[test] + fn concurrent_calls_keep_limits_and_labels_separate() { + let (release, release_rx) = mpsc::channel(); + let long_call = thread::spawn(move || { + with_deadline(long(), "long call", move || { + release_rx.recv().unwrap(); + 7 + }) + }); + let (gate, gate_rx) = mpsc::channel::<()>(); + let short_call = catch_unwind(AssertUnwindSafe(|| with_deadline(short(), "short call", move || gate_rx.recv().unwrap_or_default()))); + let text = message(short_call.unwrap_err()); + assert!(text.contains("short call") && text.contains("50ms") && !text.contains("long call"), "{text}"); + release.send(()).unwrap(); + assert_eq!(long_call.join().unwrap(), 7); + drop(gate); + } + + #[test] + fn spawn_failure_reports_cause() { + let ran = AtomicBool::new(false); + let spawn = |_job: Job, _done: Sender<()>| { + ran.store(true, Ordering::SeqCst); + Err(io::Error::other("no threads left")) + }; + let result = run_with_deadline(long(), "no thread", || (), |_| None, spawn, Arc::new(|| Duration::ZERO)); + let text = failure_message(result); + assert!(ran.load(Ordering::SeqCst)); + assert!(text.contains("no thread: could not start the worker thread: no threads left"), "{text}"); + } + + #[test] + fn invalid_override_fails() { + let printed = run_child(OsStr::new("5s")); + assert!(printed.contains("CRAFT_TESTKIT_DEADLINE_CHILD_SECS=\"5s\" is not a whole number of seconds"), "{printed}"); + } + + #[test] + fn zero_override_fails() { + let printed = run_child(OsStr::new("0")); + assert!(printed.contains("CRAFT_TESTKIT_DEADLINE_CHILD_SECS=\"0\" is zero"), "{printed}"); + } + + #[cfg(unix)] + #[test] + fn non_unicode_override_fails() { + use std::os::unix::ffi::OsStrExt; + let printed = run_child(OsStr::from_bytes(b"3\xff")); + assert!(printed.contains("CRAFT_TESTKIT_DEADLINE_CHILD_SECS is not valid Unicode"), "{printed}"); + } + + #[test] + fn valid_override_replaces_the_default() { + let printed = run_child(OsStr::new("1")); + assert!(printed.contains("child body did not finish within 1s, set by CRAFT_TESTKIT_DEADLINE_CHILD_SECS"), "{printed}"); + } + + #[test] + #[should_panic(expected = "the default limit is zero")] + fn zero_default_fails() { + with_deadline(DeadlineLimit::new(Duration::ZERO).with_variable(UNSET), "zero", || ()); + } + + #[test] + fn huge_limit_fails_cleanly() { + let huge = DeadlineLimit::new(Duration::MAX).with_variable(UNSET); + let ran = AtomicBool::new(false); + let spawn = |_job: Job, _done: Sender<()>| { + ran.store(true, Ordering::SeqCst); + Ok(()) + }; + let text = failure_message(run_with_deadline(huge, "huge", || (), |_| None, spawn, Arc::new(|| Duration::ZERO))); + assert!(text.contains("ends past the latest time"), "{text}"); + assert!(!ran.load(Ordering::SeqCst), "the body started"); + let text = message(catch_unwind(|| wait_until(huge, "a huge limit", || true)).unwrap_err()); + assert!(text.contains("waiting for a huge limit: the limit of") && text.contains("ends past the latest time"), "{text}"); + } + + #[test] + fn malformed_values_are_rejected() { + let limit = DeadlineLimit::new(Duration::from_secs(5)).with_variable("X"); + let now = Instant::now(); + for value in ["", "+5", "-5", "1.5", " 5", "5 ", "abc", "5s", "0", "000", "18446744073709551616"] { + let result = resolve(&limit, |_| Some(OsString::from(value)), now); + assert!(matches!(result, Err(LimitError::BadValue { .. })), "{value:?} gave {result:?}"); + } + let resolved = resolve(&limit, |_| Some(OsString::from("007")), now).unwrap(); + assert_eq!(resolved, Resolved { limit: Duration::from_secs(7), variable: "X", from_variable: true }); + let resolved = resolve(&limit, |_| None, now).unwrap(); + assert_eq!(resolved, Resolved { limit: Duration::from_secs(5), variable: "X", from_variable: false }); + } + + #[test] + fn bad_variable_names_are_rejected_before_reading() { + for name in ["", "A=B", "A\0B"] { + let limit = DeadlineLimit::new(Duration::from_secs(5)).with_variable(name); + let result = resolve(&limit, |_| panic!("the variable was read"), Instant::now()); + assert_eq!(result, Err(LimitError::BadName { variable: name })); + } + } + + #[test] + fn a_duration_reads_the_default_variable() { + let limit: DeadlineLimit = Duration::from_secs(3).into(); + assert_eq!(limit.variable(), DEFAULT_VARIABLE); + assert_eq!(limit.default_limit(), Duration::from_secs(3)); + } + + #[test] + fn wait_until_returns_on_condition() { + let ready = &AtomicBool::new(false); + let (asked, asked_rx) = mpsc::channel(); + let (stored, stored_rx) = mpsc::channel(); + thread::scope(|s| { + s.spawn(move || { + asked_rx.recv().unwrap(); + ready.store(true, Ordering::SeqCst); + stored.send(()).unwrap(); + }); + let mut calls = 0; + wait_until(long(), "the flag", || { + calls += 1; + if calls == 1 { + // The flag is set before this call returns, and it still answers false, so the + // condition is called a second time whatever order the threads run in. + asked.send(()).unwrap(); + stored_rx.recv().unwrap(); + return false; + } + ready.load(Ordering::SeqCst) + }); + assert_eq!(calls, 2); + }); + } + + #[test] + #[should_panic(expected = "timed out waiting for the export to finish within 50ms")] + fn wait_until_times_out_with_label() { + wait_until(short(), "the export to finish", || false); + } + + #[test] + #[should_panic(expected = "condition boom")] + fn wait_until_propagates_condition_panic() { + wait_until(long(), "a panicking condition", || panic!("condition boom")); + } + + #[test] + fn wait_until_keeps_a_typed_condition_payload() { + #[derive(Debug, PartialEq)] + struct Marker(u32); + let payload = catch_unwind(|| wait_until(long(), "a typed panic", || std::panic::panic_any(Marker(9)))).unwrap_err(); + assert_eq!(payload.downcast_ref::(), Some(&Marker(9))); + } + + /// A default limit for the loop tests, which pass their own clock. + fn fake_resolved(limit: Duration) -> Resolved<'static> { + Resolved { limit, variable: UNSET, from_variable: false } + } + + #[test] + fn wait_until_rejects_a_true_after_the_limit() { + let resolved = fake_resolved(Duration::from_millis(10)); + let mut now = Duration::ZERO; + let mut cond = || true; + // The condition took 11 ms to return. + let mut elapsed = || { + now += Duration::from_millis(11); + now + }; + let result = poll_until(&resolved, "a late flag", &mut cond, &mut elapsed, &mut |_| panic!("paused")); + let text = result.unwrap_err(); + assert!(text.contains("timed out waiting for a late flag") && text.contains("held only after the limit"), "{text}"); + } + + #[test] + fn wait_until_pauses_grow_and_stop_at_the_limit() { + let resolved = fake_resolved(Duration::from_millis(100)); + let now = std::cell::Cell::new(Duration::ZERO); + let mut pauses = Vec::new(); + let mut calls = 0; + let mut cond = || { + calls += 1; + false + }; + let mut elapsed = || now.get(); + let mut pause = |d: Duration| { + pauses.push(d); + now.set(now.get() + d); + }; + let result = poll_until(&resolved, "never", &mut cond, &mut elapsed, &mut pause); + assert!(result.is_err()); + let ms: Vec = pauses.iter().map(Duration::as_millis).collect(); + // 1 + 2 + 4 + 8 + 16 + 32 = 63, then the 37 ms left, after which the limit is reached. + assert_eq!(ms, [1, 2, 4, 8, 16, 32, 37]); + // One call before each pause, and none after the last one. + assert_eq!(calls, 7); + } + + #[test] + fn wait_until_stops_calling_after_an_overshooting_pause() { + let resolved = fake_resolved(Duration::from_millis(100)); + let now = std::cell::Cell::new(Duration::from_millis(99)); + let mut calls = 0; + let mut cond = || { + calls += 1; + false + }; + let mut elapsed = || now.get(); + // The thread wakes 50 ms late from the 1 ms pause. + let mut pause = |d: Duration| now.set(now.get() + d + Duration::from_millis(50)); + let result = poll_until(&resolved, "late wake", &mut cond, &mut elapsed, &mut pause); + assert!(result.unwrap_err().contains("timed out waiting for late wake")); + assert_eq!(calls, 1); + } + + /// Runs `run_with_deadline` with a 5 s limit on a fake clock. The worker runs on the calling + /// thread: the clock reads `start_secs` while the body runs and `received_secs` when the result + /// is received. When `sends` is false, the worker stores its result but does not send, as if its + /// thread stopped running between the two. + fn on_fake_clock( + f: impl FnOnce() -> T + Send + 'static, + start_secs: u64, + received_secs: u64, + sends: bool, + ) -> Result { + let now = Arc::new(AtomicU64::new(0)); + let clock_now = Arc::clone(&now); + let clock: Clock = Arc::new(move || Duration::from_secs(clock_now.load(Ordering::SeqCst))); + let unsent = std::cell::RefCell::new(None); + let spawn = |job: Job, done: Sender<()>| { + now.store(start_secs, Ordering::SeqCst); + job(); + now.store(received_secs, Ordering::SeqCst); + if sends { + let _ = done.send(()); + } else { + // Kept alive so that the wait times out instead of seeing a closed channel. + *unsent.borrow_mut() = Some(done); + } + Ok(()) + }; + let limit = DeadlineLimit::new(Duration::from_secs(5)).with_variable(UNSET); + run_with_deadline(limit, "fake clock", f, |_| None, spawn, clock) + } + + #[test] + fn a_value_returned_after_the_limit_fails() { + let text = failure_message(on_fake_clock(|| 1, 6, 6, true)); + assert!(text.contains("fake clock finished only after its limit of 5s"), "{text}"); + } + + #[test] + fn a_value_finished_in_time_but_received_late_passes() { + assert!(matches!(on_fake_clock(|| 1, 1, 6, true), Ok(1))); + } + + #[test] + fn a_value_finished_in_time_but_sent_late_passes() { + assert!(matches!(on_fake_clock(|| 1, 1, 6, false), Ok(1))); + } + + #[test] + fn a_panic_finished_in_time_but_sent_late_keeps_its_payload() { + let payload = match on_fake_clock(|| -> u8 { panic!("early boom") }, 1, 6, false) { + Err(Failure::Panic(payload)) => payload, + Ok(_) => panic!("the call did not fail"), + Err(Failure::Message(text)) => panic!("the payload was replaced: {text}"), + }; + assert_eq!(message(payload), "early boom"); + } + + #[test] + fn a_panic_after_the_limit_names_the_limit_and_the_panic() { + let text = failure_message(on_fake_clock(|| -> u8 { panic!("late boom") }, 6, 6, true)); + assert!(text.contains("fake clock finished only after its limit of 5s") && text.ends_with("(it panicked: late boom)"), "{text}"); + } +} diff --git a/crates/testkit/src/guards.rs b/crates/testkit/src/guards.rs new file mode 100644 index 000000000..3541ea21b --- /dev/null +++ b/crates/testkit/src/guards.rs @@ -0,0 +1,2280 @@ +//! Platform regression guards checking source code for known defects and lifecycle hazards. + +use std::collections::{BTreeMap, BTreeSet}; +use std::fmt; +#[cfg(not(target_arch = "wasm32"))] +use std::path::Path; +use std::rc::Rc; + +/// A diagnostic finding reported by a source guard. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Finding { + /// File path where the violation was detected. + pub path: String, + /// 1-based line number of the violation. + pub line: usize, + /// Diagnostic context (the offending code snippet or float literal). + pub text: String, +} + +impl Finding { + /// Create a new diagnostic finding. + pub fn new(path: impl Into, line: usize, text: impl Into) -> Self { + Self { path: path.into(), line, text: text.into() } + } +} + +impl fmt::Display for Finding { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.path.is_empty() { write!(f, "{}: {}", self.line, self.text) } else { write!(f, "{}:{}: {}", self.path, self.line, self.text) } + } +} + +/// Abstraction over a source file path and its text content. +pub trait AsFileSource { + /// Path of the source file. + fn path(&self) -> &str; + /// Text content of the source file. + fn content(&self) -> &str; +} + +impl AsFileSource for (String, String) { + fn path(&self) -> &str { + &self.0 + } + fn content(&self) -> &str { + &self.1 + } +} + +impl AsFileSource for &(String, String) { + fn path(&self) -> &str { + &self.0 + } + fn content(&self) -> &str { + &self.1 + } +} + +impl AsFileSource for (&str, &str) { + fn path(&self) -> &str { + self.0 + } + fn content(&self) -> &str { + self.1 + } +} + +impl AsFileSource for &(&str, &str) { + fn path(&self) -> &str { + self.0 + } + fn content(&self) -> &str { + self.1 + } +} + +impl AsFileSource for (String, &str) { + fn path(&self) -> &str { + &self.0 + } + fn content(&self) -> &str { + self.1 + } +} + +impl AsFileSource for &(String, &str) { + fn path(&self) -> &str { + &self.0 + } + fn content(&self) -> &str { + self.1 + } +} + +impl AsFileSource for (&str, String) { + fn path(&self) -> &str { + self.0 + } + fn content(&self) -> &str { + &self.1 + } +} + +impl AsFileSource for &(&str, String) { + fn path(&self) -> &str { + self.0 + } + fn content(&self) -> &str { + &self.1 + } +} + +/// The deepest `use` group nesting and `cfg` predicate nesting the guards follow. Deeper input is +/// read as far as this depth, so hostile nesting cannot exhaust the stack. +const MAX_NESTING: usize = 64; + +/// The most segments of a `use` path that [`use_leaves`] keeps. The guards compare paths of at most +/// three segments, so a leaf with a longer path keeps only the name it binds, and a long prefix shared +/// by many leaves is not copied into each of them. +const MAX_USE_PATH: usize = 8; + +/// The longest segment, in bytes, of a `use` path that [`use_leaves`] keeps. The longest segment the +/// guards compare is `PredefinedMenuItem`, so a leaf with a longer segment keeps only the name it binds, +/// and a long name shared by many leaves is not copied into each of them. +const MAX_USE_SEGMENT: usize = 32; + +/// The kind of a Rust token. Comments are dropped; string, character and number literals keep no text +/// except the decoded contents of strings, which `cfg` predicates compare. A string with an escape +/// that is not valid is `Other`. +#[derive(Debug, Clone, PartialEq, Eq)] +enum Kind { + Ident(String), + Punct(char), + Str(String), + Other, +} + +#[derive(Debug, Clone)] +struct Token { + kind: Kind, + line: usize, +} + +impl Token { + fn is_ident(&self, name: &str) -> bool { + matches!(&self.kind, Kind::Ident(s) if s == name) + } + + fn is_punct(&self, c: char) -> bool { + self.kind == Kind::Punct(c) + } +} + +fn is_ident_start(c: char) -> bool { + c == '_' || c.is_alphabetic() +} + +fn is_ident_continue(c: char) -> bool { + c == '_' || c.is_alphanumeric() +} + +/// Split Rust source into tokens with 1-based line numbers. +/// +/// Line comments, nested block comments, string literals (normal, byte, C and raw strings with any +/// number of `#`), character and byte literals, lifetimes and raw identifiers are recognized, so a quote +/// or comment marker inside one of them does not hide the code after it. Unterminated literals and +/// comments run to the end of the input. +fn lex_rust(code: &str) -> Vec { + let chars: Vec = code.chars().collect(); + let len = chars.len(); + let at = |i: usize| chars.get(i).copied(); + let mut tokens = Vec::new(); + let mut i = 0; + let mut line = 1usize; + + while i < len { + let c = chars[i]; + if c == '\n' { + line += 1; + i += 1; + } else if c.is_whitespace() { + i += 1; + } else if c == '/' && at(i + 1) == Some('/') { + while i < len && chars[i] != '\n' { + i += 1; + } + } else if c == '/' && at(i + 1) == Some('*') { + let mut depth = 1usize; + i += 2; + while i < len && depth > 0 { + if chars[i] == '/' && at(i + 1) == Some('*') { + depth += 1; + i += 2; + } else if chars[i] == '*' && at(i + 1) == Some('/') { + depth -= 1; + i += 2; + } else { + if chars[i] == '\n' { + line += 1; + } + i += 1; + } + } + } else if is_ident_start(c) { + let start = i; + while i < len && is_ident_continue(chars[i]) { + i += 1; + } + let word: String = chars[start..i].iter().collect(); + let token_line = line; + if matches!(word.as_str(), "r" | "br" | "cr") && matches!(at(i), Some('"' | '#')) { + let mut j = i; + while j < len && chars[j] == '#' { + j += 1; + } + let hashes = j - i; + if at(j) == Some('"') { + let (end, text) = scan_raw_string(&chars, j + 1, hashes, &mut line); + tokens.push(Token { kind: Kind::Str(text), line: token_line }); + i = end; + continue; + } + if word == "r" && hashes == 1 && at(j).is_some_and(is_ident_start) { + let name_start = j; + while j < len && is_ident_continue(chars[j]) { + j += 1; + } + tokens.push(Token { kind: Kind::Ident(raw_identifier(&chars[name_start..j])), line: token_line }); + i = j; + continue; + } + } + if matches!(word.as_str(), "b" | "c") && at(i) == Some('"') { + let (end, kind) = scan_string(&chars, i + 1, &mut line); + tokens.push(Token { kind, line: token_line }); + i = end; + continue; + } + if word == "b" + && at(i) == Some('\'') + && let Some(end) = char_literal_end(&chars, i) + { + tokens.push(Token { kind: Kind::Other, line: token_line }); + i = end; + continue; + } + tokens.push(Token { kind: Kind::Ident(word), line: token_line }); + } else if c.is_ascii_digit() { + while i < len && (is_ident_continue(chars[i]) || (chars[i] == '.' && at(i + 1).is_some_and(|d| d.is_ascii_digit()))) { + i += 1; + } + tokens.push(Token { kind: Kind::Other, line }); + } else if c == '"' { + let token_line = line; + let (end, kind) = scan_string(&chars, i + 1, &mut line); + tokens.push(Token { kind, line: token_line }); + i = end; + } else if c == '\'' { + if let Some(end) = char_literal_end(&chars, i) { + i = end; + } else { + // A lifetime or a loop label. + i += 1; + while i < len && is_ident_continue(chars[i]) { + i += 1; + } + } + tokens.push(Token { kind: Kind::Other, line }); + } else { + tokens.push(Token { kind: Kind::Punct(c), line }); + i += 1; + } + } + tokens +} + +/// The name of a raw identifier written after `r#`. A raw identifier spelled like a keyword the guards +/// read (`r#let`, `r#in`, `r#match`, `r#true`) keeps its prefix, so it is read as a name rather than as +/// that keyword or as a `cfg` literal. +fn raw_identifier(name: &[char]) -> String { + let name: String = name.iter().collect(); + let keyword = |list: &[&str]| list.contains(&name.as_str()); + if keyword(&["true", "false"]) || keyword(KEYWORDS) || keyword(BLOCK_KEYWORDS) || keyword(VALUE_KEYWORDS) { format!("r#{name}") } else { name } +} + +/// Read a normal string body starting after its opening quote. Returns the index after the closing +/// quote and the token for the body: `Str` with its escapes decoded, or `Other` when an escape is not +/// valid in a string. +fn scan_string(chars: &[char], mut i: usize, line: &mut usize) -> (usize, Kind) { + let mut text = Some(String::new()); + while i < chars.len() { + match chars[i] { + '\\' => { + let (next, decoded) = string_escape(chars, i + 1, line); + match decoded { + Ok(Some(c)) => text.iter_mut().for_each(|t| t.push(c)), + Ok(None) => {} + Err(()) => text = None, + } + i = next; + } + '"' => return (i + 1, text.map_or(Kind::Other, Kind::Str)), + c => { + if c == '\n' { + *line += 1; + } + text.iter_mut().for_each(|t| t.push(c)); + i += 1; + } + } + } + (chars.len(), text.map_or(Kind::Other, Kind::Str)) +} + +/// Decode the string escape whose `\` is right before `chars[i]` +/// (). Returns the index after the +/// escape and the character it stands for, `None` for a line continuation, or an error when it is not +/// a valid escape. +fn string_escape(chars: &[char], i: usize, line: &mut usize) -> (usize, Result, ()>) { + let hex = |k: usize| chars.get(k).and_then(|c| c.to_digit(16)); + match chars.get(i) { + Some('n') => (i + 1, Ok(Some('\n'))), + Some('r') => (i + 1, Ok(Some('\r'))), + Some('t') => (i + 1, Ok(Some('\t'))), + Some('0') => (i + 1, Ok(Some('\0'))), + Some(&c @ ('\\' | '\'' | '"')) => (i + 1, Ok(Some(c))), + Some('x') => match (hex(i + 1), hex(i + 2)) { + (Some(high), Some(low)) if high < 8 => (i + 3, Ok(char::from_u32(high * 16 + low))), + _ => (i + 1, Err(())), + }, + Some('u') if chars.get(i + 1) == Some(&'{') => { + // At most six hex digits, with underscores, before the closing brace. + let mut value = 0u32; + let mut digits = 0; + let mut j = i + 2; + while let Some(&c) = chars.get(j) { + if c == '}' { + break; + } + if c != '_' { + match c.to_digit(16) { + Some(d) if digits < 6 => { + value = value * 16 + d; + digits += 1; + } + _ => return (j, Err(())), + } + } + j += 1; + } + match (chars.get(j), char::from_u32(value)) { + (Some('}'), Some(c)) if digits > 0 && chars.get(i + 2) != Some(&'_') => (j + 1, Ok(Some(c))), + _ => (j.min(chars.len()), Err(())), + } + } + Some('\n') => { + // A line continuation skips the line break and the whitespace after it. + let mut j = i; + while let Some(&c) = chars.get(j).filter(|c| matches!(c, ' ' | '\t' | '\n' | '\r')) { + if c == '\n' { + *line += 1; + } + j += 1; + } + (j, Ok(None)) + } + Some('\r') if chars.get(i + 1) == Some(&'\n') => { + let (j, _) = string_escape(chars, i + 1, line); + (j, Ok(None)) + } + Some(_) => (i + 1, Err(())), + None => (i, Err(())), + } +} + +/// Read a raw string body starting after its opening quote; it ends at a quote followed by `hashes` `#`. +fn scan_raw_string(chars: &[char], mut i: usize, hashes: usize, line: &mut usize) -> (usize, String) { + let start = i; + while i < chars.len() { + if chars[i] == '"' && (1..=hashes).all(|h| chars.get(i + h) == Some(&'#')) { + return (i + 1 + hashes, chars[start..i].iter().collect()); + } + if chars[i] == '\n' { + *line += 1; + } + i += 1; + } + (chars.len(), chars[start.min(chars.len())..].iter().collect()) +} + +/// The index after a character or byte literal whose opening quote is at `quote`, or `None` when the +/// quote starts a lifetime or a label. +fn char_literal_end(chars: &[char], quote: usize) -> Option { + match chars.get(quote + 1) { + Some('\\') => { + // An escape: `\n`, `\'`, `\x7f`, `\u{10FFFF}`. The longest is twelve characters. + let mut j = quote + 3; + while j < chars.len() && j <= quote + 12 && chars[j] != '\'' && chars[j] != '\n' { + j += 1; + } + (chars.get(j) == Some(&'\'')).then_some(j + 1) + } + Some('\n') | None => None, + Some(_) => (chars.get(quote + 2) == Some(&'\'')).then_some(quote + 3), + } +} + +/// Rust keywords that can come right before a path that starts with `::`, as in `return ::std::time`. +/// `crate`, `self`, `super` and `Self` are path segments and are not listed. +const KEYWORDS: &[&str] = &[ + "as", "async", "await", "box", "break", "const", "continue", "dyn", "else", "enum", "extern", "fn", "for", "if", "impl", "in", "let", "loop", + "match", "mod", "move", "mut", "pub", "ref", "return", "static", "struct", "trait", "type", "unsafe", "use", "where", "while", "yield", +]; + +/// Whether `toks[i]` follows `::` that itself follows a path segment, as `time` does in `a::time`. +/// The `>` of `->` and `=>` ends no path, so `-> ::std::time` starts one. +fn continues_a_path(toks: &[Token], i: usize) -> bool { + i >= 3 + && toks[i - 1].is_punct(':') + && toks[i - 2].is_punct(':') + && match &toks[i - 3].kind { + Kind::Ident(s) => !KEYWORDS.contains(&s.as_str()), + Kind::Punct('>') => !(i >= 4 && (toks[i - 4].is_punct('-') || toks[i - 4].is_punct('='))), + _ => false, + } +} + +/// Whether `toks[i..]` starts with `::` followed by the identifier `name`. +fn then_path(toks: &[Token], i: usize, name: impl Fn(&str) -> bool) -> bool { + toks.get(i).is_some_and(|t| t.is_punct(':')) + && toks.get(i + 1).is_some_and(|t| t.is_punct(':')) + && toks.get(i + 2).is_some_and(|t| matches!(&t.kind, Kind::Ident(s) if name(s))) +} + +/// Whether the path that ends at `toks[i]` starts right after the `<` of a qualified path, as in +/// `::quit`, rather than after the `<` of generic arguments (`Vec::new`, +/// `f::`). +fn opens_qualified_path(toks: &[Token], i: usize) -> bool { + let mut start = i; + while start >= 2 && toks[start - 1].is_punct(':') && toks[start - 2].is_punct(':') { + if start >= 3 && matches!(toks[start - 3].kind, Kind::Ident(_)) { + start -= 3; + } else { + start -= 2; + break; + } + } + start >= 1 + && toks[start - 1].is_punct('<') + && match start.checked_sub(2).map(|j| &toks[j].kind) { + None => true, + Some(Kind::Ident(s)) => KEYWORDS.contains(&s.as_str()), + Some(Kind::Punct(c)) => !matches!(*c, ':' | '>'), + Some(_) => false, + } +} + +/// One name a `use` declaration brings into scope. +#[derive(Debug)] +struct UseLeaf { + /// The full path, `self` removed and without a leading `::`. It is empty for a path longer than + /// [`MAX_USE_PATH`] segments or with a segment longer than [`MAX_USE_SEGMENT`] bytes, whose leaf then + /// records only the name it binds. + path: Vec, + /// The name it is bound to, `_` for an underscore import; `None` for a glob import. The name is + /// shared with the other leaves that bind the same segment of one path. + name: Option>, + /// The first segment of the path, kept when the path is not. + root: Option>, + line: usize, + /// Index of the `use` keyword. + start: usize, + /// Index of the alias of `std::time` that the path was read through, when [`use_leaves`] was given one. + alias: Option, + /// Whether the path starts with `::`, so that its first segment names an external crate. + absolute: bool, +} + +impl UseLeaf { + /// The name the leaf binds in scope: `None` for a glob import and for an underscore import, which + /// imports an item without binding a name. + fn binding(&self) -> Option<&str> { + self.name.as_deref().filter(|name| *name != "_") + } +} + +/// Every name the `use` declarations of a file bring into scope. A path that starts with a name in +/// `time_aliases` is read as a path that starts with `std::time`, so the alias, at any length, is not +/// copied into the leaves and the limits do not drop them. +fn use_leaves(toks: &[Token], time_aliases: &BTreeSet) -> Vec { + let mut leaves = Vec::new(); + let mut i = 0; + while i < toks.len() { + // `use<...>` is a precise capturing bound on `impl Trait`, not a `use` declaration. + if toks[i].is_ident("use") && !continues_a_path(toks, i) && !toks.get(i + 1).is_some_and(|t| t.is_punct('<')) { + let start = i; + let end = (i + 1..toks.len()).find(|&j| toks[j].is_punct(';')).unwrap_or(toks.len()); + let mut pos = i + 1; + let mut prefix = Vec::new(); + parse_use_tree(&toks[..end], &mut pos, &mut prefix, (start, None, false), &mut leaves, time_aliases, 0); + i = end; + } else { + i += 1; + } + } + leaves +} + +fn parse_use_tree( + toks: &[Token], + pos: &mut usize, + prefix: &mut Vec>, + // The index of the `use` keyword, the alias of `std::time` that `prefix` was read through, and whether + // the path starts with `::`. + (start, mut via, mut absolute): (usize, Option, bool), + out: &mut Vec, + time_aliases: &BTreeSet, + depth: usize, +) { + let base = prefix.len(); + if toks.get(*pos).is_some_and(|t| t.is_punct(':')) && toks.get(*pos + 1).is_some_and(|t| t.is_punct(':')) { + absolute |= prefix.is_empty(); + *pos += 2; + } + // A leaf is reported on the line where its path starts in this tree, not where its last name is. + let line = toks.get(*pos).map_or(0, |t| t.line); + while let Some(tok) = toks.get(*pos) { + match &tok.kind { + Kind::Ident(name) => { + *pos += 1; + if toks.get(*pos).is_some_and(|t| t.is_punct(':')) && toks.get(*pos + 1).is_some_and(|t| t.is_punct(':')) { + if prefix.is_empty() && time_aliases.contains(name) { + prefix.extend([Rc::from("std"), Rc::from("time")]); + via = Some(*pos - 1); + } else { + prefix.push(Rc::from(name.as_str())); + } + *pos += 2; + continue; + } + let length = prefix.len() + usize::from(name != "self"); + let alias = match (toks.get(*pos), toks.get(*pos + 1).map(|t| &t.kind)) { + (Some(t), Some(Kind::Ident(alias))) if t.is_ident("as") => { + *pos += 2; + Some(alias) + } + _ => None, + }; + // The name the leaf binds: its alias, its last name, or for `self` the last segment of + // the prefix, shared rather than copied. + let bound = match alias { + Some(alias) => Some(Rc::from(alias.as_str())), + None if name != "self" => Some(Rc::from(name.as_str())), + None => prefix.last().cloned(), + }; + // A path past the limits is not copied, but the leaf still binds its name, so that the name + // does not read as unbound. + let path = if length <= MAX_USE_PATH && short_segments(prefix) && name.len() <= MAX_USE_SEGMENT { + let mut path: Vec = prefix.iter().map(|segment| segment.to_string()).collect(); + if name != "self" { + path.push(name.clone()); + } + path + } else { + Vec::new() + }; + if bound.is_some() { + let root = prefix.first().cloned().or_else(|| Some(Rc::from(name.as_str()))); + out.push(UseLeaf { path, name: bound, root, line, start, alias: via, absolute }); + } + break; + } + Kind::Punct('*') => { + if prefix.len() <= MAX_USE_PATH && short_segments(prefix) { + let path = prefix.iter().map(|segment| segment.to_string()).collect(); + out.push(UseLeaf { path, name: None, root: prefix.first().cloned(), line, start, alias: via, absolute }); + } + *pos += 1; + break; + } + Kind::Punct('{') => { + *pos += 1; + while let Some(t) = toks.get(*pos) { + if t.is_punct('}') { + *pos += 1; + break; + } + let before = *pos; + if !t.is_punct(',') && depth < MAX_NESTING { + parse_use_tree(toks, pos, prefix, (start, via, absolute), out, time_aliases, depth + 1); + } + if *pos == before { + *pos += 1; + } + } + break; + } + _ => break, + } + } + prefix.truncate(base); +} + +/// Whether every segment of `path` is at most [`MAX_USE_SEGMENT`] bytes long. +fn short_segments(path: &[Rc]) -> bool { + path.iter().all(|segment| segment.len() <= MAX_USE_SEGMENT) +} + +/// A value of a `cfg` predicate on `wasm32-unknown-unknown`, where the value of a feature or of +/// `test` is not known from the source. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Tri { + True, + False, + Unknown, +} + +impl Tri { + fn from_bool(b: bool) -> Self { + if b { Self::True } else { Self::False } + } + + fn not(self) -> Self { + match self { + Self::True => Self::False, + Self::False => Self::True, + Self::Unknown => Self::Unknown, + } + } +} + +/// Evaluate the `cfg` predicate at `toks[*pos]` for `wasm32-unknown-unknown`. +fn eval_cfg(toks: &[Token], pos: &mut usize, depth: usize) -> Tri { + let Some(Kind::Ident(name)) = toks.get(*pos).map(|t| &t.kind) else { + return Tri::Unknown; + }; + *pos += 1; + if toks.get(*pos).is_some_and(|t| t.is_punct('=')) { + let value = match toks.get(*pos + 1).map(|t| &t.kind) { + Some(Kind::Str(v)) => v.clone(), + _ => return Tri::Unknown, + }; + *pos += 2; + let expected = match name.as_str() { + "target_arch" => "wasm32", + "target_os" | "target_vendor" => "unknown", + "target_family" => "wasm", + "target_env" => "", + "target_pointer_width" => "32", + "target_endian" => "little", + _ => return Tri::Unknown, + }; + return Tri::from_bool(value == expected); + } + if toks.get(*pos).is_some_and(|t| t.is_punct('(')) { + if depth >= MAX_NESTING { + // Too deep to read: the predicate is skipped as a whole, and its value is not known. + *pos = matching_close(toks, *pos).saturating_add(1); + return Tri::Unknown; + } + *pos += 1; + let mut values = Vec::new(); + while let Some(t) = toks.get(*pos) { + if t.is_punct(')') { + *pos += 1; + break; + } + if t.is_punct(',') { + *pos += 1; + continue; + } + let before = *pos; + values.push(eval_cfg(toks, pos, depth + 1)); + if *pos == before { + *pos += 1; + } + } + return match name.as_str() { + "not" => values.first().map_or(Tri::Unknown, |v| v.not()), + "all" if values.contains(&Tri::False) => Tri::False, + "all" if values.contains(&Tri::Unknown) => Tri::Unknown, + "all" => Tri::True, + "any" if values.contains(&Tri::True) => Tri::True, + "any" if values.contains(&Tri::Unknown) => Tri::Unknown, + "any" => Tri::False, + _ => Tri::Unknown, + }; + } + match name.as_str() { + "unix" | "windows" | "false" => Tri::False, + "true" => Tri::True, + _ => Tri::Unknown, + } +} + +/// The index of the token that closes the bracket opened at `open`, or `toks.len()`. +fn matching_close(toks: &[Token], open: usize) -> usize { + let mut depth = 0usize; + for (j, t) in toks.iter().enumerate().skip(open) { + match t.kind { + Kind::Punct('(' | '[' | '{') => depth += 1, + Kind::Punct(')' | ']' | '}') => { + depth = depth.saturating_sub(1); + if depth == 0 { + return j; + } + } + _ => {} + } + } + toks.len() +} + +/// Keywords whose item or expression ends with its closing brace rather than with `;`. +const BLOCK_KEYWORDS: &[&str] = + &["fn", "mod", "impl", "trait", "struct", "enum", "union", "extern", "macro_rules", "if", "match", "loop", "while", "for", "unsafe"]; + +/// Keywords that start a statement whose value follows them, so a block keyword after them is part of +/// that value. +const VALUE_KEYWORDS: &[&str] = &["let", "return", "break", "yield", "become"]; + +/// Where [`item_end`] is in the head of an `if`, `while`, `match` or `for` before its body's `{`. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Condition { + /// Not in such a head: the next `{` outside brackets opens the body. + Done, + /// In the pattern of a `let` or a `for`, where a `{` opens a struct pattern. + Pattern, + /// In a condition, a scrutinee or an iterator, where the next `{` outside brackets opens the body. + Expression, +} + +/// Whether the `=` at `toks[j]` stands alone, as the `=` between a `let` pattern and its value, rather +/// than as part of `==`, `!=`, `<=`, `>=`, `=>` or the range pattern `..=`. +fn lone_equals(toks: &[Token], j: usize) -> bool { + let punct = |k: usize, cs: &[char]| toks.get(k).is_some_and(|t| cs.iter().any(|&c| t.is_punct(c))); + !punct(j + 1, &['=', '>']) && j.checked_sub(1).is_none_or(|k| !punct(k, &['=', '!', '<', '>', '.'])) +} + +/// Whether a block expression starts at `j`: `{`, a labeled block (`'label: {`) or a `const` block. +fn starts_block(toks: &[Token], j: usize) -> bool { + let punct = |k: usize, c: char| toks.get(k).is_some_and(|t| t.is_punct(c)); + match toks.get(j).map(|t| &t.kind) { + Some(Kind::Punct('{')) => true, + Some(Kind::Other) => punct(j + 1, ':') && punct(j + 2, '{'), + Some(Kind::Ident(k)) => k == "const" && punct(j + 1, '{'), + _ => false, + } +} + +/// Whether the `{` at `toks[j]`, in the condition, scrutinee or iterator of an `if`, `while`, `match` or +/// `for`, stands where an operand is still needed, so it opens a block expression rather than the body: +/// right after the keyword, after `in`, after the `=` of a `let`, after an operator or `!`, after +/// `unsafe`, `const`, `loop`, `async` or `async move`, or after a loop label's `:`. +fn operand_expected(toks: &[Token], j: usize) -> bool { + let Some(k) = j.checked_sub(1) else { return false }; + match &toks[k].kind { + Kind::Ident(prev) => matches!(prev.as_str(), "if" | "while" | "match" | "in" | "unsafe" | "const" | "loop" | "async" | "move"), + Kind::Punct('=' | '+' | '-' | '*' | '/' | '%' | '^' | '|' | '&' | '<' | '>' | '!') => true, + Kind::Punct(':') => k.checked_sub(1).is_some_and(|l| toks[l].kind == Kind::Other), + _ => false, + } +} + +/// The index after the item, statement, field or match arm that starts at `start`, or with `inner`, the +/// index of the bracket that closes the enclosing scope. +/// +/// It ends after a `;` or a `,` outside brackets, after the closing brace of a block item, or before a +/// closing bracket of the enclosing scope. A block item starts with `{`, a labeled block, a `const` +/// block, a keyword in [`BLOCK_KEYWORDS`] or a macro call with braces, at the head of the item or of a +/// match arm's body (after its `=>`). An `async` block is not a block item: it runs on to its `;`. A +/// block keyword or macro brace after a `=`, a binary operator or a keyword in [`VALUE_KEYWORDS`] +/// outside brackets is part of an expression, which runs on to its `;` or `,`. When a `.` or `?` +/// follows the closing brace of a block item, the item runs on to its `;` or `,`. A `,` inside the angle +/// brackets of a field's or a `let`'s type ends the item early, which leaves the rest of it checked. +/// +/// The body of an `if`, `while`, `match` or `for` at the head is the first `{` outside brackets after its +/// condition, scrutinee or iterator. A `{` where the condition still needs an operand (see +/// [`operand_expected`]) opens a block expression in it, as in `if { ready() } { ... }`, and the body is +/// the next `{` after that block. A `{` in the pattern of a `let` (`if let S { .. } = s`, also after +/// `&&` in a chain) or of a `for` opens a struct pattern and does not end the item. An `else` chain ends +/// at its last block. +/// +/// In the head of an `fn`, `impl`, `trait`, `struct`, `enum` or `union` item, a block inside angle +/// brackets (a const generic default or argument such as ``, also in a +/// return type or a `where` clause) does not end the item. The `>` of `->` does not close an angle +/// bracket. +fn item_end(toks: &[Token], start: usize, inner: bool) -> usize { + let mut depth = 0usize; + let mut block_item = false; + let mut arm = false; + let mut head = true; + let mut condition = Condition::Done; + let mut generics = false; + let mut angle = 0usize; + let mut j = start; + while j < toks.len() { + let at_top = depth == 0 && !inner; + if j == start && at_top && starts_block(toks, j) { + block_item = true; + } + match &toks[j].kind { + Kind::Ident(k) if at_top && head && (BLOCK_KEYWORDS.contains(&k.as_str()) || starts_block(toks, j)) => { + if !block_item { + condition = match k.as_str() { + "if" | "while" | "match" => Condition::Expression, + "for" => Condition::Pattern, + _ => Condition::Done, + }; + } + if matches!(k.as_str(), "fn" | "impl" | "trait" | "struct" | "enum" | "union") { + generics = true; + } + block_item = true; + } + Kind::Punct('<') if at_top && generics => angle += 1, + Kind::Punct('>') if at_top && generics && !j.checked_sub(1).is_some_and(|k| toks[k].is_punct('-')) => { + angle = angle.saturating_sub(1); + } + Kind::Ident(k) if at_top && condition == Condition::Expression && k == "let" => condition = Condition::Pattern, + Kind::Ident(k) if at_top && condition == Condition::Pattern && k == "in" => condition = Condition::Expression, + Kind::Punct('=') if at_top && condition == Condition::Pattern && lone_equals(toks, j) => condition = Condition::Expression, + Kind::Punct('{') if at_top && condition == Condition::Expression => { + // A `{` where the condition still needs an operand opens a block expression inside it + // (`if { ready() } { ... }`); the body's `{` comes after the condition's last operand. + if !operand_expected(toks, j) { + condition = Condition::Done; + } + depth += 1; + } + Kind::Ident(k) if at_top && VALUE_KEYWORDS.contains(&k.as_str()) => head = false, + Kind::Punct('!') if at_top && head && toks.get(j + 1).is_some_and(|t| t.is_punct('{')) => block_item = true, + Kind::Punct('=') if at_top && toks.get(j + 1).is_some_and(|t| t.is_punct('>')) => { + // A match arm's body has a head of its own: a block or block keyword there ends the arm. + arm = true; + head = true; + block_item = starts_block(toks, j + 2); + condition = Condition::Done; + j += 2; + continue; + } + // The `:` after a field's or a static's name starts its type, where `fn` is a function pointer + // type rather than an item. The `:` of a path (`::`) and the one after a loop label are not. + Kind::Punct(':') + if at_top + && !block_item + && !toks.get(j + 1).is_some_and(|t| t.is_punct(':')) + && j.checked_sub(1).is_none_or(|k| !toks[k].is_punct(':') && toks[k].kind != Kind::Other) => + { + head = false; + } + Kind::Punct('=' | '+' | '-' | '*' | '/' | '%' | '^' | '|' | '&' | '<' | '>' | '!') if at_top && !block_item => head = false, + Kind::Punct('(' | '[' | '{') => depth += 1, + Kind::Punct(c @ (')' | ']' | '}')) => { + if depth == 0 { + return j; + } + depth -= 1; + if depth == 0 && *c == '}' && block_item && !inner && condition == Condition::Done && angle == 0 { + if toks.get(j + 1).is_some_and(|t| t.is_ident("else")) { + // `else if` has a condition of its own before its body; `else {` opens the body. + if toks.get(j + 2).is_some_and(|t| t.is_ident("if")) { + condition = Condition::Expression; + } + j += 1; + continue; + } + if toks.get(j + 1).is_some_and(|t| t.is_punct('.') || t.is_punct('?')) { + // A method call, field or `?` after the block continues the expression to its `;` or `,`. + block_item = false; + head = false; + j += 1; + continue; + } + let comma = toks.get(j + 1).is_some_and(|t| t.is_punct(',')); + return if arm && comma { j + 2 } else { j + 1 }; + } + } + Kind::Punct(';') if at_top => return j + 1, + Kind::Punct(',') if at_top && (arm || !block_item) => return j + 1, + _ => {} + } + j += 1; + } + toks.len() +} + +/// Mark the tokens of every item whose `cfg` attribute is false on `wasm32-unknown-unknown`. +/// +/// An outer attribute (`#[cfg(...)]`) covers the item after it; an inner attribute (`#![cfg(...)]`) +/// covers the rest of the module or block it is in. +fn native_only(toks: &[Token]) -> Vec { + cfg_covered(toks, false) +} + +/// Mark the tokens of every item that a `cfg` attribute covers: with `any`, whatever its predicate, +/// otherwise only when the predicate is false on `wasm32-unknown-unknown`, as [`native_only`] does. +fn cfg_covered(toks: &[Token], any: bool) -> Vec { + let mut skip = vec![false; toks.len()]; + let mut i = 0; + while i < toks.len() { + if !toks[i].is_punct('#') { + i += 1; + continue; + } + let inner = toks.get(i + 1).is_some_and(|t| t.is_punct('!')); + let open = if inner { i + 2 } else { i + 1 }; + if !toks.get(open).is_some_and(|t| t.is_punct('[')) { + i += 1; + continue; + } + let close = matching_close(toks, open); + if close >= toks.len() { + // An unclosed attribute: no later bracket closes it, so nothing after it is an item. + break; + } + let excluded = toks.get(open + 1).is_some_and(|t| t.is_ident("cfg")) && toks.get(open + 2).is_some_and(|t| t.is_punct('(')) && { + let mut pos = open + 3; + any || eval_cfg(&toks[..close], &mut pos, 0) == Tri::False + }; + if !excluded { + i = close + 1; + continue; + } + // Further outer attributes on the same item. + let mut item = close + 1; + while !inner && item < toks.len() && toks[item].is_punct('#') && toks.get(item + 1).is_some_and(|t| t.is_punct('[')) { + item = matching_close(toks, item + 1).saturating_add(1); + } + let end = item_end(toks, item.min(toks.len()), inner); + for s in skip.iter_mut().take(end).skip(i) { + *s = true; + } + i = end.max(i + 1); + } + skip +} + +/// The index of the keyword of every item that carries an outer `cfg` attribute of its own whose predicate +/// is not known to be true on `wasm32-unknown-unknown`, so the item may be absent from the web build: the +/// token after the item's outer attributes and its visibility (`pub`, `pub(...)`). +fn cfg_items(toks: &[Token]) -> BTreeSet { + let mut items = BTreeSet::new(); + let mut i = 0; + while i < toks.len() { + let cfg = toks[i].is_punct('#') + && toks.get(i + 1).is_some_and(|t| t.is_punct('[')) + && toks.get(i + 2).is_some_and(|t| t.is_ident("cfg")) + && toks.get(i + 3).is_some_and(|t| t.is_punct('(')); + if !cfg { + i += 1; + continue; + } + let close = matching_close(toks, i + 1); + if close >= toks.len() { + break; + } + let mut pos = i + 4; + if eval_cfg(&toks[..close], &mut pos, 0) == Tri::True { + i = close + 1; + continue; + } + let mut item = close + 1; + while item < toks.len() && toks[item].is_punct('#') && toks.get(item + 1).is_some_and(|t| t.is_punct('[')) { + item = matching_close(toks, item + 1).saturating_add(1); + } + if toks.get(item).is_some_and(|t| t.is_ident("pub")) { + item += 1; + if toks.get(item).is_some_and(|t| t.is_punct('(')) { + item = matching_close(toks, item).saturating_add(1); + } + } + if item < toks.len() { + items.insert(item); + } + i = item.max(i + 1); + } + items +} + +/// A module declaration, `mod name;` or the head of `mod name { ... }`. +#[derive(Debug, Clone)] +struct ModName { + name: String, + /// The string of a `#[path = "..."]` attribute on the declaration. + path_attribute: Option, +} + +/// An out-of-line module declaration, `mod name;`. +#[derive(Debug)] +struct ModDecl { + module: ModName, + /// The inline modules (`mod a { ... }`) the declaration is inside, outermost first, shared between the + /// declarations inside them. + inline: Vec>, + /// Whether the declaration is inside an item whose `cfg` is false on `wasm32-unknown-unknown`. + native: bool, +} + +/// The most tokens [`path_attribute`] reads back from one `mod` keyword, so that hostile nesting cannot +/// make the scan quadratic. +const ATTRIBUTE_SCAN_LIMIT: usize = 4096; + +/// The index of the bracket `open` that matches the bracket `close` at `toks[end - 1]`, reading back +/// from `end`, or `None` when it is not found within `budget` tokens. +fn matching_open_back(toks: &[Token], end: usize, open: char, close: char, budget: &mut usize) -> Option { + let mut depth = 0usize; + let mut j = end; + while j > 0 && *budget > 0 { + j -= 1; + *budget -= 1; + if toks[j].is_punct(close) { + depth += 1; + } else if toks[j].is_punct(open) { + depth = depth.saturating_sub(1); + if depth == 0 { + return Some(j); + } + } + } + None +} + +/// The string of a `#[path = "..."]` outer attribute on the item whose `mod` keyword is at `toks[m]`. +fn path_attribute(toks: &[Token], m: usize) -> Option { + let mut budget = ATTRIBUTE_SCAN_LIMIT; + let mut k = m; + if k > 0 && toks[k - 1].is_punct(')') { + // A visibility such as `pub(crate)`. + let j = matching_open_back(toks, k, '(', ')', &mut budget)?; + if j > 0 && toks[j - 1].is_ident("pub") { + k = j - 1; + } + } else if k > 0 && toks[k - 1].is_ident("pub") { + k -= 1; + } + let mut found = None; + while k > 0 && toks[k - 1].is_punct(']') { + let Some(j) = matching_open_back(toks, k, '[', ']', &mut budget) else { break }; + if j == 0 || !toks[j - 1].is_punct('#') { + break; + } + if toks.get(j + 1).is_some_and(|t| t.is_ident("path")) + && toks.get(j + 2).is_some_and(|t| t.is_punct('=')) + && let Some(Kind::Str(s)) = toks.get(j + 3).map(|t| &t.kind) + { + found = Some(s.clone()); + } + k = j - 1; + } + found +} + +/// What an open brace in [`module_decls`] opened. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Brace { + /// A block that is not an inline module. + Block, + /// An inline module, pushed on the inline stack. + Module, + /// An inline module deeper than [`MAX_NESTING`], not tracked. + TooDeep, +} + +/// The name of a module's file or folder: a raw identifier such as `r#match` names `match`. +fn module_name(name: &str) -> String { + name.strip_prefix("r#").unwrap_or(name).to_owned() +} + +/// Every out-of-line module declaration in a file, in one pass that tracks the inline modules around it. +/// +/// Declarations inside more than [`MAX_NESTING`] inline modules are left out, so their files are checked. +fn module_decls(toks: &[Token], skip: &[bool]) -> Vec { + let mut decls = Vec::new(); + let mut braces: Vec = Vec::new(); + let mut inline: Vec> = Vec::new(); + let mut too_deep = 0usize; + for (i, tok) in toks.iter().enumerate() { + match &tok.kind { + Kind::Punct('{') => { + let brace = match i.checked_sub(2).map(|j| (&toks[j], &toks[j + 1].kind)) { + Some((m, Kind::Ident(name))) if m.is_ident("mod") => { + if too_deep == 0 && inline.len() < MAX_NESTING { + inline.push(Rc::new(ModName { name: module_name(name), path_attribute: path_attribute(toks, i - 2) })); + Brace::Module + } else { + too_deep += 1; + Brace::TooDeep + } + } + _ => Brace::Block, + }; + braces.push(brace); + } + Kind::Punct('}') => match braces.pop() { + Some(Brace::Module) => { + inline.pop(); + } + Some(Brace::TooDeep) => too_deep -= 1, + _ => {} + }, + Kind::Ident(k) if k == "mod" && too_deep == 0 => { + if let Some(Kind::Ident(name)) = toks.get(i + 1).map(|t| &t.kind) + && toks.get(i + 2).is_some_and(|t| t.is_punct(';')) + { + decls.push(ModDecl { + module: ModName { name: module_name(name), path_attribute: path_attribute(toks, i) }, + inline: inline.clone(), + native: skip.get(i).copied().unwrap_or(false), + }); + } + } + _ => {} + } + } + decls +} + +/// A path with `\` read as `/`, empty and `.` components removed and `..` applied to the component before it. +fn normalize_path(path: &str) -> String { + let path = path.replace('\\', "/"); + let absolute = path.starts_with('/'); + let mut parts: Vec<&str> = Vec::new(); + for part in path.split('/') { + match part { + "" | "." => {} + ".." if parts.last().is_some_and(|p| *p != "..") => { + parts.pop(); + } + p => parts.push(p), + } + } + let joined = parts.join("/"); + if absolute { format!("/{joined}") } else { joined } +} + +/// `child` below the folder `parent`, normalized. A `child` that is an absolute path (`/x`, `\x` or `C:...`) +/// stands alone. `\\` is read as one separator, since a string literal keeps its escapes. +fn join_path(parent: &str, child: &str) -> String { + let child = child.replace("\\\\", "/"); + if parent.is_empty() || child.starts_with('/') || child.starts_with('\\') || child.as_bytes().get(1) == Some(&b':') { + normalize_path(&child) + } else { + normalize_path(&format!("{parent}/{child}")) + } +} + +/// The paths, normalized, where the module `decl` declared in the file at `parent` (normalized) can be. +/// +/// The modules of `lib.rs`, `main.rs` and `mod.rs`, and with `path_loaded`, of a file that a `#[path]` +/// attribute names, are in the file's folder, and those of any other file are in the folder named after +/// its stem. Each inline module around the declaration adds a folder named +/// after it, or with a `path` attribute, moves to that path relative to the current folder (without the +/// stem folder). The module is then `.rs` or `/mod.rs` there, or with a `path` attribute, that +/// path relative to the current folder, again without the stem folder. +fn module_files(parent: &str, decl: &ModDecl, path_loaded: bool) -> Vec { + let (dir, file) = match parent.rsplit_once('/') { + Some(("", file)) => ("/", file), + Some(split) => split, + None => ("", parent), + }; + let mut folder = dir.to_string(); + let mut stem = (!path_loaded && !matches!(file, "lib.rs" | "main.rs" | "mod.rs")).then(|| file.strip_suffix(".rs").unwrap_or(file)); + for inline in &decl.inline { + if let Some(attr) = &inline.path_attribute { + folder = join_path(&folder, attr); + stem = None; + } else { + if let Some(stem) = stem.take() { + folder = join_path(&folder, stem); + } + folder = join_path(&folder, &inline.name); + } + } + if let Some(attr) = &decl.module.path_attribute { + return vec![join_path(&folder, attr)]; + } + if let Some(stem) = stem { + folder = join_path(&folder, stem); + } + let name = &decl.module.name; + vec![join_path(&folder, &format!("{name}.rs")), join_path(&folder, &format!("{name}/mod.rs"))] +} + +/// For each file, whether it is native-only: a `mod` declaration in a native-only item, or any declaration +/// in a native-only file, names it, and no chain of declarations outside native-only items reaches it from +/// a crate root (a file that no declaration names, or in a cycle of declarations that no file outside the +/// cycle names, its `lib.rs` or `main.rs`, or every member when it has neither). +fn native_only_files(paths: &[String], decls: &[Vec]) -> Vec { + let mut index: BTreeMap<&str, Vec> = BTreeMap::new(); + for (i, path) in paths.iter().enumerate() { + index.entry(path.as_str()).or_default().push(i); + } + // rustc reads a file that a `#[path]` attribute names as if it were a `mod.rs`, so the modules it + // declares are in its own folder rather than its stem folder. Each file is therefore two nodes: node + // `2 * file` for the file loaded by a plain `mod name;` (or as a crate root), and node `2 * file + 1` + // for the file loaded through a `#[path]` attribute. Each node lists the nodes its declarations name, + // with whether the declaration is in a native-only item. + let edges: Vec> = (0..paths.len() * 2) + .map(|node| { + let (parent, path_loaded) = (node / 2, node % 2 == 1); + let mut targets = Vec::new(); + for decl in &decls[parent] { + let by_path = usize::from(decl.module.path_attribute.is_some()); + for candidate in module_files(&paths[parent], decl, path_loaded) { + for &j in index.get(candidate.as_str()).into_iter().flatten() { + targets.push((decl.native, 2 * j + by_path)); + } + } + } + targets + }) + .collect(); + + // A group of files that name each other in a cycle, and that no file outside the group names, has no + // member that a crate root can reach. rustc does not load a cycle of enabled declarations, so such a + // cycle runs through a declaration the build leaves out. When the group holds a `lib.rs` or `main.rs`, + // those files are its crate roots and the declarations that name them are read as left out; otherwise + // no declaration of the group names its own members, and each of them can be a crate root. + let file_edges: Vec> = + (0..paths.len()).map(|file| edges[2 * file].iter().chain(&edges[2 * file + 1]).map(|&(_, target)| target / 2).collect()).collect(); + let component = components(&file_edges); + let root_file = |file: usize| matches!(paths[file].rsplit('/').next(), Some("lib.rs" | "main.rs")); + let mut entered = vec![false; paths.len()]; + let mut has_root = vec![false; paths.len()]; + for (file, targets) in file_edges.iter().enumerate() { + has_root[component[file]] |= root_file(file); + for &target in targets { + if component[target] != component[file] { + entered[component[target]] = true; + } + } + } + let names = |node: usize, target: usize| { + let group = component[target / 2]; + component[node / 2] != group || entered[group] || (has_root[group] && !root_file(target / 2)) + }; + + // The nodes that are loaded: a file that no loaded node names is a crate root, loaded plainly, and a + // node is loaded when a loaded node names it. Every file starts out loaded plainly, and the set is + // narrowed until it stops changing, or after `MAX_MODULE_ROUNDS` rounds. + let mut loaded: Vec = (0..paths.len() * 2).map(|node| node % 2 == 0).collect(); + let mut named = vec![false; paths.len()]; + for _ in 0..MAX_MODULE_ROUNDS { + let mut reached = vec![false; paths.len() * 2]; + for (node, targets) in edges.iter().enumerate() { + if loaded[node] { + for &(_, target) in targets { + if names(node, target) { + reached[target] = true; + } + } + } + } + named = (0..paths.len()).map(|j| reached[2 * j] || reached[2 * j + 1]).collect(); + let next: Vec = (0..paths.len() * 2).map(|node| reached[node] || (node % 2 == 0 && !named[node / 2])).collect(); + if next == loaded { + break; + } + loaded = next; + } + + // Files the web build compiles: crate roots, and every file a declaration outside a native-only item + // of such a file names. + let mut web = vec![false; paths.len() * 2]; + let mut queue: Vec = (0..paths.len()).filter(|&j| !named[j]).map(|j| 2 * j).collect(); + for &node in &queue { + web[node] = true; + } + while let Some(parent) = queue.pop() { + for &(_, target) in edges[parent].iter().filter(|(is_native, _)| !is_native) { + if !web[target] { + web[target] = true; + queue.push(target); + } + } + } + + // Files a native-only declaration of a loaded node names, and their descendants. + let mut native = vec![false; paths.len() * 2]; + let starts = edges.iter().enumerate().filter(|&(node, _)| loaded[node]).flat_map(|(_, targets)| targets.iter()); + for &(_, target) in starts.filter(|(is_native, _)| *is_native) { + if !native[target] { + native[target] = true; + queue.push(target); + } + } + while let Some(parent) = queue.pop() { + for &(_, target) in &edges[parent] { + if !native[target] { + native[target] = true; + queue.push(target); + } + } + } + (0..paths.len()).map(|j| (native[2 * j] || native[2 * j + 1]) && !web[2 * j] && !web[2 * j + 1]).collect() +} + +/// The most rounds [`native_only_files`] spends narrowing the set of loaded files. +const MAX_MODULE_ROUNDS: usize = 16; + +/// For each node of the graph `edges`, the number of its strongly connected component (Tarjan's +/// algorithm, with an explicit stack in place of recursion). +fn components(edges: &[Vec]) -> Vec { + const UNSEEN: usize = usize::MAX; + let mut index = vec![UNSEEN; edges.len()]; + let mut low = vec![0; edges.len()]; + let mut on_stack = vec![false; edges.len()]; + let mut component = vec![UNSEEN; edges.len()]; + let mut stack = Vec::new(); + let mut next = 0; + let mut count = 0; + for root in 0..edges.len() { + if index[root] != UNSEEN { + continue; + } + index[root] = next; + low[root] = next; + next += 1; + stack.push(root); + on_stack[root] = true; + // Each frame is a node and the position of the next edge of it to follow. + let mut work = vec![(root, 0usize)]; + while let Some(frame) = work.last_mut() { + let node = frame.0; + if let Some(&target) = edges[node].get(frame.1) { + frame.1 += 1; + if index[target] == UNSEEN { + index[target] = next; + low[target] = next; + next += 1; + stack.push(target); + on_stack[target] = true; + work.push((target, 0)); + } else if on_stack[target] { + low[node] = low[node].min(index[target]); + } + continue; + } + work.pop(); + if let Some(&(parent, _)) = work.last() { + low[parent] = low[parent].min(low[node]); + } + if low[node] == index[node] { + while let Some(member) = stack.pop() { + on_stack[member] = false; + component[member] = count; + if member == node { + break; + } + } + count += 1; + } + } + } + component +} + +/// The innermost `{` around each token, and for each `}` the `{` it closes. +fn brace_scopes(toks: &[Token]) -> (Vec>, Vec>) { + let mut around = vec![None; toks.len()]; + let mut opener = vec![None; toks.len()]; + let mut open = Vec::new(); + for (j, t) in toks.iter().enumerate() { + around[j] = open.last().copied(); + if t.is_punct('{') { + open.push(j); + } else if t.is_punct('}') { + opener[j] = open.pop(); + } + } + (around, opener) +} + +/// Look a name up from `toks[at]` outward through the braces around it (`around`, from +/// [`brace_scopes`]): the first value that `get` returns for a pair of braces, given the index of its +/// `{`, or for the file, given `None`. The search ends with the body of the `mod` it starts in, since a +/// module does not see the names its parent binds. `Some(None)` means no braces bind the name; `None` +/// means the search passed [`MAX_NESTING`] levels. +fn lookup(toks: &[Token], around: &[Option], at: usize, get: impl Fn(Option) -> Option) -> Option> { + let mut scope = around.get(at).copied().flatten(); + for _ in 0..=MAX_NESTING { + if let Some(value) = get(scope) { + return Some(Some(value)); + } + match scope { + Some(o) if o >= 2 && toks[o - 2].is_ident("mod") && matches!(toks[o - 1].kind, Kind::Ident(_)) => return Some(None), + Some(o) => scope = around[o], + None => return Some(None), + } + } + None +} + +/// Add one finding for each line in `lines`, in line order, with the trimmed text of that line. +fn report(findings: &mut Vec, path: &str, content: &str, lines: &BTreeSet) { + if lines.is_empty() { + return; + } + let text: Vec<&str> = content.lines().collect(); + for &line in lines { + let line_text = text.get(line.saturating_sub(1)).map_or("", |s| s.trim()); + findings.push(Finding::new(path, line, line_text)); + } +} + +/// For each pair of braces (`None` for the file) and a name: whether a binding in those braces binds the +/// name to the muda crate, and whether one binds it to something else. +type CrateBindings<'a> = BTreeMap<(&'a str, Option), (bool, bool)>; + +/// The names of the muda crate in a file, and where each is bound. The names are `muda` itself and each +/// name that `use muda as m;`, `use muda::{self as m};` or `extern crate muda as m;` gives it. For each +/// name and each pair of braces, the bindings record whether a `use` or an `extern crate` in them binds +/// the name to the crate, and whether a `use`, an `extern crate` or a `mod` in them binds it to something +/// else. A binding to something else inside an item with a `cfg` attribute, or through `crate::`, `self::` +/// or `super::` (which may be a re-export of the crate), is left out. +fn muda_crate_bindings<'a>( + toks: &'a [Token], + leaves: &'a [UseLeaf], + around: &[Option], + gated: &[bool], +) -> (BTreeSet<&'a str>, CrateBindings<'a>) { + // Each `extern crate c;` or `extern crate c as n;`: whether `c` is muda, and the index of the name it + // binds. + let mut externs: Vec<(bool, &'a str, usize)> = Vec::new(); + for i in 0..toks.len() { + if toks[i].is_ident("extern") + && toks.get(i + 1).is_some_and(|t| t.is_ident("crate")) + && let Some(Kind::Ident(krate)) = toks.get(i + 2).map(|t| &t.kind) + { + let at = if toks.get(i + 3).is_some_and(|t| t.is_ident("as")) { i + 4 } else { i + 2 }; + if let Some(Kind::Ident(name)) = toks.get(at).map(|t| &t.kind) + && name != "_" + { + externs.push((krate == "muda", name.as_str(), at)); + } + } + } + let mut names: BTreeSet<&'a str> = BTreeSet::from(["muda"]); + names.extend(leaves.iter().filter(|leaf| leaf.path == ["muda"]).filter_map(UseLeaf::binding)); + names.extend(externs.iter().filter(|&&(muda, _, _)| muda).map(|&(_, name, _)| name)); + let mut bound: CrateBindings<'a> = BTreeMap::new(); + // `use muda as m;` binds `m` to the crate only where `muda` itself names the crate, so it is read + // after every other binding. + let renames = |leaf: &UseLeaf| leaf.path == ["muda"] && leaf.binding() != Some("muda"); + for leaf in leaves.iter().filter(|leaf| !renames(leaf)) { + let Some(name) = leaf.binding().filter(|name| names.contains(name)) else { continue }; + let local = leaf.root.as_deref().is_some_and(|s| matches!(s, "crate" | "self" | "super")); + let entry = bound.entry((name, around.get(leaf.start).copied().flatten())); + if leaf.path == ["muda"] { + entry.or_default().0 = true; + } else if !local && !gated.get(leaf.start).copied().unwrap_or(false) { + entry.or_default().1 = true; + } + } + for &(muda, name, at) in &externs { + if !names.contains(name) { + continue; + } + if muda { + bound.entry((name, around[at])).or_default().0 = true; + } else if !gated[at] { + bound.entry((name, around[at])).or_default().1 = true; + } + } + for (i, t) in toks.iter().enumerate() { + if let Kind::Ident(name) = &t.kind + && names.contains(name.as_str()) + && i.checked_sub(1).is_some_and(|k| toks[k].is_ident("mod")) + && !gated[i] + { + bound.entry((name, around[i])).or_default().1 = true; + } + } + let mut renamed = Vec::new(); + for leaf in leaves.iter().filter(|leaf| renames(leaf)) { + let Some(name) = leaf.binding() else { continue }; + let crate_here = + leaf.absolute || !matches!(lookup(toks, around, leaf.start, |scope| bound.get(&("muda", scope)).copied()), Some(Some((false, true)))); + let local = leaf.root.as_deref().is_some_and(|s| matches!(s, "crate" | "self" | "super")); + let scope = around.get(leaf.start).copied().flatten(); + if crate_here { + renamed.push((name, scope, true)); + } else if !local && !gated.get(leaf.start).copied().unwrap_or(false) { + renamed.push((name, scope, false)); + } + } + for (name, scope, muda) in renamed { + let entry = bound.entry((name, scope)).or_default(); + if muda { + entry.0 = true; + } else { + entry.1 = true; + } + } + (names, bound) +} + +/// Guard: detects direct calls to `muda`'s predefined Quit menu item. +/// +/// Using `PredefinedMenuItem::quit` connects the macOS menu directly to `NSApplication terminate:`, +/// which bypasses dirty document checks, unsaved changes save prompts, and lifecycle cleanup. +/// Applications should route Quit through custom menu items or interceptors connected to `craft-quit`. +/// +/// The guard reads Rust tokens, so comments, strings and character literals are ignored, and whitespace +/// or line breaks around `::` do not hide a call. A qualified path such as `::quit` +/// is reported too. A path that names the type through another crate or module, such as +/// `other::PredefinedMenuItem::quit`, is not, and neither is a path through a name `muda` that the innermost +/// braces binding it bind only to something else (`use other_menu as muda;`, `mod muda`), read like a name +/// used on its own below; `::muda::PredefinedMenuItem` always names the crate. A name that the crate is +/// imported under (`use muda as menu;`, `use muda::{self as menu};`, `extern crate muda as menu;`) is read +/// like `muda` in the braces that bind it, so `menu::PredefinedMenuItem::quit` is reported there. It also +/// follows a `use` or a `type` alias that renames muda's type (`use muda::PredefinedMenuItem as Item;` then +/// `Item::quit`); a rename of another crate's `PredefinedMenuItem` is not followed, including one through a +/// name `muda` that the braces around the `use` or `type` bind to something else, read as for a qualified +/// path. +/// +/// A name used on its own (`PredefinedMenuItem::quit`, `Item::quit`) is read in the innermost braces +/// around it, within its module, or the file, that bind the name. It is reported when those braces +/// import or alias muda's type under it, and not when they only import another type under it +/// (`use other::PredefinedMenuItem;`) or define one (`struct PredefinedMenuItem`). Where no braces bind +/// it, `PredefinedMenuItem` is reported and a renamed name is not. An import or definition inside an +/// item with a `cfg` attribute, an import or a type alias through `crate::`, `self::` or `super::` +/// (which may be a re-export of muda's type), and an associated `type` in an `impl` or `trait` do not +/// bind the name. Each finding names the line where the type's name starts, once per line, in line order. +pub fn predefined_quit(files: I) -> Vec +where + I: IntoIterator, + I::Item: AsFileSource, +{ + let mut findings = Vec::new(); + for file in files { + let path = file.path(); + let content = file.content(); + let toks = lex_rust(content); + let leaves = use_leaves(&toks, &BTreeSet::new()); + let (around, opener) = brace_scopes(&toks); + let gated = cfg_covered(&toks, true); + let (crate_names, crate_bound) = muda_crate_bindings(&toks, &leaves, &around, &gated); + let names_crate = |s: &str| crate_names.contains(s); + // Whether `name`, read at token `i`, names the crate. After a leading `::` only `muda` does. + // Otherwise `muda` does unless the innermost braces around `i` that bind `muda` bind it only to + // something else, and another name of the crate does when those braces bind it to the crate, or + // only to nothing else. + let crate_name_at = |name: &str, i: usize, absolute: bool| { + if absolute || !names_crate(name) { + return name == "muda"; + } + match lookup(&toks, &around, i, |scope| crate_bound.get(&(name, scope)).copied()) { + None => true, + Some(Some((muda, other))) => muda || !other, + Some(None) => name == "muda", + } + }; + // Whether the identifier at `i` names the crate. + let crate_is_muda = |i: usize| { + let Kind::Ident(name) = &toks[i].kind else { return false }; + let absolute = i >= 2 && toks[i - 1].is_punct(':') && toks[i - 2].is_punct(':'); + crate_name_at(name, i, absolute) + }; + // A `use` path to muda's type, read through a name of the crate bound where the `use` is. After + // a leading `::` the first segment is an external crate, so only `muda` names it. + let is_muda = + |leaf: &UseLeaf| leaf.path.len() == 2 && leaf.path[1] == "PredefinedMenuItem" && crate_name_at(&leaf.path[0], leaf.start, leaf.absolute); + // Whether the braces opened at `o` are the body of an `impl` or a `trait`, whose `type` items are + // associated types rather than names in scope. The head is read back from `o` to the `;`, `{` or + // `}` before it, stepping over a block in angle brackets (`impl Foo<{ N }>`), for at most + // `ATTRIBUTE_SCAN_LIMIT` tokens. + let assoc_body = |o: usize| { + let mut j = o; + let mut depth = 0usize; + let mut start = None; + for _ in 0..ATTRIBUTE_SCAN_LIMIT { + let Some(k) = j.checked_sub(1) else { + start = Some(0); + break; + }; + let t = &toks[k]; + if depth == 0 && (t.is_punct(';') || t.is_punct('{')) { + start = Some(k + 1); + break; + } + if depth == 0 && t.is_punct('}') { + let generic = opener[k].filter(|&b| b.checked_sub(1).is_some_and(|p| ['<', ',', '='].iter().any(|&c| toks[p].is_punct(c)))); + if let Some(b) = generic { + j = b; + continue; + } + start = Some(k + 1); + break; + } + match t.kind { + Kind::Punct(')' | ']') => depth += 1, + Kind::Punct('(' | '[') => depth = depth.saturating_sub(1), + _ => {} + } + j = k; + } + let Some(mut s) = start else { return false }; + // Attributes and modifiers before the keyword. + while s < o { + let next = |c: char| toks.get(s + 1).is_some_and(|t| t.is_punct(c)); + if (toks[s].is_punct('#') && next('[')) || (toks[s].is_ident("pub") && next('(')) { + // `#[...]` or `pub(...)`. + s = matching_close(&toks, s + 1).saturating_add(1); + } else if ["pub", "unsafe", "auto", "default"].iter().any(|m| toks[s].is_ident(m)) { + s += 1; + } else { + break; + } + } + s < o && (toks[s].is_ident("impl") || toks[s].is_ident("trait")) + }; + // Whether the `type` name at `i` is followed by `= muda::PredefinedMenuItem;` (or `::muda::...`, or + // a name of the crate in place of `muda`). + let aliases_muda = |i: usize| { + let mut j = i + 1; + if !toks.get(j).is_some_and(|t| t.is_punct('=')) { + return false; + } + j += 1; + if toks.get(j).is_some_and(|t| t.is_punct(':')) && toks.get(j + 1).is_some_and(|t| t.is_punct(':')) { + j += 2; + } + j < toks.len() + && crate_is_muda(j) + && then_path(&toks, j + 1, |s| s == "PredefinedMenuItem") + && toks.get(j + 4).is_some_and(|t| t.is_punct(';')) + }; + let definition = + |i: usize| i.checked_sub(1).is_some_and(|k| ["struct", "enum", "union", "type", "trait"].iter().any(|kw| toks[k].is_ident(kw))); + // The names of muda's type: its own, and each name a `use` or a `type` alias gives it. + let mut names: BTreeSet<&str> = BTreeSet::from(["PredefinedMenuItem"]); + names.extend(leaves.iter().filter(|leaf| is_muda(leaf)).filter_map(UseLeaf::binding)); + for (i, t) in toks.iter().enumerate() { + if let Kind::Ident(name) = &t.kind + && i.checked_sub(1).is_some_and(|k| toks[k].is_ident("type")) + && aliases_muda(i) + { + names.insert(name); + } + } + // For each of those names and each pair of braces (`None` for the file), whether a `use` or a + // type alias in it binds the name to muda's type, and whether a `use` or an item definition in it + // binds the name to another type. A binding to another type inside an item with a `cfg` + // attribute may not apply on the platform the guard protects, so it is left out. + let mut bound: BTreeMap<(&str, Option), (bool, bool)> = BTreeMap::new(); + for leaf in &leaves { + let Some(name) = leaf.binding().filter(|name| names.contains(name)) else { continue }; + let muda = is_muda(leaf); + // A path into this crate (`crate::`, `self::`, `super::`) may lead to a re-export of muda's + // type, so it is not counted as another type. + let local = leaf.root.as_deref().is_some_and(|s| matches!(s, "crate" | "self" | "super")); + if muda || (!local && !gated.get(leaf.start).copied().unwrap_or(false)) { + let entry = bound.entry((name, around.get(leaf.start).copied().flatten())).or_default(); + if muda { + entry.0 = true; + } else { + entry.1 = true; + } + } + } + for (i, t) in toks.iter().enumerate() { + let Kind::Ident(name) = &t.kind else { continue }; + let Some(k) = i.checked_sub(1) else { continue }; + if !names.contains(name.as_str()) || !definition(i) { + continue; + } + if toks[k].is_ident("type") && around[i].is_some_and(assoc_body) { + continue; + } + // An alias of a path into this crate may name a re-export of muda's type, as an import does. + let local_alias = toks.get(i + 1).is_some_and(|t| t.is_punct('=')) + && toks.get(i + 2).is_some_and(|t| t.is_ident("crate") || t.is_ident("self") || t.is_ident("super")); + if toks[k].is_ident("type") && local_alias { + continue; + } + let key = (name.as_str(), around[i]); + if toks[k].is_ident("type") && aliases_muda(i) { + bound.entry(key).or_default().0 = true; + } else if !gated[i] { + bound.entry(key).or_default().1 = true; + } + } + // A name used on its own is muda's type when the innermost braces around it that bind it (within + // its module) bind it to muda's type, or only to nothing else. Where no braces bind it, only + // `PredefinedMenuItem` itself is muda's type. Past `MAX_NESTING` levels it is read as muda's type. + let bare_is_muda = |name: &str, i: usize| match lookup(&toks, &around, i, |scope| bound.get(&(name, scope)).copied()) { + None => true, + Some(Some((muda, other))) => muda || !other, + Some(None) => name == "PredefinedMenuItem", + }; + let mut lines = BTreeSet::new(); + for (i, tok) in toks.iter().enumerate() { + let Kind::Ident(name) = &tok.kind else { continue }; + if !names.contains(name.as_str()) { + continue; + } + // A qualified name counts only as `muda::PredefinedMenuItem` (or `::muda::...`, or a name of the + // crate in place of `muda`), not as another crate's or module's type of the same name. + let named = if continues_a_path(&toks, i) { + name == "PredefinedMenuItem" && !continues_a_path(&toks, i - 3) && crate_is_muda(i - 3) + } else { + bare_is_muda(name, i) + }; + let quit = |at: usize| then_path(&toks, at, |s| s == "quit" || s == "Quit"); + // `::quit`, a qualified path to the same function. + let qualified = || toks.get(i + 1).is_some_and(|t| t.is_punct('>')) && quit(i + 2) && opens_qualified_path(&toks, i); + if named && (quit(i + 1) || qualified()) { + lines.insert(tok.line); + } + } + report(&mut findings, path, content, &lines); + } + findings +} + +/// Guard: detects uses of `std::time::Instant` in code paths running on the web. +/// +/// On `wasm32-unknown-unknown`, `std::time::Instant::now()` panics with "time not implemented +/// on this platform". Crates running in the browser should use `web_time::Instant`, which maps to +/// `std::time::Instant` on native platforms and `performance.now()` on the web. +/// +/// ### Syntactic Scope and Limitations +/// +/// * **Targeted files**: Callers should pass files from crates that compile into the application's web build. +/// * **What is reported**: the paths `std::time::Instant` and `::std::time::Instant`; `::Instant` where a +/// `use` outside a native-only item binds `m` to `std::time` (`use std::time;`, `use std::time as t;`, +/// `use std::time::{self as t}`); +/// and every `use` that imports `std::time::Instant` or `std::time::*`, under any name, directly or through +/// such an `m` of any length (`use m::{Instant as Clock};`, `use m::*;`). Whitespace, comments +/// and line breaks may separate the parts of a path. Each finding names the line where the path starts (in a +/// `use` group, the line where the name's part of the group starts), once per line, in line order. +/// * **What is skipped**: comments, string and character literals, other crates' `Instant` (`web_time::Instant`, +/// a `time` module that is not `std::time`), and items whose `cfg` attribute is false on `wasm32-unknown-unknown` +/// (for example `#[cfg(not(target_arch = "wasm32"))]`, `#[cfg(all(unix, ...))]`, `#[cfg(target_os = "macos")]`, +/// or `#![cfg(...)]` for the rest of a module). Such an attribute covers only its item: code before the +/// attribute or after the item on the same line is checked. A statement whose value holds a block, such as +/// `let n = match x { ... } as u64 + ...;`, is covered up to its `;`. `cfg(false)` is false and `cfg(true)` +/// is true. A predicate that depends on a feature or on `test`, or that is nested more than 64 levels deep, +/// is not known from the source, so the item is checked. Escapes in a predicate's string values are decoded. +/// * **Module files**: a `mod name;` inside such an item makes the module's file native-only, and so are the +/// files of the modules declared in it, when they are among `files`, unless a chain of declarations outside +/// such items also reaches the file from a crate root (a file that no declaration names, or in a cycle of +/// declarations that no file outside the cycle names, such as `lib.rs` naming itself through `#[path]`, the +/// cycle's `lib.rs` or `main.rs`, or every file of the cycle when it has neither). The module's file is `name.rs` or +/// `name/mod.rs` next to `lib.rs`, `main.rs`, `mod.rs` or a file that a `#[path]` attribute names, and in +/// the folder named after any other file's stem, below the folders of enclosing inline modules; a `#[path = "..."]` attribute on the declaration +/// or on an enclosing inline module is followed. Paths are compared as text with `\` read as `/`, so the files must be named the same way +/// (for example all from one `rust_sources` call), and a crate root other than `lib.rs` or `main.rs` +/// (such as `src/bin/tool.rs`) is read as an ordinary module file. A declaration inside more than 64 inline +/// modules is not followed, so its file is checked. +/// * **Limitations**: This is a lexical source guard. It does not expand macros, resolve paths through other +/// modules or re-exports, or rename `std` itself (`use std as s;`). An alias of `std::time` counts inside the +/// braces of its `use` (or the file), down to but not into the body of a nested `mod`; nearer braces whose +/// `use` or `mod` binds the same name to something else hide it, unless that `use` or `mod` carries a `cfg` +/// attribute of its own that is not known to be true on `wasm32-unknown-unknown`, which may leave it out of +/// the web build. +pub fn std_instant(files: I) -> Vec +where + I: IntoIterator, + I::Item: AsFileSource, +{ + let files: Vec = files.into_iter().collect(); + let paths: Vec = files.iter().map(|file| normalize_path(file.path())).collect(); + // Only the declarations are kept from this pass, so the tokens of every file are never held at once. + let decls: Vec> = files + .iter() + .map(|file| { + let toks = lex_rust(file.content()); + module_decls(&toks, &native_only(&toks)) + }) + .collect(); + let excluded = native_only_files(&paths, &decls); + + let mut findings = Vec::new(); + for (file, excluded) in files.iter().zip(excluded) { + if excluded { + continue; + } + let path = file.path(); + let content = file.content(); + let toks = lex_rust(content); + let skip = native_only(&toks); + let leaves = use_leaves(&toks, &BTreeSet::new()); + let modules: BTreeSet = leaves + .iter() + .filter(|leaf| leaf.path == ["std", "time"] && !skip.get(leaf.start).copied().unwrap_or(false)) + .filter_map(|leaf| leaf.binding().map(str::to_owned)) + .collect(); + // For each alias name and each pair of braces (`None` for the file), whether a `use` in it binds + // the name to `std::time`, and whether a `use` or a `mod` binds the name to something else, unless + // its own `cfg` attribute may be false on the web. + let (around, _) = brace_scopes(&toks); + let gated = cfg_items(&toks); + let mut bound: BTreeMap<(&str, Option), (bool, bool)> = BTreeMap::new(); + for leaf in &leaves { + let Some(name) = leaf.binding().filter(|name| modules.contains(*name)) else { continue }; + let time = leaf.path == ["std", "time"]; + // A `use` of something else that carries a `cfg` attribute of its own that may be false on the + // web may be absent from the web build, so it does not hide an outer alias. + if !skip.get(leaf.start).copied().unwrap_or(false) && (time || !gated.contains(&leaf.start)) { + let entry = bound.entry((name, around.get(leaf.start).copied().flatten())).or_default(); + if time { + entry.0 = true; + } else { + entry.1 = true; + } + } + } + for (i, t) in toks.iter().enumerate() { + if let Kind::Ident(name) = &t.kind + && modules.contains(name) + && !skip[i] + && i.checked_sub(1).is_some_and(|k| toks[k].is_ident("mod") && !gated.contains(&k)) + { + bound.entry((name.as_str(), around[i])).or_default().1 = true; + } + } + // Whether `name` at `toks[at]` is an alias of `std::time`: the innermost braces around it that + // bind the name (within its module) bind it to `std::time`. Past `MAX_NESTING` levels it is. + let time_alias = |name: &str, at: usize| match lookup(&toks, &around, at, |scope| bound.get(&(name, scope)).copied()) { + None => true, + Some(Some((time, _))) => time, + Some(None) => false, + }; + // Read again with the aliases of `std::time`, so a path through an alias starts with `std::time`. + // The leaves read without aliases are checked as well, since an alias does not reach every `use` + // whose path starts with its name (an alias in a nested module named `std`). + let aliased = if modules.is_empty() { Vec::new() } else { use_leaves(&toks, &modules) }; + let is_std_time = |path: &[String]| path == ["std", "time"]; + + let mut lines = BTreeSet::new(); + for leaf in leaves.iter().chain(&aliased) { + if skip.get(leaf.start).copied().unwrap_or(false) { + continue; + } + let imports_instant = if leaf.name.is_none() { + is_std_time(&leaf.path) + } else { + leaf.path.split_last().is_some_and(|(last, parent)| last == "Instant" && is_std_time(parent)) + }; + // A path read through an alias counts only where the alias names `std::time`. + let through_other = leaf.alias.is_some_and(|a| matches!(&toks[a].kind, Kind::Ident(root) if !time_alias(root, leaf.start))); + if imports_instant && !through_other { + lines.insert(leaf.line); + } + } + for (i, tok) in toks.iter().enumerate() { + if skip[i] || continues_a_path(&toks, i) { + continue; + } + let Kind::Ident(name) = &tok.kind else { continue }; + let hit = if name == "std" { + then_path(&toks, i + 1, |s| s == "time") && then_path(&toks, i + 4, |s| s == "Instant") + } else { + modules.contains(name) && then_path(&toks, i + 1, |s| s == "Instant") && time_alias(name, i) + }; + if hit { + lines.insert(tok.line); + } + } + report(&mut findings, path, content, &lines); + } + findings +} + +/// Replace WGSL comments (`//` and nested `/* ... */`) with spaces, and write each WGSL line break as +/// `\n`: line feed, vertical tab, form feed, carriage return, carriage return and line feed as one +/// break, next line (U+0085), line separator (U+2028) and paragraph separator (U+2029) +/// (). A line comment ends at any of them, and `str::lines` +/// then counts lines the way WGSL does. +fn mask_wgsl_comments(code: &str) -> String { + let chars: Vec = code.chars().collect(); + let len = chars.len(); + let mut out = String::with_capacity(code.len()); + let mut i = 0; + let mut depth = 0usize; + let mut in_line = false; + while i < len { + let c = chars[i]; + let next = chars.get(i + 1).copied(); + let line_break = match c { + '\r' if next == Some('\n') => 2, + '\n' | '\u{000B}' | '\u{000C}' | '\r' | '\u{0085}' | '\u{2028}' | '\u{2029}' => 1, + _ => 0, + }; + if line_break > 0 { + in_line = false; + out.push('\n'); + i += line_break; + } else if in_line { + out.push(' '); + i += 1; + } else if c == '/' && next == Some('*') { + depth += 1; + out.push_str(" "); + i += 2; + } else if depth > 0 && c == '*' && next == Some('/') { + depth -= 1; + out.push_str(" "); + i += 2; + } else if depth > 0 { + out.push(' '); + i += 1; + } else if c == '/' && next == Some('/') { + in_line = true; + out.push_str(" "); + i += 2; + } else { + out.push(c); + i += 1; + } + } + out +} + +/// The largest finite value of the type a WGSL float literal with this suffix has. +fn float_limit(suffix: Option) -> f64 { + if suffix == Some(b'h') { 65504.0 } else { f64::from(f32::MAX) } +} + +/// Read a WGSL hexadecimal literal starting at the `0` of `0x`. Returns the index after it, the length of +/// its text without a suffix, and whether it is a float above the range of its type. +fn scan_wgsl_hex(code: &[u8], start: usize) -> (usize, usize, bool) { + // Significant hex digits kept; 15 hold 60 bits, more than an f64 mantissa. + const KEPT: usize = 15; + let mut i = start + 2; + let mut mantissa = 0f64; + let mut kept = 0usize; + let mut exponent: i64 = 0; + let mut digits = 0usize; + let mut digit = |d: u8, fraction: bool, exponent: &mut i64| { + let value = (d as char).to_digit(16).unwrap_or(0); + if mantissa == 0.0 && value == 0 { + if fraction { + *exponent -= 4; + } + } else if kept < KEPT { + mantissa = mantissa * 16.0 + f64::from(value); + kept += 1; + if fraction { + *exponent -= 4; + } + } else if !fraction { + *exponent += 4; + } + }; + while i < code.len() && code[i].is_ascii_hexdigit() { + digit(code[i], false, &mut exponent); + digits += 1; + i += 1; + } + let mut is_float = false; + if code.get(i) == Some(&b'.') { + is_float = true; + i += 1; + while i < code.len() && code[i].is_ascii_hexdigit() { + digit(code[i], true, &mut exponent); + digits += 1; + i += 1; + } + } + let mut has_exponent = false; + if matches!(code.get(i), Some(b'p' | b'P')) { + let mut j = i + 1; + let negative = code.get(j) == Some(&b'-'); + if matches!(code.get(j), Some(b'+' | b'-')) { + j += 1; + } + if code.get(j).is_some_and(u8::is_ascii_digit) { + let mut e: i64 = 0; + while j < code.len() && code[j].is_ascii_digit() { + e = (e * 10 + i64::from(code[j] - b'0')).min(1_000_000); + j += 1; + } + exponent += if negative { -e } else { e }; + has_exponent = true; + is_float = true; + i = j; + } + } + let text_len = i - start; + let suffix = if has_exponent && matches!(code.get(i), Some(b'f' | b'h')) { code.get(i).copied() } else { None }; + let end = if suffix.is_some() || (!is_float && matches!(code.get(i), Some(b'i' | b'u'))) { i + 1 } else { i }; + if !is_float || digits == 0 || mantissa == 0.0 { + return (end, text_len, false); + } + let scale = i32::try_from(exponent.clamp(-4000, 4000)).unwrap_or(0); + let value = mantissa * 2f64.powi(scale); + (end, text_len, !value.is_finite() || value > float_limit(suffix)) +} + +/// Guard: scans a single WGSL shader string and detects float literals outside their type's finite range. +/// +/// WebGPU compilers in browsers reject a float literal above `f32::MAX` (e.g. `3.40282347e38`) with a +/// pipeline build error, which turns off GPU rendering. `f32::MAX` can be written as the hex float +/// `0x1.fffffep+127f`, which equals it, or as the decimal `3.4028234663852886e38`, which rounds to it. +/// +/// ### Policy and Limitations +/// +/// * **Conservative mathematical bound**: The guard compares the literal's value, rounded to `f64`, +/// with `f64::from(f32::MAX)`, or with 65504 (the largest finite `f16`) for an `h` suffix. Any literal +/// above it is reported, even one that a compiler would round down to the maximum. A literal that exceeds +/// the maximum by less than `f64` precision (such as `0x1.fffffe000000001p+127f`) rounds to the maximum and +/// is not reported. +/// * **Which literals**: decimal floats (with a `.`, an exponent or an `f`/`h` suffix) and hexadecimal floats +/// (`0x` with a `.` or a `p` exponent). Integer literals (`123`, `123u`, `0xFFi`, `0x1f`) are not floats and +/// are not reported. A `-` in front is not part of the literal, so `-1e39` is reported as `1e39`. +/// * **Comments**: Comments (`//` and nested `/* ... */`) are masked out prior to scanning. A `//` comment +/// ends at any WGSL line break, a lone carriage return included, and finding lines count every one of them. +/// * **Identifiers**: digits inside an identifier, `π1e39` included, are not a literal. +pub fn wgsl_float_literals(wgsl: &str) -> Vec { + wgsl_float_literals_in([("", wgsl)]) +} + +/// Guard: scans multiple WGSL shader files and detects float literals outside their type's finite range, +/// with the rules of [`wgsl_float_literals`]. +pub fn wgsl_float_literals_in(files: I) -> Vec +where + I: IntoIterator, + I::Item: AsFileSource, +{ + let mut findings = Vec::new(); + for file in files { + let path = file.path(); + let masked = mask_wgsl_comments(file.content()); + + for (n, line) in masked.lines().enumerate() { + let code = line.as_bytes(); + let mut i = 0; + while i < code.len() { + let digit_or_dot = code[i].is_ascii_digit() || (code[i] == b'.' && code.get(i + 1).is_some_and(u8::is_ascii_digit)); + // Outside comments, a character that is not ASCII and not blankspace (U+200E, U+200F) is part of an + // identifier, as in `π1e39`. + let word_before = digit_or_dot + && i > 0 + && (code[i - 1].is_ascii_alphanumeric() + || code[i - 1] == b'_' + || code[i - 1] == b'.' + || (!code[i - 1].is_ascii() && !line[..i].ends_with(['\u{200E}', '\u{200F}']))); + if !digit_or_dot || word_before { + i += 1; + continue; + } + let start = i; + if code[i] == b'0' && matches!(code.get(i + 1), Some(b'x' | b'X')) { + let (end, text_len, over) = scan_wgsl_hex(code, i); + if over { + findings.push(Finding::new(path, n + 1, String::from_utf8_lossy(&code[start..start + text_len]).into_owned())); + } + i = end.max(i + 1); + continue; + } + let mut is_float = false; + while i < code.len() && code[i].is_ascii_digit() { + i += 1; + } + if code.get(i) == Some(&b'.') { + is_float = true; + i += 1; + while i < code.len() && code[i].is_ascii_digit() { + i += 1; + } + } + if matches!(code.get(i), Some(b'e' | b'E')) { + let mut j = i + 1; + if matches!(code.get(j), Some(b'+' | b'-')) { + j += 1; + } + if code.get(j).is_some_and(u8::is_ascii_digit) { + is_float = true; + i = j; + while i < code.len() && code[i].is_ascii_digit() { + i += 1; + } + } + } + let literal = String::from_utf8_lossy(&code[start..i]).into_owned(); + let suffix = match code.get(i) { + Some(&s @ (b'f' | b'h')) => { + is_float = true; + i += 1; + Some(s) + } + Some(b'i' | b'u') if !is_float => { + i += 1; + None + } + _ => None, + }; + if !is_float { + continue; + } + let is_overflow = match literal.parse::() { + Ok(v) => !v.is_finite() || v.abs() > float_limit(suffix), + Err(_) => true, + }; + if is_overflow { + findings.push(Finding::new(path, n + 1, literal)); + } + } + } + } + findings +} + +/// Size limits for [`rust_sources_with_limits`]. An input over a limit is an error. +#[cfg(not(target_arch = "wasm32"))] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct SourceLimits { + /// The largest single file read, in bytes. Default 8 MiB. + pub max_file_bytes: u64, + /// The largest total of all files read, in bytes. Default 256 MiB. + pub max_total_bytes: u64, + /// The most files read. Default 50,000. + pub max_files: usize, +} + +#[cfg(not(target_arch = "wasm32"))] +impl Default for SourceLimits { + fn default() -> Self { + Self { max_file_bytes: 8 << 20, max_total_bytes: 256 << 20, max_files: 50_000 } + } +} + +/// The first line of a `CACHEDIR.TAG` file, which marks a folder of generated files +/// (). Cargo writes one in each target folder. +#[cfg(not(target_arch = "wasm32"))] +const CACHEDIR_SIGNATURE: &[u8] = b"Signature: 8a477f597d28d172789f06886806bc55"; + +/// Whether `dir` holds a `CACHEDIR.TAG` regular file that starts with [`CACHEDIR_SIGNATURE`]. A marker +/// that is a symbolic link, a folder, a FIFO or another special file is not opened and does not count. +#[cfg(not(target_arch = "wasm32"))] +fn is_cache_dir(dir: &Path) -> bool { + use std::io::Read; + let marker = dir.join("CACHEDIR.TAG"); + if !std::fs::symlink_metadata(&marker).is_ok_and(|m| m.file_type().is_file()) { + return false; + } + let Ok(file) = std::fs::File::open(&marker) else { + return false; + }; + let mut head = Vec::with_capacity(CACHEDIR_SIGNATURE.len()); + file.take(CACHEDIR_SIGNATURE.len() as u64).read_to_end(&mut head).is_ok() && head == CACHEDIR_SIGNATURE +} + +/// Recursively collect all Rust source files under `dir`, excluding test files and test directories, +/// with the default [`SourceLimits`]. +/// +/// Excluded files: +/// * `tests.rs` +/// * `tests_*.rs` +/// * `*_tests.rs` +/// * `*_test.rs` +/// * `test.rs` +/// * Any file below `dir` within a directory named `tests`, `benches`, `examples` or `target`, or whose +/// name starts with `.`, or that holds a `CACHEDIR.TAG` regular file starting with the signature line +/// `Signature: 8a477f597d28d172789f06886806bc55` (a build output folder with another name). A +/// `CACHEDIR.TAG` that is a symbolic link or a special file such as a FIFO is not read, and its folder +/// is listed. +/// * Symbolic links (and, on Windows, junctions) to files or folders, which are not followed. +/// +/// Results are sorted lexicographically by file path for deterministic behavior across platforms. +#[cfg(not(target_arch = "wasm32"))] +pub fn rust_sources(dir: impl AsRef) -> std::io::Result> { + rust_sources_with_limits(dir, SourceLimits::default()) +} + +/// [`rust_sources`] with explicit size limits. A file over `max_file_bytes` returns an error of kind +/// [`std::io::ErrorKind::FileTooLarge`]; more than `max_files` files or `max_total_bytes` bytes in all +/// returns an error of kind [`std::io::ErrorKind::QuotaExceeded`]. +#[cfg(not(target_arch = "wasm32"))] +pub fn rust_sources_with_limits(dir: impl AsRef, limits: SourceLimits) -> std::io::Result> { + use std::io::{Error, ErrorKind, Read}; + + let root = dir.as_ref(); + let mut files = Vec::new(); + let mut total: u64 = 0; + let mut dirs = vec![root.to_path_buf()]; + + while let Some(d) = dirs.pop() { + for entry in std::fs::read_dir(&d)? { + let entry = entry?; + let p = entry.path(); + let kind = entry.file_type()?; + if kind.is_symlink() { + continue; + } + let name = entry.file_name(); + let name = name.to_string_lossy(); + if kind.is_dir() { + if name.starts_with('.') || name == "target" || name == "tests" || name == "benches" || name == "examples" || is_cache_dir(&p) { + continue; + } + dirs.push(p); + } else if kind.is_file() { + if !name.ends_with(".rs") { + continue; + } + if name == "tests.rs" || name.starts_with("tests_") || name.ends_with("_tests.rs") || name.ends_with("_test.rs") || name == "test.rs" + { + continue; + } + if files.len() >= limits.max_files { + return Err(Error::new( + ErrorKind::QuotaExceeded, + format!("more than {} Rust source files under {}", limits.max_files, root.display()), + )); + } + let over_file = || Error::new(ErrorKind::FileTooLarge, format!("{} is larger than {} bytes", p.display(), limits.max_file_bytes)); + if entry.metadata()?.len() > limits.max_file_bytes { + return Err(over_file()); + } + let mut content = String::new(); + std::fs::File::open(&p)?.take(limits.max_file_bytes.saturating_add(1)).read_to_string(&mut content)?; + let size = content.len() as u64; + if size > limits.max_file_bytes { + return Err(over_file()); + } + total = total.saturating_add(size); + if total > limits.max_total_bytes { + return Err(Error::new( + ErrorKind::QuotaExceeded, + format!("Rust sources under {} are larger than {} bytes in all", root.display(), limits.max_total_bytes), + )); + } + files.push((p.display().to_string(), content)); + } + } + } + + files.sort_by(|a, b| a.0.cmp(&b.0)); + Ok(files) +} + +#[cfg(test)] +mod tests { + use std::collections::BTreeSet; + + use super::{MAX_USE_PATH, MAX_USE_SEGMENT, lex_rust, use_leaves}; + + fn paths_and_names(source: &str) -> Vec<(Vec, Option)> { + use_leaves(&lex_rust(source), &BTreeSet::new()).into_iter().map(|leaf| (leaf.path, leaf.name.map(|name| name.to_string()))).collect() + } + + #[test] + fn use_leaves_keep_no_path_longer_than_the_limit() { + let source = format!("use {}{{x, y::z, *}};\nuse std::time::{{Instant, self as t}};\n", "a::".repeat(MAX_USE_PATH)); + let some = |name: &str| Some(name.to_string()); + let path = |segments: &[&str]| segments.iter().map(|s| s.to_string()).collect::>(); + assert_eq!( + paths_and_names(&source), + vec![ + (Vec::new(), some("x")), + (Vec::new(), some("z")), + (vec!["a".to_string(); MAX_USE_PATH], None), + (path(&["std", "time", "Instant"]), some("Instant")), + (path(&["std", "time"]), some("t")), + ] + ); + } + + #[test] + fn use_leaves_keep_no_segment_longer_than_the_limit() { + let long = "n".repeat(MAX_USE_SEGMENT + 1); + let fits = "n".repeat(MAX_USE_SEGMENT); + let source = format!("use {long}::{{self, x, *}};\nuse a::{long};\nuse {fits}::{{self as b, *}};\n"); + assert_eq!( + paths_and_names(&source), + vec![ + (Vec::new(), Some(long.clone())), + (Vec::new(), Some("x".to_string())), + (Vec::new(), Some(long)), + (vec![fits.clone()], Some("b".to_string())), + (vec![fits], None), + ] + ); + } +} diff --git a/crates/testkit/src/hostile.rs b/crates/testkit/src/hostile.rs new file mode 100644 index 000000000..4480d6aaf --- /dev/null +++ b/crates/testkit/src/hostile.rs @@ -0,0 +1,817 @@ +//! Hostile inputs made from a valid seed, for tests that a parser fails cleanly on damaged data. +//! +//! [`mutations`] yields a fixed sequence of damaged copies of a seed: each round applies one to +//! four edits (a bit flip, a random byte, a 1, 2, 4 or 8-byte overwrite with an extreme value in +//! either byte order, a deletion of up to 16 bytes, or a truncation) and labels the result with +//! the edits it made. The sequence depends only on the seed's length and the number of rounds, so +//! a failing case can be found again from its label. It builds for every target, `wasm32` +//! included. +//! +//! [`hammer`] runs a parser on the unmodified seed, on every truncation of it and on the +//! mutations, one case after another on a worker thread, with a time limit for each case. It fails +//! the test, naming the case and the input length, when the parser panics or does not finish in +//! time. The number of rounds is a count the caller gives, multiplied by a variable the caller +//! names. [`hammer`] and [`HammerConfig`] are not built for `wasm32`. + +use std::fmt::Write as _; + +#[cfg(not(target_arch = "wasm32"))] +use crate::deadline::{DeadlineLimit, Resolved, resolve}; +#[cfg(not(target_arch = "wasm32"))] +use std::any::Any; +#[cfg(not(target_arch = "wasm32"))] +use std::ffi::OsString; +#[cfg(not(target_arch = "wasm32"))] +use std::fmt; +#[cfg(not(target_arch = "wasm32"))] +use std::panic::{AssertUnwindSafe, catch_unwind}; +#[cfg(not(target_arch = "wasm32"))] +use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError}; +#[cfg(not(target_arch = "wasm32"))] +use std::thread; +#[cfg(not(target_arch = "wasm32"))] +use std::time::{Duration, Instant}; + +/// The name of the thread [`hammer`] runs its cases on. +#[cfg(not(target_arch = "wasm32"))] +const WORKER_NAME: &str = "craft-testkit hostile"; + +/// The variable that multiplies a [`HammerConfig`]'s rounds unless +/// [`HammerConfig::with_multiplier_variable`] names another. +pub const DEFAULT_MULTIPLIER_VARIABLE: &str = "CRAFT_HOSTILE_MULT"; + +/// The default limit for one case of [`hammer`]. The limit's variable, `CRAFT_TEST_TIMEOUT_SECS` +/// unless [`HammerConfig::with_timeout`] names another, replaces it. +#[cfg(not(target_arch = "wasm32"))] +pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(5); + +/// The most cases one [`hammer`] call runs: the unmodified seed, its truncations and the rounds +/// together. A call that would run more fails before it runs any. +pub const MAX_CASES: usize = 10_000_000; + +/// The values the overwrite edits write, in 1, 2, 4 or 8 bytes. +const EXTREMES: [u64; 8] = [0, 1, 0x7F, 0x80, 0xFF, 0xFFFF, 0x8000_0000, u64::MAX]; + +/// The xorshift64* generator behind [`mutations`]. +/// +/// ``` +/// use craft_testkit::hostile::Rng; +/// +/// let mut rng = Rng(1); +/// assert_eq!(rng.next_u64(), 0x47E4_CE4B_896C_DD1D); +/// ``` +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Rng(pub u64); + +impl Rng { + /// The next value. + pub fn next_u64(&mut self) -> u64 { + self.0 ^= self.0 >> 12; + self.0 ^= self.0 << 25; + self.0 ^= self.0 >> 27; + self.0.wrapping_mul(0x2545_F491_4F6C_DD1D) + } + + /// A value below `n`, or 0 when `n` is 0. + pub fn below(&mut self, n: usize) -> usize { + (self.next_u64() % n.max(1) as u64) as usize + } +} + +/// One damaged copy of a seed, from [`mutations`]. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Mutation { + /// The damaged bytes. + pub bytes: Vec, + /// The edits made, in order, separated by spaces, such as `flip[3].5 del[7..9]`. + pub label: String, +} + +/// `rounds` damaged copies of `seed`, in a sequence fixed by the seed's length and `rounds`. +/// +/// An empty seed yields no copies, since there is no byte to edit. +/// +/// ``` +/// use craft_testkit::hostile::mutations; +/// +/// let all: Vec<_> = mutations(&[0, 1, 2, 3], 2).collect(); +/// assert_eq!(all[1].label, "trunc2"); +/// assert_eq!(all[1].bytes, [0, 1]); +/// ``` +pub fn mutations(seed: &[u8], rounds: usize) -> Mutations<'_> { + Mutations { seed, rng: Rng((0x5EED_F00D ^ seed.len() as u64) | 1), remaining: if seed.is_empty() { 0 } else { rounds } } +} + +/// The iterator [`mutations`] returns. +#[derive(Clone, Debug)] +pub struct Mutations<'a> { + seed: &'a [u8], + rng: Rng, + remaining: usize, +} + +impl Iterator for Mutations<'_> { + type Item = Mutation; + + fn next(&mut self) -> Option { + if self.remaining == 0 { + return None; + } + self.remaining -= 1; + let rng = &mut self.rng; + let mut d = self.seed.to_vec(); + let mut label = String::new(); + for _ in 0..1 + rng.below(4) { + if d.is_empty() { + break; + } + if !label.is_empty() { + label.push(' '); + } + let pos = rng.below(d.len()); + match rng.below(7) { + 0 => { + let b = rng.below(8); + if let Some(byte) = d.get_mut(pos) { + *byte ^= 1 << b; + } + let _ = write!(label, "flip[{pos}].{b}"); + } + 1 => { + let value = rng.next_u64() as u8; + if let Some(byte) = d.get_mut(pos) { + *byte = value; + } + let _ = write!(label, "set[{pos}]={value:#x}"); + } + 2..=4 => { + let w = [1usize, 2, 4, 8][rng.below(4)]; + let v = EXTREMES[rng.below(EXTREMES.len())]; + let be = rng.below(2) == 0; + let all = if be { v.to_be_bytes() } else { v.to_le_bytes() }; + let bytes = if be { &all[8 - w..] } else { &all[..w] }; + let end = (pos + w).min(d.len()); + if let (Some(target), Some(source)) = (d.get_mut(pos..end), bytes.get(..end - pos)) { + target.copy_from_slice(source); + } + let _ = write!(label, "put{w}{}[{pos}]={v:#x}", if be { "be" } else { "le" }); + } + 5 => { + let n = 1 + rng.below(16); + let end = (pos + n).min(d.len()); + d.drain(pos..end); + let _ = write!(label, "del[{pos}..{end}]"); + } + _ => { + d.truncate(pos); + let _ = write!(label, "trunc{pos}"); + } + } + } + Some(Mutation { bytes: d, label }) + } + + fn size_hint(&self) -> (usize, Option) { + (self.remaining, Some(self.remaining)) + } +} + +impl ExactSizeIterator for Mutations<'_> {} + +/// The settings of one [`hammer`] call: a label for its failures, the number of rounds, the +/// variable that multiplies them, and the limit for each case. +/// +/// ``` +/// use std::time::Duration; +/// use craft_testkit::hostile::HammerConfig; +/// +/// // 2,000 rounds, times CRAFT_HOSTILE_MULT when it is set; 5 s per case, or CRAFT_TEST_TIMEOUT_SECS. +/// let config = HammerConfig::new("png seed 3", 2_000); +/// +/// // The app's own variables instead. +/// let config = HammerConfig::new("mxf primer", 5_000) +/// .with_multiplier_variable("CRAFT_MEDIA_HOSTILE_MULT") +/// .with_timeout(craft_testkit::DeadlineLimit::new(Duration::from_secs(5)).with_variable("FILMCRAFT_HOSTILE_TIMEOUT_SECS")); +/// assert_eq!(config.multiplier_variable(), "CRAFT_MEDIA_HOSTILE_MULT"); +/// ``` +#[cfg(not(target_arch = "wasm32"))] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct HammerConfig<'a> { + label: &'a str, + iterations: usize, + multiplier_variable: &'a str, + timeout: DeadlineLimit<'a>, +} + +#[cfg(not(target_arch = "wasm32"))] +impl<'a> HammerConfig<'a> { + /// `iterations` rounds of mutations under `label`, multiplied by [`DEFAULT_MULTIPLIER_VARIABLE`], + /// with a limit of [`DEFAULT_TIMEOUT`] for each case. + pub fn new(label: &'a str, iterations: usize) -> Self { + Self { label, iterations, multiplier_variable: DEFAULT_MULTIPLIER_VARIABLE, timeout: DeadlineLimit::new(DEFAULT_TIMEOUT) } + } + + /// The same settings, with the rounds multiplied by the variable `name` instead. + pub fn with_multiplier_variable(self, name: &'a str) -> Self { + Self { multiplier_variable: name, ..self } + } + + /// The same settings, with `limit` for each case. + pub fn with_timeout(self, limit: impl Into>) -> Self { + Self { timeout: limit.into(), ..self } + } + + /// The label that failures start with. + pub fn label(&self) -> &'a str { + self.label + } + + /// The rounds before the multiplier. + pub fn iterations(&self) -> usize { + self.iterations + } + + /// The name of the variable that multiplies the rounds. + pub fn multiplier_variable(&self) -> &'a str { + self.multiplier_variable + } + + /// The limit for each case. + pub fn timeout(&self) -> DeadlineLimit<'a> { + self.timeout + } + + /// The rounds after reading the multiplier variable, or why they cannot be used. + /// + /// When the variable is not set, the multiplier is 1. A value must be a whole number above + /// zero written in decimal digits. + pub fn rounds(&self) -> Result { + self.rounds_with(read_variable) + } + + /// [`Self::rounds`] with the variable lookup passed in. + fn rounds_with(&self, lookup: impl FnOnce(&str) -> Option) -> Result { + let variable = self.multiplier_variable.to_owned(); + if variable.is_empty() || variable.contains(['=', '\0']) { + return Err(HammerError::BadName { variable }); + } + let multiplier = match lookup(&variable) { + None => 1, + Some(raw) => { + let value = raw.into_string().map_err(|_| HammerError::NotUnicode { variable: variable.clone() })?; + parse_multiplier(&value).map_err(|reason| HammerError::BadValue { variable: variable.clone(), value, reason })? + } + }; + self.iterations.checked_mul(multiplier).ok_or(HammerError::Overflow { iterations: self.iterations, variable, multiplier }) + } +} + +/// Why a [`hammer`] call cannot run. +#[cfg(not(target_arch = "wasm32"))] +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum HammerError { + /// The multiplier variable's name is empty or holds `=` or a NUL character. + BadName { + /// The name. + variable: String, + }, + /// The multiplier variable's value is not Unicode. + NotUnicode { + /// The variable's name. + variable: String, + }, + /// The multiplier variable's value is not a whole number above zero. + BadValue { + /// The variable's name. + variable: String, + /// The value. + value: String, + /// What is wrong with the value. + reason: &'static str, + }, + /// The rounds times the multiplier is more than a `usize` holds. + Overflow { + /// The rounds before the multiplier. + iterations: usize, + /// The variable's name. + variable: String, + /// The multiplier. + multiplier: usize, + }, + /// The call would run more than [`MAX_CASES`] cases. + TooManyCases { + /// The cases the call would run. + cases: u128, + }, +} + +#[cfg(not(target_arch = "wasm32"))] +impl fmt::Display for HammerError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::BadName { variable } => write!(f, "{variable:?} cannot name a variable: it is empty or holds `=` or a NUL character"), + Self::NotUnicode { variable } => write!(f, "{variable} is not valid Unicode; set it to a whole number above zero"), + Self::BadValue { variable, value, reason } => write!(f, "{variable}={value:?} {reason}; set it to a whole number above zero"), + Self::Overflow { iterations, variable, multiplier } => { + write!(f, "{iterations} rounds times {variable}={multiplier} is more than a usize holds") + } + Self::TooManyCases { cases } => write!(f, "{cases} cases is more than the limit of {MAX_CASES}"), + } + } +} + +#[cfg(not(target_arch = "wasm32"))] +impl std::error::Error for HammerError {} + +/// Parses a whole number above zero, written as decimal digits only. +#[cfg(not(target_arch = "wasm32"))] +fn parse_multiplier(value: &str) -> Result { + if value.is_empty() { + return Err("is empty"); + } + if !value.bytes().all(|b| b.is_ascii_digit()) { + return Err("is not a whole number written in digits"); + } + match value.parse::() { + Ok(0) => Err("is zero"), + Ok(n) => Ok(n), + Err(_) => Err("is more than a usize holds"), + } +} + +/// Reads the variable `name` from the process environment. +#[cfg(not(target_arch = "wasm32"))] +fn read_variable(name: &str) -> Option { + std::env::var_os(name) +} + +/// Runs `parse` on `seed`, on every truncation of it (lengths 0 to one less than its length), and +/// on [`mutations`] of it for the config's rounds, failing the calling test on the first case +/// that panics or does not finish within the config's limit. +/// +/// The cases run one after another on one worker thread, while the calling thread fails the test +/// when a case runs past the config's limit. A case that finishes after the limit also fails it. A +/// failure starts with the config's label and names the case (`the unmodified seed`, +/// `truncation to N` or the mutation's edits), the input's length and the panic's message. A case +/// that does not finish fails the test while the worker thread is left running, so `parse` must be +/// owned and able to move to another thread. The multiplier and limit variables are read before +/// any case runs; a value that cannot be used, or a call that would run more than [`MAX_CASES`] +/// cases, fails the test before `parse` is called. +/// +/// ``` +/// use craft_testkit::hostile::{HammerConfig, hammer}; +/// +/// hammer(&HammerConfig::new("length prefix", 200), &[3, b'a', b'b', b'c'], |data| { +/// if let Some((&n, rest)) = data.split_first() { +/// let _ = rest.get(..usize::from(n)); +/// } +/// }); +/// ``` +// Failing the calling test is what this function is for. +#[cfg(not(target_arch = "wasm32"))] +#[allow(clippy::panic)] +#[track_caller] +pub fn hammer(config: &HammerConfig<'_>, seed: &[u8], parse: F) +where + F: Fn(&[u8]) + Send + Sync + 'static, +{ + if let Err(message) = hammer_with(config, seed, parse, read_variable) { + panic!("{message}"); + } +} + +/// [`hammer`] with the variable lookup passed in, returning the failure's message. +#[cfg(not(target_arch = "wasm32"))] +fn hammer_with(config: &HammerConfig<'_>, seed: &[u8], parse: F, lookup: impl Fn(&str) -> Option) -> Result<(), String> +where + F: Fn(&[u8]) + Send + Sync + 'static, +{ + let label = config.label; + let rounds = config.rounds_with(&lookup).map_err(|error| format!("{label}: {error}"))?; + let rounds = if seed.is_empty() { 0 } else { rounds }; + let cases = 1 + seed.len() as u128 + rounds as u128; + if cases > MAX_CASES as u128 { + return Err(format!("{label}: {}", HammerError::TooManyCases { cases })); + } + let start = Instant::now(); + let limit = resolve(&config.timeout, &lookup, start).map(Limit::from).map_err(|error| format!("{label}: {error}"))?; + let clock: Clock = Arc::new(move || start.elapsed()); + run_cases(label.to_owned(), limit, Arc::from(seed), rounds, parse, clock) +} + +/// A clock that returns the time since a [`hammer`] call started. +#[cfg(not(target_arch = "wasm32"))] +type Clock = Arc Duration + Send + Sync>; + +/// The limit for each case, and how failures describe it. +#[cfg(not(target_arch = "wasm32"))] +#[derive(Clone, Debug)] +struct Limit { + limit: Duration, + text: String, +} + +#[cfg(not(target_arch = "wasm32"))] +impl From> for Limit { + fn from(resolved: Resolved<'_>) -> Self { + Self { limit: resolved.limit, text: resolved.to_string() } + } +} + +/// The case the worker is running, shared with the calling thread that watches it. +#[cfg(not(target_arch = "wasm32"))] +struct Progress { + /// The case's description: `the unmodified seed`, `truncation to N` or the mutation's edits. + case: String, + /// The length of the case's input. + len: usize, + /// The clock reading when the case started. + started: Duration, + /// Whether the case is still running. + running: bool, + /// Set when the worker stops: `Ok` after the last case, or the failure's message. + outcome: Option>, +} + +/// [`Progress`] under a lock, with a signal for when the worker stops. +#[cfg(not(target_arch = "wasm32"))] +struct Watch { + state: Mutex, + stopped: Condvar, +} + +#[cfg(not(target_arch = "wasm32"))] +impl Watch { + fn lock(&self) -> MutexGuard<'_, Progress> { + self.state.lock().unwrap_or_else(PoisonError::into_inner) + } +} + +/// Hands the worker's outcome to the calling thread when the worker stops, including when it +/// unwinds outside a case. +#[cfg(not(target_arch = "wasm32"))] +struct Report { + watch: Arc, + outcome: Result<(), String>, +} + +#[cfg(not(target_arch = "wasm32"))] +impl Drop for Report { + fn drop(&mut self) { + let mut state = self.watch.lock(); + state.outcome = Some(std::mem::replace(&mut self.outcome, Ok(()))); + self.watch.stopped.notify_all(); + } +} + +/// Starts the worker that runs every case, then waits for it, failing when a case runs past the +/// limit. +#[cfg(not(target_arch = "wasm32"))] +fn run_cases(label: String, limit: Limit, seed: Arc<[u8]>, rounds: usize, parse: F, clock: Clock) -> Result<(), String> +where + F: Fn(&[u8]) + Send + Sync + 'static, +{ + // No case counts as running until the worker starts it, so a worker that starts late is not + // reported as a case past its limit. + let first = Progress { case: "the unmodified seed".to_owned(), len: seed.len(), started: clock(), running: false, outcome: None }; + let watch = Arc::new(Watch { state: Mutex::new(first), stopped: Condvar::new() }); + let (worker_watch, worker_clock, worker_label, worker_limit) = (Arc::clone(&watch), Arc::clone(&clock), label.clone(), limit.clone()); + thread::Builder::new() + .name(WORKER_NAME.to_owned()) + .spawn(move || { + let mut report = Report { watch: worker_watch, outcome: Err(format!("{worker_label}: the worker thread stopped without a result")) }; + report.outcome = run_all(&report.watch, &worker_clock, &worker_label, &worker_limit, &seed, rounds, &parse); + }) + .map_err(|error| format!("{label}: could not start the worker thread: {error}"))?; + let mut state = watch.lock(); + loop { + if let Some(outcome) = state.outcome.take() { + return outcome; + } + let deadline = state.started.saturating_add(limit.limit); + let now = clock(); + if state.running && now >= deadline { + return Err(format!("{label}: {} (len {}) did not finish within {}; its thread is left running", state.case, state.len, limit.text)); + } + let wait = if state.running { deadline.saturating_sub(now) } else { limit.limit }; + state = watch.stopped.wait_timeout(state, wait).unwrap_or_else(PoisonError::into_inner).0; + } +} + +/// Runs the unmodified seed, its truncations and its mutations in order, stopping at the first +/// failure. +#[cfg(not(target_arch = "wasm32"))] +fn run_all(watch: &Watch, clock: &Clock, label: &str, limit: &Limit, seed: &[u8], rounds: usize, parse: &F) -> Result<(), String> { + for n in std::iter::once(seed.len()).chain(0..seed.len()) { + let case = if n == seed.len() { "the unmodified seed".to_owned() } else { format!("truncation to {n}") }; + run_one(watch, clock, label, limit, case, seed.get(..n).unwrap_or_default(), parse)?; + } + for mutation in mutations(seed, rounds) { + run_one(watch, clock, label, limit, mutation.label, &mutation.bytes, parse)?; + } + Ok(()) +} + +/// Runs one case, recording it in `watch` while it runs, and returns the failure's message when it +/// panics or finishes after the limit. +#[cfg(not(target_arch = "wasm32"))] +fn run_one(watch: &Watch, clock: &Clock, label: &str, limit: &Limit, case: String, data: &[u8], parse: &F) -> Result<(), String> { + let started = clock(); + { + let mut state = watch.lock(); + (state.case, state.len, state.started, state.running) = (case, data.len(), started, true); + } + let outcome = catch_unwind(AssertUnwindSafe(|| parse(data))); + let spent = clock().saturating_sub(started); + // The payload is dropped before the lock is taken and while the case still counts as running, so + // the watchdog still reports a payload whose drop does not return. + let panicked = outcome.err().map(|payload| panic_text(payload.as_ref())); + let mut state = watch.lock(); + state.running = false; + let name = || format!("{label}: {} (len {})", state.case, state.len); + if spent > limit.limit { + let panicked = panicked.map_or_else(String::new, |text| format!(" (it panicked: {text})")); + return Err(format!("{} finished only after its limit of {}{panicked}", name(), limit.text)); + } + panicked.map_or(Ok(()), |text| Err(format!("{}: the parser panicked: {text}", name()))) +} + +/// The text of a panic payload. +#[cfg(not(target_arch = "wasm32"))] +fn panic_text(payload: &(dyn Any + Send)) -> String { + payload + .downcast_ref::() + .cloned() + .or_else(|| payload.downcast_ref::<&str>().map(|text| (*text).to_owned())) + .unwrap_or_else(|| "a payload that is not a message".to_owned()) +} + +#[cfg(test)] +#[cfg(not(target_arch = "wasm32"))] +mod tests { + use super::*; + use std::collections::BTreeMap; + use std::sync::Mutex; + use std::sync::mpsc; + + /// A variable no test sets, so the environment cannot change a test's rounds or limits. + const UNSET: &str = "CRAFT_TESTKIT_HOSTILE_UNSET_IN_TESTS"; + + fn config(label: &'static str, iterations: usize) -> HammerConfig<'static> { + HammerConfig::new(label, iterations) + .with_multiplier_variable(UNSET) + .with_timeout(DeadlineLimit::new(Duration::from_secs(600)).with_variable(UNSET)) + } + + fn unset(_: &str) -> Option { + None + } + + /// Runs `hammer_with` with no variables set and returns the inputs `parse` saw, in order. + fn inputs(config: &HammerConfig<'_>, seed: &[u8]) -> Vec> { + let seen = Arc::new(Mutex::new(Vec::new())); + let record = Arc::clone(&seen); + hammer_with(config, seed, move |d| record.lock().unwrap().push(d.to_vec()), unset).unwrap(); + seen.lock().unwrap().clone() + } + + #[test] + fn reports_truncation_panic_with_case_and_payload() { + let result = hammer_with(&config("demo seed 2", 0), &[9, 8, 7, 6], |d| assert!(d.len() != 2, "boom at {}", d.len()), unset); + let message = result.unwrap_err(); + assert!(message.starts_with("demo seed 2: truncation to 2 (len 2): the parser panicked: "), "{message}"); + assert!(message.contains("boom at 2"), "{message}"); + } + + #[test] + fn a_panic_without_a_message_is_reported() { + let message = hammer_with(&config("demo", 0), &[1], |_| std::panic::panic_any(7u8), unset).unwrap_err(); + assert_eq!(message, "demo: the unmodified seed (len 1): the parser panicked: a payload that is not a message"); + } + + #[test] + fn deadline_names_stuck_case() { + let (release, wait) = mpsc::channel::<()>(); + let wait = Mutex::new(wait); + let short = config("stuck demo", 0).with_timeout(DeadlineLimit::new(Duration::from_millis(50)).with_variable(UNSET)); + let result = hammer_with( + &short, + &[1, 2, 3], + move |d| { + if d.len() == 3 { + let _ = wait.lock().unwrap().recv(); + } + }, + unset, + ); + release.send(()).unwrap(); + let message = result.unwrap_err(); + assert!(message.contains("stuck demo: the unmodified seed (len 3) did not finish within 50ms"), "{message}"); + } + + #[test] + fn a_case_that_finishes_after_the_limit_fails() { + let readings = Arc::new(Mutex::new(vec![Duration::from_secs(6), Duration::ZERO])); + let clock: Clock = Arc::new(move || readings.lock().unwrap().pop().unwrap_or(Duration::MAX)); + let watch = Watch { + state: Mutex::new(Progress { case: String::new(), len: 0, started: Duration::ZERO, running: true, outcome: None }), + stopped: Condvar::new(), + }; + let limit = Limit { limit: Duration::from_secs(5), text: "5s".to_owned() }; + let message = run_one(&watch, &clock, "late", &limit, "the unmodified seed".to_owned(), &[1], &|_: &[u8]| {}).unwrap_err(); + assert_eq!(message, "late: the unmodified seed (len 1) finished only after its limit of 5s"); + assert!(!watch.lock().running); + } + + /// A panic payload that records, when it is dropped, whether the progress lock was free and + /// whether the case still counted as running. + struct DropProbe { + watch: Arc, + seen: Arc>>>, + } + + impl Drop for DropProbe { + fn drop(&mut self) { + let running = self.watch.state.try_lock().ok().map(|state| state.running); + *self.seen.lock().unwrap() = Some(running); + } + } + + #[test] + fn a_panic_payload_is_dropped_outside_the_lock_while_the_case_runs() { + let watch = Arc::new(Watch { + state: Mutex::new(Progress { case: String::new(), len: 0, started: Duration::ZERO, running: false, outcome: None }), + stopped: Condvar::new(), + }); + let seen = Arc::new(Mutex::new(None)); + let clock: Clock = Arc::new(|| Duration::ZERO); + let limit = Limit { limit: Duration::from_secs(5), text: "5s".to_owned() }; + let probe = (Arc::clone(&watch), Arc::clone(&seen)); + let parse = move |_: &[u8]| std::panic::panic_any(DropProbe { watch: Arc::clone(&probe.0), seen: Arc::clone(&probe.1) }); + let message = run_one(&watch, &clock, "probe", &limit, "the unmodified seed".to_owned(), &[1], &parse).unwrap_err(); + assert_eq!(message, "probe: the unmodified seed (len 1): the parser panicked: a payload that is not a message"); + assert_eq!(*seen.lock().unwrap(), Some(Some(true)), "the payload was dropped with the lock held or the case stopped"); + assert!(!watch.lock().running); + } + + #[test] + fn a_worker_that_starts_late_is_not_a_case_past_its_limit() { + // The worker's first clock reading waits until the calling thread has checked the limit once, + // and that check reads an hour after the start; every other reading is zero. + let (checked, worker_waits) = mpsc::channel::<()>(); + let (checked, worker_waits) = (Mutex::new(Some(checked)), Mutex::new(worker_waits)); + let caller_reads = Mutex::new(0usize); + let clock: Clock = Arc::new(move || { + if thread::current().name() == Some(WORKER_NAME) { + let _ = worker_waits.lock().unwrap().recv(); + return Duration::ZERO; + } + let mut reads = caller_reads.lock().unwrap(); + *reads += 1; + if *reads == 2 { + drop(checked.lock().unwrap().take()); + return Duration::from_secs(3600); + } + Duration::ZERO + }); + let limit = Limit { limit: Duration::from_secs(5), text: "5s".to_owned() }; + assert_eq!(run_cases("late start".to_owned(), limit, Arc::from(&[1u8][..]), 0, |_: &[u8]| {}, clock), Ok(())); + } + + #[test] + fn every_case_runs_on_one_worker_thread() { + let threads = Arc::new(Mutex::new(std::collections::BTreeSet::new())); + let record = Arc::clone(&threads); + let parse = move |_: &[u8]| { + record.lock().unwrap().insert(format!("{:?}", thread::current().id())); + }; + hammer_with(&config("one thread", 50), &[1, 2, 3, 4], parse, unset).unwrap(); + let threads = threads.lock().unwrap(); + assert_eq!(threads.len(), 1, "{threads:?}"); + assert!(!threads.contains(&format!("{:?}", thread::current().id()))); + } + + #[test] + fn invalid_limit_variable_fails_before_any_parse() { + let called = Arc::new(Mutex::new(false)); + let flag = Arc::clone(&called); + let config = config("bad limit", 3).with_timeout(DeadlineLimit::new(Duration::from_secs(5)).with_variable("MY_LIMIT")); + let lookup = |name: &str| (name == "MY_LIMIT").then(|| OsString::from("soon")); + let message = hammer_with(&config, &[1, 2], move |_| *flag.lock().unwrap() = true, lookup).unwrap_err(); + assert_eq!( + message, + r#"bad limit: MY_LIMIT="soon" is not a whole number of seconds written in digits; set it to a whole number of seconds above zero"# + ); + assert!(!*called.lock().unwrap()); + } + + #[test] + fn mutation_sequence_matches_source() { + let all: Vec<_> = mutations(&[0, 1, 2, 3], 4).collect(); + let bytes: Vec<&[u8]> = all.iter().map(|m| m.bytes.as_slice()).collect(); + let labels: Vec<&str> = all.iter().map(|m| m.label.as_str()).collect(); + assert_eq!(bytes, [&[0u8, 1, 2, 0][..], &[0, 1][..], &[0, 1, 0, 0][..], &[0, 0, 0, 1][..]]); + assert_eq!(labels, ["put1be[3]=0x0", "trunc2", "put4le[1]=0x1", "put4be[0]=0x1"]); + let mut expected: Vec> = vec![vec![0, 1, 2, 3], vec![], vec![0], vec![0, 1], vec![0, 1, 2]]; + expected.extend(all.into_iter().map(|m| m.bytes)); + assert_eq!(inputs(&config("sequence", 4), &[0, 1, 2, 3]), expected); + } + + #[test] + fn mutation_labels_join_edits_with_spaces() { + let seed: Vec = (0..64).collect(); + let joined = mutations(&seed, 200).find(|m| m.label.contains(' ')).unwrap(); + assert!(!joined.label.starts_with(' ') && !joined.label.ends_with(' ') && !joined.label.contains(" "), "{:?}", joined.label); + } + + #[test] + fn uses_caller_multiplier_variable() { + let config = config("mult", 7).with_multiplier_variable("CRAFT_MEDIA_HOSTILE_MULT"); + let lookup = |name: &str| match name { + "CRAFT_MEDIA_HOSTILE_MULT" => Some(OsString::from("3")), + DEFAULT_MULTIPLIER_VARIABLE => Some(OsString::from("100")), + _ => None, + }; + assert_eq!(config.rounds_with(lookup), Ok(21)); + let calls = Arc::new(Mutex::new(0usize)); + let count = Arc::clone(&calls); + hammer_with(&config, &[5, 6], move |_| *count.lock().unwrap() += 1, lookup).unwrap(); + // The seed, two truncations and 21 rounds. + assert_eq!(*calls.lock().unwrap(), 24); + assert_eq!(HammerConfig::new("default", 7).multiplier_variable(), "CRAFT_HOSTILE_MULT"); + assert_eq!(config.rounds_with(unset), Ok(7)); + } + + #[test] + fn invalid_multiplier_names_variable() { + for (value, reason) in [ + ("0", "is zero"), + ("", "is empty"), + ("ten", "is not a whole number written in digits"), + ("-2", "is not a whole number written in digits"), + ("1.5", "is not a whole number written in digits"), + ("99999999999999999999999999", "is more than a usize holds"), + ] { + let called = Arc::new(Mutex::new(false)); + let flag = Arc::clone(&called); + let message = hammer_with( + &config("bad mult", 3).with_multiplier_variable("MY_MULT"), + &[1, 2], + move |_| *flag.lock().unwrap() = true, + |_| Some(OsString::from(value)), + ) + .unwrap_err(); + assert_eq!(message, format!("bad mult: MY_MULT={value:?} {reason}; set it to a whole number above zero")); + assert!(!*called.lock().unwrap(), "parse ran before the bad value {value:?} was reported"); + } + for name in ["", "A=B", "A\0B"] { + assert_eq!(config("x", 1).with_multiplier_variable(name).rounds_with(unset), Err(HammerError::BadName { variable: name.to_owned() })); + } + } + + #[test] + fn empty_seed_runs_once() { + assert_eq!(inputs(&config("empty", 1_000), &[]), [Vec::::new()]); + } + + #[test] + fn large_seed_checks_every_prefix() { + let seed: Vec = (0..10_000u32).map(|i| i as u8).collect(); + let mut lengths = BTreeMap::new(); + for input in inputs(&config("large", 0), &seed) { + assert_eq!(input, seed[..input.len()]); + *lengths.entry(input.len()).or_insert(0usize) += 1; + } + assert_eq!(lengths.len(), 10_001); + assert!(lengths.values().all(|&n| n == 1), "a length ran more than once"); + } + + #[test] + fn iteration_product_overflow_is_error() { + let config = config("overflow", usize::MAX / 2 + 1); + let error = config.rounds_with(|_| Some(OsString::from("2"))).unwrap_err(); + assert_eq!(error, HammerError::Overflow { iterations: usize::MAX / 2 + 1, variable: UNSET.to_owned(), multiplier: 2 }); + let message = hammer_with(&config, &[1], |_| {}, |_| Some(OsString::from("2"))).unwrap_err(); + assert!(message.starts_with("overflow: ") && message.ends_with("is more than a usize holds"), "{message}"); + } + + #[test] + fn too_many_cases_fail_before_any_parse() { + let called = Arc::new(Mutex::new(false)); + let flag = Arc::clone(&called); + // The seed and its two truncations make three cases more. + let message = hammer_with(&config("budget", MAX_CASES - 2), &[1, 2], move |_| *flag.lock().unwrap() = true, unset).unwrap_err(); + assert_eq!(message, format!("budget: {} cases is more than the limit of {MAX_CASES}", MAX_CASES + 1)); + assert!(!*called.lock().unwrap()); + } + + #[test] + fn mutations_are_deterministic_and_count_rounds() { + let seed = b"a seed of some length"; + let first: Vec<_> = mutations(seed, 50).collect(); + assert_eq!(first.len(), 50); + assert_eq!(first, mutations(seed, 50).collect::>()); + assert_eq!(mutations(seed, 50).len(), 50); + assert_eq!(mutations(&[], 50).count(), 0); + } +} diff --git a/crates/testkit/src/lib.rs b/crates/testkit/src/lib.rs new file mode 100644 index 000000000..020938371 --- /dev/null +++ b/crates/testkit/src/lib.rs @@ -0,0 +1,48 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] + +//! Source-level platform regression guards and verification utilities for the Craft application suite. +//! +//! This crate provides non-panicking source analysis guards that detect known platform hazards +//! and lifecycle regressions across Craft applications: +//! +//! * [`guards::predefined_quit`]: flags direct calls to `muda`'s predefined Quit item that bypass +//! dirty document save prompts and window teardown on macOS. +//! * [`guards::wgsl_float_literals`]: flags WGSL float literals above the finite range of `f32` +//! (or of `f16` for an `h` suffix), which browser WebGPU compilers reject. +//! * [`guards::std_instant`]: flags uses of `std::time::Instant` in code paths targeting the web, +//! where `std::time::Instant::now()` panics on `wasm32`. +//! * [`guards::rust_sources`]: directory crawler collecting `.rs` sources while excluding tests, with +//! size limits ([`guards::rust_sources_with_limits`]). It is not built for `wasm32`. +//! +//! On targets other than `wasm32`, [`deadline::with_deadline`] runs a test body on its own thread +//! and fails the test when the body does not finish within its limit, and [`deadline::wait_until`] +//! polls a condition until it holds and fails the test when the limit passes first. Both read an +//! optional limit variable that the caller names, `CRAFT_TEST_TIMEOUT_SECS` by default. +//! +//! [`hostile::mutations`] yields a fixed sequence of damaged copies of a valid seed, on every +//! target. On targets other than `wasm32`, [`hostile::hammer`] runs a parser on the seed, every +//! truncation of it and those copies, one after another on a worker thread with a time limit for +//! each, and fails the test with the case that panicked or did not finish. +//! +//! ### Epistemic Scope: What Source Guards Establish vs What They Cannot +//! +//! Source-level guards provide deterministic syntactic evidence about codebase structure. +//! They establish that checked source files do not contain forbidden symbol names, known invalid +//! literal patterns, or unsafe platform invocations. +//! +//! They do **not** establish runtime correctness, protocol concurrency safety, or platform behavior +//! under foreign host environments. They are lightweight syntactic regression gates designed to complement, +//! rather than replace, platform integration tests and CI execution. + +#[cfg(not(target_arch = "wasm32"))] +pub mod deadline; +pub mod guards; +pub mod hostile; + +#[cfg(not(target_arch = "wasm32"))] +pub use deadline::{DeadlineLimit, wait_until, with_deadline}; +pub use guards::{AsFileSource, Finding, predefined_quit, std_instant, wgsl_float_literals, wgsl_float_literals_in}; + +#[cfg(not(target_arch = "wasm32"))] +pub use guards::{SourceLimits, rust_sources, rust_sources_with_limits}; diff --git a/crates/testkit/tests/guards.rs b/crates/testkit/tests/guards.rs new file mode 100644 index 000000000..64f54bfc5 --- /dev/null +++ b/crates/testkit/tests/guards.rs @@ -0,0 +1,1846 @@ +use craft_testkit::{Finding, predefined_quit, std_instant, wgsl_float_literals, wgsl_float_literals_in}; + +/// A scratch folder under the system temporary folder that this test made and removes on drop, +/// also when the test panics. A folder that already exists is never emptied or reused. +#[cfg(not(target_arch = "wasm32"))] +struct ScratchDir(std::path::PathBuf); + +#[cfg(not(target_arch = "wasm32"))] +impl ScratchDir { + fn new(tag: &str) -> Self { + let base = std::env::temp_dir(); + for n in 0..1000u32 { + let path = base.join(format!("craft_testkit_{tag}_{}_{n}", std::process::id())); + match std::fs::create_dir(&path) { + Ok(()) => return Self(path), + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue, + Err(e) => panic!("create scratch folder {}: {e}", path.display()), + } + } + panic!("no free scratch folder name for {tag}"); + } + + fn path(&self) -> &std::path::Path { + &self.0 + } + + fn write(&self, relative: &str, content: &str) { + let path = self.0.join(relative); + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent).expect("create parent folder"); + } + std::fs::write(path, content).expect("write file"); + } +} + +#[cfg(not(target_arch = "wasm32"))] +impl Drop for ScratchDir { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } +} + +#[cfg(not(target_arch = "wasm32"))] +fn file_names(sources: &[(String, String)]) -> Vec { + sources.iter().map(|(p, _)| std::path::Path::new(p).file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default()).collect() +} + +fn lines(findings: &[Finding]) -> Vec { + findings.iter().map(|f| f.line).collect() +} + +#[test] +fn test_finding_display() { + let f1 = Finding::new("src/menu.rs", 42, "PredefinedMenuItem::quit()"); + assert_eq!(f1.to_string(), "src/menu.rs:42: PredefinedMenuItem::quit()"); + + let f2 = Finding::new("", 156, "3.40282347e38"); + assert_eq!(f2.to_string(), "156: 3.40282347e38"); +} + +#[test] +fn test_predefined_quit_positive() { + let source = r#" +fn setup_menu() { + let quit = PredefinedMenuItem::quit(Some("Quit")); + bar.add(quit); +} +"#; + let findings = predefined_quit([("src/menu.rs", source)]); + assert_eq!(findings.len(), 1); + assert_eq!(findings[0].path, "src/menu.rs"); + assert_eq!(findings[0].line, 3); + assert_eq!(findings[0].text, "let quit = PredefinedMenuItem::quit(Some(\"Quit\"));"); + assert_eq!(findings[0].to_string(), "src/menu.rs:3: let quit = PredefinedMenuItem::quit(Some(\"Quit\"));"); +} + +#[test] +fn test_predefined_quit_negative_and_comments() { + let source = r#" +// PredefinedMenuItem::quit(None) - do not use this! +/* + * PredefinedMenuItem::quit is hazardous + */ +fn setup_menu() { + let sep = PredefinedMenuItem::separator(); + let hide = PredefinedMenuItem::hide(Some("Hide")); + let note = "PredefinedMenuItem::quit in string should not trigger"; +} +"#; + let findings = predefined_quit([("src/safe_menu.rs", source)]); + assert!(findings.is_empty(), "expected zero findings for comments, separators, and strings: {findings:?}"); +} + +/// Historical failure: FilmCraft `apps/filmcraft/src/native_menu.rs:56` +/// used `PredefinedMenuItem::quit`, routing directly to `NSApplication terminate:` and bypassing save prompts. +#[test] +fn test_historical_filmcraft_native_menu_quit_regression() { + let problematic_source = r#" + let predefined = [ + (hide_label, PredefinedMenuItem::hide(Some(app.ui.language.tr(hide_label)))), + (hide_others_label, PredefinedMenuItem::hide_others(Some(app.ui.language.tr(hide_others_label)))), + (quit_label, PredefinedMenuItem::quit(Some(app.ui.language.tr(quit_label)))), + ]; +"#; + let findings = predefined_quit([("apps/filmcraft/src/native_menu.rs", problematic_source)]); + assert_eq!(findings.len(), 1); + assert_eq!(findings[0].line, 5); + assert_eq!(findings[0].text, "(quit_label, PredefinedMenuItem::quit(Some(app.ui.language.tr(quit_label)))),"); + + // Corrected source wires Quit via MenuItem::with_id and craft-quit lifecycle + let corrected_source = r#" + let quit = MenuItem::with_id("app.quit", label("app.quit", "Quit FilmCraft"), true, None); + let predefined = [ + (hide_label, PredefinedMenuItem::hide(Some(app.ui.language.tr(hide_label)))), + (hide_others_label, PredefinedMenuItem::hide_others(Some(app.ui.language.tr(hide_others_label)))), + ]; +"#; + let corrected_findings = predefined_quit([("apps/filmcraft/src/native_menu.rs", corrected_source)]); + assert!(corrected_findings.is_empty()); +} + +#[test] +fn test_std_instant_positive() { + let source = r#" +use std::time::Instant; +use std::time::{Duration, Instant as StdInstant}; + +fn measure() { + let t0 = std::time::Instant::now(); +} +"#; + let findings = std_instant([("src/clock.rs", source)]); + assert_eq!(findings.len(), 3); + assert_eq!(findings[0].line, 2); + assert_eq!(findings[1].line, 3); + assert_eq!(findings[2].line, 6); +} + +#[test] +fn test_std_instant_negative_and_web_time() { + let source = r#" +// std::time::Instant::now() causes wasm panic +/* + use std::time::Instant; +*/ +use web_time::Instant; +use std::time::Duration; + +fn measure() { + let t0 = web_time::Instant::now(); + let d = Duration::from_millis(100); +} +"#; + let findings = std_instant([("src/safe_clock.rs", source)]); + assert!(findings.is_empty(), "expected zero findings for web_time, duration, and comments: {findings:?}"); +} + +/// Historical failure: FilmCraft `crates/engine/src/wasm_clock_tests.rs` +/// jobs running synchronously on wasm32 called `std::time::Instant::now()`, panicking at runtime. +#[test] +fn test_historical_filmcraft_wasm_clock_instant_regression() { + let problematic_source = r#" + use std::time::Instant; + + pub fn run_job() { + let t0 = std::time::Instant::now(); + } +"#; + let findings = std_instant([("crates/engine/src/job.rs", problematic_source)]); + assert_eq!(findings.len(), 2); + + let corrected_source = r#" + use web_time::Instant; + + pub fn run_job() { + let t0 = web_time::Instant::now(); + } +"#; + let corrected_findings = std_instant([("crates/engine/src/job.rs", corrected_source)]); + assert!(corrected_findings.is_empty()); + + // User's specific conditional compilation test case: + let native_guarded_inline = r#" +#[cfg(not(target_arch = "wasm32"))] fn timestamp() -> std::time::Instant { std::time::Instant::now() } +"#; + assert!(std_instant([("src/native.rs", native_guarded_inline)]).is_empty()); + + let native_guarded_multiline = r#" +#[cfg(not(target_arch = "wasm32"))] +fn timestamp() -> std::time::Instant { + std::time::Instant::now() +} +"#; + assert!(std_instant([("src/native.rs", native_guarded_multiline)]).is_empty()); +} + +/// Historical failure: PhotoCraft PR #1722 / #1982 `crates/gpu/src/compose.wgsl:156` +/// `const F32_MAX: f32 = 3.40282347e38;` is slightly above f32::MAX. Naga rounds it natively, +/// but Chromium WebGPU compiler (Tint) rejects it, disabling the GPU compositor. +#[test] +fn test_historical_photocraft_compose_wgsl_regression() { + let problematic_shader = r#" +// line 156 of compose.wgsl +const F32_MAX: f32 = 3.40282347e38; +"#; + let findings = wgsl_float_literals(problematic_shader); + assert_eq!(findings.len(), 1); + assert_eq!(findings[0].line, 3); + assert_eq!(findings[0].text, "3.40282347e38"); + + // Corrected shader uses exact hexadecimal float representation + let corrected_shader = r#" +// exact f32::MAX bit for bit +const F32_MAX: f32 = 0x1.fffffep+127f; +"#; + let corrected_findings = wgsl_float_literals(corrected_shader); + assert!(corrected_findings.is_empty()); +} + +#[test] +fn test_wgsl_float_literals_controls() { + // 1. Scientific notation and signed values + let signed_and_sci = r#" +const OVER_POS: f32 = 1e39; +const OVER_NEG: f32 = -1e39; +const VALID_POS: f32 = 1.5e10; +const VALID_NEG: f32 = -1.5e10; +const VALID_TINY: f32 = 1e-40; +"#; + let findings = wgsl_float_literals(signed_and_sci); + assert_eq!(findings.len(), 2); + assert_eq!(findings[0].text, "1e39"); + assert_eq!(findings[1].text, "1e39"); // Scanned magnitude of -1e39 + + // 2. Hexadecimal floating-point representations + let hex_floats = r#" +const EXACT_MAX: f32 = 0x1.fffffep+127f; +const ONE_P0: f32 = 0x1.0p0; +const HALF_HEX: f32 = 0x1.0p-1h; +"#; + assert!(wgsl_float_literals(hex_floats).is_empty()); + + // 3. Literals appearing inside comments + let in_comments = r#" +// const F32_MAX = 3.40282347e38; +/* + const OVERFLOW = 1e40; +*/ +"#; + assert!(wgsl_float_literals(in_comments).is_empty()); + + // 4. Numeric suffixes (f, h) + let suffixes = r#" +const BAD_F: f32 = 3.40282347e38f; +const GOOD_F: f32 = 1.0f; +const GOOD_H: f16 = 0.5h; +"#; + let suffix_findings = wgsl_float_literals(suffixes); + assert_eq!(suffix_findings.len(), 1); + assert_eq!(suffix_findings[0].text, "3.40282347e38"); + + // 5. Embedded in identifiers or other tokens + let idents = r#" +let var_1 = 1.0; +let layer2 = 2.0; +let coord_0 = 0.0; +"#; + assert!(wgsl_float_literals(idents).is_empty()); +} + +#[test] +fn test_wgsl_float_literals_in_multiple_files() { + let s1 = "const A: f32 = 1.0;\nconst B: f32 = 3.40282347e38;\n"; + let s2 = "const OK: f32 = 0x1.fffffep+127f;\n"; + let s3 = "const BAD: f32 = 9.99e38;\n"; + + let files = [("shaders/compose.wgsl", s1), ("shaders/clean.wgsl", s2), ("shaders/fx.wgsl", s3)]; + let findings = wgsl_float_literals_in(files); + assert_eq!(findings.len(), 2); + assert_eq!(findings[0].path, "shaders/compose.wgsl"); + assert_eq!(findings[0].line, 2); + assert_eq!(findings[0].text, "3.40282347e38"); + assert_eq!(findings[1].path, "shaders/fx.wgsl"); + assert_eq!(findings[1].line, 1); + assert_eq!(findings[1].text, "9.99e38"); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn test_predefined_quit_qualified_and_whitespace() { + let source = r#" +fn setup() { + let q1 = muda::PredefinedMenuItem::quit(None); + let q2 = PredefinedMenuItem :: quit(Some("Quit")); + let q3 = ::muda::PredefinedMenuItem::Quit; +} +"#; + let findings = predefined_quit([("src/menu.rs", source)]); + assert_eq!(findings.len(), 3); + assert_eq!(findings[0].line, 3); + assert_eq!(findings[1].line, 4); + assert_eq!(findings[2].line, 5); +} + +#[test] +fn test_std_instant_module_alias() { + let source = r#" +use std::time; + +fn run() { + let t0 = time::Instant::now(); +} +"#; + let findings = std_instant([("src/worker.rs", source)]); + assert_eq!(findings.len(), 1); + assert_eq!(findings[0].line, 5); +} + +#[test] +fn test_wgsl_exact_f32_max_boundary() { + // 3.4028234663852886e38 rounds to f32::MAX, as an f32 and as an f64 + let exact_valid = r#" +const EXACT_MAX: f32 = 3.4028234663852886e38; +const NEG_EXACT_MAX: f32 = -3.4028234663852886e38; +"#; + assert!(wgsl_float_literals(exact_valid).is_empty()); + + // 3.40282347e38 is slightly above f32::MAX + let just_over = r#" +const OVER_POS: f32 = 3.40282347e38; +const OVER_NEG: f32 = -3.40282347e38; +"#; + let findings = wgsl_float_literals(just_over); + assert_eq!(findings.len(), 2); + assert_eq!(findings[0].text, "3.40282347e38"); + assert_eq!(findings[1].text, "3.40282347e38"); +} + +#[test] +fn character_literals_do_not_hide_the_code_after_them() { + let source = r#" +fn menu<'a>(label: &'a str) { + let quote = '"'; + let quit = PredefinedMenuItem::quit(None); + let apostrophe = '\''; + let t0 = std::time::Instant::now(); + let b = b'"'; + let later = PredefinedMenuItem::quit(Some(label)); +} +"#; + assert_eq!(lines(&predefined_quit([("src/menu.rs", source)])), vec![4, 8]); + assert_eq!(lines(&std_instant([("src/menu.rs", source)])), vec![6]); +} + +#[test] +fn strings_hold_comment_markers_without_hiding_code() { + let source = r##" +fn menu() { + let a = "/* not a comment"; + let quit = PredefinedMenuItem::quit(None); + let b = b"// not a comment either"; + let c = r#"*/ " still a raw string"#; + let t0 = std::time::Instant::now(); + let d = "PredefinedMenuItem::quit std::time::Instant"; +} +"##; + assert_eq!(lines(&predefined_quit([("src/menu.rs", source)])), vec![4]); + assert_eq!(lines(&std_instant([("src/menu.rs", source)])), vec![7]); +} + +#[test] +fn nested_block_comments_stay_masked() { + let source = "/* outer /* inner */ PredefinedMenuItem::quit(None); std::time::Instant::now(); */\nlet q = PredefinedMenuItem::quit(None);\n"; + assert_eq!(lines(&predefined_quit([("src/menu.rs", source)])), vec![2]); + assert!(std_instant([("src/menu.rs", source)]).is_empty()); +} + +#[test] +fn predefined_quit_sees_tabs_and_line_breaks_around_the_path_separator() { + let source = "fn menu() {\n let a = PredefinedMenuItem\t::\tquit(None);\n let b = muda::PredefinedMenuItem\n ::quit(None);\n}\n"; + let findings = predefined_quit([("src/menu.rs", source)]); + assert_eq!(lines(&findings), vec![2, 3]); + assert_eq!(findings[1].text, "let b = muda::PredefinedMenuItem"); +} + +#[test] +fn predefined_quit_matches_whole_names_only() { + let other_names = r#" +fn menu() { + let a = MyPredefinedMenuItem::quit(None); + let b = PredefinedMenuItem::quitter(); + let c = PredefinedMenuItem::quit_label(); +} +"#; + assert!(predefined_quit([("src/menu.rs", other_names)]).is_empty()); + + let aliased = r#" +use muda::{PredefinedMenuItem as Item, MenuItem}; +fn menu() { + let a = Item::quit(None); + let b = other::Item::quit(None); +} +"#; + assert_eq!(lines(&predefined_quit([("src/menu.rs", aliased)])), vec![4]); + + let other_crate = r#" +use other_menu::PredefinedMenuItem as Item; +fn menu() { + let a = Item::quit(None); +} +"#; + assert!(predefined_quit([("src/menu.rs", other_crate)]).is_empty()); +} + +#[test] +fn predefined_quit_reports_a_qualified_path() { + let source = r#" +use muda::PredefinedMenuItem as Item; +fn menu() { + let a = ::quit(None); + let b = < PredefinedMenuItem >::quit(None); + let c = ::quit(None); + let d = Vec::::quit(None); + let e = Wrapper::quit(None); +} +"#; + assert_eq!(lines(&predefined_quit([("src/menu.rs", source)])), vec![4, 5, 6]); +} + +#[test] +fn predefined_quit_ignores_another_crates_type_in_a_qualified_path() { + let source = r#" +use other_menu::PredefinedMenuItem as Other; +fn menu() { + let a = other_crate::PredefinedMenuItem::quit(None); + let b = ::quit(None); + let c = crate::menu::PredefinedMenuItem::Quit; + let d = other::muda::PredefinedMenuItem::quit(None); + let e = ::PredefinedMenuItem::quit(None); + let f = Other::quit(None); + let g = muda::PredefinedMenuItem::quit(None); + let h = ::quit(None); + let i = PredefinedMenuItem::quit(None); +} +"#; + assert_eq!(lines(&predefined_quit([("src/menu.rs", source)])), vec![10, 11, 12]); +} + +#[test] +fn predefined_quit_ignores_a_muda_path_that_a_local_binding_of_muda_names() { + let source = r#" +use other_menu as muda; +fn menu() { + let a = muda::PredefinedMenuItem::quit(None); + let b = ::quit(None); + let c = ::muda::PredefinedMenuItem::quit(None); +} +mod inner { + use ::muda; + fn menu() { let d = muda::PredefinedMenuItem::quit(None); } +} +mod defined { + mod muda {} + fn menu() { let e = muda::PredefinedMenuItem::quit(None); } +} +mod gated { + #[cfg(unix)] + use other_menu as muda; + fn menu() { let f = muda::PredefinedMenuItem::quit(None); } +} +"#; + assert_eq!(lines(&predefined_quit([("src/menu.rs", source)])), vec![6, 10, 19]); +} + +#[test] +fn predefined_quit_ignores_a_rename_through_a_local_binding_of_muda() { + let source = r#" +use other_menu as muda; +use muda::PredefinedMenuItem as Item; +type Alias = muda::PredefinedMenuItem; +fn menu() { + let a = Item::quit(None); + let b = Alias::quit(None); +} +mod absolute { + use other_menu as muda; + use ::muda::PredefinedMenuItem as Item; + type Alias = ::muda::PredefinedMenuItem; + use ::{muda::PredefinedMenuItem as Grouped}; + fn menu() { let c = Item::quit(None); let d = Alias::quit(None); let e = Grouped::quit(None); } +} +mod crate_root { + use muda::PredefinedMenuItem as Item; + type Alias = muda::PredefinedMenuItem; + fn menu() { let f = Item::quit(None); let g = Alias::quit(None); } +} +"#; + assert_eq!(lines(&predefined_quit([("src/menu.rs", source)])), vec![14, 19]); +} + +#[test] +fn predefined_quit_follows_a_name_that_the_muda_crate_is_imported_under() { + let source = r#" +use muda as menu; +fn menu_bar() { + let a = menu::PredefinedMenuItem::quit(None); + let b = ::quit(None); + let c = ::menu::PredefinedMenuItem::quit(None); +} +mod grouped { + use muda::{self as m}; + fn menu_bar() { let d = m::PredefinedMenuItem::quit(None); } +} +mod renamed { + extern crate muda as native; + type Item = native::PredefinedMenuItem; + fn menu_bar() { let e = native::PredefinedMenuItem::quit(None); let f = Item::quit(None); } +} +mod imported { + use muda::{self as m}; + use m::PredefinedMenuItem as Quit; + fn menu_bar() { let g = Quit::quit(None); } +} +mod unbound { + fn menu_bar() { let h = m::PredefinedMenuItem::quit(None); } +} +mod shadowed { + use other_menu as menu; + fn menu_bar() { let i = menu::PredefinedMenuItem::quit(None); } +} +"#; + assert_eq!(lines(&predefined_quit([("src/menu.rs", source)])), vec![4, 5, 10, 15, 20]); +} + +#[test] +fn predefined_quit_reads_a_crate_name_in_an_import_where_the_import_is() { + let source = r#" +mod native { + use muda as menu; + use menu::PredefinedMenuItem as Item; + type Alias = menu::PredefinedMenuItem; + fn menu_bar() { let a = Item::quit(None); let b = Alias::quit(None); } +} +mod other { + use other_menu as menu; + use menu::PredefinedMenuItem as Item; + type Alias = menu::PredefinedMenuItem; + fn menu_bar() { let c = Item::quit(None); let d = Alias::quit(None); } +} +mod external { + use muda as menu; + use ::menu::PredefinedMenuItem as Item; + type Alias = ::menu::PredefinedMenuItem; + fn menu_bar() { let e = Item::quit(None); let f = Alias::quit(None); } +} +mod grouped { + use muda as menu; + use {::menu::PredefinedMenuItem as Item}; + fn menu_bar() { let g = Item::quit(None); } +} +mod shadowed { + use muda as menu; + fn menu_bar() { + extern crate other_menu as menu; + let h = menu::PredefinedMenuItem::quit(None); + } +} +mod renamed_module { + use other_menu as muda; + use muda as menu; + fn menu_bar() { let i = menu::PredefinedMenuItem::quit(None); } +} +mod renamed_crate { + mod muda {} + use ::muda as menu; + fn menu_bar() { let j = menu::PredefinedMenuItem::quit(None); } +} +"#; + assert_eq!(lines(&predefined_quit([("src/menu.rs", source)])), vec![6, 40]); +} + +#[test] +fn predefined_quit_ignores_a_bare_name_that_another_import_or_a_local_type_binds_in_its_scope() { + let imported = r#" +use other_menu::PredefinedMenuItem; +fn menu() { + let a = PredefinedMenuItem::quit(None); + let b = muda::PredefinedMenuItem::quit(None); +} +"#; + let grouped = "use other_menu::{PredefinedMenuItem, Menu};\nfn menu() { let a = PredefinedMenuItem::quit(None); }\n"; + let local = r#" +struct PredefinedMenuItem; +fn menu() { + let a = PredefinedMenuItem::quit(None); + let b = ::quit(None); +} +"#; + let both = r#" +use muda::PredefinedMenuItem; +mod other { pub struct PredefinedMenuItem; } +fn menu() { + let a = PredefinedMenuItem::quit(None); +} +"#; + let scoped = r#" +use muda::*; +mod other { + use other_menu::PredefinedMenuItem; + fn menu() { let a = PredefinedMenuItem::quit(None); } +} +mod local { struct PredefinedMenuItem; fn menu() { let b = PredefinedMenuItem::quit(None); } } +fn menu() { + let c = PredefinedMenuItem::quit(None); + { use other_menu::PredefinedMenuItem; let d = PredefinedMenuItem::quit(None); } + let e = PredefinedMenuItem::quit(None); +} +"#; + assert_eq!(lines(&predefined_quit([("src/imported.rs", imported)])), vec![5]); + assert!(predefined_quit([("src/grouped.rs", grouped)]).is_empty()); + assert!(predefined_quit([("src/local.rs", local)]).is_empty()); + assert_eq!(lines(&predefined_quit([("src/both.rs", both)])), vec![5]); + assert_eq!(lines(&predefined_quit([("src/scoped.rs", scoped)])), vec![9, 11]); + let child = r#" +use other_menu::PredefinedMenuItem; +mod child { + use muda::*; + fn menu() { let a = PredefinedMenuItem::quit(None); } +} +fn menu() { let b = PredefinedMenuItem::quit(None); } +"#; + assert_eq!(lines(&predefined_quit([("src/child.rs", child)])), vec![5]); + let renamed = r#" +mod a { + use muda::PredefinedMenuItem as Item; + fn menu() { let a = Item::quit(None); } +} +mod b { + struct Item; + fn menu() { let b = Item::quit(None); } +} +mod c { fn menu() { let c = Item::quit(None); } } +mod d { type Quit = muda::PredefinedMenuItem; fn menu() { let d = Quit::quit(None); } } +"#; + assert_eq!(lines(&predefined_quit([("src/renamed.rs", renamed)])), vec![4, 11]); + let aliased_or_gated = r#" +mod alias { + type PredefinedMenuItem = ::muda::PredefinedMenuItem; + fn menu() { let a = PredefinedMenuItem::quit(None); } +} +mod gated { + use muda::*; + #[cfg(windows)] + struct PredefinedMenuItem; + #[cfg(windows)] use other_menu::PredefinedMenuItem; + fn menu() { let b = PredefinedMenuItem::quit(None); } +} +mod other { type PredefinedMenuItem = other_menu::PredefinedMenuItem; fn menu() { let c = PredefinedMenuItem::quit(None); } } +mod assoc { + use muda::PredefinedMenuItem; + trait Menu { type PredefinedMenuItem; fn menu() { let d = PredefinedMenuItem::quit(None); } } + #[allow(dead_code)] unsafe impl Menu for Wrap<{ N }> { + fn menu() { let e = PredefinedMenuItem::quit(None); } + type PredefinedMenuItem = u8; + } +} +mod reexported { + use crate::PredefinedMenuItem; + fn menu() { let f = PredefinedMenuItem::quit(None); } + mod inner { use super::super::{self as up, PredefinedMenuItem}; fn menu() { let g = PredefinedMenuItem::quit(None); } } + mod aliased { type PredefinedMenuItem = crate::menu::PredefinedMenuItem; fn menu() { let h = PredefinedMenuItem::quit(None); } } +} +"#; + assert_eq!(lines(&predefined_quit([("src/aliased.rs", aliased_or_gated)])), vec![4, 11, 16, 18, 24, 25, 26]); +} + +#[test] +fn std_instant_reads_a_std_time_alias_only_in_its_scope() { + let source = r#" +mod native { + use std::time as clock; + fn now() { let a = clock::Instant::now(); } + use clock::Instant as A; +} +mod web { + use web_time as clock; + fn now() { let b = clock::Instant::now(); } + use clock::Instant as B; + use {clock::*, std::time::*}; +} +mod child { fn now() { let c = clock::Instant::now(); } } +fn run() { + use std::time as t; + let d = t::Instant::now(); + { mod t {} let e = t::Instant::now(); } +} +"#; + assert_eq!(lines(&std_instant([("src/lib.rs", source)])), vec![4, 5, 11, 16]); +} + +#[test] +fn std_instant_keeps_an_alias_that_a_cfg_gated_import_may_shadow() { + let source = r#" +use std::time as clock; +fn now() { + #[cfg(feature = "web-clock")] + use web_time as clock; + let a = clock::Instant::now(); +} +fn gated() { + #[cfg(feature = "web-clock")] + mod clock {} + let b = clock::Instant::now(); +} +#[cfg(feature = "web-clock")] +fn web() { + use web_time as clock; + let c = clock::Instant::now(); +} +fn wasm() { + #[cfg(target_arch = "wasm32")] + use web_time as clock; + let d = clock::Instant::now(); +} +"#; + assert_eq!(lines(&std_instant([("src/lib.rs", source)])), vec![6, 11]); +} + +#[test] +fn std_instant_ignores_an_alias_that_only_a_native_cfg_imports() { + let source = r#" +#[cfg(not(target_arch = "wasm32"))] +use std::time as clock; +#[cfg(target_arch = "wasm32")] +use web_time as clock; + +fn now() { + let t = clock::Instant::now(); +} +"#; + assert!(std_instant([("src/clock.rs", source)]).is_empty()); +} + +#[test] +fn std_instant_reads_cfg_from_code_not_from_strings_or_comments() { + let cfg_in_string = r##" +fn now() -> u64 { let _s = r#"cfg(target_os = "linux")"#; + let t = std::time::Instant::now(); + 0 +} +"##; + assert_eq!(lines(&std_instant([("src/a.rs", cfg_in_string)])), vec![3]); + + let cfg_in_comment = r#" +fn web() { // cfg(target_os = "linux") + let t = std::time::Instant::now(); +} +"#; + assert_eq!(lines(&std_instant([("src/b.rs", cfg_in_comment)])), vec![3]); +} + +#[test] +fn std_instant_skips_only_the_item_a_native_cfg_covers() { + let source = r#" +#[cfg(not(target_arch = "wasm32"))] +fn native() -> std::time::Instant { std::time::Instant::now() } +fn web() -> std::time::Instant { std::time::Instant::now() } + +#[cfg(all(unix, not(target_arch = "wasm32")))] +#[inline] +fn unix_only() { + let t = std::time::Instant::now(); +} + +#[cfg(feature = "native-clock")] +fn by_feature() { let t = std::time::Instant::now(); } + +#[cfg(any(target_arch = "wasm32", windows))] +fn also_web() { let t = std::time::Instant::now(); } + +#[cfg(target_os = "unknown")] +fn unknown_os() { let t = std::time::Instant::now(); } + +#[cfg(target_os = "macos")] +use std::time::Instant as MacInstant; +"#; + assert_eq!(lines(&std_instant([("src/clock.rs", source)])), vec![4, 13, 16, 19]); +} + +#[test] +fn std_instant_decodes_escapes_in_cfg_values() { + let source = r#" +#[cfg(target_arch = "\x77asm32")] +fn web() { let t = std::time::Instant::now(); } +#[cfg(target_arch = "w\u{61}sm32")] +fn web_two() { let t = std::time::Instant::now(); } +#[cfg(target_arch = r"\x77asm32")] +fn native() { let t = std::time::Instant::now(); } +#[cfg(not(target_arch = "\q"))] +fn unknown() { let t = std::time::Instant::now(); } +#[cfg(target_os = "lin\ + ux")] +fn native_two() { let t = std::time::Instant::now(); } +fn after() { let t = std::time::Instant::now(); } +"#; + assert_eq!(lines(&std_instant([("src/clock.rs", source)])), vec![3, 5, 9, 13]); +} + +#[test] +fn std_instant_reads_cfg_true_and_false() { + let source = r#" +#[cfg(false)] +fn disabled() { let t = std::time::Instant::now(); } +#[cfg(true)] +fn enabled() { let t = std::time::Instant::now(); } +#[cfg(not(true))] +fn never() { let t = std::time::Instant::now(); } +#[cfg(r#false)] +fn named_option() { let t = std::time::Instant::now(); } +#[cfg(not(r#true))] +fn other_option() { let t = std::time::Instant::now(); } +"#; + assert_eq!(lines(&std_instant([("src/clock.rs", source)])), vec![5, 9, 11]); +} + +#[test] +fn std_instant_checks_an_item_whose_cfg_is_nested_past_the_limit() { + let source = format!( + "#[cfg({}target_arch = \"wasm32\"{})]\nfn web() {{ let t = std::time::Instant::now(); }}\n#[cfg({}unix{})]\nfn native() {{ let t = std::time::Instant::now(); }}\n", + "any(".repeat(65), + ")".repeat(65), + "all(".repeat(10), + ")".repeat(10), + ); + assert_eq!(lines(&std_instant([("src/clock.rs", source.as_str())])), vec![2]); +} + +#[test] +fn std_instant_checks_the_fields_after_a_native_cfg_function_pointer_field() { + let source = r#" +struct S { + #[cfg(unix)] + callback: fn(), + clock: std::time::Instant, + #[cfg(unix)] + native: fn() -> std::time::Instant, + #[cfg(unix)] + pub(crate) boxed: Box, + other: std::time::Instant, +} +fn run() { + #[cfg(unix)] + 'outer: loop { let _a = std::time::Instant::now(); } + let t = std::time::Instant::now(); +} +"#; + assert_eq!(lines(&std_instant([("src/clock.rs", source)])), vec![5, 10, 15]); +} + +#[test] +fn std_instant_ends_a_native_cfg_block_statement_at_its_closing_brace() { + let source = r#" +fn run() { + #[cfg(unix)] { + let _a = std::time::Instant::now(); + } + let t = std::time::Instant::now(); +} +"#; + assert_eq!(lines(&std_instant([("src/run.rs", source)])), vec![6]); +} + +#[test] +fn std_instant_skips_a_method_call_after_a_native_cfg_block() { + let source = r#" +fn run(x: Option) -> Option { + #[cfg(unix)] + { 0u64 }.saturating_add(std::time::Instant::now().elapsed().as_nanos() as u64); + #[cfg(unix)] + unsafe { x }.map(|_| std::time::Instant::now())?; + let t = std::time::Instant::now(); + x +} +"#; + assert_eq!(lines(&std_instant([("src/run.rs", source)])), vec![7]); +} + +#[test] +fn std_instant_skips_an_operator_after_a_block_inside_a_native_cfg_expression() { + let source = r#" +fn run(flag: bool) -> u64 { + #[cfg(unix)] + let _ = if flag { 1 } else { 2 } + std::time::Instant::now().elapsed().as_secs(); + #[cfg(unix)] + return if flag { 1 } else { 2 } * std::time::Instant::now().elapsed().as_secs(); + let t = std::time::Instant::now(); + 0 +} +"#; + assert_eq!(lines(&std_instant([("src/run.rs", source)])), vec![7]); +} + +#[test] +fn std_instant_skips_a_cast_after_a_match_inside_a_native_cfg_expression() { + let source = r#" +fn run(x: Option) -> u64 { + #[cfg(unix)] + let n = match x { Some(v) => v, None => 0 } as u64 + std::time::Instant::now().elapsed().as_secs(); + let t = std::time::Instant::now(); + 0 +} +"#; + assert_eq!(lines(&std_instant([("src/run.rs", source)])), vec![5]); +} + +#[test] +fn std_instant_runs_a_native_cfg_match_arm_to_its_comma_when_a_block_is_inside_its_value() { + let source = r#" +fn run(x: Option, y: bool) -> u64 { + match x { + #[cfg(unix)] + Some(v) => v as u64 + match y { true => 1, false => 0 } + std::time::Instant::now().elapsed().as_secs(), + #[cfg(unix)] + None => if y { 1 } else { 0 }, + #[cfg(unix)] Some(0) => { 0 } _ => std::time::Instant::now().elapsed().as_secs(), + } +} +"#; + assert_eq!(lines(&std_instant([("src/run.rs", source)])), vec![8]); +} + +#[test] +fn std_instant_ends_a_native_cfg_const_or_labeled_block_at_its_closing_brace() { + let source = r#" +fn run(x: Option) -> u64 { + #[cfg(unix)] + const { assert!(true) } + let a = std::time::Instant::now(); + #[cfg(unix)] + 'done: { let _ = std::time::Instant::now(); break 'done; } + let b = std::time::Instant::now(); + match x { + #[cfg(unix)] + Some(_) => 'arm: { let _ = std::time::Instant::now(); break 'arm; } + _ => { let c = std::time::Instant::now(); } + } + 0 +} +"#; + assert_eq!(lines(&std_instant([("src/run.rs", source)])), vec![5, 8, 12]); +} + +#[test] +fn std_instant_ends_a_native_cfg_if_let_at_its_body_not_at_a_struct_pattern() { + let source = r#" +enum State { Ready { at: u64 }, Idle } +fn run(state: State, other: State) { + #[cfg(unix)] + if let State::Ready { .. } = state { let _a = std::time::Instant::now(); } + let b = std::time::Instant::now(); + #[cfg(unix)] + while let State::Ready { at } = other { let _c = std::time::Instant::now(); } + let d = std::time::Instant::now(); + #[cfg(unix)] + if let State::Idle = state { 0 } else if let State::Ready { at: 0..=9 } = state { let _e = std::time::Instant::now(); } else { let _f = std::time::Instant::now(); } + let g = std::time::Instant::now(); + #[cfg(unix)] + if let State::Ready { .. } = state && let State::Ready { at } = other { let _h = std::time::Instant::now(); } + let i = std::time::Instant::now(); + #[cfg(unix)] + for State::Ready { at } in [state] { let _j = std::time::Instant::now(); } + let k = std::time::Instant::now(); +} +"#; + assert_eq!(lines(&std_instant([("src/run.rs", source)])), vec![6, 9, 12, 15, 18]); +} + +#[test] +fn std_instant_ends_a_native_cfg_if_at_its_body_not_at_a_block_in_its_condition() { + let source = r#" +fn run(ready: bool, items: Vec) { + #[cfg(unix)] + if { ready } { let _a = std::time::Instant::now(); } + let b = std::time::Instant::now(); + #[cfg(unix)] + while !{ ready } { let _c = std::time::Instant::now(); } + let d = std::time::Instant::now(); + #[cfg(unix)] + if ready && unsafe { check() } { let _e = std::time::Instant::now(); } + let f = std::time::Instant::now(); + #[cfg(unix)] + for item in { items } { let _g = std::time::Instant::now(); } + let h = std::time::Instant::now(); + #[cfg(unix)] + match { 1 } { _ => { let _i = std::time::Instant::now(); } } + let j = std::time::Instant::now(); + #[cfg(unix)] + if let true = { ready } { let _k = std::time::Instant::now(); } + let l = std::time::Instant::now(); + #[cfg(unix)] + if loop { break ready } { let _m = std::time::Instant::now(); } + let n = std::time::Instant::now(); + #[cfg(unix)] + if async move { ready }.await { let _o = std::time::Instant::now(); } + let p = std::time::Instant::now(); +} +"#; + assert_eq!(lines(&std_instant([("src/run.rs", source)])), vec![5, 8, 11, 14, 17, 20, 23, 26]); +} + +#[test] +fn std_instant_ends_a_native_cfg_let_else_and_match_with_struct_patterns_where_they_end() { + let source = r#" +enum State { Ready { at: u64 }, Idle } +fn run(state: State, other: State) -> u64 { + #[cfg(unix)] + let State::Ready { at } = state else { let _a = std::time::Instant::now(); return 0; }; + let b = std::time::Instant::now(); + #[cfg(unix)] + match other { State::Ready { at } => { let _c = std::time::Instant::now(); } State::Idle => {} } + let d = std::time::Instant::now(); + match state { + #[cfg(unix)] + State::Ready { at } => if let State::Ready { .. } = other { let _e = std::time::Instant::now(); }, + _ => { let f = std::time::Instant::now(); } + } + 0 +} +"#; + assert_eq!(lines(&std_instant([("src/run.rs", source)])), vec![6, 9, 13]); +} + +#[test] +fn std_instant_reads_raw_identifiers_as_names_not_keywords() { + let source = r#" +fn run(r#let: bool, r#in: [u8; 1]) { + #[cfg(unix)] + if r#let { let _a = std::time::Instant::now(); } + let b = std::time::Instant::now(); + #[cfg(unix)] + for x in r#in { let _c = std::time::Instant::now(); } + let d = std::time::Instant::now(); +} +"#; + assert_eq!(lines(&std_instant([("src/run.rs", source)])), vec![5, 8]); + + let lib = "#[cfg(unix)]\nmod r#match;\nmod r#loop;\n"; + let native = "use std::time::Instant;\n"; + let web = "use std::time::Instant;\n"; + assert_eq!(finding_paths(&std_instant([("src/lib.rs", lib), ("src/match.rs", native), ("src/loop.rs", web)])), vec!["src/loop.rs"]); +} + +#[test] +fn std_instant_runs_a_native_cfg_async_block_statement_to_its_semicolon() { + let source = r#" +async fn run() { + #[cfg(unix)] + async { std::time::Instant::now() } + .await; + #[cfg(unix)] + async move { std::time::Instant::now() }; + let a = std::time::Instant::now(); +} +"#; + assert_eq!(lines(&std_instant([("src/run.rs", source)])), vec![8]); +} + +#[test] +fn std_instant_checks_code_before_and_after_a_native_cfg_item_on_the_same_line() { + let source = "fn run() { + let a = std::time::Instant::now(); #[cfg(unix)] let b = std::time::Instant::now(); let c = std::time::Instant::now(); + let d = 1; #[cfg(unix)] let e = std::time::Instant::now(); + #[cfg(unix)] let f = std::time::Instant::now(); let g = std::time::Instant::now(); +} +"; + assert_eq!(lines(&std_instant([("src/run.rs", source)])), vec![2, 4]); +} + +#[test] +fn std_instant_checks_an_item_after_a_native_cfg_item_on_the_same_line() { + let source = r#"#[cfg(target_os="linux")] fn native() {} fn web() { std::time::Instant::now(); } +"#; + assert_eq!(lines(&std_instant([("src/clock.rs", source)])), vec![1]); +} + +#[test] +fn std_instant_skips_gated_code_that_follows_a_native_cfg_item_on_the_same_line() { + let source = r#"#[cfg(target_os = "linux")] fn a() {} #[cfg(target_os = "macos")] fn b() { std::time::Instant::now(); } +#[cfg(unix)] fn c() {} #[cfg(windows)] use std::time::Instant as Native; +"#; + assert!(std_instant([("src/clock.rs", source)]).is_empty()); +} + +#[test] +fn std_instant_ends_native_cfg_impl_mod_and_struct_items_where_they_end_on_the_line() { + let source = r#"struct S; #[cfg(unix)] impl S { fn now() { std::time::Instant::now(); } } fn web() { std::time::Instant::now(); } +#[cfg(unix)] mod native { fn now() { std::time::Instant::now(); } } fn web_two() { std::time::Instant::now(); } +#[cfg(unix)] struct P { at: std::time::Instant } struct Q { at: std::time::Instant } +#[cfg(unix)] struct T(std::time::Instant); struct U(std::time::Instant); +#[cfg(unix)] impl S { fn a() {} } #[cfg(unix)] mod m { fn b() { std::time::Instant::now(); } } +"#; + assert_eq!(lines(&std_instant([("src/clock.rs", source)])), vec![1, 2, 3, 4]); +} + +#[test] +fn std_instant_reads_blocks_in_generic_brackets_as_part_of_a_native_cfg_item_head() { + let source = r#"#[cfg(unix)] struct Native { clock: std::time::Instant } +#[cfg(unix)] enum E { A(std::time::Instant) } +#[cfg(unix)] trait Tr: Sized where Self: Into> { fn now() -> std::time::Instant { std::time::Instant::now() } } +#[cfg(unix)] impl Tr<{ N }> for Native<{ N }> { fn now() -> std::time::Instant { std::time::Instant::now() } } +#[cfg(unix)] fn native() -> W<{ 1 + 1 }> where T: Into> { std::time::Instant::now(); W } +#[cfg(unix)] fn arrow u8>(f: F) -> impl Fn() -> W<{ 3 }> where F: Copy { let _t = std::time::Instant::now(); f } +struct Web { clock: std::time::Instant } +fn web() -> W<{ 1 }> where u8: Into> { std::time::Instant::now(); W } +#[cfg(unix)] struct A { a: std::time::Instant } fn after() { std::time::Instant::now(); } +"#; + assert_eq!(lines(&std_instant([("src/clock.rs", source)])), vec![7, 8, 9]); +} + +#[test] +fn std_instant_reads_paths_split_over_lines_and_comments_with_crlf() { + let source = "use std::time::\r\n Instant;\r\nfn run() {\r\n let t = std::time\r\n ::Instant::now();\r\n let u = std /* a */ :: time // b\r\n :: Instant::now();\r\n}\r\n"; + assert_eq!(lines(&std_instant([("src/run.rs", source)])), vec![1, 4, 6]); +} + +#[test] +fn std_instant_reports_a_use_on_the_line_where_its_path_starts() { + let source = "use std::time::\n Instant as Clock;\nuse std::time::{\n Duration,\n Instant,\n};\nuse std::\n time::*;\n"; + assert_eq!(lines(&std_instant([("src/run.rs", source)])), vec![1, 5, 7]); +} + +#[test] +fn std_instant_ignores_other_time_modules_longer_names_and_a_renamed_duration() { + let source = r#" +use tokio::time::Instant; +use std::time::{Duration as Instant}; +fn run() { + let a = my_time::Instant::now(); + let b = runtime::Instant::now(); + let c = crate::time::Instant::now(); + let d = tokio::time::Instant::now(); + let e = core::time::Instant::now(); + let f = std::time::InstantLike::now(); + let g = mystd::time::Instant::now(); + let h = std::time::Instant::now(); +} +"#; + assert_eq!(lines(&std_instant([("src/run.rs", source)])), vec![12]); +} + +#[test] +fn std_instant_ignores_paths_in_literals_and_comments_and_reads_labels_as_code() { + let source = r##" +fn run<'a>(s: &'a str) -> char { + let _x = "std::time::Instant"; let _r = r#"std::time::Instant"#; let _b = b"std::time::Instant"; let _c = c"std::time::Instant"; + // std::time::Instant + /* std::time::Instant */ + 'outer: loop { break 'outer; } + let q = '"'; let t = std::time::Instant::now(); + 'x' +} +"##; + assert_eq!(lines(&std_instant([("src/run.rs", source)])), vec![7]); +} + +#[test] +fn std_instant_scans_a_large_source_once() { + let mut source = String::from("fn run() {\n"); + for i in 0..20_000 { + if i % 2 == 0 { + source.push_str(" #[cfg(unix)] let a = std::time::Instant::now();\n"); + } else { + source.push_str(" let b = std::time::Instant::now();\n"); + } + } + source.push_str("}\n"); + let found = lines(&std_instant([("src/run.rs", source.as_str())])); + assert_eq!(found.len(), 10_000); + assert_eq!(found.first(), Some(&3)); + assert_eq!(found.last(), Some(&20_001)); +} + +#[test] +fn std_instant_inner_cfg_covers_the_whole_file() { + let source = r#" +#![cfg(not(target_arch = "wasm32"))] +use std::time::Instant; + +fn now() -> Instant { + std::time::Instant::now() +} +"#; + assert!(std_instant([("src/native.rs", source)]).is_empty()); +} + +#[test] +fn std_instant_reports_a_std_use_on_a_line_that_also_names_web_time() { + let source = "fn pair() {\n let p = (web_time::Instant::now(), std::time::Instant::now());\n}\n"; + assert_eq!(lines(&std_instant([("src/pair.rs", source)])), vec![2]); +} + +#[test] +fn std_instant_needs_std_behind_a_bare_time_path() { + let source = r#" +use crate::time; + +fn tick() { + let t = time::Instant::new(); +} +"#; + assert!(std_instant([("src/tick.rs", source)]).is_empty()); +} + +#[test] +fn std_instant_follows_module_and_type_aliases() { + let source = r#" +use std::time as t; +use std::time::{self as clock, Duration}; +use std::{time::Instant as I, fmt}; +use web_time as wt; + +fn run() { + let a = t::Instant::now(); + let b = clock::Instant::now(); + let c = wt::Instant::now(); + let d = x::t::Instant::now(); +} +"#; + assert_eq!(lines(&std_instant([("src/run.rs", source)])), vec![4, 8, 9]); +} + +#[test] +fn std_instant_reads_a_use_group_split_over_several_lines() { + let source = r#" +use std::time::{ + Instant +}; +use std::time::{ + Duration, + Instant as Clock, +}; +"#; + let findings = std_instant([("src/clock.rs", source)]); + assert_eq!(lines(&findings), vec![3, 7]); + assert_eq!(findings[0].text, "Instant"); +} + +#[test] +fn std_instant_reads_a_use_after_a_precise_capturing_bound() { + let source = "fn clock() -> impl Fn() + use<> {\n use std::{time::Instant};\n || { let _ = Instant::now(); }\n}\n"; + assert_eq!(lines(&std_instant([("src/clock.rs", source)])), vec![2]); +} + +#[test] +fn std_instant_does_not_count_brace_character_literals_as_braces() { + let source = r#" +#[cfg(unix)] +fn native() { let open = '{'; let b = b'{'; let t = std::time::Instant::now(); } +fn web() { let close = '}'; let t = std::time::Instant::now(); } +#[cfg(unix)] +fn native_two() { let close = '}'; let t = std::time::Instant::now(); } +fn web_two() { let t = std::time::Instant::now(); } +"#; + assert_eq!(lines(&std_instant([("src/clock.rs", source)])), vec![4, 7]); +} + +#[test] +fn an_escaped_line_break_in_a_string_keeps_line_numbers() { + let source = + "fn web() {\n let s = \"one \\\n two\";\n let t = std::time::Instant::now();\n let q = PredefinedMenuItem::quit(None);\n}\n"; + assert_eq!(lines(&std_instant([("src/web.rs", source)])), vec![4]); + assert_eq!(lines(&predefined_quit([("src/web.rs", source)])), vec![5]); +} + +#[test] +fn deeply_nested_use_groups_and_cfg_predicates_do_not_exhaust_the_stack() { + let depth = 100_000; + let use_groups = format!("use std::time::{}Instant{};\nfn f() {{}}\n", "{".repeat(depth), "}".repeat(depth)); + assert!(std_instant([("src/deep.rs", use_groups.as_str())]).is_empty()); + assert!(predefined_quit([("src/deep.rs", use_groups.as_str())]).is_empty()); + + let cfg = format!("#[cfg({}unix{})]\nfn f() {{ let t = std::time::Instant::now(); }}\n", "not(".repeat(depth), ")".repeat(depth)); + assert_eq!(lines(&std_instant([("src/deep.rs", cfg.as_str())])), vec![2]); + + let shallow = "use std::time::{{{Instant}}};\n"; + assert_eq!(lines(&std_instant([("src/shallow.rs", shallow)])), vec![1]); +} + +#[test] +fn a_long_use_path_shared_by_many_names_is_read_without_copying_it_to_each() { + // 10,000 path segments shared by 10,000 names, followed by a `use` the guard reports. + let names: Vec = (0..10_000).map(|n| format!("x{n}")).collect(); + let source = format!("use {}{{{}}};\nuse std::time::Instant;\n", "a::".repeat(10_000), names.join(", ")); + assert_eq!(lines(&std_instant([("src/wide.rs", source.as_str())])), vec![2]); + assert!(predefined_quit([("src/wide.rs", source.as_str())]).is_empty()); +} + +#[test] +fn a_long_name_shared_by_many_use_leaves_is_read_without_copying_it_to_each() { + // A 40,000-byte name shared by 10,000 leaves, followed by a `use` the guard reports. + let source = format!("use {}::{{{}}};\nuse std::time::Instant;\n", "n".repeat(40_000), ["self"; 10_000].join(", ")); + assert_eq!(lines(&std_instant([("src/wide.rs", source.as_str())])), vec![2]); + assert!(predefined_quit([("src/wide.rs", source.as_str())]).is_empty()); +} + +#[test] +fn std_instant_reports_a_grouped_import_through_a_long_alias_of_std_time() { + let source = r#" +use std::time as standard_time_module_with_a_long_name; +use standard_time_module_with_a_long_name::{Instant as Clock}; +fn now() { + let _ = Clock::now(); +} +"#; + assert_eq!(lines(&std_instant([("src/clock.rs", source)])), vec![3]); +} + +#[test] +fn std_instant_reports_a_glob_through_a_long_alias_of_std_time() { + let source = r#" +use std::time as standard_time_module_with_a_long_name; +use standard_time_module_with_a_long_name::*; +use standard_time_module_with_a_long_name::{Duration, *}; +"#; + assert_eq!(lines(&std_instant([("src/clock.rs", source)])), vec![3, 4]); +} + +#[test] +fn std_instant_reports_a_path_of_nine_or_more_segments_that_starts_at_std_time() { + let source = r#" +use std::time as standard_time_module_with_a_long_name; +use std::time::Instant::a::b::c::d::e::f; +use standard_time_module_with_a_long_name::Instant::a::b::c::d::e::f::g; +"#; + assert_eq!(lines(&std_instant([("src/clock.rs", source)])), vec![3, 4]); +} + +#[test] +fn the_guards_read_an_absolute_path_after_a_return_arrow_or_a_match_arrow() { + let clock = r#" +fn now() -> ::std::time::Instant { + match 0 { _ => ::std::time::Instant::now() } +} +fn closure() { let _ = || -> ::std::time::Instant { todo() }; } +"#; + assert_eq!(lines(&std_instant([("src/clock.rs", clock)])), vec![2, 3, 5]); + + let menu = r#" +fn item() -> Item { + match 0 { _ => ::muda::PredefinedMenuItem::quit(None) } +} +"#; + assert_eq!(lines(&predefined_quit([("src/menu.rs", menu)])), vec![3]); +} + +#[test] +fn a_long_alias_of_std_time_shared_by_many_use_leaves_is_read_without_copying_it_to_each() { + // A 40,000-byte alias of `std::time` shared by 10,000 leaves. + let alias = "n".repeat(40_000); + let source = format!("use std::time as {alias};\nuse {alias}::{{{}}};\n", ["Instant"; 10_000].join(", ")); + assert_eq!(lines(&std_instant([("src/wide.rs", source.as_str())])), vec![2]); +} + +#[test] +fn a_use_past_the_path_limits_still_binds_its_name() { + let segment = r#" +use other_menu::MenuItemWithAnExceptionallyLongName as PredefinedMenuItem; +fn quit() { PredefinedMenuItem::quit(None); } +"#; + let length = r#" +use a::b::c::d::e::f::g::h::Item as PredefinedMenuItem; +fn quit() { PredefinedMenuItem::quit(None); } +"#; + let shared = r#" +use other_menu::MenuItemWithAnExceptionallyLongName::{self as PredefinedMenuItem}; +fn quit() { PredefinedMenuItem::quit(None); } +"#; + assert!(predefined_quit([("src/menu.rs", segment), ("src/len.rs", length), ("src/shared.rs", shared)]).is_empty()); + + // A path into this crate past the limits may still be a re-export of muda's type. + let local = r#" +use crate::module_with_an_exceptionally_long_name::PredefinedMenuItem; +fn quit() { PredefinedMenuItem::quit(None); } +"#; + assert_eq!(lines(&predefined_quit([("src/local.rs", local)])), vec![3]); + + let clock = r#" +use std::time as t; +fn now() { + use other_clock::ClockModuleWithAnExceptionallyLongName as t; + let _ = t::Instant::now(); +} +fn later() { + use other_clock::ClockModuleWithAnExceptionallyLongName::{self as t}; + let _ = t::Instant::now(); +} +fn web() { let _ = t::Instant::now(); } +"#; + assert_eq!(lines(&std_instant([("src/clock.rs", clock)])), vec![11]); +} + +#[test] +fn std_instant_reports_an_underscore_import_of_instant_in_a_group() { + let source = r#" +use std::{time::Instant as _}; +use std::time::{Instant as _}; +use std::time::Instant as _; +use std::time::{self as _}; +"#; + assert_eq!(lines(&std_instant([("src/clock.rs", source)])), vec![2, 3, 4]); + assert!(predefined_quit([("src/menu.rs", "use muda::{PredefinedMenuItem as _};\nfn f() { _::quit(); }\n")]).is_empty()); +} + +#[test] +fn std_instant_keeps_an_import_that_an_alias_in_a_nested_module_shares_a_name_with() { + let source = r#" +mod unrelated { use ::std::time as std; } +use std::{time::Instant}; +"#; + assert_eq!(lines(&std_instant([("src/clock.rs", source)])), vec![3]); +} + +fn finding_paths(findings: &[Finding]) -> Vec<&str> { + findings.iter().map(|f| f.path.as_str()).collect() +} + +#[test] +fn std_instant_skips_module_files_a_native_cfg_declares() { + let lib = r#" +#[cfg(not(target_arch = "wasm32"))] +mod native; +mod web; +#[cfg(feature = "extra")] +mod extra; +"#; + let native = "mod clock;\npub fn now() -> std::time::Instant { std::time::Instant::now() }\n"; + let native_clock = "use std::time::Instant;\n"; + let web = "#[cfg(unix)]\nmod imp;\npub fn now() { let _t = std::time::Instant::now(); }\n"; + let web_imp = "use std::time::Instant;\n"; + let sibling_imp = "use std::time::Instant;\n"; + let extra = "use std::time::Instant;\n"; + let files = [ + ("src/lib.rs", lib), + ("src/native.rs", native), + ("src/native/clock.rs", native_clock), + ("src/web.rs", web), + ("src/web/imp.rs", web_imp), + ("src/imp.rs", sibling_imp), + ("src/extra.rs", extra), + ]; + assert_eq!(finding_paths(&std_instant(files)), vec!["src/web.rs", "src/imp.rs", "src/extra.rs"]); +} + +#[test] +fn std_instant_skips_native_module_files_by_mod_rs_path_attribute_and_inline_module() { + let main = r#" +#[cfg(unix)] +pub(crate) mod sys; +#[cfg(target_os = "macos")] +#[path = "platform/mac_clock.rs"] +mod mac; +#[cfg(windows)] +mod outer { + mod inner; +} +#[cfg(unix)] +#[path = "platform/native.rs"] +pub(in crate) mod native; +"#; + let clock = "use std::time::Instant;\n"; + let files = [ + ("C:\\app\\src\\main.rs", main), + ("C:\\app\\src\\platform\\native.rs", clock), + ("C:\\app\\src\\sys\\mod.rs", "mod detail;\n"), + ("C:\\app\\src\\sys\\detail.rs", clock), + ("C:\\app\\src\\platform\\mac_clock.rs", clock), + ("C:\\app\\src\\outer\\inner.rs", clock), + ("C:\\app\\src\\mac.rs", clock), + ]; + assert_eq!(finding_paths(&std_instant(files)), vec!["C:\\app\\src\\mac.rs"]); +} + +#[test] +fn std_instant_checks_a_module_file_that_a_web_declaration_also_names() { + let lib = r#" +#[cfg(windows)] +mod imp; +#[cfg(target_arch = "wasm32")] +mod imp; +#[cfg(unix)] +mod sys; +"#; + let files = [ + ("src/lib.rs", lib), + ("src/imp.rs", "mod detail;\n"), + ("src/imp/detail.rs", "use std::time::Instant;\n"), + ("src/sys.rs", "#[path = \"imp/detail.rs\"]\nmod shared;\n"), + ]; + assert_eq!(finding_paths(&std_instant(files)), vec!["src/imp/detail.rs"]); +} + +#[test] +fn std_instant_reads_an_inline_module_path_attribute_as_the_folder_of_its_modules() { + let lib = r#" +#[cfg(unix)] +#[path = "native"] +mod platform { + mod clock; +} +mod web; +"#; + let web = r#" +#[path = "shared"] +mod inner { + #[cfg(windows)] + mod tick; +} +"#; + let clock = "use std::time::Instant;\n"; + let files = [ + ("src/lib.rs", lib), + ("src/native/clock.rs", clock), + ("src/platform/clock.rs", clock), + ("src/web.rs", web), + ("src/shared/tick.rs", clock), + ("src/web/shared/tick.rs", clock), + ]; + assert_eq!(finding_paths(&std_instant(files)), vec!["src/platform/clock.rs", "src/web/shared/tick.rs"]); +} + +#[test] +fn std_instant_reads_top_level_path_attributes_of_a_non_mod_rs_file_from_its_folder() { + let a = r#" +#[cfg(unix)] +#[path = "native.rs"] +mod imp; +#[cfg(unix)] +#[path = "native"] +mod platform { + mod clock; +} +"#; + let clock = "use std::time::Instant;\n"; + let files = [ + ("src/lib.rs", "mod a;\n"), + ("src/a.rs", a), + ("src/native.rs", clock), + ("src/native/clock.rs", clock), + ("src/a/native.rs", clock), + ("src/a/native/clock.rs", clock), + ]; + assert_eq!(finding_paths(&std_instant(files)), vec!["src/a/native.rs", "src/a/native/clock.rs"]); +} + +#[test] +fn std_instant_finds_the_modules_of_a_path_attribute_file_next_to_it() { + let lib = r#" +#[cfg(not(target_arch = "wasm32"))] +#[path = "platform/native.rs"] +mod native; +"#; + let clock = "use std::time::Instant;\n"; + let files = [("src/lib.rs", lib), ("src/platform/native.rs", "mod clock;\n"), ("src/platform/clock.rs", clock), ("src/web.rs", clock)]; + assert_eq!(finding_paths(&std_instant(files)), vec!["src/web.rs"]); +} + +#[test] +fn std_instant_resolves_a_file_loaded_both_plainly_and_by_path_attribute_once_for_each() { + // `platform.rs` is a web module, and a native-only `#[path]` declaration loads it again. Its + // `mod clock;` is `src/platform/clock.rs` for the first and `src/clock.rs` for the second. + let lib = "mod platform;\n#[cfg(unix)]\n#[path = \"platform.rs\"]\nmod native;\n"; + let clock = "use std::time::Instant;\n"; + let files = [("src/lib.rs", lib), ("src/platform.rs", "mod clock;\n"), ("src/platform/clock.rs", clock), ("src/clock.rs", clock)]; + assert_eq!(finding_paths(&std_instant(files)), vec!["src/platform/clock.rs"]); +} + +#[test] +fn std_instant_checks_a_file_that_a_path_attribute_module_names_for_the_web_build() { + // The web module `entry.rs`, loaded through `#[path]`, names `src/clock.rs`, which a native-only + // module also names through `#[path]`. + let lib = "#[path = \"entry.rs\"]\nmod web;\n#[cfg(unix)]\nmod native;\n"; + let clock = "use std::time::Instant;\n"; + let files = + [("src/lib.rs", lib), ("src/entry.rs", "mod clock;\n"), ("src/native.rs", "#[path = \"clock.rs\"]\nmod clock;\n"), ("src/clock.rs", clock)]; + assert_eq!(finding_paths(&std_instant(files)), vec!["src/clock.rs"]); +} + +#[test] +fn std_instant_checks_module_files_declared_deeper_than_its_nesting_limit() { + let lib = format!("{}#[cfg(unix)]\nmod deep;\n{}", "mod m {\n".repeat(70), "}\n".repeat(70)); + let shallow_lib = format!("{}#[cfg(unix)]\nmod deep;\n{}", "mod m {\n".repeat(3), "}\n".repeat(3)); + let deep_path = format!("src/{}deep.rs", "m/".repeat(70)); + let shallow_path = format!("src/{}deep.rs", "m/".repeat(3)); + let clock = "use std::time::Instant;\n"; + let files = [("src/lib.rs", lib.as_str()), (deep_path.as_str(), clock)]; + assert_eq!(finding_paths(&std_instant(files)), vec![deep_path.as_str()]); + let files = [("src/lib.rs", shallow_lib.as_str()), (shallow_path.as_str(), clock)]; + assert!(std_instant(files).is_empty()); +} + +#[test] +fn std_instant_checks_a_crate_root_that_a_disabled_declaration_names() { + let lib = r#" +#[cfg(any())] +#[path = "lib.rs"] +mod never; + +fn web() { + std::time::Instant::now(); +} +"#; + assert_eq!(lines(&std_instant([("src/lib.rs", lib)])), vec![7]); + + // A cycle of two files that only each other name, through declarations the web build leaves out. + let a = "#[cfg(unix)]\n#[path = \"b.rs\"]\nmod b;\nfn web() { std::time::Instant::now(); }\n"; + let b = "#[cfg(unix)]\n#[path = \"a.rs\"]\nmod a;\n"; + assert_eq!(finding_paths(&std_instant([("src/a.rs", a), ("src/b.rs", b)])), vec!["src/a.rs"]); + + // In a cycle with `lib.rs`, only `lib.rs` is a crate root, so its native-only module stays out. + let lib = "#[cfg(unix)]\n#[path = \"a.rs\"]\nmod a;\nfn web() {}\n"; + let a = "#[cfg(any())]\n#[path = \"lib.rs\"]\nmod root;\nuse std::time::Instant;\n"; + assert!(std_instant([("src/lib.rs", lib), ("src/a.rs", a)]).is_empty()); + + // A cycle that a crate root reaches keeps its native-only member out. + let lib = "mod a;\n"; + let a = "#[cfg(unix)]\nmod b;\n"; + let b = "#[path = \"../a.rs\"]\nmod again;\nuse std::time::Instant;\n"; + assert!(std_instant([("src/lib.rs", lib), ("src/a.rs", a), ("src/a/b.rs", b)]).is_empty()); +} + +#[test] +fn wgsl_integer_literals_are_not_floats() { + let shader = r#" +const BIG_ABSTRACT = 400000000000000000000000000000000000000000; +const BIG_U = 1000000000000000000000000000000000000000u; +const BIG_I = 1000000000000000000000000000000000000000i; +const HEX_INT = 0xFFFFFFFFu; +const HEX_F_DIGIT = 0x1f; +const FLOAT_DOT: f32 = 340282356779733661637539395458142568448.0; +const FLOAT_SUFFIX: f32 = 400000000000000000000000000000000000000f; +const NEG_EXP: f32 = -4e38; +"#; + let findings = wgsl_float_literals(shader); + assert_eq!(lines(&findings), vec![7, 8, 9]); + assert_eq!(findings[0].text, "340282356779733661637539395458142568448.0"); + assert_eq!(findings[1].text, "400000000000000000000000000000000000000"); + assert_eq!(findings[2].text, "4e38"); +} + +#[test] +fn wgsl_hex_floats_outside_the_range_are_reported() { + let shader = r#" +const EXACT_MAX: f32 = 0x1.fffffep+127f; +const HUGE: f32 = 0x1p+500f; +const JUST_OVER: f32 = 0x1.000002p+128; +const UPPER: f32 = 0X1P128; +const TINY: f32 = 0x1p-500; +const ZERO: f32 = 0x0.0p99999; +const F16_MAX: f16 = 0x1.ffcp15h; +const F16_OVER: f16 = 0x1.0p16h; +"#; + let findings = wgsl_float_literals(shader); + assert_eq!(lines(&findings), vec![3, 4, 5, 9]); + assert_eq!(findings[0].text, "0x1p+500"); + assert_eq!(findings[3].text, "0x1.0p16"); +} + +#[test] +fn wgsl_literals_after_a_hex_literal_and_an_operator_are_checked() { + let shader = "const A = 0x1.0p0+1e39;\nconst B = 0xFF+1e39;\nconst C = 0x1f-4e38;\nconst D = 0xAu*2.0+5e38;\nconst E = 0x1p1-0x1p+500;\n"; + let findings = wgsl_float_literals(shader); + assert_eq!(lines(&findings), vec![1, 2, 3, 4, 5]); + let texts: Vec<&str> = findings.iter().map(|f| f.text.as_str()).collect(); + assert_eq!(texts, vec!["1e39", "1e39", "4e38", "5e38", "0x1p+500"]); +} + +#[test] +fn wgsl_block_comment_markers_inside_line_comments_do_not_open_a_comment() { + let shader = "// a /* inside a line comment\nconst BAD: f32 = 4e38;\n/* block // with a line marker */ const ALSO_BAD: f32 = 5e38;\n"; + let findings = wgsl_float_literals(shader); + assert_eq!(lines(&findings), vec![2, 3]); +} + +#[test] +fn wgsl_half_floats_use_the_f16_range() { + let shader = "const OK: f16 = 65504.0h;\nconst OVER: f16 = 70000.0h;\nconst F32_OK: f32 = 70000.0f;\n"; + let findings = wgsl_float_literals(shader); + assert_eq!(lines(&findings), vec![2]); + assert_eq!(findings[0].text, "70000.0"); +} + +#[test] +fn wgsl_line_comments_end_and_lines_count_at_every_line_break() { + let shader = "// note\rconst A: f32 = 4e38;\r\nconst B: f32 = 5e38;\n// x\u{2028}const C: f32 = 6e38;\u{2029}const D: f32 = 7e38;\u{000B}// y\u{0085}const E: f32 = 8e38;\u{000C}const F: f32 = 9e38;"; + let findings = wgsl_float_literals(shader); + assert_eq!(lines(&findings), vec![2, 3, 5, 6, 8, 9]); + assert_eq!(findings[0].text, "4e38"); +} + +#[test] +fn wgsl_digits_inside_unicode_identifiers_are_not_literals() { + let shader = "const π1e39 = 1.0;\nconst é2e40: f32 = 2.0;\nconst A: f32 =\u{200E}1e39;\n"; + let findings = wgsl_float_literals(shader); + assert_eq!(lines(&findings), vec![3]); + assert_eq!(findings[0].text, "1e39"); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn test_rust_sources_filtering() { + use craft_testkit::rust_sources; + + let dir = ScratchDir::new("filtering"); + dir.write("src/lib.rs", "pub fn lib() {}"); + dir.write("src/sub/mod.rs", "pub fn sub() {}"); + dir.write("src/tests.rs", "// should exclude"); + dir.write("src/tests_foo.rs", "// should exclude"); + dir.write("src/foo_tests.rs", "// should exclude"); + dir.write("src/foo_test.rs", "// should exclude"); + dir.write("tests/integ.rs", "// should exclude"); + dir.write("target/gen.rs", "// should exclude"); + dir.write(".github/action.rs", "// should exclude dot-dir"); + dir.write("src/README.md", "# Not a rust source"); + + let sources = rust_sources(dir.path()).expect("rust_sources"); + assert_eq!(file_names(&sources), vec!["lib.rs", "mod.rs"]); + + // Test passing &[(String, String)] to guards + let findings = predefined_quit(&sources); + assert!(findings.is_empty()); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn rust_sources_lists_a_root_inside_a_folder_named_tests() { + let dir = ScratchDir::new("root_in_tests"); + dir.write("tests/app/src/lib.rs", "pub fn lib() {}"); + let sources = craft_testkit::rust_sources(dir.path().join("tests").join("app")).expect("rust_sources"); + assert_eq!(file_names(&sources), vec!["lib.rs"]); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn rust_sources_skips_build_output_folders_marked_with_cachedir_tag() { + let dir = ScratchDir::new("cachedir"); + dir.write("src/lib.rs", "pub fn lib() {}"); + dir.write("out/CACHEDIR.TAG", "Signature: 8a477f597d28d172789f06886806bc55\n# build output\n"); + dir.write("out/debug/build/gen.rs", "pub fn generated() {}"); + dir.write("notes/CACHEDIR.TAG", "not a cache tag\n"); + dir.write("notes/kept.rs", "pub fn kept() {}"); + let sources = craft_testkit::rust_sources(dir.path()).expect("rust_sources"); + assert_eq!(file_names(&sources), vec!["kept.rs", "lib.rs"]); +} + +#[cfg(unix)] +#[test] +fn rust_sources_does_not_read_a_cachedir_tag_through_a_symlink() { + let outside = ScratchDir::new("cachedir_link_target"); + outside.write("CACHEDIR.TAG", "Signature: 8a477f597d28d172789f06886806bc55\n"); + let dir = ScratchDir::new("cachedir_link"); + dir.write("src/lib.rs", "pub fn lib() {}"); + dir.write("linked/kept.rs", "pub fn kept() {}"); + std::os::unix::fs::symlink(outside.path().join("CACHEDIR.TAG"), dir.path().join("linked").join("CACHEDIR.TAG")).expect("symlink marker"); + let sources = craft_testkit::rust_sources(dir.path()).expect("rust_sources"); + assert_eq!(file_names(&sources), vec!["kept.rs", "lib.rs"]); +} + +#[cfg(unix)] +#[test] +fn rust_sources_does_not_open_a_cachedir_tag_fifo() { + let dir = ScratchDir::new("cachedir_fifo"); + dir.write("src/lib.rs", "pub fn lib() {}"); + dir.write("piped/kept.rs", "pub fn kept() {}"); + let fifo = dir.path().join("piped").join("CACHEDIR.TAG"); + match std::process::Command::new("mkfifo").arg(&fifo).status() { + Ok(status) if status.success() => {} + other => { + eprintln!("skipped: mkfifo is not available here ({other:?})"); + return; + } + } + // Opening the FIFO for reading and writing does not wait for another end on Linux or macOS. The + // signature waits in the pipe, so a scan that opened the FIFO would read it and leave out `kept.rs` + // without blocking. + let mut pipe = std::fs::OpenOptions::new().read(true).write(true).open(&fifo).expect("open FIFO"); + std::io::Write::write_all(&mut pipe, b"Signature: 8a477f597d28d172789f06886806bc55\n").expect("write signature"); + let sources = craft_testkit::rust_sources(dir.path()).expect("rust_sources"); + drop(pipe); + assert_eq!(file_names(&sources), vec!["kept.rs", "lib.rs"]); +} + +#[cfg(unix)] +#[test] +fn rust_sources_does_not_follow_a_symlink_out_of_the_root() { + let outside = ScratchDir::new("symlink_outside"); + outside.write("evil.rs", "pub fn evil() {}"); + let dir = ScratchDir::new("symlink_root"); + dir.write("src/lib.rs", "pub fn lib() {}"); + std::os::unix::fs::symlink(outside.path(), dir.path().join("src").join("linked")).expect("symlink folder"); + std::os::unix::fs::symlink(outside.path().join("evil.rs"), dir.path().join("src").join("evil.rs")).expect("symlink file"); + let sources = craft_testkit::rust_sources(dir.path()).expect("rust_sources"); + assert_eq!(file_names(&sources), vec!["lib.rs"]); +} + +#[cfg(unix)] +#[test] +fn rust_sources_does_not_loop_on_a_symlink_cycle() { + let dir = ScratchDir::new("symlink_cycle"); + dir.write("src/lib.rs", "pub fn lib() {}"); + std::os::unix::fs::symlink(dir.path().join("src"), dir.path().join("src").join("again")).expect("symlink cycle"); + let sources = craft_testkit::rust_sources(dir.path()).expect("rust_sources"); + assert_eq!(file_names(&sources), vec!["lib.rs"]); +} + +#[cfg(unix)] +#[test] +fn rust_sources_skips_a_broken_symlink() { + let dir = ScratchDir::new("symlink_broken"); + dir.write("src/lib.rs", "pub fn lib() {}"); + std::os::unix::fs::symlink(dir.path().join("missing.rs"), dir.path().join("src").join("gone.rs")).expect("symlink file"); + std::os::unix::fs::symlink(dir.path().join("missing"), dir.path().join("src").join("gone")).expect("symlink folder"); + let sources = craft_testkit::rust_sources(dir.path()).expect("rust_sources"); + assert_eq!(file_names(&sources), vec!["lib.rs"]); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn scratch_dirs_leave_existing_folders_alone_and_clean_up_on_panic() { + let taken = std::env::temp_dir().join(format!("craft_testkit_sentinel_{}_0", std::process::id())); + std::fs::create_dir(&taken).expect("create the folder the helper would pick first"); + std::fs::write(taken.join("sentinel"), "keep").expect("write sentinel"); + + let made = std::panic::catch_unwind(|| { + let dir = ScratchDir::new("sentinel"); + let path = dir.path().to_path_buf(); + assert!(path.exists()); + std::panic::panic_any(path); + }); + let payload = made.expect_err("the closure panics"); + let path = payload.downcast_ref::().expect("panic payload is the path").clone(); + + assert_ne!(path, taken); + assert!(!path.exists(), "the scratch folder is removed when the test panics"); + assert_eq!(std::fs::read_to_string(taken.join("sentinel")).expect("sentinel survives"), "keep"); + std::fs::remove_dir_all(&taken).expect("remove the pre-made folder"); +} diff --git a/crates/testkit/tests/source_limits.rs b/crates/testkit/tests/source_limits.rs new file mode 100644 index 000000000..a90b7f4cf --- /dev/null +++ b/crates/testkit/tests/source_limits.rs @@ -0,0 +1,99 @@ +#![cfg(not(target_arch = "wasm32"))] + +use craft_testkit::{SourceLimits, rust_sources_with_limits}; +use std::io::ErrorKind; +use std::path::{Path, PathBuf}; + +/// A scratch folder that this test made and removes on drop, also when the test panics. +struct ScratchDir(PathBuf); + +impl ScratchDir { + fn new(tag: &str) -> Self { + let base = std::env::temp_dir(); + for n in 0..1000u32 { + let path = base.join(format!("craft_testkit_limits_{tag}_{}_{n}", std::process::id())); + match std::fs::create_dir(&path) { + Ok(()) => return Self(path), + Err(e) if e.kind() == ErrorKind::AlreadyExists => continue, + Err(e) => panic!("create scratch folder {}: {e}", path.display()), + } + } + panic!("no free scratch folder name for {tag}"); + } + + fn path(&self) -> &Path { + &self.0 + } + + fn write(&self, relative: &str, content: &str) { + let path = self.0.join(relative); + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent).expect("create parent folder"); + } + std::fs::write(path, content).expect("write file"); + } +} + +impl Drop for ScratchDir { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } +} + +fn three_files(tag: &str) -> ScratchDir { + let dir = ScratchDir::new(tag); + dir.write("src/a.rs", "0123456789"); + dir.write("src/b.rs", "0123456789"); + dir.write("src/c.rs", "0123456789"); + dir +} + +#[test] +fn defaults_are_documented_values() { + let limits = SourceLimits::default(); + assert_eq!(limits.max_file_bytes, 8 << 20); + assert_eq!(limits.max_total_bytes, 256 << 20); + assert_eq!(limits.max_files, 50_000); +} + +#[test] +fn files_within_the_limits_are_read() { + let dir = three_files("within"); + let limits = SourceLimits { max_file_bytes: 10, max_total_bytes: 30, max_files: 3 }; + let sources = rust_sources_with_limits(dir.path(), limits).expect("within limits"); + assert_eq!(sources.len(), 3); + assert!(sources.iter().all(|(_, content)| content == "0123456789")); +} + +#[test] +fn a_file_over_the_size_limit_is_an_error() { + let dir = three_files("file"); + let limits = SourceLimits { max_file_bytes: 9, ..SourceLimits::default() }; + let err = rust_sources_with_limits(dir.path(), limits).expect_err("file over the limit"); + assert_eq!(err.kind(), ErrorKind::FileTooLarge); +} + +#[test] +fn files_over_the_total_limit_are_an_error() { + let dir = three_files("total"); + let limits = SourceLimits { max_total_bytes: 29, ..SourceLimits::default() }; + let err = rust_sources_with_limits(dir.path(), limits).expect_err("total over the limit"); + assert_eq!(err.kind(), ErrorKind::QuotaExceeded); +} + +#[test] +fn more_files_than_the_count_limit_is_an_error() { + let dir = three_files("count"); + let limits = SourceLimits { max_files: 2, ..SourceLimits::default() }; + let err = rust_sources_with_limits(dir.path(), limits).expect_err("too many files"); + assert_eq!(err.kind(), ErrorKind::QuotaExceeded); +} + +#[test] +fn files_the_filter_skips_do_not_count_against_the_limits() { + let dir = three_files("skipped"); + dir.write("src/tests.rs", &"x".repeat(100)); + dir.write("tests/big.rs", &"x".repeat(100)); + let limits = SourceLimits { max_file_bytes: 10, max_total_bytes: 30, max_files: 3 }; + assert_eq!(rust_sources_with_limits(dir.path(), limits).expect("skipped files are not read").len(), 3); +}