From 7e243276880416fdd34cae02b5dfbb5efcf86d98 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 14 Nov 2026 14:59:56 -0800 Subject: [PATCH 01/12] craft-xtask: add the display-name scanner for words the app lists `craft_xtask::names` reads the words an app lists in its Rust string literals (escapes decoded, test-only code skipped), the package description of a `Cargo.toml` and text files, and returns each finding with its path and line. The app passes the word list, the inputs, the literals and exceptions that may keep a word, and its allow marker. No app switches yet; the crate README lists the planned switches and the results that change for each app. --- crates/xtask/README.md | 93 +- crates/xtask/src/lib.rs | 3 + crates/xtask/src/names.rs | 1790 +++++++++++++++++++++++++++++++++++++ 3 files changed, 1885 insertions(+), 1 deletion(-) create mode 100644 crates/xtask/src/names.rs diff --git a/crates/xtask/README.md b/crates/xtask/README.md index 75228c3d66..2ae5c66e5a 100644 --- a/crates/xtask/README.md +++ b/crates/xtask/README.md @@ -7,6 +7,7 @@ The crate is native only and is a dependency of an app's `xtask` package only. | Module | Feature | Contents | |---|---|---| | `runner` | always | `Step`, `run_steps` (steps in order, stop at the first failure, the `ok`, `FAIL` and `skip` summary), `root`, `cargo`, `run`, `run_cancelable`, `nextest_or_test`, `doctest`, `Cancel` | +| `names` | always | `Policy`, `Input`, `scan`, `scan_source`, `literals`, `test_path`: the display-name scanner, which looks for the words an app lists in its string literals, `Cargo.toml` descriptions and text files | | `lock` | `lock` | `LockConfig`, `try_acquire`, `acquire`, `holders`: 1 to 64 slots held across processes through file locks in a directory the app chooses | | `report` | `report` | `write` and `to_json`: the per-step JSON report (schema 1), replaced atomically through `craft-atomic` | @@ -59,6 +60,86 @@ or file could not be opened or locked) or `Report` (the run passed but the repor written). A report that cannot be written after a failed run is printed to stderr, and the run keeps its `Step` error. +## Names + +The app owns its word list, the paths it scans, the literals and exceptions that may keep a word, +the marker, the command or test that runs the scan, and the message it prints. The library reads the +files and returns the findings: + +```rust,no_run +use std::path::Path; +use craft_xtask::names::{self, Input, Policy}; + +const BANNED: &[&str] = &["Amber Tool", "AzureKit"]; +const ALLOWED: &[&str] = &["AzureKit Design"]; +const POLICY: Policy<'static> = Policy { allowed: ALLOWED, ..Policy::new(BANNED) }; + +#[test] +fn production_display_names_stay_neutral() { + let base = Path::new(env!("CARGO_MANIFEST_DIR")); + let inputs = [Input::rust(Path::new("src")), Input::rust(Path::new("../engine/src/cmd"))]; + let found = names::scan(base, &inputs, &POLICY).unwrap(); + let list: Vec = found.iter().map(ToString::to_string).collect(); + assert!(list.is_empty(), "Banned display names:\n{}", list.join("\n")); +} +``` + +- `Input::rust` takes a Rust file or a folder, which is read recursively for `.rs` files that + `Policy::skip` keeps, given their path below the folder (`test_path` by default: `tests.rs`, + `*_tests.rs` and files under a `tests` folder). Links to folders are not followed. A file input + is read whatever its name. +- `Input::manifest` reads the values at the key paths `package.description` and + `workspace.package.description` of a `Cargo.toml`: the table header (`[package]`, + `[workspace.package]`) joined with the key, which may be bare, quoted or dotted. Headers and keys + inside strings and arrays do not count, and a key inside an inline table is not read. A + `description` under any other table, or before the first header, is not read. A TOML string, + single-line or multi-line, is decoded, and a comment after it is not read; any other value is + read as the rest of its line. `Input::text` reads every line of a text file, and + `Input::text_mark_above` also lets a `#` line with the marker exempt the line below it. Inputs + must not include binary files. +- In Rust, the scanner reads `"…"` and `r"…"` literals with any number of `#`, decodes every escape + (`\n`, `\x7A`, `\u{…}`, a backslash before a line break) and reads CRLF as LF. `b"…"`, `br"…"`, + `c"…"` and `cr"…"` are read when `Policy::literals` asks for them. Comments, character, byte and + lifetime tokens, and the rest of a module after `#![cfg(test)]` are skipped. So is the code under + `#[cfg(test)]`, comments in the attribute included: a `const`, `static` or `let` up to its `;`, + another item up to the `}` that closes it or the `;` that ends it, and a field, argument, arm or + statement up to its `,` or `;`. A block expression (a block, a macro's braces, a labeled block, or + `if`, `match`, `loop`, `while`, `for`, `unsafe`, `async` or `const`), as the whole statement or as + an arm's expression after `=>`, ends at its `}`, or at the last `}` of an `if` with `else` + branches. When a method call, field or `?` follows that `}`, it runs up to its `,` or `;` + instead. Other `cfg` expressions and macros are not evaluated. A source the lexer cannot read + (an unterminated literal or comment, an unknown escape, unbalanced delimiters) is an error. +- A word matches as a whole word: the characters next to it are not letters, digits or `_`, in any + script. `MatchCase::Exact` compares characters exactly, and `MatchCase::AsciiInsensitive` ignores + the case of ASCII letters. `allowed` and `exceptions` exempt a whole decoded literal or + description or trimmed text line, never part of one. +- In Rust, the marker counts only inside a comment on a line the literal spans. In a manifest it + counts anywhere on a line the description spans, and in a text file anywhere on the line. +- `scan` reads each file once, in path order, and returns the findings in path order, then source + order, then the order of `banned`. A `Finding` has the path, the first line of the literal or + description, the word, the decoded text or text line and the kind; its `Display` text is + `path:line: "word" in "text"`. `scan_source` scans one source already in memory, and + `literals` returns a source's literals. +- `names::Error` is `Policy` (no banned words, an empty word or an empty marker), `Io` (with the + path), `InvalidUtf8`, `FileTooLarge`, `TotalTooLarge`, `TooDeep`, `Conflict` (a file given as two + kinds of input) or `Syntax` (with the path and line). `DEFAULT_LIMITS` is 8 MiB per file, 512 MiB + per scan and folders 64 deep; a file over the limit is rejected before it is read. + +The source copies differ in a few rules. An app that switches lists the results that change: + +- The eight `display_names.rs` tests compare exactly (`MatchCase::Exact`), read byte and C strings + like other strings, skip only `#[cfg(test)] mod` (GridCraft skips any `#[cfg(test)]` item up to + its `}` or `;`), pass `\x` and `\u{…}` through undecoded, can take the quote in `'\"'` as the + start of a string (LightCraft, FigCraft, PhotoCraft), follow folder links, skip a file root named + like a test file, and stop with a panic on a file they cannot read (FigCraft returns the error). + Each finds more or less here accordingly. +- VectorCraft's `brands` check compares ASCII letters without case (`MatchCase::AsciiInsensitive`) + and reads byte and C strings (`Literals`). It also takes a non-ASCII letter or `_` next to a word + as a word boundary, takes its marker anywhere on a line, literals included, reads an escape as a + space, reads manifest lines that start with `description` as they are (comments included, one + line of a multi-line value), and skips a file it cannot read. +- PhotoCraft's check of its locale catalogs stays in the app. + ## Lock `RunOptions::with_lock(LockConfig::new(dir, slots, owner_label))` takes one of `slots` lock files in @@ -98,7 +179,15 @@ Machine, GPU, corpus and skip details stay in the app's own report. | `runner`: the step loop and summary, `root`, `cargo`, `run`, the nextest probe and fallback, doctests | DesignCraft `xtask/src/main.rs:68-82`, `:160-201`, `:204-249` at [8b169f8](https://github.com/storytold/designcraft/blob/8b169f8ee7832e60ed1ac4c9f5dd34f954d3a992/xtask/src/main.rs) | | `lock` and its tests `one_slot_is_exclusive_until_dropped`, `more_slots_admit_more_runs` | LightCraft `xtask/src/ci_lock.rs` at [25ffe3e](https://github.com/storytold/lightcraft/blob/25ffe3e5d4ac754829b99dd7368d94185adbf8d5/xtask/src/ci_lock.rs) | | `report`: the per-step fields | LightCraft `xtask/src/ci.rs:347-414` at [25ffe3e](https://github.com/storytold/lightcraft/blob/25ffe3e5d4ac754829b99dd7368d94185adbf8d5/xtask/src/ci.rs#L347-L414) | -| The root and slot-count errors, the `--exclude` of the xtask package, cancellation, the bounded owner read and the atomic report | written here | +| `names`: the literal lexer, whole-word matching, allowed literals, the tree walk and the tests `production_display_names_stay_neutral`, `guard_distinguishes_labels_from_identifiers_and_comments`, `guard_scans_production_after_test_only_items`, `guard_skips_escaped_character_literals`, `guard_decodes_whitespace_escapes` | CADCraft `crates/ui-egui/tests/display_names.rs` at [8e69890](https://github.com/storytold/cadcraft/blob/8e6989022398ff5f79adaab77d19fdd7137591f3/crates/ui-egui/tests/display_names.rs) and DeckCraft at [b0c77eb](https://github.com/storytold/deckcraft/blob/b0c77ebd6a6fe6c9037cfbe1c8a7f397304b2205/crates/ui-egui/tests/display_names.rs) | +| `names`: the test `guard_scans_after_escaped_character_literals` | AniCraft `crates/engine/tests/display_names.rs` at [3435e98](https://github.com/storytold/anicraft/blob/3435e9868150312e54694c0a70082609b2fe8573/crates/engine/tests/display_names.rs) | +| `names`: read errors returned, and the test `source_read_failures_are_reported` | FigCraft `crates/mcp/tests/display_names.rs` at [bad07e4](https://github.com/storytold/figcraft/blob/bad07e4433240cad046589129c084bd2fc36f097/crates/mcp/tests/display_names.rs) | +| `names`: the tests `guard_scans_production_after_test_only_items_and_parses_raw_labels`, `guard_skips_escaped_character_literals_without_hiding_labels`, and `exceptions` for a group-label prefix | WordCraft `crates/ui-egui/tests/display_names.rs` at [323cf78](https://github.com/storytold/wordcraft/blob/323cf78232ff3c07f9dbb7989ed729d5851c9773/crates/ui-egui/tests/display_names.rs) | +| `names`: `exceptions` for a message prefix | LightCraft `crates/ui-egui/tests/display_names.rs` at [6b2b7e6](https://github.com/storytold/lightcraft/blob/6b2b7e6d1117e683a1701568e56f979d061e4699/crates/ui-egui/tests/display_names.rs) | +| `names`: a single file as a root | GridCraft `crates/ui-egui/tests/display_names.rs` at [f204433](https://github.com/storytold/gridcraft/blob/f2044337c7e195bdf6bec1b3914937a3dd386749/crates/ui-egui/tests/display_names.rs) | +| `names`: allowed literals | PhotoCraft `crates/ui-egui/tests/display_names.rs` at [d6557b9](https://github.com/storytold/photocraft/blob/d6557b9a8b40f0542a99e7382ec2b5250548e27b/crates/ui-egui/tests/display_names.rs) | +| `names`: `MatchCase::AsciiInsensitive`, manifest descriptions, text files, the marker and the line above it, the literal's first line, and the tests `rust_literals_skip_comments_identifiers_tests_and_marked_lines`, `whole_words_only`, `docs_packaging_and_manifests`, `scope`, `injected_strings_fail_the_gate`, `the_tree_is_clean` | VectorCraft `xtask/src/brands.rs` at [107078a](https://github.com/storytold/vectorcraft/blob/107078ad10870f8a8fdeade9bd5627a3eea34a67/xtask/src/brands.rs) | +| The root and slot-count errors, the `--exclude` of the xtask package, cancellation, the bounded owner read and the atomic report; in `names`, the full escape decoder, byte and C strings as an option, every `#[cfg(test)]` item form and `#![cfg(test)]`, the syntax errors, the marker only in comments, word boundaries in every script, the limits, the sorted findings and the skipped folder links | written here | ## Migration @@ -107,6 +196,8 @@ No app uses this crate yet. | App | Status | |---|---| | DesignCraft | Planned: `cmd_ci`, `root`, `cargo`, `run`, `cmd_test` and `cmd_doctest` move to `runner`, with `TestMode::PreferNextest`. The step list, `TEST_TARGETS` and the i18n report stay in the app. | +| AniCraft, CADCraft, DeckCraft, FigCraft, GridCraft, LightCraft, PhotoCraft, WordCraft | Planned: the lexer and matcher of `display_names.rs` move to `names`, with `MatchCase::Exact`. `BANNED`, `ALLOWED`, `ROOTS`, the prefix exceptions (LightCraft, WordCraft), PhotoCraft's catalog test and the tests named after them stay in the app. | +| VectorCraft | Planned: `cargo xtask brands` calls `names::scan` with `MatchCase::AsciiInsensitive`, byte and C strings, `brand-ok` and its desktop entry as `Input::text_mark_above`. The word list, the file selection from `git ls-files` and the message stay in the app. | | LightCraft | Planned: `ci_lock.rs` moves to `lock`, with `LIGHTCRAFT_CI_LOCK` and `LIGHTCRAFT_CI_SLOTS` read by the app. The machine, GPU, corpus and skip report stays in the app. | Maintainers: the craft-libs maintainers, until a responsible maintainer or team is agreed in the diff --git a/crates/xtask/src/lib.rs b/crates/xtask/src/lib.rs index 2ed8390e00..7abdc46b08 100644 --- a/crates/xtask/src/lib.rs +++ b/crates/xtask/src/lib.rs @@ -9,6 +9,8 @@ //! - [`runner`]: the gate's step runner. [`runner::run_steps`] runs the steps in order, stops at the //! first failure and prints the `ok`, `FAIL` and `skip` summary. [`runner::root`], //! [`runner::cargo`], [`runner::run`] and [`runner::nextest_or_test`] build and run the commands. +//! - [`names`]: the display-name scanner. [`names::scan`] looks for the words an app lists in its +//! Rust string literals, `Cargo.toml` descriptions and text files, and returns what it finds. //! - `lock` (feature `lock`): a fixed number of slots, held across processes through file locks, so //! that gate runs in several checkouts of one machine take turns. //! - `report` (feature `report`): the per-step JSON report, replaced atomically. @@ -37,6 +39,7 @@ #[cfg(feature = "lock")] pub mod lock; +pub mod names; #[cfg(feature = "report")] pub mod report; pub mod runner; diff --git a/crates/xtask/src/names.rs b/crates/xtask/src/names.rs new file mode 100644 index 0000000000..57d4f89de8 --- /dev/null +++ b/crates/xtask/src/names.rs @@ -0,0 +1,1790 @@ +//! The display-name scanner: words an app must not show users, looked for in the text users read. +//! +//! The app supplies the words, the files and folders to read, the literals that may keep a word, +//! and the marker that exempts a line. The library reads: +//! +//! - [`Kind::Rust`]: the string literals of Rust sources, with escapes decoded, outside comments and +//! test-only code. A folder is read recursively for `.rs` files. +//! - [`Kind::Manifest`]: the package descriptions of a `Cargo.toml`. +//! - [`Kind::Text`] and [`Kind::TextMarkAbove`]: every line of a text file. +//! +//! It returns the [`Finding`]s; the app prints them and decides the exit status. The scan is +//! lexical: it does not resolve macros or evaluate `cfg` expressions other than `cfg(test)`, and it +//! cannot tell whether a literal is shown. It never starts a process or calls git. + +use std::collections::HashSet; +use std::fmt; +use std::fs::{self, File}; +use std::io::{self, Read}; +use std::path::{Path, PathBuf}; + +/// The limits [`Policy::new`] sets: 8 MiB per file, 512 MiB in all, folders 64 deep. +pub const DEFAULT_LIMITS: Limits = Limits { max_file_bytes: 8 << 20, max_total_bytes: 512 << 20, max_depth: 64 }; + +/// How a banned word is compared with the text. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum MatchCase { + /// Character for character. + Exact, + /// ASCII letters compare without case; other characters compare exactly. + AsciiInsensitive, +} + +/// Which Rust literal forms besides `"…"` and `r"…"` are read. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct Literals { + /// `b"…"` and `br"…"`. + pub byte_strings: bool, + /// `c"…"` and `cr"…"`. + pub c_strings: bool, +} + +/// Bounds on what [`scan`] reads. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Limits { + /// The largest file read, in bytes. + pub max_file_bytes: u64, + /// The most bytes read by one scan, across all files. + pub max_total_bytes: u64, + /// The deepest folder read below a [`Kind::Rust`] folder input. + pub max_depth: usize, +} + +/// The app's rules for one scan. +#[derive(Clone, Copy)] +pub struct Policy<'a> { + /// The words users must not see. A word matches only as a whole word: the characters next to it + /// are not letters, digits or `_`. + pub banned: &'a [&'a str], + /// How the words are compared. + pub case: MatchCase, + /// Whole literals (or text lines, trimmed) that may keep a banned word. + pub allowed: &'a [&'a str], + /// Functions that return `true` for a whole literal or trimmed text line that may keep a banned + /// word. + pub exceptions: &'a [fn(&str) -> bool], + /// A marker that exempts a line. In Rust it counts only inside a comment on a line the literal + /// spans; in a text file it counts anywhere on the line, and in [`Kind::TextMarkAbove`] files also + /// on a `#` line just above. + pub allow_marker: Option<&'a str>, + /// The optional Rust literal forms. + pub literals: Literals, + /// Returns `true` for a Rust file in a folder input to leave out, given its path below that + /// folder. [`test_path`] is the default. A file input is read whatever this returns. + pub skip: fn(&Path) -> bool, + /// Bounds on what is read. + pub limits: Limits, +} + +impl<'a> Policy<'a> { + /// A policy with these banned words, exact comparison, no allowed literals, exceptions or + /// marker, only `"…"` and `r"…"` literals, [`test_path`] and [`DEFAULT_LIMITS`]. + pub const fn new(banned: &'a [&'a str]) -> Self { + Policy { + banned, + case: MatchCase::Exact, + allowed: &[], + exceptions: &[], + allow_marker: None, + literals: Literals { byte_strings: false, c_strings: false }, + skip: test_path, + limits: DEFAULT_LIMITS, + } + } + + /// The banned words in `text`, in the order of [`Policy::banned`]. Empty when `text` is allowed + /// or an exception returns `true` for it. + pub fn words_in(&self, text: &str) -> Vec<&'a str> { + if self.allowed.contains(&text) || self.exceptions.iter().any(|exempt| exempt(text)) { + return Vec::new(); + } + self.banned.iter().copied().filter(|word| contains_word(text, word, self.case)).collect() + } + + fn validate(&self) -> Result<(), Error> { + if self.banned.is_empty() { + return Err(Error::Policy("the list of banned words is empty")); + } + if self.banned.iter().any(|word| word.trim().is_empty()) { + return Err(Error::Policy("a banned word is empty")); + } + if self.allow_marker.is_some_and(|marker| marker.trim().is_empty()) { + return Err(Error::Policy("the allow marker is empty")); + } + if self.limits.max_file_bytes == 0 || self.limits.max_total_bytes == 0 { + return Err(Error::Policy("a byte limit is zero")); + } + Ok(()) + } +} + +/// The apps' rule for test code: a file named `tests.rs` or ending in `_tests.rs`, or a file under +/// a folder named `tests`. +pub fn test_path(path: &Path) -> bool { + let file = path.file_name().map(|name| name.to_string_lossy()).unwrap_or_default(); + file == "tests.rs" || file.ends_with("_tests.rs") || path.parent().is_some_and(|dir| dir.components().any(|part| part.as_os_str() == "tests")) +} + +/// What a path holds and how it is read. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum Kind { + /// A Rust file, or a folder whose `.rs` files are read. + Rust, + /// A `Cargo.toml`, whose `package.description` and `workspace.package.description` values are + /// read, decoded when they are TOML strings. + Manifest, + /// A text file, every line. + Text, + /// A text file whose comments take whole `#` lines (a desktop entry): a `#` line with the marker + /// also exempts the line below it. + TextMarkAbove, +} + +/// One path to scan, relative to the base folder [`scan`] takes, or absolute. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Input<'a> { + /// How the path is read. + pub kind: Kind, + /// The file or, for [`Kind::Rust`], folder. + pub path: &'a Path, +} + +impl<'a> Input<'a> { + /// A Rust file or folder. + pub fn rust(path: &'a Path) -> Self { + Input { kind: Kind::Rust, path } + } + + /// A `Cargo.toml`. + pub fn manifest(path: &'a Path) -> Self { + Input { kind: Kind::Manifest, path } + } + + /// A text file. + pub fn text(path: &'a Path) -> Self { + Input { kind: Kind::Text, path } + } + + /// A text file whose `#` lines can mark the line below them. + pub fn text_mark_above(path: &'a Path) -> Self { + Input { kind: Kind::TextMarkAbove, path } + } +} + +/// A banned word found in a file. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Finding { + /// The file, as the input named it, joined with the path below a folder input. + pub path: PathBuf, + /// The 1-based line the literal starts on, or the text line. + pub line: usize, + /// The banned word, as [`Policy::banned`] lists it. + pub word: String, + /// The decoded literal or description, or the trimmed text line. + pub text: String, + /// How the file was read. + pub kind: Kind, +} + +impl fmt::Display for Finding { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}:{}: {:?} in {:?}", self.path.display(), self.line, self.word, self.text) + } +} + +/// A Rust source the scanner cannot read as tokens. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct SyntaxError { + /// The 1-based line the problem starts on. + pub line: usize, + /// What is wrong. + pub message: &'static str, +} + +impl fmt::Display for SyntaxError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "line {}: {}", self.line, self.message) + } +} + +/// Why a scan did not finish. +#[derive(Debug)] +pub enum Error { + /// The policy cannot be used. + Policy(&'static str), + /// A file or folder could not be read. + Io { + /// The path, as reported. + path: PathBuf, + /// The cause. + source: io::Error, + }, + /// A file is not UTF-8. + InvalidUtf8 { + /// The path, as reported. + path: PathBuf, + }, + /// A file is larger than [`Limits::max_file_bytes`]. + FileTooLarge { + /// The path, as reported. + path: PathBuf, + /// The limit, in bytes. + limit: u64, + }, + /// Reading this file took the scan past [`Limits::max_total_bytes`]. + TotalTooLarge { + /// The path, as reported. + path: PathBuf, + /// The limit, in bytes. + limit: u64, + }, + /// A folder is deeper than [`Limits::max_depth`]. + TooDeep { + /// The path, as reported. + path: PathBuf, + /// The limit. + limit: usize, + }, + /// A file is given as two kinds of input. + Conflict { + /// The path, as reported. + path: PathBuf, + }, + /// A Rust source could not be read as tokens. + Syntax { + /// The path, as reported. + path: PathBuf, + /// Where and what. + error: SyntaxError, + }, +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Error::Policy(m) => write!(f, "policy: {m}"), + Error::Io { path, source } => write!(f, "{}: {source}", path.display()), + Error::InvalidUtf8 { path } => write!(f, "{}: not UTF-8", path.display()), + Error::FileTooLarge { path, limit } => write!(f, "{}: larger than {limit} bytes", path.display()), + Error::TotalTooLarge { path, limit } => write!(f, "{}: the scan read more than {limit} bytes", path.display()), + Error::TooDeep { path, limit } => write!(f, "{}: more than {limit} folders deep", path.display()), + Error::Conflict { path } => write!(f, "{}: given as two kinds of input", path.display()), + Error::Syntax { path, error } => write!(f, "{}: {error}", path.display()), + } + } +} + +impl std::error::Error for Error { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + Error::Io { source, .. } => Some(source), + _ => None, + } + } +} + +impl From for String { + fn from(e: Error) -> Self { + e.to_string() + } +} + +/// Every finding in `inputs`, read from `base`. Files are read once each, in path order, and the +/// findings come in path order, then in source order, then in the order of [`Policy::banned`]. +/// +/// A [`Kind::Rust`] folder is read recursively for `.rs` files that [`Policy::skip`] keeps; links to +/// folders are not followed. A file input is read whatever its name. Every error stops the scan: a +/// missing input, a file that cannot be read, is not UTF-8 or is over a limit, and a Rust source the +/// lexer cannot read. Inputs must not include binary files. +pub fn scan(base: &Path, inputs: &[Input<'_>], policy: &Policy<'_>) -> Result, Error> { + policy.validate()?; + let mut files = Vec::new(); + for input in inputs { + if input.kind == Kind::Rust { + collect_rust(base, input.path, policy, &mut files)?; + } else { + files.push((input.path.to_path_buf(), input.kind)); + } + } + files.sort(); + files.dedup(); + if let Some(pair) = files.windows(2).find(|pair| pair[0].0 == pair[1].0) { + return Err(Error::Conflict { path: pair[0].0.clone() }); + } + let mut total = 0; + let mut findings = Vec::new(); + for (path, kind) in &files { + let source = read_limited(&base.join(path), path, &policy.limits, &mut total)?; + findings.extend(scan_source(path, &source, *kind, policy)?); + } + Ok(findings) +} + +/// The findings in one source that is already in memory. `path` is only reported. A +/// [`Kind::Rust`] source is a single file here. +pub fn scan_source(path: &Path, source: &str, kind: Kind, policy: &Policy<'_>) -> Result, Error> { + policy.validate()?; + let finding = + |line: usize, word: &str, text: &str| Finding { path: path.to_path_buf(), line, word: word.to_string(), text: text.to_string(), kind }; + let mut out = Vec::new(); + if kind == Kind::Rust { + let lexed = lex(source, policy.literals, policy.allow_marker).map_err(|error| Error::Syntax { path: path.to_path_buf(), error })?; + for lit in &lexed.literals { + if (lit.first_line..=lit.last_line).any(|line| lexed.marked_lines.contains(&line)) { + continue; + } + out.extend(policy.words_in(&lit.text).into_iter().map(|word| finding(lit.first_line, word, &lit.text))); + } + return Ok(out); + } + let lines: Vec<&str> = source.lines().collect(); + let marked = |line: &str| policy.allow_marker.is_some_and(|marker| line.contains(marker)); + if kind == Kind::Manifest { + for (first, last, text) in descriptions(source) { + if lines.get(first - 1..last.min(lines.len())).is_some_and(|span| span.iter().any(|line| marked(line))) { + continue; + } + out.extend(policy.words_in(&text).into_iter().map(|word| finding(first, word, &text))); + } + return Ok(out); + } + for (i, line) in lines.iter().enumerate() { + let above = i.checked_sub(1).and_then(|j| lines.get(j)).is_some_and(|l| l.trim_start().starts_with('#') && marked(l)); + if marked(line) || (kind == Kind::TextMarkAbove && above) { + continue; + } + let text = line.trim(); + out.extend(policy.words_in(text).into_iter().map(|word| finding(i + 1, word, text))); + } + Ok(out) +} + +/// The package descriptions of a `Cargo.toml`, the values at the key paths `package.description` +/// and `workspace.package.description`: the 1-based first and last line of each, and its text. A +/// TOML string is decoded; any other value is the rest of its line, trimmed. Table headers count +/// outside strings and arrays; keys inside inline tables are not read. +fn descriptions(source: &str) -> Vec<(usize, usize, String)> { + let mut out = Vec::new(); + let (mut offset, mut number, mut resume, mut depth) = (0, 0, 0, 0); + // The current table's key path, or `None` under an array of tables or an unreadable header. + let mut table = Some(Vec::new()); + for line in source.split_inclusive('\n') { + let start = offset; + offset += line.len(); + number += 1; + if start < resume { + continue; + } + let top = depth == 0; + resume = toml_line_end(source, start, line.len(), &mut depth); + if !top { + continue; + } + // A byte order mark may start the file. + let body = if start == 0 { line.strip_prefix('\u{feff}').unwrap_or(line) } else { line }; + let trimmed = body.trim_start_matches([' ', '\t']); + if trimmed.starts_with('[') { + table = toml_header(trimmed); + depth = 0; + continue; + } + let Some((key, value)) = toml_key(body).and_then(|(key, rest)| Some((key, rest.strip_prefix('=')?.trim_start()))) else { + continue; + }; + let path: Vec<&str> = table.iter().flatten().chain(&key).map(String::as_str).collect(); + if table.is_none() || !matches!(path.as_slice(), ["package", "description"] | ["workspace", "package", "description"]) { + continue; + } + let at = start + line.len() - value.len(); + let (text, len) = toml_string(source.get(at..).unwrap_or_default()).unwrap_or_else(|| (value.trim().to_string(), value.trim_end().len())); + resume = resume.max(at + len); + let last = number + source.get(at..at + len).unwrap_or_default().matches('\n').count(); + out.push((number, last, text)); + } + out +} + +/// The key path of a TOML table header (`[a.b]`) at the start of `line`, or `None` for an array of +/// tables (`[[a.b]]`) or a header that cannot be read. +fn toml_header(line: &str) -> Option> { + let (parts, rest) = toml_key(line.strip_prefix('[').filter(|rest| !rest.starts_with('['))?)?; + let rest = rest.strip_prefix(']')?.trim_start_matches([' ', '\t']); + (rest.is_empty() || rest.starts_with(['#', '\r', '\n'])).then_some(parts) +} + +/// The parts of the TOML key (bare, quoted or dotted) at the start of `text`, after any spaces, and +/// the text after the key and the spaces that follow it. +fn toml_key(text: &str) -> Option<(Vec, &str)> { + let mut parts = Vec::new(); + let mut rest = text; + loop { + rest = rest.trim_start_matches([' ', '\t']); + if rest.starts_with("\"\"\"") || rest.starts_with("'''") { + return None; + } + let len = if rest.starts_with(['"', '\'']) { + let (part, len) = toml_string(rest)?; + parts.push(part); + len + } else { + let len = rest.len() - rest.trim_start_matches(|c: char| c.is_ascii_alphanumeric() || c == '_' || c == '-').len(); + parts.push(rest.get(..len).filter(|part| !part.is_empty())?.to_string()); + len + }; + rest = rest.get(len..)?.trim_start_matches([' ', '\t']); + match rest.strip_prefix('.') { + Some(after) => rest = after, + None => return Some((parts, rest)), + } + } +} + +/// The end of the TOML line of `len` bytes at `start`, or past the last string that starts on it, +/// so that the later lines of a multi-line string are not read as keys. `depth` counts the arrays +/// and inline tables open after the line, outside strings and comments. +fn toml_line_end(source: &str, start: usize, len: usize, depth: &mut usize) -> usize { + let (mut at, mut end) = (start, start + len); + while at < end { + let Some(c) = source.get(at..).and_then(|rest| rest.chars().next()) else { break }; + match c { + '#' => break, + '[' | '{' => { + *depth += 1; + at += 1; + } + ']' | '}' => { + *depth = depth.saturating_sub(1); + at += 1; + } + '"' | '\'' => match toml_string(source.get(at..).unwrap_or_default()) { + Some((_, string)) => { + at += string; + if at > end { + // The string closes on a later line: read the rest of that line too. + end = source.get(at..).and_then(|rest| rest.find('\n')).map_or(source.len(), |i| at + i + 1); + } + } + None => break, + }, + _ => at += c.len_utf8(), + } + } + end +} + +/// The TOML string at the start of `value`, decoded, and its length in bytes. `None` when `value` +/// does not start with a complete, valid string. +fn toml_string(value: &str) -> Option<(String, usize)> { + let delim = ["\"\"\"", "'''", "\"", "'"].into_iter().find(|d| value.starts_with(d))?; + let (basic, multi) = (delim.starts_with('"'), delim.len() == 3); + let mut at = delim.len(); + if multi { + let after = value.get(at..)?; + at += if after.starts_with("\r\n") { 2 } else { usize::from(after.starts_with('\n')) }; + } + let mut text = String::new(); + loop { + let rest = value.get(at..)?; + let c = rest.chars().next()?; + if rest.starts_with(delim) { + if !multi { + return Some((text, at + 1)); + } + // Up to two quotes just before the closing three belong to the text. + let extra = rest.chars().take_while(|&q| q == c).count().saturating_sub(3).min(2); + text.extend(std::iter::repeat_n(c, extra)); + return Some((text, at + extra + 3)); + } + match c { + '\\' if basic => at = toml_escape(value, at + 1, multi, &mut text)?, + '\n' if !multi => return None, + '\r' if rest.starts_with("\r\n") => at += 1, + _ => { + text.push(c); + at += c.len_utf8(); + } + } + } +} + +/// The escape after a backslash at `at` in a TOML basic string, decoded into `text`, and the index +/// after it. +fn toml_escape(value: &str, at: usize, multi: bool, text: &mut String) -> Option { + let rest = value.get(at..)?; + let c = rest.chars().next()?; + let simple = match c { + 'b' => Some('\u{8}'), + 't' => Some('\t'), + 'n' => Some('\n'), + 'f' => Some('\u{c}'), + 'r' => Some('\r'), + 'e' => Some('\u{1b}'), + '"' | '\\' => Some(c), + _ => None, + }; + if let Some(decoded) = simple { + text.push(decoded); + return Some(at + 1); + } + let digits = match c { + 'x' => 2, + 'u' => 4, + 'U' => 8, + _ => 0, + }; + if digits > 0 { + let hex = rest.get(1..1 + digits).filter(|hex| hex.chars().all(|h| h.is_ascii_hexdigit()))?; + text.push(u32::from_str_radix(hex, 16).ok().and_then(char::from_u32)?); + return Some(at + 1 + digits); + } + // A backslash at the end of a line in a multi-line string: the line break and the whitespace + // after it are left out. + if multi && rest.trim_start_matches([' ', '\t']).starts_with(['\n', '\r']) { + return Some(at + rest.len() - rest.trim_start_matches([' ', '\t', '\n', '\r']).len()); + } + None +} + +/// A Rust string literal. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Literal { + /// The 1-based line of its opening quote. + pub first_line: usize, + /// The 1-based line of its closing quote. + pub last_line: usize, + /// Its text, with escapes decoded (not in raw literals) and CRLF read as LF. + pub text: String, +} + +/// The string literals of a Rust source outside comments and test-only code, in source order. +/// +/// Test-only code is the code under `#[cfg(test)]` and the rest of a module that starts with +/// `#![cfg(test)]`. Under `#[cfg(test)]`, a `const`, `static` or `let` runs up to its `;`, another +/// item up to its closing `}` or its `;`, and a field, argument, arm or statement up to its `,` or +/// `;`. A block expression (a block, a macro's braces, a labeled block, or `if`, `match`, `loop`, +/// `while`, `for`, `unsafe`, `async` or `const`), as the whole statement or as an arm's expression +/// after `=>`, ends at its `}`, or at the last `}` of an `if` with `else` branches. When a method +/// call, field or `?` follows that `}`, it runs up to its `,` or `;` instead. Character, byte and +/// lifetime tokens are read, so a quote in them does not start a string. +pub fn literals(source: &str, kinds: Literals) -> Result, SyntaxError> { + lex(source, kinds, None).map(|lexed| lexed.literals) +} + +fn contains_word(text: &str, word: &str, case: MatchCase) -> bool { + match case { + MatchCase::Exact => text.match_indices(word).any(|(at, _)| whole_word(text, at, word.len())), + MatchCase::AsciiInsensitive => { + let lower = text.to_ascii_lowercase(); + let word = word.to_ascii_lowercase(); + lower.match_indices(&word).any(|(at, _)| whole_word(text, at, word.len())) + } + } +} + +fn is_word(c: char) -> bool { + c.is_alphanumeric() || c == '_' +} + +fn whole_word(text: &str, at: usize, len: usize) -> bool { + let before = text.get(..at).and_then(|s| s.chars().next_back()); + let after = text.get(at + len..).and_then(|s| s.chars().next()); + !before.is_some_and(is_word) && !after.is_some_and(is_word) +} + +fn io_error(path: &Path) -> impl FnOnce(io::Error) -> Error + '_ { + move |source| Error::Io { path: path.to_path_buf(), source } +} + +fn collect_rust(base: &Path, path: &Path, policy: &Policy<'_>, out: &mut Vec<(PathBuf, Kind)>) -> Result<(), Error> { + let meta = fs::metadata(base.join(path)).map_err(io_error(path))?; + if meta.is_dir() { + walk(base, path, path, policy, 0, out) + } else { + out.push((path.to_path_buf(), Kind::Rust)); + Ok(()) + } +} + +fn walk(base: &Path, input: &Path, dir: &Path, policy: &Policy<'_>, depth: usize, out: &mut Vec<(PathBuf, Kind)>) -> Result<(), Error> { + if depth > policy.limits.max_depth { + return Err(Error::TooDeep { path: dir.to_path_buf(), limit: policy.limits.max_depth }); + } + let mut entries = fs::read_dir(base.join(dir)).and_then(|entries| entries.collect::>>()).map_err(io_error(dir))?; + entries.sort_by_key(fs::DirEntry::file_name); + for entry in entries { + let path = dir.join(entry.file_name()); + let file_type = entry.file_type().map_err(io_error(&path))?; + if file_type.is_dir() { + walk(base, input, &path, policy, depth + 1, out)?; + continue; + } + if !path.extension().is_some_and(|ext| ext == "rs") || (policy.skip)(path.strip_prefix(input).unwrap_or(&path)) { + continue; + } + // A link to a folder is not followed; a link to a file is read like the file. + let is_file = if file_type.is_symlink() { fs::metadata(base.join(&path)).map_err(io_error(&path))?.is_file() } else { file_type.is_file() }; + if is_file { + out.push((path, Kind::Rust)); + } + } + Ok(()) +} + +fn read_limited(full: &Path, path: &Path, limits: &Limits, total: &mut u64) -> Result { + let file = File::open(full).map_err(io_error(path))?; + let len = file.metadata().map_err(io_error(path))?.len(); + if len > limits.max_file_bytes { + return Err(Error::FileTooLarge { path: path.to_path_buf(), limit: limits.max_file_bytes }); + } + let mut bytes = Vec::with_capacity(usize::try_from(len).unwrap_or(0)); + file.take(limits.max_file_bytes.saturating_add(1)).read_to_end(&mut bytes).map_err(io_error(path))?; + let read = u64::try_from(bytes.len()).unwrap_or(u64::MAX); + if read > limits.max_file_bytes { + return Err(Error::FileTooLarge { path: path.to_path_buf(), limit: limits.max_file_bytes }); + } + *total = total.saturating_add(read); + if *total > limits.max_total_bytes { + return Err(Error::TotalTooLarge { path: path.to_path_buf(), limit: limits.max_total_bytes }); + } + let mut text = String::from_utf8(bytes).map_err(|_| Error::InvalidUtf8 { path: path.to_path_buf() })?; + if text.starts_with('\u{feff}') { + text.remove(0); + } + Ok(text) +} + +struct Lexed { + literals: Vec, + marked_lines: HashSet, +} + +/// The literal forms, which differ in the escapes they take and in whether the scan reads them. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Form { + Str, + Char, + Byte, + C, +} + +/// The keywords that start an item, whose header can hold commas. +const ITEM_KEYWORDS: &[&str] = + &["const", "enum", "extern", "fn", "impl", "let", "macro_rules", "mod", "static", "struct", "trait", "type", "union", "use"]; + +/// The keywords of items that end at their `;`, whatever braces their value holds. +const SEMICOLON_KEYWORDS: &[&str] = &["const", "let", "static"]; + +/// The keywords that start an expression ending at the `}` of its block (or of its last `else`). +const BLOCK_KEYWORDS: &[&str] = &["async", "const", "for", "if", "loop", "match", "unsafe", "while"]; + +/// Test-only code being skipped. +#[derive(Debug, Clone, Copy)] +enum TestRegion { + /// An item, field, argument, arm or statement under `#[cfg(test)]`, at this delimiter depth. + /// `start` is the index where its code (or an arm's expression, after `=>`) starts; `opened` is + /// set once its first `{` is read, `item` once an item keyword (`fn`, `impl`, `mod`…) is read, + /// and `semi` while the last such keyword is `const`, `let` or `static`, whose items end at their + /// `;`. Without an item keyword, `block` is set when that first `{` starts a block expression (a + /// block, a macro's braces, or `if`, `match`, `loop` and the like), which ends at its `}` unless + /// an `else` follows. A `.` or `?` after that `}` clears `block`, so the expression ends at its + /// `;` or `,`. + Item { level: usize, start: usize, opened: bool, item: bool, semi: bool, block: bool }, + /// The rest of the module that `#![cfg(test)]` starts, at this delimiter depth. + Inner { level: usize }, +} + +struct Lexer<'s> { + chars: Vec, + at: usize, + line: usize, + kinds: Literals, + marker: Option<&'s str>, + /// Open delimiters and the line of each. + delims: Vec<(char, usize)>, + test: Option, + out: Lexed, +} + +fn lex(source: &str, kinds: Literals, marker: Option<&str>) -> Result { + let mut lexer = Lexer { + chars: source.chars().collect(), + at: 0, + line: 1, + kinds, + marker, + delims: Vec::new(), + test: None, + out: Lexed { literals: Vec::new(), marked_lines: HashSet::new() }, + }; + lexer.run()?; + Ok(lexer.out) +} + +impl Lexer<'_> { + fn peek(&self, ahead: usize) -> Option { + self.chars.get(self.at + ahead).copied() + } + + fn next(&mut self) -> Option { + let c = self.peek(0)?; + self.at += 1; + Some(c) + } + + fn error(line: usize, message: &'static str) -> SyntaxError { + SyntaxError { line, message } + } + + fn run(&mut self) -> Result<(), SyntaxError> { + while let Some(c) = self.peek(0) { + match c { + '\n' => { + self.line += 1; + self.at += 1; + } + '/' if self.peek(1) == Some('/') => self.line_comment(), + '/' if self.peek(1) == Some('*') => self.block_comment()?, + '"' => { + self.at += 1; + self.string(Form::Str)?; + } + '\'' => self.quote(Form::Char)?, + '#' => self.hash(), + '{' | '(' | '[' => self.open(c), + '}' | ')' | ']' => self.close(c)?, + ';' => self.item_end(false), + ',' => self.item_end(true), + '=' if self.peek(1) == Some('>') => self.arrow(), + c if c.is_ascii_digit() => self.number(), + c if is_word(c) => self.word()?, + _ => self.at += 1, + } + } + match self.delims.last() { + Some(&(_, line)) => Err(Self::error(line, "unclosed delimiter")), + None => Ok(()), + } + } + + fn comment_text(&mut self, first: usize, start: usize) { + if let Some(marker) = self.marker { + let text: String = self.chars.get(start..self.at).unwrap_or_default().iter().collect(); + let marked = text.split('\n').enumerate().filter(|(_, line)| line.contains(marker)); + self.out.marked_lines.extend(marked.map(|(i, _)| first + i)); + } + } + + fn line_comment(&mut self) { + let start = self.at; + while self.peek(0).is_some_and(|c| c != '\n') { + self.at += 1; + } + self.comment_text(self.line, start); + } + + fn block_comment(&mut self) -> Result<(), SyntaxError> { + let (first, start) = (self.line, self.at); + self.at += 2; + let mut depth = 1_usize; + while depth > 0 { + match (self.next(), self.peek(0)) { + (None, _) => return Err(Self::error(first, "unterminated block comment")), + (Some('/'), Some('*')) => { + self.at += 1; + depth += 1; + } + (Some('*'), Some('/')) => { + self.at += 1; + depth -= 1; + } + (Some('\n'), _) => self.line += 1, + _ => {} + } + } + self.comment_text(first, start); + Ok(()) + } + + fn reads(&self, form: Form) -> bool { + match form { + Form::Str => true, + Form::Byte => self.kinds.byte_strings, + Form::C => self.kinds.c_strings, + Form::Char => false, + } + } + + fn push(&mut self, first: usize, text: String, form: Form) { + if self.reads(form) && self.test.is_none() { + self.out.literals.push(Literal { first_line: first, last_line: self.line, text }); + } + } + + /// A `"…"` body after its opening quote. + fn string(&mut self, form: Form) -> Result<(), SyntaxError> { + let first = self.line; + let mut text = String::new(); + loop { + match self.next() { + None => return Err(Self::error(first, "unterminated string literal")), + Some('"') => break, + Some('\\') => self.escape(form, &mut text, first)?, + Some('\r') if self.peek(0) == Some('\n') => {} + Some('\n') => { + self.line += 1; + text.push('\n'); + } + Some(c) => text.push(c), + } + } + self.push(first, text, form); + Ok(()) + } + + /// The escape after a backslash, decoded into `text`. + fn escape(&mut self, form: Form, text: &mut String, first: usize) -> Result<(), SyntaxError> { + let decoded = match self.next() { + None => return Err(Self::error(first, "unterminated escape")), + Some('n') => '\n', + Some('r') => '\r', + Some('t') => '\t', + Some('0') => '\0', + Some(c @ ('\\' | '\'' | '"')) => c, + Some('x') => self.hex_escape(form)?, + Some('u') if form != Form::Byte => self.unicode_escape()?, + Some('\n') if form != Form::Char => { + self.continuation(); + return Ok(()); + } + Some('\r') if form != Form::Char && self.peek(0) == Some('\n') => { + self.at += 1; + self.continuation(); + return Ok(()); + } + Some(_) => return Err(Self::error(self.line, "unknown character escape")), + }; + text.push(decoded); + Ok(()) + } + + /// A backslash before a line break: the break and the whitespace after it are left out. + fn continuation(&mut self) { + self.line += 1; + while let Some(c @ (' ' | '\t' | '\n' | '\r')) = self.peek(0) { + self.line += usize::from(c == '\n'); + self.at += 1; + } + } + + fn hex_escape(&mut self, form: Form) -> Result { + let digits = (self.peek(0).and_then(|c| c.to_digit(16)), self.peek(1).and_then(|c| c.to_digit(16))); + let (Some(high), Some(low)) = digits else { + return Err(Self::error(self.line, "a \\x escape needs two hex digits")); + }; + self.at += 2; + let value = high * 16 + low; + match (value, form) { + (0..=0x7f, _) => Ok(char::from_u32(value).unwrap_or(char::REPLACEMENT_CHARACTER)), + (_, Form::Byte | Form::C) => Ok(char::REPLACEMENT_CHARACTER), + _ => Err(Self::error(self.line, "a \\x escape above 0x7F")), + } + } + + fn unicode_escape(&mut self) -> Result { + let bad = Self::error(self.line, "an invalid \\u{...} escape"); + if self.next() != Some('{') || self.peek(0) == Some('_') { + return Err(bad); + } + let (mut value, mut digits) = (0_u32, 0_usize); + loop { + match self.next() { + Some('}') => break, + Some('_') => {} + Some(c) if digits < 6 => { + value = value * 16 + c.to_digit(16).ok_or_else(|| bad.clone())?; + digits += 1; + } + _ => return Err(bad), + } + } + if digits == 0 { + return Err(bad); + } + char::from_u32(value).ok_or(bad) + } + + /// A quote: a character or byte literal, or the start of a lifetime or label. + fn quote(&mut self, form: Form) -> Result<(), SyntaxError> { + match (self.peek(1), self.peek(2)) { + (Some('\\'), _) => { + let first = self.line; + self.at += 2; + self.escape(form, &mut String::new(), first)?; + if self.next() != Some('\'') { + return Err(Self::error(first, "unterminated character literal")); + } + } + (Some(c), Some('\'')) if c != '\'' && c != '\n' => self.at += 3, + _ => self.at += 1, + } + Ok(()) + } + + fn number(&mut self) { + while self.peek(0).is_some_and(is_word) { + self.at += 1; + } + } + + /// An identifier, or the prefix of a byte, C or raw literal. + fn word(&mut self) -> Result<(), SyntaxError> { + let start = self.at; + self.number(); + let word: String = self.chars.get(start..self.at).unwrap_or_default().iter().collect(); + // `const { .. }` is a block expression, not an item. + let const_block = word == "const" && self.chars.get(self.skip_space(self.at)) == Some(&'{'); + if let Some(TestRegion::Item { level, opened: false, item, semi, .. }) = &mut self.test + && *level == self.delims.len() + && ITEM_KEYWORDS.contains(&word.as_str()) + && !const_block + { + *item = true; + *semi = SEMICOLON_KEYWORDS.contains(&word.as_str()); + } + match (word.as_str(), self.peek(0)) { + ("b", Some('\'')) => self.quote(Form::Byte), + ("b", Some('"')) => { + self.at += 1; + self.string(Form::Byte) + } + ("c", Some('"')) => { + self.at += 1; + self.string(Form::C) + } + ("r", Some('"' | '#')) => self.raw(Form::Str), + ("br", Some('"' | '#')) => self.raw(Form::Byte), + ("cr", Some('"' | '#')) => self.raw(Form::C), + _ => Ok(()), + } + } + + /// A raw literal after its `r`, or a raw identifier (`r#name`), which is left to the caller. + fn raw(&mut self, form: Form) -> Result<(), SyntaxError> { + let hashes = self.chars.get(self.at..).unwrap_or_default().iter().take_while(|&&c| c == '#').count(); + if self.peek(hashes) != Some('"') { + return Ok(()); + } + let first = self.line; + self.at += hashes + 1; + let mut text = String::new(); + loop { + match self.next() { + None => return Err(Self::error(first, "unterminated raw string literal")), + Some('"') if (0..hashes).all(|n| self.peek(n) == Some('#')) => break, + Some('\r') if self.peek(0) == Some('\n') => {} + Some(c) => { + self.line += usize::from(c == '\n'); + text.push(c); + } + } + } + self.at += hashes; + self.push(first, text, form); + Ok(()) + } + + /// The index after the whitespace and comments at `at`. + fn skip_space(&self, mut at: usize) -> usize { + loop { + match (self.chars.get(at), self.chars.get(at + 1)) { + (Some(c), _) if c.is_whitespace() => at += 1, + (Some('/'), Some('/')) => { + while self.chars.get(at).is_some_and(|&c| c != '\n') { + at += 1; + } + } + (Some('/'), Some('*')) => { + let mut depth = 0_usize; + loop { + match (self.chars.get(at), self.chars.get(at + 1)) { + (None, _) => return at, + (Some('/'), Some('*')) => { + depth += 1; + at += 2; + } + (Some('*'), Some('/')) => { + depth -= 1; + at += 2; + if depth == 0 { + break; + } + } + _ => at += 1, + } + } + } + _ => return at, + } + } + } + + /// The end of a `#[cfg(test)]` or `#![cfg(test)]` attribute at the current `#`, and whether it + /// is the inner form. + fn cfg_test_end(&self) -> Option<(usize, bool)> { + let mut at = self.at + 1; + let inner = self.chars.get(at) == Some(&'!'); + at += usize::from(inner); + for token in ["[", "cfg", "(", "test", ")", "]"] { + at = self.skip_space(at); + let end = at + token.chars().count(); + if !self.chars.get(at..end).is_some_and(|got| got.iter().copied().eq(token.chars())) { + return None; + } + if token.starts_with(is_word) && self.chars.get(end).is_some_and(|&c| is_word(c)) { + return None; + } + at = end; + } + Some((at, inner)) + } + + fn hash(&mut self) { + let Some((end, inner)) = self.cfg_test_end() else { + self.at += 1; + return; + }; + let skipped = self.chars.get(self.at..end).unwrap_or_default(); + self.line += skipped.iter().filter(|&&c| c == '\n').count(); + self.at = end; + if self.test.is_none() { + let level = self.delims.len(); + self.test = Some(if inner { + TestRegion::Inner { level } + } else { + TestRegion::Item { level, start: end, opened: false, item: false, semi: false, block: false } + }); + } + } + + /// A `=>` at the level of a test-only arm: its expression starts after it. + fn arrow(&mut self) { + self.at += 2; + if let Some(TestRegion::Item { level, item: false, .. }) = self.test + && level == self.delims.len() + { + self.test = Some(TestRegion::Item { level, start: self.at, opened: false, item: false, semi: false, block: false }); + } + } + + /// Whether the code at `start` is a block expression whose first `{` is at the current index: + /// the block itself, a macro's braces, a labeled block or loop, or a block keyword's expression. + /// Outer attributes before it are passed over. + fn starts_block(&self, start: usize) -> bool { + let mut at = self.skip_space(start); + while self.chars.get(at) == Some(&'#') { + at = self.skip_space(at + 1); + if self.chars.get(at) != Some(&'[') { + return false; + } + let mut depth = 0_usize; + loop { + match self.chars.get(at) { + None => return false, + Some('[') => depth += 1, + Some(']') => { + depth -= 1; + if depth == 0 { + break; + } + } + _ => {} + } + at += 1; + } + at = self.skip_space(at + 1); + } + at == self.at || self.chars.get(at) == Some(&'\'') || BLOCK_KEYWORDS.contains(&self.word_at(at).as_str()) || self.macro_braces(at) + } + + /// Whether the code at `at` is a macro path, `!` and the `{` at the current index. + fn macro_braces(&self, mut at: usize) -> bool { + if self.chars.get(at..at + 2) == Some(&[':', ':'][..]) { + at = self.skip_space(at + 2); + } + loop { + let len = self.word_at(at).chars().count(); + if len == 0 { + return false; + } + at = self.skip_space(at + len); + if self.chars.get(at..at + 2) != Some(&[':', ':'][..]) { + break; + } + at = self.skip_space(at + 2); + } + self.chars.get(at) == Some(&'!') && self.skip_space(at + 1) == self.at + } + + fn word_at(&self, at: usize) -> String { + self.chars.get(at..).unwrap_or_default().iter().take_while(|&&c| is_word(c)).collect() + } + + fn open(&mut self, c: char) { + if let Some(TestRegion::Item { level, start, opened: false, item, semi, .. }) = self.test + && level == self.delims.len() + && c == '{' + { + let block = !item && self.starts_block(start); + self.test = Some(TestRegion::Item { level, start, opened: true, item, semi, block }); + } + self.delims.push((c, self.line)); + self.at += 1; + } + + fn close(&mut self, c: char) -> Result<(), SyntaxError> { + let expected = match self.delims.pop() { + Some(('{', _)) => '}', + Some(('(', _)) => ')', + Some(_) => ']', + None => return Err(Self::error(self.line, "unmatched closing delimiter")), + }; + if c != expected { + return Err(Self::error(self.line, "mismatched closing delimiter")); + } + self.at += 1; + let depth = self.delims.len(); + let ends = match self.test { + Some(TestRegion::Item { level, start, opened, item, semi, block }) => { + let block_end = opened && depth == level && c == '}'; + if block_end && !item && block && self.postfix_follows() { + // A method call, field or `?` continues the expression to its `;` or `,`. + self.test = Some(TestRegion::Item { level, start, opened, item, semi, block: false }); + false + } else { + depth < level || (block_end && if item { !semi } else { block && self.word_at(self.skip_space(self.at)) != "else" }) + } + } + Some(TestRegion::Inner { level }) => depth < level, + None => false, + }; + if ends { + self.test = None; + } + Ok(()) + } + + /// Whether a `?` or a `.` (not `..`) follows the current index, past whitespace and comments. + fn postfix_follows(&self) -> bool { + let at = self.skip_space(self.at); + match self.chars.get(at) { + Some('?') => true, + Some('.') => self.chars.get(at + 1) != Some(&'.'), + _ => false, + } + } + + /// A `;` ends a test-only statement, or an item that has no body or ends at its `;`, and a `,` + /// inside a delimiter ends a test-only field, argument or arm. A `,` in an item's header + /// (`impl`, a `where` clause) does not. + fn item_end(&mut self, comma: bool) { + if let Some(TestRegion::Item { level, opened, item, semi, .. }) = self.test + && level == self.delims.len() + && if comma { level > 0 && !item } else { !item || !opened || semi } + { + self.test = None; + } + self.at += 1; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const BANNED: &[&str] = &["Amber Tool", "AzureKit", "BlueCore"]; + const ALLOWED: &[&str] = &["AzureKit Design"]; + const POLICY: Policy<'static> = Policy { allowed: ALLOWED, ..Policy::new(BANNED) }; + + fn banned_display(text: &str) -> bool { + !POLICY.words_in(text).is_empty() + } + + fn literals(source: &str) -> Vec { + super::literals(source, Literals::default()).unwrap().into_iter().map(|lit| lit.text).collect() + } + + fn write(dir: &Path, path: &str, text: &str) { + let path = dir.join(path); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, text).unwrap(); + } + + fn hits(findings: &[Finding]) -> Vec<(String, usize, &str)> { + findings.iter().map(|f| (f.path.to_string_lossy().replace('\\', "/"), f.line, f.word.as_str())).collect() + } + + #[test] + fn production_display_names_stay_neutral() { + let dir = tempfile::tempdir().unwrap(); + let base = dir.path().join("crates/ui"); + write(&base, "src/lib.rs", "pub const LABEL: &str = \"Open\";\n#[cfg(test)]\nmod tests { const S: &str = \"AzureKit\"; }\n"); + write(&base, "src/tests.rs", "const S: &str = \"AzureKit\";\n"); + write(&base, "src/panel_tests.rs", "const S: &str = \"BlueCore\";\n"); + write(&base, "src/tests/helper.rs", "const S: &str = \"Amber Tool\";\n"); + write(&base, "src/notes.txt", "AzureKit\n"); + write(dir.path(), "crates/engine/src/cmd/file.rs", "pub const DESIGN: &str = \"AzureKit Design\";\n"); + let roots = [Path::new("src"), Path::new("../engine/src/cmd")]; + let inputs: Vec> = roots.iter().map(|root| Input::rust(root)).collect(); + let found = scan(&base, &inputs, &POLICY).unwrap(); + assert!(found.is_empty(), "{found:#?}"); + } + + #[test] + fn guard_distinguishes_labels_from_identifiers_and_comments() { + for name in BANNED { + assert!(banned_display(name), "{name}"); + } + for name in ALLOWED { + assert!(!banned_display(name), "{name}"); + } + assert!(!banned_display("filter.azureKit")); + assert!(!banned_display("tool.guidedBlueCore")); + assert!(!banned_display("insert.amberTool")); + assert_eq!(literals("// \"ignored\"\n/* \"ignored\" */ \"label\""), ["label"]); + assert_eq!(literals("r#\"raw label\"#"), ["raw label"]); + } + + #[test] + fn guard_scans_production_after_test_only_items() { + let source = r###"#[cfg(test)] mod tests { let _ = "ignored"; } + #[cfg(test)] use fixtures::Helper; + fn production() { let _ = r#"live label"#; } + "###; + assert_eq!(literals(source), ["live label"]); + assert_eq!(literals("#[cfg(test)] mod tests; fn live() { let _ = \"live label\"; }"), ["live label"]); + } + + #[test] + fn guard_skips_escaped_character_literals() { + assert_eq!(literals(r#"let q = '\"'; "label""#), ["label"]); + assert_eq!(literals(r#"let q = '\''; "label""#), ["label"]); + assert_eq!(literals(r#"let q = '\\'; "label""#), ["label"]); + assert_eq!(literals(r#"let q = '\u{22}'; "label""#), ["label"]); + } + + #[test] + fn guard_decodes_whitespace_escapes() { + assert_eq!(literals(r#""one\ntwo\tthree\rfour\0""#), ["one\ntwo\tthree\rfour\0"]); + assert_eq!(literals("\"joined\\\n label\""), ["joinedlabel"]); + for name in BANNED { + let source = format!("\"\\t{name}\\n\""); + assert!(literals(&source).iter().any(|label| banned_display(label))); + } + } + + #[test] + fn guard_scans_after_escaped_character_literals() { + let source = r###"let a = '\"'; let b = '\''; let c = '\\'; + let d = '\u{22}'; let e = '\x22'; + let label = "Amber Tool"; + fn lifetime<'a>(value: &'a str) { 'outer: loop { break 'outer; } let _ = "BlueCore"; } + "###; + assert_eq!(literals(source), ["Amber Tool", "BlueCore"]); + } + + #[test] + fn guard_scans_production_after_test_only_items_and_parses_raw_labels() { + let source = r###"#[cfg(test)] mod tests { let _ = "AzureKit"; } + #[cfg(test)] use fixtures::Helper; + fn production() { let _ = r#"BlueCore"#; } + "###; + assert_eq!(literals(source), ["BlueCore"]); + assert!(literals(source).iter().any(|label| banned_display(label))); + assert_eq!(literals("#[cfg(test)] mod tests; fn live() { let _ = \"AzureKit\"; }"), ["AzureKit"]); + } + + #[test] + fn guard_skips_escaped_character_literals_without_hiding_labels() { + let source = r#"let quote = '\"'; let label = "BlueCore"; // "ignored""#; + assert_eq!(literals(source), ["BlueCore"]); + assert!(literals(source).iter().any(|label| banned_display(label))); + assert_eq!(literals(r#"let quote = '\"'; // "BlueCore""#), Vec::::new()); + } + + #[test] + fn source_read_failures_are_reported() { + let dir = tempfile::tempdir().unwrap(); + write(dir.path(), "src/lib.rs", "const S: &str = \"Open\";\n"); + // A file cannot be read as a folder, and a missing root is an error, not an empty scan. + for root in ["src/lib.rs/not-a-folder.rs", "missing"] { + let err = scan(dir.path(), &[Input::rust(Path::new(root))], &POLICY).unwrap_err(); + assert!(matches!(err, Error::Io { .. }), "{root}: {err}"); + } + let err = scan(dir.path(), &[Input::text(Path::new("missing.txt"))], &POLICY).unwrap_err(); + assert!(matches!(err, Error::Io { .. }), "{err}"); + } + + const DENY: &[&str] = &["azurekit", "bluecore", "copperline", "dunemark", "emberly", "fernwave", "glintpad"]; + const VECTOR: Policy<'static> = Policy { + case: MatchCase::AsciiInsensitive, + allow_marker: Some("name-ok"), + literals: Literals { byte_strings: true, c_strings: true }, + ..Policy::new(DENY) + }; + + fn rust_hits(src: &str) -> Vec<(usize, &str)> { + let found = scan_source(Path::new("lib.rs"), src, Kind::Rust, &VECTOR).unwrap(); + found.iter().map(|f| (f.line, DENY.iter().copied().find(|w| *w == f.word).unwrap())).collect() + } + + #[test] + fn rust_literals_skip_comments_identifiers_tests_and_marked_lines() { + let src = r##"// BlueCore in a comment +/// AzureKit in a doc comment +/* block AzureKit /* nested */ Copperline */ +fn f() { let a = "Made for BlueCore users"; let k = "copyEmberly"; let q = '"'; let l: &'static str = r#"raw "Dunemark" text"#; } +const OLD: &str = "BlueCore Defaults"; // name-ok: legacy alias +fn g() -> &'static str { "line\nEMBERLY" } +#[cfg(test)] +mod tests { + fn t() { let x = "AzureKit"; if true { let y = "Copperline"; } } +} +#[cfg(test)] +mod other; +fn h() { let s = b"Fernwave"; let m = "multi +line Glintpad"; } +"##; + assert_eq!(rust_hits(src), [(4, "bluecore"), (4, "dunemark"), (6, "emberly"), (13, "fernwave"), (13, "glintpad")]); + let lits = super::literals(src, VECTOR.literals).unwrap(); + let multi = lits.iter().find(|l| l.text.contains("Glintpad")).unwrap(); + assert_eq!((multi.first_line, multi.last_line), (13, 14)); + } + + #[test] + fn whole_words_only() { + assert_eq!(VECTOR.words_in("AzureKit's app"), ["azurekit"]); + assert_eq!(VECTOR.words_in("an BLUECORE-style UI"), ["bluecore"]); + assert!(VECTOR.words_in("bluecores bluecoreish copyEmberly emberlyMode").is_empty()); + } + + #[test] + fn docs_packaging_and_manifests() { + let scan = |text: &str, kind: Kind| -> Vec<(usize, String)> { + scan_source(Path::new("f"), text, kind, &VECTOR).unwrap().into_iter().map(|f| (f.line, f.word)).collect() + }; + let one = |line: usize, word: &str| vec![(line, word.to_string())]; + assert_eq!(scan("Keywords=vector;bluecore;\n", Kind::Text), one(1, "bluecore")); + assert_eq!(scan("Like AzureKit apps\nnot here: AzureKit # name-ok\n", Kind::Text), one(1, "azurekit")); + let desktop = "# shared type (name-ok)\nMimeType=application/bluecore;\n# a comment\nMimeType=application/bluecore;\n"; + assert_eq!(scan(desktop, Kind::TextMarkAbove), one(4, "bluecore"), "a `#` line marks the line below it only"); + assert_eq!( + scan(desktop, Kind::Text), + [(2, "bluecore".to_string()), (4, "bluecore".to_string())], + "only mark-above files take a mark from above" + ); + assert_eq!(scan("x = 1 # name-ok\nBlueCore\n", Kind::TextMarkAbove), one(2, "bluecore"), "a code line's comment marks that line only"); + let manifest = "[package]\nname = \"azurekit-thing\"\ndescription = \"BlueCore-style UI\"\n"; + assert_eq!(scan(manifest, Kind::Manifest), one(3, "bluecore")); + let multi = + "[package]\ndescription = \"\"\"\nA BlueCore\n-style UI\"\"\"\nkeywords = [\"azurekit\"]\ndescription = '''one line''' # azurekit\n"; + assert_eq!(scan(multi, Kind::Manifest), one(2, "bluecore"), "a multi-line value is read whole; a comment is not read"); + assert_eq!(scan("[package]\ndescription = '''\nAzureKit'''\nname = \"bluecore\"\n", Kind::Manifest), one(2, "azurekit")); + let escaped = "[package]\ndescription = \"\"\"say \\\"\"\" and\nBlueCore \\\\\"\"\"\nname = \"azurekit\"\n"; + assert_eq!(scan(escaped, Kind::Manifest), one(2, "bluecore"), "an escaped quote does not close the description"); + assert_eq!( + scan("[package]\ndescription = \"Blue\\u0043ore and Azure\\U0000004Bit\"\n", Kind::Manifest), + [(2, "azurekit".to_string()), (2, "bluecore".to_string())] + ); + assert_eq!(scan("[package]\ndescription = \"\"\"Blue\\\n Core\"\"\"\n", Kind::Manifest), one(2, "bluecore")); + assert!(scan("[package]\ndescription = \"Neutral\" # BlueCore\ndescriptions = \"AzureKit\"\n", Kind::Manifest).is_empty()); + let notes = "[package]\nnotes = \"\"\"\n[dependencies]\ndescription = \"BlueCore\"\n\"\"\" # \"\"\"\nquoted = 'a \"\"\" b'\ndescription = \"AzureKit\"\n"; + assert_eq!(scan(notes, Kind::Manifest), one(7, "azurekit"), "lines inside another key's string are not keys or headers"); + let keys = "package.description = 'BlueCore'\n[package]\n\"description\" = \"AzureKit\"\n'description'='Copperline'\n# description = \"Dunemark\"\nother.description = \"Emberly\"\n"; + assert_eq!(scan(keys, Kind::Manifest), [(1, "bluecore".to_string()), (3, "azurekit".to_string()), (4, "copperline".to_string())]); + let marked = + "[package]\ndescription = \"\"\"\nBlueCore long\n\"\"\" # name-ok\nname = \"y\"\n[workspace.package]\ndescription = \"BlueCore\"\n"; + assert_eq!(scan(marked, Kind::Manifest), one(7, "bluecore"), "a marker on any line of the value counts"); + let text = scan_source(Path::new("Cargo.toml"), "[package]\ndescription = '''\nBlueCore\nstyle'''\n", Kind::Manifest, &VECTOR).unwrap(); + assert_eq!(text[0].text, "BlueCore\nstyle"); + } + + #[test] + fn only_package_descriptions_are_read() { + let scan = |text: &str| -> Vec<(usize, String)> { + scan_source(Path::new("Cargo.toml"), text, Kind::Manifest, &VECTOR).unwrap().into_iter().map(|f| (f.line, f.word)).collect() + }; + let other = "description = \"Glintpad\"\n[dependencies.foo]\ndescription = \"BlueCore\"\n[package.metadata.x]\ndescription = \"AzureKit\"\n\ + [[bin]]\ndescription = \"Copperline\"\n[[package]]\ndescription = \"Copperline\"\n[package]\nmetadata.description = \"Dunemark\"\n\ + dependencies = { description = \"Dunemark\" }\n[workspace]\ndescription = \"Emberly\"\n"; + assert!(scan(other).is_empty(), "{:?}", scan(other)); + assert_eq!(scan("\u{feff}[package]\ndescription = \"BlueCore\"\n"), [(2, "bluecore".to_string())], "a byte order mark is skipped"); + assert_eq!(scan("\u{feff}package.description = \"AzureKit\"\n"), [(1, "azurekit".to_string())]); + let tables = "[ \"package\" ] # x\ndescription = \"BlueCore\"\n[dependencies.foo]\nx = [\n [\"package\"]\n]\ndescription = \"AzureKit\"\n\ + [workspace . 'package']\ndescription = \"Fernwave\"\n[dependencies]\nworkspace.package.description = \"Emberly\"\n\ + y = [ \"\"\"\n]\n\"\"\", ]\n[package]\ndescription = \"Glintpad\"\n"; + assert_eq!( + scan(tables), + [(2, "bluecore".to_string()), (9, "fernwave".to_string()), (16, "glintpad".to_string())], + "a line inside an array is not a header, and an array closed after a multi-line string is closed" + ); + } + + #[test] + fn scope() { + for (path, test) in [ + ("src/cmd/file.rs", false), + ("src/tests_file.rs", false), + ("src/text_tests.rs", true), + ("src/cms/tests.rs", true), + ("tests/command_sweep.rs", true), + ("src/tests/helper.rs", true), + ("../engine/tests/a.rs", true), + ] { + assert_eq!(test_path(Path::new(path)), test, "{path}"); + } + let dir = tempfile::tempdir().unwrap(); + for file in ["src/a.rs", "src/b.txt", "src/c.rs.bak", "src/d/e.rs", "src/tests/f.rs", "src/g_tests.rs"] { + write(dir.path(), file, "\"AzureKit\"\n"); + } + let found = scan(dir.path(), &[Input::rust(Path::new("src"))], &POLICY).unwrap(); + assert_eq!(hits(&found), [("src/a.rs".to_string(), 1, "AzureKit"), ("src/d/e.rs".to_string(), 1, "AzureKit")]); + let named = scan(dir.path(), &[Input::rust(Path::new("src/g_tests.rs"))], &POLICY).unwrap(); + assert_eq!(hits(&named), [("src/g_tests.rs".to_string(), 1, "AzureKit")], "a file input is read whatever its name"); + write(dir.path(), "tests/app/src/lib.rs", "\"AzureKit\"\n"); + let below = scan(dir.path(), &[Input::rust(Path::new("tests/app/src"))], &POLICY).unwrap(); + assert_eq!(hits(&below), [("tests/app/src/lib.rs".to_string(), 1, "AzureKit")], "folders above the input do not count"); + } + + #[test] + fn injected_strings_fail_the_gate() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path(); + write(root, "crates/x/src/lib.rs", "pub const LABEL: &str = \"Export for BlueCore\"; // a comment: AzureKit\n"); + write(root, "crates/x/Cargo.toml", "[package]\nname = \"x\"\ndescription = \"A Copperline-style crate\"\n"); + write(root, "docs/x.md", "Works like Copperline.\n"); + write(root, "docs/x.txt", "Works like Copperline.\n"); + write(root, "packaging/x.desktop", "Keywords=vector;bluecore;\n"); + let marked = "# shared type (name-ok)\nMimeType=application/bluecore;\n"; + write(root, "packaging/linux/app.desktop", marked); + write(root, "packaging/x.yaml", marked); + write(root, "README.md", "AzureKit trademark notice\n"); + let path = Path::new; + let inputs = [ + Input::rust(path("crates")), + Input::manifest(path("crates/x/Cargo.toml")), + Input::text(path("docs/x.txt")), + Input::text(path("packaging/x.desktop")), + Input::text_mark_above(path("packaging/linux/app.desktop")), + Input::text(path("packaging/x.yaml")), + ]; + let found = scan(root, &inputs, &VECTOR).unwrap(); + let expected = [ + ("crates/x/Cargo.toml", 3, "copperline"), + ("crates/x/src/lib.rs", 1, "bluecore"), + ("docs/x.txt", 1, "copperline"), + ("packaging/x.desktop", 1, "bluecore"), + ("packaging/x.yaml", 2, "bluecore"), + ]; + assert_eq!(hits(&found), expected.map(|(p, l, w)| (p.to_string(), l, w))); + } + + #[test] + fn the_tree_is_clean() { + // This crate's own sources hold none of the synthetic words outside their tests, and the + // lexer reads every one of them. + let base = Path::new(env!("CARGO_MANIFEST_DIR")); + let found = scan(base, &[Input::rust(Path::new("src"))], &VECTOR).unwrap(); + assert!(found.is_empty(), "{found:#?}"); + } + + #[test] + fn escapes_are_decoded_before_matching() { + for source in [r#""A\x7AureKit""#, r#""A\u{7A}ureKit""#, r#""A\u{7_A}ureKit""#, r#""Azure\u{4B}it""#] { + assert_eq!(literals(source), ["AzureKit"], "{source}"); + } + assert_eq!(literals(r#""Blue\x43ore""#), ["BlueCore"]); + // A continuation leaves out the line break and the indentation, and adds no space. + assert_eq!(literals("\"Blue\\\n Core\""), ["BlueCore"]); + assert_eq!(literals("\"Blue\\\r\n\tCore\""), ["BlueCore"]); + assert_eq!(literals(r#""say \"BlueCore\" \\ ok""#), [r#"say "BlueCore" \ ok"#]); + } + + #[test] + fn test_only_items_of_every_form_are_skipped() { + let source = r#"#[cfg(test)] pub mod t { const S: &str = "AzureKit"; } +#[cfg(test)] fn f() { let _ = "AzureKit"; } +#[cfg(test)] const C: &str = "AzureKit"; +#[cfg(test)] static D: [&str; 2] = ["AzureKit", "AzureKit"]; +#[cfg(test)] +#[allow(dead_code)] +fn g() -> [u8; 2] { let _ = "AzureKit"; [0, 1] } +# [ cfg ( test ) ] mod spaced { "AzureKit"; } +struct S { #[cfg(test)] probe: &'static str, label: &'static str } +fn h() { let s = S { #[cfg(test)] probe: "AzureKit", label: "one" }; } +#[cfg(test)] mod declared; +mod generic { + #[cfg(test)] + impl Probe for Pair where A: Clone, B: Copy { fn p() { let _ = "AzureKit"; } } +} +mod inner { + #![cfg(test)] + fn i() { let _ = "AzureKit"; } +} +#[cfg(test)] const PICK: &str = if true { "AzureKit" } else { "AzureKit" }; +#[cfg(/* a reason */ test)] const NOTED: &str = "AzureKit"; +#[cfg( // a reason + test)] const NOTED_LINE: &str = "AzureKit"; +#[cfg(test)] const fn k() -> u8 { 1 } +#[cfg(not(test))] const E: &str = "two"; +#[cfg(tests)] const F: &str = "three"; +fn live() { let _ = "four"; } +"#; + assert_eq!(literals(source), ["one", "two", "three", "four"]); + assert_eq!(literals("#![cfg(test)]\nfn a() { \"AzureKit\"; }\nfn b() { \"AzureKit\"; }\n"), Vec::::new()); + } + + #[test] + fn test_only_arms_and_statements_end_after_their_last_block() { + let source = r#"fn pick(x: u8, c: bool) -> &'static str { + match x { + #[cfg(test)] 0 => if c { "AzureKit" } else { "AzureKit" }, + #[cfg(test)] 1 => if c { "AzureKit" } else if !c { "AzureKit" } else { "AzureKit" } + #[cfg(test)] 2 => { "AzureKit" } + 3 => "one", + #[cfg(test)] 4 => Probe { a: "AzureKit" }.label("AzureKit"), + #[cfg(test)] Probe { a: 5 } => "AzureKit", + #[cfg(test)] 6 => match c { true => "AzureKit", false => "AzureKit" } + #[cfg(test)] #[allow(unused)] 7 => unsafe { "AzureKit" } + _ => "two", + } +} +fn statements(c: bool) { + #[cfg(test)] if c { "AzureKit"; } else { "AzureKit"; } + let _ = "three"; + #[cfg(test)] { "AzureKit"; } + #[cfg(test)] probe! { "AzureKit" } + #[cfg(test)] crate::probe! /* noted */ { "AzureKit" } + let _ = "four"; + #[cfg(test)] Probe { a: "AzureKit" }.run("AzureKit"); + #[cfg(test)] 'outer: loop { "AzureKit"; } + #[cfg(test)] const { "AzureKit"; } + let _ = "five"; +} +fn tail() -> Probe { + #[cfg(test)] Probe { a: "AzureKit" }.with("AzureKit") +} +fn after() { let _ = "six"; } +"#; + assert_eq!(literals(source), ["one", "two", "three", "four", "five", "six"]); + } + + #[test] + fn test_only_blocks_keep_their_method_calls_and_question_marks() { + let source = r#"fn statements(c: bool) -> Result<(), Probe> { + #[cfg(test)] { "x" }.replace("x", "AzureKit"); + let _ = "one"; + #[cfg(test)] unsafe { probe("x") }?; + #[cfg(test)] unsafe { probe("x") }?.with("AzureKit"); + let _ = "two"; + #[cfg(test)] { "x" } /* noted */ .replace("x", "AzureKit"); + #[cfg(test)] { "x" } // noted + .replace("x", "AzureKit"); + #[cfg(test)] match c { _ => probe("x") }?.with("AzureKit"); + #[cfg(test)] probe! { "x" }.with("AzureKit"); + #[cfg(test)] { (1, "x") }.0.with("AzureKit"); + let _ = "three"; + #[cfg(test)] { "AzureKit"; } + ..; + let _ = "four"; + Ok(()) +} +fn pick(x: u8, c: bool) -> String { + match x { + #[cfg(test)] 8 => if c { "x" } else { "y" }.to_string().replace("x", "AzureKit"), + 9 => "five".into(), + #[cfg(test)] 10 => { "x" }.replace("x", "AzureKit"), + _ => "six".into(), + } +} +fn tail() -> String { + #[cfg(test)] { "x" }.replace("x", "AzureKit") +} +fn after() { let _ = "seven"; } +"#; + assert_eq!(literals(source), ["one", "two", "three", "four", "five", "six", "seven"]); + } + + #[test] + fn the_marker_counts_only_in_a_comment() { + let policy = Policy { allow_marker: Some("name-ok"), ..POLICY }; + let rust = |src: &str| scan_source(Path::new("lib.rs"), src, Kind::Rust, &policy).unwrap().len(); + assert_eq!(rust("let label = \"AzureKit name-ok\";"), 1); + assert_eq!(rust("let label = \"AzureKit\"; // name-ok: compatibility"), 0); + assert_eq!(rust("let label = \"AzureKit\"; /* name-ok */"), 0); + assert_eq!(rust("/* name-ok */ let label = \"AzureKit\";"), 0); + assert_eq!(rust("/* name-ok\n */ let label = \"AzureKit\";"), 1, "a block comment marks only its lines that hold the marker"); + assert_eq!(rust("/* a\n b name-ok */ let label = \"AzureKit\";"), 0); + assert_eq!(rust("/* a\n\n name-ok */ let label = \"AzureKit\";"), 0, "the marker's line is the comment's third"); + assert_eq!(rust("/* a\n name-ok\n */ let label = \"AzureKit\nx\";"), 1); + assert_eq!(rust("let a = \"name-ok\"; let b = \"AzureKit\";"), 1); + assert_eq!(rust("// name-ok\nlet label = \"AzureKit\";"), 1, "the line above does not count in Rust"); + assert_eq!(rust("let label = \"Azure\nKit\nAzureKit\"; // name-ok"), 0, "a mark on any line of the literal counts"); + } + + #[test] + fn letters_of_any_script_are_part_of_a_word() { + for policy in [POLICY, Policy { case: MatchCase::AsciiInsensitive, ..POLICY }] { + assert!(policy.words_in("éAzureKit").is_empty()); + assert!(policy.words_in("AzureKitß").is_empty()); + assert!(policy.words_in("AzureKit_2").is_empty()); + assert_eq!(policy.words_in("é AzureKit, é"), ["AzureKit"]); + } + } + + #[test] + fn case_follows_the_policy() { + assert!(POLICY.words_in("azurekit").is_empty()); + assert_eq!(Policy { case: MatchCase::AsciiInsensitive, ..POLICY }.words_in("azureKIT and amber TOOL"), ["Amber Tool", "AzureKit"]); + } + + #[test] + fn allowed_literals_and_exceptions_match_whole_text() { + fn group(text: &str) -> bool { + text.strip_prefix("AzureKit Design › ").is_some_and(|rest| !banned_display(rest)) + } + let policy = Policy { exceptions: &[group], ..POLICY }; + assert!(policy.words_in("AzureKit Design").is_empty()); + assert_eq!(policy.words_in("AzureKit Design extra"), ["AzureKit"]); + assert!(policy.words_in("AzureKit Design › Layout").is_empty()); + assert_eq!(policy.words_in("AzureKit Design › BlueCore Styles"), ["AzureKit", "BlueCore"]); + } + + #[test] + fn byte_and_c_strings_are_read_only_when_asked() { + let source = r##"let a = b"AzureKit"; let b = br#"AzureKit"#; let c = c"BlueCore"; let d = cr"BlueCore"; let e = b'"'; let f = b'\xFF'; let g = r#"plain"#;"##; + assert_eq!(literals(source), ["plain"]); + let bytes = super::literals(source, Literals { byte_strings: true, c_strings: false }).unwrap(); + assert_eq!(bytes.iter().map(|l| l.text.as_str()).collect::>(), ["AzureKit", "AzureKit", "plain"]); + let both = super::literals(source, Literals { byte_strings: true, c_strings: true }).unwrap(); + assert_eq!(both.len(), 5); + assert_eq!(literals("let r#type = 1; let s = \"BlueCore\";"), ["BlueCore"]); + } + + #[test] + fn raw_strings_close_at_the_same_number_of_hashes() { + assert_eq!(literals(r###"r##"a "# b"## "next""###), [r##"a "# b"##, "next"]); + assert_eq!(literals(r#"r"\x41""#), [r"\x41"]); + } + + #[test] + fn malformed_source_is_an_error() { + for (source, line) in [ + ("let a = \"open", 1), + ("\n let a = r#\"open\"", 2), + ("/* /* */", 1), + ("fn f() { }}", 1), + ("fn f() {\n", 1), + ("fn f( ]", 1), + (r#""\q""#, 1), + (r#""\x80""#, 1), + (r#""\x4""#, 1), + (r#""\u{D800}""#, 1), + (r#""\u{1234567}""#, 1), + (r#""\u{}""#, 1), + (r"let c = '\q';", 1), + (r"let c = '\n", 1), + ] { + let err = super::literals(source, Literals::default()).unwrap_err(); + assert_eq!(err.line, line, "{source}: {err}"); + } + let err = scan_source(Path::new("bad.rs"), "\"open", Kind::Rust, &POLICY).unwrap_err(); + assert!(matches!(err, Error::Syntax { .. }), "{err}"); + } + + #[test] + fn lines_count_crlf_and_words_at_literal_edges() { + let src = "fn a() {}\r\n\r\nconst A: &str = \"AzureKit\";\r\nconst B: &str = \"x\r\ny BlueCore\";\r\n"; + let found = scan_source(Path::new("lib.rs"), src, Kind::Rust, &POLICY).unwrap(); + assert_eq!(found.iter().map(|f| (f.line, f.word.as_str())).collect::>(), [(3, "AzureKit"), (4, "BlueCore")]); + assert_eq!(found[1].text, "x\ny BlueCore"); + let text = scan_source(Path::new("a.txt"), "one\r\nBlueCore\r\n", Kind::Text, &POLICY).unwrap(); + assert_eq!((text[0].line, text[0].text.as_str()), (2, "BlueCore")); + } + + #[test] + fn unreadable_and_oversized_inputs_are_errors() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path(); + fs::create_dir_all(root.join("bad")).unwrap(); + fs::write(root.join("bad/lib.rs"), b"const S: &str = \"\xff\";").unwrap(); + let err = scan(root, &[Input::rust(Path::new("bad"))], &POLICY).unwrap_err(); + assert!(matches!(err, Error::InvalidUtf8 { .. }), "{err}"); + + write(root, "big/lib.rs", &"/* */".repeat(40)); + let small = Policy { limits: Limits { max_file_bytes: 199, ..DEFAULT_LIMITS }, ..POLICY }; + let err = scan(root, &[Input::rust(Path::new("big"))], &small).unwrap_err(); + assert!(matches!(err, Error::FileTooLarge { limit: 199, .. }), "{err}"); + let exact = Policy { limits: Limits { max_file_bytes: 200, ..DEFAULT_LIMITS }, ..POLICY }; + assert!(scan(root, &[Input::rust(Path::new("big"))], &exact).unwrap().is_empty()); + + write(root, "two/a.rs", "\"one\""); + write(root, "two/b.rs", "\"two\""); + let total = Policy { limits: Limits { max_total_bytes: 9, ..DEFAULT_LIMITS }, ..POLICY }; + let err = scan(root, &[Input::rust(Path::new("two"))], &total).unwrap_err(); + assert!(matches!(&err, Error::TotalTooLarge { path, .. } if path.ends_with("b.rs")), "{err}"); + + write(root, "deep/a/b/c/lib.rs", "\"one\""); + let shallow = Policy { limits: Limits { max_depth: 2, ..DEFAULT_LIMITS }, ..POLICY }; + let err = scan(root, &[Input::rust(Path::new("deep"))], &shallow).unwrap_err(); + assert!(matches!(err, Error::TooDeep { limit: 2, .. }), "{err}"); + let enough = Policy { limits: Limits { max_depth: 3, ..DEFAULT_LIMITS }, ..POLICY }; + assert!(scan(root, &[Input::rust(Path::new("deep"))], &enough).unwrap().is_empty()); + } + + #[cfg(unix)] + #[test] + fn folder_links_are_not_followed() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path(); + write(root, "outside/lib.rs", "\"AzureKit\""); + write(root, "src/lib.rs", "\"BlueCore\""); + std::os::unix::fs::symlink(root.join("outside"), root.join("src/linked")).unwrap(); + std::os::unix::fs::symlink(root.join("outside/lib.rs"), root.join("src/file_link.rs")).unwrap(); + std::os::unix::fs::symlink(root.join("gone"), root.join("src/dangling.txt")).unwrap(); + std::os::unix::fs::symlink(root.join("gone"), root.join("src/tests.rs")).unwrap(); + let found = scan(root, &[Input::rust(Path::new("src"))], &POLICY).unwrap(); + assert_eq!(hits(&found), [("src/file_link.rs".to_string(), 1, "AzureKit"), ("src/lib.rs".to_string(), 1, "BlueCore")]); + } + + #[test] + fn findings_are_sorted_and_files_read_once() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path(); + write(root, "src/b.rs", "\"BlueCore\"\n\"AzureKit BlueCore Amber Tool\""); + write(root, "src/a.rs", "\"AzureKit\""); + let src = Path::new("src"); + let inputs = [Input::rust(Path::new("src/b.rs")), Input::rust(src), Input::rust(src)]; + let found = scan(root, &inputs, &POLICY).unwrap(); + let expected = [ + ("src/a.rs", 1, "AzureKit"), + ("src/b.rs", 1, "BlueCore"), + ("src/b.rs", 2, "Amber Tool"), + ("src/b.rs", 2, "AzureKit"), + ("src/b.rs", 2, "BlueCore"), + ]; + assert_eq!(hits(&found), expected.map(|(p, l, w)| (p.to_string(), l, w))); + assert_eq!(found[0].to_string(), format!("{}:1: \"AzureKit\" in \"AzureKit\"", Path::new("src").join("a.rs").display())); + let twice = [Input::rust(src), Input::text(Path::new("src/a.rs"))]; + let err = scan(root, &twice, &POLICY).unwrap_err(); + assert!(matches!(&err, Error::Conflict { path } if path.ends_with("a.rs")), "{err}"); + } + + #[test] + fn policies_that_match_nothing_are_errors() { + let empty: &[&str] = &[]; + for policy in [Policy::new(empty), Policy::new(&["AzureKit", " "]), Policy { allow_marker: Some(""), ..POLICY }] { + let err = scan_source(Path::new("f"), "", Kind::Text, &policy).unwrap_err(); + assert!(matches!(err, Error::Policy(_)), "{err}"); + } + } +} From 3096cf1b80523ca3fb2b82d4f57a7c5f316fb8bc Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 23 Nov 2026 23:59:09 -0800 Subject: [PATCH 02/12] craft-xtask: fix the name scanner's review findings - A banned word that overlaps itself is found at every candidate, not only the first (`A-A` in `XA-A-A`). - A link to a folder is not followed when it is an input either. - The scan's remaining byte budget is checked before a file is read. - A shebang line is skipped before the lexer reads tokens. - Under `#[cfg(test)]`, const parameters, const arguments in a return type and struct patterns in `if let` no longer end an item early. - Under `#[cfg(test)]`, an item keyword counts only after an item's qualifiers, so the `fn` in `helper::(..)` or in a field's type does not start an item that hides the code after it, and the braces of a macro in an item's header (`-> ty! { u8 }`) are not its body. - The allow marker counts in a comment inside a `cfg(test)` attribute, and attribute strings are read as tokens when outer attributes are skipped. - A manifest's dotted `package.description` before the first header is read, as documented. - A relative and an absolute input for one file are read once, also under a relative base folder. --- crates/xtask/README.md | 29 +- crates/xtask/src/names.rs | 550 ++++++++++++++++++++++++++++++++------ 2 files changed, 483 insertions(+), 96 deletions(-) diff --git a/crates/xtask/README.md b/crates/xtask/README.md index 80659f8597..c6a74f1488 100644 --- a/crates/xtask/README.md +++ b/crates/xtask/README.md @@ -87,24 +87,29 @@ fn production_display_names_stay_neutral() { - `Input::rust` takes a Rust file or a folder, which is read recursively for `.rs` files that `Policy::skip` keeps, given their path below the folder (`test_path` by default: `tests.rs`, - `*_tests.rs` and files under a `tests` folder). Links to folders are not followed. A file input - is read whatever its name. + `*_tests.rs` and files under a `tests` folder). Links to folders are not followed, also when the + link is an input. A file input is read whatever its name. - `Input::manifest` reads the values at the key paths `package.description` and `workspace.package.description` of a `Cargo.toml`: the table header (`[package]`, `[workspace.package]`) joined with the key, which may be bare, quoted or dotted. Headers and keys - inside strings and arrays do not count, and a key inside an inline table is not read. A - `description` under any other table, or before the first header, is not read. A TOML string, + inside strings and arrays do not count, and a key inside an inline table is not read. Before the + first header, the dotted keys `package.description` and `workspace.package.description` are read, + and a bare `description` is not. A `description` under any other table is not read. A TOML string, single-line or multi-line, is decoded, and a comment after it is not read; any other value is read as the rest of its line. `Input::text` reads every line of a text file, and `Input::text_mark_above` also lets a `#` line with the marker exempt the line below it. Inputs must not include binary files. - In Rust, the scanner reads `"…"` and `r"…"` literals with any number of `#`, decodes every escape (`\n`, `\x7A`, `\u{…}`, a backslash before a line break) and reads CRLF as LF. `b"…"`, `br"…"`, - `c"…"` and `cr"…"` are read when `Policy::literals` asks for them. Comments, character, byte and - lifetime tokens, and the rest of a module after `#![cfg(test)]` are skipped. So is the code under - `#[cfg(test)]`, comments in the attribute included: a `const`, `static` or `let` up to its `;`, - another item up to the `}` that closes it or the `;` that ends it, and a field, argument, arm or - statement up to its `,` or `;`. A block expression (a block, a macro's braces, a labeled block, or + `c"…"` and `cr"…"` are read when `Policy::literals` asks for them; their `\x` escapes are decoded + together as UTF-8, and bytes that are not UTF-8 become U+FFFD. A shebang line, comments, + character, byte and lifetime tokens, and the rest of a module after `#![cfg(test)]` are skipped. + So is the code under `#[cfg(test)]`, comments in the attribute included: a `const`, `static` or + `let` up to its `;`, another item up to the `}` that closes it or the `;` that ends it, and a + field, argument, arm or statement up to its `,` or `;`. An item keyword counts only when nothing + but qualifiers (`pub`, `unsafe`, `extern "C"` and the like) comes before it, so the `fn` in + `f::(…)` or in a field's type does not start an item, and the braces of a macro in an + item's header (`-> ty! { u8 }`) are not its body. A block expression (a block, a macro's braces, a labeled block, or `if`, `match`, `loop`, `while`, `for`, `unsafe`, `async` or `const`), as the whole statement or as an arm's expression after `=>`, ends at its `}`, or at the last `}` of an `if` with `else` branches. When a method call, field or `?` follows that `}`, it runs up to its `,` or `;` @@ -116,6 +121,9 @@ fn production_display_names_stay_neutral() { description or trimmed text line, never part of one. - In Rust, the marker counts only inside a comment on a line the literal spans. In a manifest it counts anywhere on a line the description spans, and in a text file anywhere on the line. +- `scan` identifies a file by its input path joined to the base folder, and joins a relative base + folder to the current folder first, so a relative and an absolute input for one file are read + once; `..` and links are not resolved. - `scan` reads each file once, in path order, and returns the findings in path order, then source order, then the order of `banned`. A `Finding` has the path, the first line of the literal or description, the word, the decoded text or text line and the kind; its `Display` text is @@ -124,7 +132,8 @@ fn production_display_names_stay_neutral() { - `names::Error` is `Policy` (no banned words, an empty word or an empty marker), `Io` (with the path), `InvalidUtf8`, `FileTooLarge`, `TotalTooLarge`, `TooDeep`, `Conflict` (a file given as two kinds of input) or `Syntax` (with the path and line). `DEFAULT_LIMITS` is 8 MiB per file, 512 MiB - per scan and folders 64 deep; a file over the limit is rejected before it is read. + per scan and folders 64 deep; a file larger than the per-file limit, or than what is left of the + scan's limit, is rejected before it is read. The source copies differ in a few rules. An app that switches lists the results that change: diff --git a/crates/xtask/src/names.rs b/crates/xtask/src/names.rs index 57d4f89de8..0cbb1a0b20 100644 --- a/crates/xtask/src/names.rs +++ b/crates/xtask/src/names.rs @@ -292,10 +292,14 @@ impl From for String { /// Every finding in `inputs`, read from `base`. Files are read once each, in path order, and the /// findings come in path order, then in source order, then in the order of [`Policy::banned`]. /// +/// A file is identified by its path joined to `base`, and a relative `base` is joined to the +/// current folder first, so a relative and an absolute path to it are one file, read once and +/// reported by the input path that sorts first; `..` and links are not resolved. +/// /// A [`Kind::Rust`] folder is read recursively for `.rs` files that [`Policy::skip`] keeps; links to -/// folders are not followed. A file input is read whatever its name. Every error stops the scan: a -/// missing input, a file that cannot be read, is not UTF-8 or is over a limit, and a Rust source the -/// lexer cannot read. Inputs must not include binary files. +/// folders are not followed, also when the link is an input. A file input is read whatever its +/// name. Every error stops the scan: a missing input, a file that cannot be read, is not UTF-8 or +/// is over a limit, and a Rust source the lexer cannot read. Inputs must not include binary files. pub fn scan(base: &Path, inputs: &[Input<'_>], policy: &Policy<'_>) -> Result, Error> { policy.validate()?; let mut files = Vec::new(); @@ -306,15 +310,24 @@ pub fn scan(base: &Path, inputs: &[Input<'_>], policy: &Policy<'_>) -> Result = files.into_iter().map(|(path, kind)| (root.join(&path), kind, path)).collect(); files.sort(); - files.dedup(); + files.dedup_by(|later, first| later.0 == first.0 && later.1 == first.1); if let Some(pair) = files.windows(2).find(|pair| pair[0].0 == pair[1].0) { - return Err(Error::Conflict { path: pair[0].0.clone() }); + return Err(Error::Conflict { path: pair[0].2.clone() }); } + files.sort_by(|a, b| a.2.cmp(&b.2)); let mut total = 0; let mut findings = Vec::new(); - for (path, kind) in &files { - let source = read_limited(&base.join(path), path, &policy.limits, &mut total)?; + for (full, kind, path) in &files { + let source = read_limited(full, path, &policy.limits, &mut total)?; findings.extend(scan_source(path, &source, *kind, policy)?); } Ok(findings) @@ -565,22 +578,33 @@ pub struct Literal { /// `while`, `for`, `unsafe`, `async` or `const`), as the whole statement or as an arm's expression /// after `=>`, ends at its `}`, or at the last `}` of an `if` with `else` branches. When a method /// call, field or `?` follows that `}`, it runs up to its `,` or `;` instead. Character, byte and -/// lifetime tokens are read, so a quote in them does not start a string. +/// lifetime tokens are read, so a quote in them does not start a string. A shebang first line is +/// skipped. pub fn literals(source: &str, kinds: Literals) -> Result, SyntaxError> { lex(source, kinds, None).map(|lexed| lexed.literals) } fn contains_word(text: &str, word: &str, case: MatchCase) -> bool { match case { - MatchCase::Exact => text.match_indices(word).any(|(at, _)| whole_word(text, at, word.len())), - MatchCase::AsciiInsensitive => { - let lower = text.to_ascii_lowercase(); - let word = word.to_ascii_lowercase(); - lower.match_indices(&word).any(|(at, _)| whole_word(text, at, word.len())) - } + MatchCase::Exact => any_whole_word(text, text, word), + MatchCase::AsciiInsensitive => any_whole_word(&text.to_ascii_lowercase(), text, &word.to_ascii_lowercase()), } } +/// Whether `word` occurs in `haystack` at an index where it is a whole word of `text`, which has the +/// same byte offsets. Every occurrence is checked, overlapping ones included. +fn any_whole_word(haystack: &str, text: &str, word: &str) -> bool { + let mut from = 0; + while let Some(found) = haystack.get(from..).and_then(|rest| rest.find(word)) { + let at = from + found; + if whole_word(text, at, word.len()) { + return true; + } + from = at + haystack.get(at..).and_then(|rest| rest.chars().next()).map_or(1, char::len_utf8); + } + false +} + fn is_word(c: char) -> bool { c.is_alphanumeric() || c == '_' } @@ -591,14 +615,21 @@ fn whole_word(text: &str, at: usize, len: usize) -> bool { !before.is_some_and(is_word) && !after.is_some_and(is_word) } +fn push_char(text: &mut Vec, c: char) { + text.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes()); +} + fn io_error(path: &Path) -> impl FnOnce(io::Error) -> Error + '_ { move |source| Error::Io { path: path.to_path_buf(), source } } fn collect_rust(base: &Path, path: &Path, policy: &Policy<'_>, out: &mut Vec<(PathBuf, Kind)>) -> Result<(), Error> { - let meta = fs::metadata(base.join(path)).map_err(io_error(path))?; + let full = base.join(path); + let link = fs::symlink_metadata(&full).map_err(io_error(path))?.file_type().is_symlink(); + let meta = fs::metadata(&full).map_err(io_error(path))?; if meta.is_dir() { - walk(base, path, path, policy, 0, out) + // A link to a folder is not followed, also when it is an input. + if link { Ok(()) } else { walk(base, path, path, policy, 0, out) } } else { out.push((path.to_path_buf(), Kind::Rust)); Ok(()) @@ -636,16 +667,23 @@ fn read_limited(full: &Path, path: &Path, limits: &Limits, total: &mut u64) -> R if len > limits.max_file_bytes { return Err(Error::FileTooLarge { path: path.to_path_buf(), limit: limits.max_file_bytes }); } + let remaining = limits.max_total_bytes.saturating_sub(*total); + let over_total = || Error::TotalTooLarge { path: path.to_path_buf(), limit: limits.max_total_bytes }; + if len > remaining { + return Err(over_total()); + } let mut bytes = Vec::with_capacity(usize::try_from(len).unwrap_or(0)); - file.take(limits.max_file_bytes.saturating_add(1)).read_to_end(&mut bytes).map_err(io_error(path))?; + // One byte past the smaller limit shows a file that grew after its length was read. + let cap = limits.max_file_bytes.min(remaining); + file.take(cap.saturating_add(1)).read_to_end(&mut bytes).map_err(io_error(path))?; let read = u64::try_from(bytes.len()).unwrap_or(u64::MAX); if read > limits.max_file_bytes { return Err(Error::FileTooLarge { path: path.to_path_buf(), limit: limits.max_file_bytes }); } - *total = total.saturating_add(read); - if *total > limits.max_total_bytes { - return Err(Error::TotalTooLarge { path: path.to_path_buf(), limit: limits.max_total_bytes }); + if read > remaining { + return Err(over_total()); } + *total = total.saturating_add(read); let mut text = String::from_utf8(bytes).map_err(|_| Error::InvalidUtf8 { path: path.to_path_buf() })?; if text.starts_with('\u{feff}') { text.remove(0); @@ -682,17 +720,30 @@ const BLOCK_KEYWORDS: &[&str] = &["async", "const", "for", "if", "loop", "match" enum TestRegion { /// An item, field, argument, arm or statement under `#[cfg(test)]`, at this delimiter depth. /// `start` is the index where its code (or an arm's expression, after `=>`) starts; `opened` is - /// set once its first `{` is read, `item` once an item keyword (`fn`, `impl`, `mod`…) is read, - /// and `semi` while the last such keyword is `const`, `let` or `static`, whose items end at their - /// `;`. Without an item keyword, `block` is set when that first `{` starts a block expression (a - /// block, a macro's braces, or `if`, `match`, `loop` and the like), which ends at its `}` unless - /// an `else` follows. A `.` or `?` after that `}` clears `block`, so the expression ends at its - /// `;` or `,`. - Item { level: usize, start: usize, opened: bool, item: bool, semi: bool, block: bool }, + /// set once its first `{` is read, `item` once its first item keyword (`fn`, `impl`, `mod`…) is + /// read where only qualifiers (`pub`, `unsafe`, `extern "C"`…) come before it, and `semi` when that keyword is `const` (not `const fn`), `let` or `static`, whose items + /// end at their `;`. Without an item keyword, `block` is set when that first `{` starts a block + /// expression (a block, a macro's braces, or `if`, `match`, `loop` and the like), which ends at + /// its `}` unless an `else` follows. A `.` or `?` after that `}` clears `block`, so the expression + /// ends at its `;` or `,`. A `{` does not count as the first while `pattern` is set (between the + /// `let` of an `if` or `while` condition and its `=`, or a `for` and its `in`) or while `angle` + /// generic brackets of an item's header are open. + Item { level: usize, start: usize, opened: bool, item: bool, semi: bool, block: bool, pattern: bool, angle: usize }, /// The rest of the module that `#![cfg(test)]` starts, at this delimiter depth. Inner { level: usize }, } +impl TestRegion { + /// Test-only code at `level` whose code starts at `start`. + fn item(level: usize, start: usize) -> Self { + TestRegion::Item { level, start, opened: false, item: false, semi: false, block: false, pattern: false, angle: 0 } + } +} + +/// The keywords that start a block expression whose header can hold item keywords that do not start +/// an item (`if let`, `for … in`). +const HEADER_KEYWORDS: &[&str] = &["for", "if", "loop", "match", "while"]; + struct Lexer<'s> { chars: Vec, at: usize, @@ -702,6 +753,11 @@ struct Lexer<'s> { /// Open delimiters and the line of each. delims: Vec<(char, usize)>, test: Option, + /// Whether the last token read, past whitespace and comments, is a word. + after_word: bool, + /// Whether the last token read, past whitespace and comments, is a macro's `!`: a `!` after a + /// word, not the `!` of a never type (`-> !`). + after_bang: bool, out: Lexed, } @@ -714,6 +770,8 @@ fn lex(source: &str, kinds: Literals, marker: Option<&str>) -> Result { } fn run(&mut self) -> Result<(), SyntaxError> { + // A first line that starts with `#!` is a shebang, unless a `[` follows (an inner attribute). + let start = usize::from(self.peek(0) == Some('\u{feff}')); + if self.chars.get(start..start + 2) == Some(&['#', '!'][..]) && self.chars.get(self.skip_space(start + 2)) != Some(&'[') { + while self.peek(0).is_some_and(|c| c != '\n') { + self.at += 1; + } + } while let Some(c) = self.peek(0) { + let token = !c.is_whitespace() && !(c == '/' && matches!(self.peek(1), Some('/' | '*'))); match c { '\n' => { self.line += 1; @@ -749,16 +815,22 @@ impl Lexer<'_> { self.string(Form::Str)?; } '\'' => self.quote(Form::Char)?, - '#' => self.hash(), + '#' => self.hash()?, '{' | '(' | '[' => self.open(c), '}' | ')' | ']' => self.close(c)?, ';' => self.item_end(false), ',' => self.item_end(true), '=' if self.peek(1) == Some('>') => self.arrow(), + '=' => self.equals(), + '<' | '>' => self.angle(c), c if c.is_ascii_digit() => self.number(), c if is_word(c) => self.word()?, _ => self.at += 1, } + if token { + self.after_bang = c == '!' && self.after_word; + self.after_word = is_word(c); + } } match self.delims.last() { Some(&(_, line)) => Err(Self::error(line, "unclosed delimiter")), @@ -820,10 +892,12 @@ impl Lexer<'_> { } } - /// A `"…"` body after its opening quote. + /// A `"…"` body after its opening quote. The text is collected as bytes, so that the `\x` + /// escapes of a byte or C string decode as UTF-8 together; bytes that are not UTF-8 become + /// U+FFFD. fn string(&mut self, form: Form) -> Result<(), SyntaxError> { let first = self.line; - let mut text = String::new(); + let mut text = Vec::new(); loop { match self.next() { None => return Err(Self::error(first, "unterminated string literal")), @@ -832,17 +906,17 @@ impl Lexer<'_> { Some('\r') if self.peek(0) == Some('\n') => {} Some('\n') => { self.line += 1; - text.push('\n'); + text.push(b'\n'); } - Some(c) => text.push(c), + Some(c) => push_char(&mut text, c), } } - self.push(first, text, form); + self.push(first, String::from_utf8_lossy(&text).into_owned(), form); Ok(()) } /// The escape after a backslash, decoded into `text`. - fn escape(&mut self, form: Form, text: &mut String, first: usize) -> Result<(), SyntaxError> { + fn escape(&mut self, form: Form, text: &mut Vec, first: usize) -> Result<(), SyntaxError> { let decoded = match self.next() { None => return Err(Self::error(first, "unterminated escape")), Some('n') => '\n', @@ -850,7 +924,10 @@ impl Lexer<'_> { Some('t') => '\t', Some('0') => '\0', Some(c @ ('\\' | '\'' | '"')) => c, - Some('x') => self.hex_escape(form)?, + Some('x') => { + text.push(self.hex_escape(form)?); + return Ok(()); + } Some('u') if form != Form::Byte => self.unicode_escape()?, Some('\n') if form != Form::Char => { self.continuation(); @@ -863,7 +940,7 @@ impl Lexer<'_> { } Some(_) => return Err(Self::error(self.line, "unknown character escape")), }; - text.push(decoded); + push_char(text, decoded); Ok(()) } @@ -876,16 +953,16 @@ impl Lexer<'_> { } } - fn hex_escape(&mut self, form: Form) -> Result { + /// The byte of a `\x` escape after its `x`. + fn hex_escape(&mut self, form: Form) -> Result { let digits = (self.peek(0).and_then(|c| c.to_digit(16)), self.peek(1).and_then(|c| c.to_digit(16))); let (Some(high), Some(low)) = digits else { return Err(Self::error(self.line, "a \\x escape needs two hex digits")); }; self.at += 2; - let value = high * 16 + low; + let value = u8::try_from(high * 16 + low).unwrap_or(u8::MAX); match (value, form) { - (0..=0x7f, _) => Ok(char::from_u32(value).unwrap_or(char::REPLACEMENT_CHARACTER)), - (_, Form::Byte | Form::C) => Ok(char::REPLACEMENT_CHARACTER), + (0..=0x7f, _) | (_, Form::Byte | Form::C) => Ok(value), _ => Err(Self::error(self.line, "a \\x escape above 0x7F")), } } @@ -919,7 +996,7 @@ impl Lexer<'_> { (Some('\\'), _) => { let first = self.line; self.at += 2; - self.escape(form, &mut String::new(), first)?; + self.escape(form, &mut Vec::new(), first)?; if self.next() != Some('\'') { return Err(Self::error(first, "unterminated character literal")); } @@ -941,16 +1018,7 @@ impl Lexer<'_> { let start = self.at; self.number(); let word: String = self.chars.get(start..self.at).unwrap_or_default().iter().collect(); - // `const { .. }` is a block expression, not an item. - let const_block = word == "const" && self.chars.get(self.skip_space(self.at)) == Some(&'{'); - if let Some(TestRegion::Item { level, opened: false, item, semi, .. }) = &mut self.test - && *level == self.delims.len() - && ITEM_KEYWORDS.contains(&word.as_str()) - && !const_block - { - *item = true; - *semi = SEMICOLON_KEYWORDS.contains(&word.as_str()); - } + self.header_word(start, &word); match (word.as_str(), self.peek(0)) { ("b", Some('\'')) => self.quote(Form::Byte), ("b", Some('"')) => { @@ -968,6 +1036,63 @@ impl Lexer<'_> { } } + /// Reads a word at `at` in the header of test-only code: its first item keyword decides how the + /// item ends, and `let`, `for` and `in` mark where a pattern's braces are. + fn header_word(&mut self, at: usize, word: &str) { + let Some(TestRegion::Item { level, start, opened: false, item, .. }) = self.test else { return }; + if level != self.delims.len() { + return; + } + let code = self.code_start(start); + let lead = self.word_at(code); + let next = self.word_at(self.skip_space(self.at)); + let header = HEADER_KEYWORDS.contains(&lead.as_str()); + let (keyword, pattern) = match word { + "let" if matches!(lead.as_str(), "if" | "while") => (false, Some(true)), + "for" if lead == "for" && at == code => (false, Some(true)), + "in" if lead == "for" => (false, Some(false)), + // `const { .. }` is a block expression, not an item. + "const" if self.chars.get(self.skip_space(self.at)) == Some(&'{') => (false, None), + // An item keyword counts only after the item's qualifiers, so `fn` in `f::(..)` does not. + _ => (!item && !header && ITEM_KEYWORDS.contains(&word) && self.item_qualifiers(code, at), None), + }; + if let Some(TestRegion::Item { item, semi, pattern: reading, .. }) = &mut self.test { + if keyword { + *item = true; + *semi = SEMICOLON_KEYWORDS.contains(&word) && !(word == "const" && matches!(next.as_str(), "async" | "extern" | "fn" | "unsafe")); + } + if let Some(pattern) = pattern { + *reading = pattern; + } + } + } + + /// A `=` that is not part of `==`, `!=`, `<=`, `>=` or `..=` ends a `let` pattern. + fn equals(&mut self) { + let before = self.at.checked_sub(1).and_then(|i| self.chars.get(i)).copied(); + let compares = matches!(before, Some('=' | '!' | '<' | '>' | '.')) || self.peek(1) == Some('='); + if let Some(TestRegion::Item { level, pattern, .. }) = &mut self.test + && *level == self.delims.len() + && !compares + { + *pattern = false; + } + self.at += 1; + } + + /// A `<` or `>` (not the `>` of `->`) in an item's header before its body opens or closes a + /// generic bracket. + fn angle(&mut self, c: char) { + let arrow = c == '>' && self.at.checked_sub(1).and_then(|i| self.chars.get(i)) == Some(&'-'); + if let Some(TestRegion::Item { level, opened: false, item: true, angle, .. }) = &mut self.test + && *level == self.delims.len() + && !arrow + { + *angle = if c == '<' { *angle + 1 } else { angle.saturating_sub(1) }; + } + self.at += 1; + } + /// A raw literal after its `r`, or a raw identifier (`r#name`), which is left to the caller. fn raw(&mut self, form: Form) -> Result<(), SyntaxError> { let hashes = self.chars.get(self.at..).unwrap_or_default().iter().take_while(|&&c| c == '#').count(); @@ -1048,22 +1173,29 @@ impl Lexer<'_> { Some((at, inner)) } - fn hash(&mut self) { + fn hash(&mut self) -> Result<(), SyntaxError> { let Some((end, inner)) = self.cfg_test_end() else { self.at += 1; - return; + return Ok(()); }; - let skipped = self.chars.get(self.at..end).unwrap_or_default(); - self.line += skipped.iter().filter(|&&c| c == '\n').count(); - self.at = end; + // The attribute holds only its tokens, whitespace and comments; the comments can hold the + // marker. + while self.at < end { + match (self.peek(0), self.peek(1)) { + (Some('/'), Some('/')) => self.line_comment(), + (Some('/'), Some('*')) => self.block_comment()?, + (Some('\n'), _) => { + self.line += 1; + self.at += 1; + } + _ => self.at += 1, + } + } if self.test.is_none() { let level = self.delims.len(); - self.test = Some(if inner { - TestRegion::Inner { level } - } else { - TestRegion::Item { level, start: end, opened: false, item: false, semi: false, block: false } - }); + self.test = Some(if inner { TestRegion::Inner { level } } else { TestRegion::item(level, end) }); } + Ok(()) } /// A `=>` at the level of a test-only arm: its expression starts after it. @@ -1072,7 +1204,7 @@ impl Lexer<'_> { if let Some(TestRegion::Item { level, item: false, .. }) = self.test && level == self.delims.len() { - self.test = Some(TestRegion::Item { level, start: self.at, opened: false, item: false, semi: false, block: false }); + self.test = Some(TestRegion::item(level, self.at)); } } @@ -1080,30 +1212,78 @@ impl Lexer<'_> { /// the block itself, a macro's braces, a labeled block or loop, or a block keyword's expression. /// Outer attributes before it are passed over. fn starts_block(&self, start: usize) -> bool { + let at = self.code_start(start); + at == self.at || self.chars.get(at) == Some(&'\'') || BLOCK_KEYWORDS.contains(&self.word_at(at).as_str()) || self.macro_braces(at) + } + + /// The index of the code at `start`, past whitespace, comments and outer attributes. + fn code_start(&self, start: usize) -> usize { let mut at = self.skip_space(start); while self.chars.get(at) == Some(&'#') { - at = self.skip_space(at + 1); - if self.chars.get(at) != Some(&'[') { - return false; - } - let mut depth = 0_usize; - loop { - match self.chars.get(at) { - None => return false, - Some('[') => depth += 1, - Some(']') => { - depth -= 1; - if depth == 0 { - break; - } + let open = self.skip_space(at + 1); + let Some(end) = self.chars.get(open).filter(|&&c| c == '[').and_then(|_| self.group_end(open)) else { + break; + }; + at = self.skip_space(end); + } + at + } + + /// The index after the delimiter group that opens at `at`, read as tokens: delimiters inside + /// strings, character literals and comments do not count. `None` when it does not close. + fn group_end(&self, mut at: usize) -> Option { + let mut depth = 0_usize; + loop { + let c = *self.chars.get(at)?; + match c { + '[' | '(' | '{' => depth += 1, + ']' | ')' | '}' => { + depth = depth.checked_sub(1)?; + if depth == 0 { + return Some(at + 1); + } + } + '/' if matches!(self.chars.get(at + 1), Some('/' | '*')) => { + at = self.skip_space(at); + continue; + } + '"' => { + at = self.quoted_end(at + 1, false, 0)?; + continue; + } + '\'' => { + at += match (self.chars.get(at + 1), self.chars.get(at + 2)) { + (Some('\\'), _) => self.chars.get(at + 3..)?.iter().position(|&q| q == '\'')? + 4, + (Some(_), Some('\'')) => 3, + _ => 1, + }; + continue; + } + c if is_word(c) => { + let word = self.word_at(at); + at += word.chars().count(); + let hashes = self.chars.get(at..).unwrap_or_default().iter().take_while(|&&h| h == '#').count(); + if matches!(word.as_str(), "r" | "br" | "cr") && self.chars.get(at + hashes) == Some(&'"') { + at = self.quoted_end(at + hashes + 1, true, hashes)?; } - _ => {} + continue; } - at += 1; + _ => {} + } + at += 1; + } + } + + /// The index after the closing quote of a string whose text starts at `at`: a raw string closed + /// by a quote and `hashes` hashes, or a string whose backslashes escape the next character. + fn quoted_end(&self, mut at: usize, raw: bool, hashes: usize) -> Option { + loop { + match *self.chars.get(at)? { + '\\' if !raw => at += 2, + '"' if (1..=hashes).all(|n| self.chars.get(at + n) == Some(&'#')) => return Some(at + 1 + hashes), + _ => at += 1, } - at = self.skip_space(at + 1); } - at == self.at || self.chars.get(at) == Some(&'\'') || BLOCK_KEYWORDS.contains(&self.word_at(at).as_str()) || self.macro_braces(at) } /// Whether the code at `at` is a macro path, `!` and the `{` at the current index. @@ -1125,17 +1305,52 @@ impl Lexer<'_> { self.chars.get(at) == Some(&'!') && self.skip_space(at + 1) == self.at } + /// Whether the code from `at` to `end` holds only an item's qualifiers: `pub` with its optional + /// `(..)`, `async`, `const`, `default`, `extern` with its optional ABI string, `safe` and + /// `unsafe`. + fn item_qualifiers(&self, mut at: usize, end: usize) -> bool { + loop { + at = self.skip_space(at); + if at >= end { + return at == end; + } + let word = self.word_at(at); + match word.as_str() { + "pub" => { + at += 3; + let open = self.skip_space(at); + if open < end && self.chars.get(open) == Some(&'(') { + let Some(close) = self.group_end(open) else { return false }; + at = close; + } + } + "async" | "const" | "default" | "extern" | "safe" | "unsafe" => at += word.chars().count(), + "" if self.chars.get(at) == Some(&'"') => { + let Some(close) = self.quoted_end(at + 1, false, 0) else { return false }; + at = close; + } + _ => return false, + } + } + } + fn word_at(&self, at: usize) -> String { self.chars.get(at..).unwrap_or_default().iter().take_while(|&&c| is_word(c)).collect() } fn open(&mut self, c: char) { - if let Some(TestRegion::Item { level, start, opened: false, item, semi, .. }) = self.test + if let Some(TestRegion::Item { level, start, opened: false, item, pattern: false, angle: 0, .. }) = self.test && level == self.delims.len() && c == '{' { - let block = !item && self.starts_block(start); - self.test = Some(TestRegion::Item { level, start, opened: true, item, semi, block }); + let starts = !item && self.starts_block(start); + // In an item's header, the braces of a macro (`fn f() -> ty! { u8 } { .. }`) are not its body. + if !(item && self.after_bang) + && let Some(TestRegion::Item { opened, block, .. }) = &mut self.test + { + *opened = true; + *block = starts; + } } self.delims.push((c, self.line)); self.at += 1; @@ -1154,14 +1369,23 @@ impl Lexer<'_> { self.at += 1; let depth = self.delims.len(); let ends = match self.test { - Some(TestRegion::Item { level, start, opened, item, semi, block }) => { + Some(TestRegion::Item { level, opened, item, semi, block, .. }) => { let block_end = opened && depth == level && c == '}'; + let branch = block_end && !item && block && self.word_at(self.skip_space(self.at)) == "else"; if block_end && !item && block && self.postfix_follows() { // A method call, field or `?` continues the expression to its `;` or `,`. - self.test = Some(TestRegion::Item { level, start, opened, item, semi, block: false }); + if let Some(TestRegion::Item { block, .. }) = &mut self.test { + *block = false; + } + false + } else if branch { + // The `else` branch's header (`else if let P { .. } = p`) is read like the first. + if let Some(TestRegion::Item { opened, .. }) = &mut self.test { + *opened = false; + } false } else { - depth < level || (block_end && if item { !semi } else { block && self.word_at(self.skip_space(self.at)) != "else" }) + depth < level || (block_end && if item { !semi } else { block }) } } Some(TestRegion::Inner { level }) => depth < level, @@ -1754,6 +1978,9 @@ fn after() { let _ = "seven"; } std::os::unix::fs::symlink(root.join("gone"), root.join("src/tests.rs")).unwrap(); let found = scan(root, &[Input::rust(Path::new("src"))], &POLICY).unwrap(); assert_eq!(hits(&found), [("src/file_link.rs".to_string(), 1, "AzureKit"), ("src/lib.rs".to_string(), 1, "BlueCore")]); + let explicit = [Input::rust(Path::new("src/linked")), Input::rust(Path::new("src/file_link.rs"))]; + let found = scan(root, &explicit, &POLICY).unwrap(); + assert_eq!(hits(&found), [("src/file_link.rs".to_string(), 1, "AzureKit")]); } #[test] @@ -1779,6 +2006,157 @@ fn after() { let _ = "seven"; } assert!(matches!(&err, Error::Conflict { path } if path.ends_with("a.rs")), "{err}"); } + #[test] + fn overlapping_candidates_are_each_checked() { + for case in [MatchCase::Exact, MatchCase::AsciiInsensitive] { + let policy = Policy { case, ..Policy::new(&["A-A"]) }; + assert_eq!(policy.words_in("XA-A-A"), ["A-A"]); + assert!(policy.words_in("XA-A-AX").is_empty()); + } + } + + #[test] + fn a_shebang_line_is_not_read_as_code() { + let source = "#!/opt/tool(v1/bin/rust-script\nconst S: &str = \"AzureKit\";\n"; + let found = scan_source(Path::new("main.rs"), source, Kind::Rust, &POLICY).unwrap(); + assert_eq!(found.iter().map(|f| (f.line, f.word.as_str())).collect::>(), [(2, "AzureKit")]); + assert_eq!(literals("#!/usr/bin/env run-cargo-script\n\"live\""), ["live"]); + assert_eq!(literals("#![cfg(test)]\nfn a() { \"AzureKit\"; }\n"), Vec::::new()); + assert_eq!(literals("#! /* note */ [cfg(test)]\nfn a() { \"AzureKit\"; }\n"), Vec::::new()); + assert_eq!(literals("\"one\" #!x"), ["one"]); + } + + #[test] + fn test_only_item_headers_do_not_end_items_early() { + let source = r#"#[cfg(test)] fn generic() {} +fn one() { let _ = "one"; } +#[cfg(test)] impl Probe {} +fn two() { let _ = "two"; } +#[cfg(test)] fn fixture() -> Tag<{ 1 }> { let _ = "AzureKit"; Tag } +fn three() { let _ = "three"; } +#[cfg(test)] fn nested() -> Tag> where Tag<{ 3 }>: Clone { let _ = "AzureKit"; Tag } +#[cfg(test)] const fn k() -> u8 { 1 } +fn four() { let _ = "four"; } +#[cfg(test)] const unsafe fn u() -> u8 { 1 } +fn five() { let _ = "five"; } +#[cfg(test)] const C: fn() -> u8 = k; +fn six() { let _ = "six"; } +"#; + assert_eq!(literals(source), ["one", "two", "three", "four", "five", "six"]); + } + + #[test] + fn item_keywords_inside_test_only_expressions_do_not_start_items() { + let source = r#"fn arms(x: u8) { + match x { + #[cfg(test)] + 0 => helper::("AzureKit"), + _ => helper::("one"), + } + call(#[cfg(test)] cast::("AzureKit"), "two"); +} +struct S { + #[cfg(test)] + hook: fn() -> u8, + name: Tag<"three">, +} +#[cfg(test)] pub(crate) unsafe extern "C" fn ffi() { let _ = "AzureKit"; } +fn four() { let _ = "four"; } +#[cfg(test)] fn typed() -> ty! { u8 } { let _ = "AzureKit"; 0 } +fn five() { let _ = "five"; } +#[cfg(test)] fn noted() -> ty! /* a { */ { u8 } { let _ = "AzureKit"; 0 } +fn six() { let _ = "six"; } +#[cfg(test)] fn halt() -> ! { let _ = "AzureKit"; loop {} } +fn seven() { let _ = "seven"; } +"#; + assert_eq!(literals(source), ["one", "two", "three", "four", "five", "six", "seven"]); + } + + #[test] + fn test_only_let_conditions_end_at_their_body() { + let source = r#"fn statements(p: P, o: Option) { + #[cfg(test)] if let P { x: 0 } = p { let _ = "AzureKit"; } + let _ = "one"; + #[cfg(test)] if let Some(x) = o { "AzureKit"; } + let _ = "two"; + #[cfg(test)] while let P { x: 1 } = p { "AzureKit"; } + let _ = "three"; + #[cfg(test)] for P { x } in [p] { "AzureKit"; } + let _ = "four"; + #[cfg(test)] if true && let P { x: 0..=2 } = p { "AzureKit"; } else { "AzureKit"; } + let _ = "five"; + #[cfg(test)] if o == Some(1) { "AzureKit"; } + let _ = "six"; + #[cfg(test)] if o.is_none() { "AzureKit"; } else if let P { x: 0 } = p { "AzureKit"; } else { "AzureKit"; } + let _ = "seven"; +} +"#; + assert_eq!(literals(source), ["one", "two", "three", "four", "five", "six", "seven"]); + } + + #[test] + fn attribute_strings_and_comments_are_read_as_tokens() { + let source = r#"fn statements() { + #[cfg(test)] #[allow(unused, reason = "]")] { "AzureKit"; } + let _ = "one"; + #[cfg(test)] #[doc = r"]"] #[allow(unused, /* ] */)] { "AzureKit"; } + let _ = "two"; +} +"#; + assert_eq!(literals(source), ["one", "two"]); + let policy = Policy { allow_marker: Some("name-ok"), ..POLICY }; + let rust = |src: &str| scan_source(Path::new("lib.rs"), src, Kind::Rust, &policy).unwrap().len(); + assert_eq!(rust("const LIVE: &str = \"AzureKit\"; #[cfg(/* name-ok */ test)] const TEST: &str = \"\";"), 0); + assert_eq!(rust("const LIVE: &str = \"AzureKit\"; #[cfg( // name-ok\n test)] const TEST: &str = \"\";"), 0); + assert_eq!(rust("const LIVE: &str = \"AzureKit\"; #[cfg(test)] const TEST: &str = \"\"; // other"), 1); + } + + #[test] + fn escaped_bytes_are_decoded_as_utf8() { + let both = Literals { byte_strings: true, c_strings: true }; + let texts = |source: &str| super::literals(source, both).unwrap().into_iter().map(|lit| lit.text).collect::>(); + let source = r#"c"Caf\xC3\xA9" b"Caf\xc3\xa9" c"Caf\u{e9}" b"Caf\xC3" b"\xFFok""#; + assert_eq!(texts(source), ["Café", "Café", "Café", "Caf\u{FFFD}", "\u{FFFD}ok"]); + } + + #[test] + fn aliases_of_one_file_are_read_once() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path(); + write(root, "src/a.rs", "\"AzureKit\""); + let absolute = root.join("src/a.rs"); + let fits = Policy { limits: Limits { max_total_bytes: 10, ..DEFAULT_LIMITS }, ..POLICY }; + let same = [Input::rust(Path::new("src/a.rs")), Input::rust(&absolute), Input::rust(Path::new("./src/a.rs"))]; + let found = scan(root, &same, &fits).unwrap(); + assert_eq!(found.len(), 1, "{found:#?}"); + let mixed = [Input::text(Path::new("src/a.rs")), Input::rust(&absolute)]; + let err = scan(root, &mixed, &POLICY).unwrap_err(); + assert!(matches!(&err, Error::Conflict { path } if path.ends_with("a.rs")), "{err}"); + } + + #[test] + fn aliases_under_a_relative_base_are_read_once() { + let absolute = std::env::current_dir().unwrap().join("Cargo.toml"); + let size = std::fs::metadata(&absolute).unwrap().len(); + let fits = Policy { limits: Limits { max_total_bytes: size, ..DEFAULT_LIMITS }, ..POLICY }; + let same = [Input::manifest(Path::new("Cargo.toml")), Input::manifest(&absolute)]; + scan(Path::new("."), &same, &fits).unwrap(); + } + + #[test] + fn the_total_budget_is_checked_before_reading() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path(); + write(root, "a.rs", "\"one\""); + write(root, "b.rs", "\"two\""); + let inputs = [Input::rust(Path::new("a.rs")), Input::rust(Path::new("b.rs"))]; + let exact = Policy { limits: Limits { max_total_bytes: 10, ..DEFAULT_LIMITS }, ..POLICY }; + assert!(scan(root, &inputs, &exact).unwrap().is_empty()); + let short = Policy { limits: Limits { max_total_bytes: 9, ..DEFAULT_LIMITS }, ..POLICY }; + let err = scan(root, &inputs, &short).unwrap_err(); + assert!(matches!(&err, Error::TotalTooLarge { path, limit: 9 } if path.ends_with("b.rs")), "{err}"); + } + #[test] fn policies_that_match_nothing_are_errors() { let empty: &[&str] = &[]; From 59500168a8971cad8fbad0b7e699e421bbe5ee85 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 24 Nov 2026 05:19:42 -0800 Subject: [PATCH 03/12] craft-xtask: fix more of the name scanner's review findings - The base folder is made absolute once, before any input is listed, so listing and reading use the same folder. On Windows, a drive-relative base such as `C:src` is joined to the current folder of its drive, so its keys match the absolute paths of the same files. - A doc comment between `#!` and `[` on the first line makes the line a shebang, as in rustc; other comments still leave an inner attribute. - Under `#[cfg(test)]`, a `,` in an expression's generic arguments (`pair::(..)`) or in the parameters of a closure that starts the expression no longer ends the test-only arm or argument. - Braces in the condition of a test-only `if`, `while`, `match` or `for` (`if ready! { } { .. }`, `if { ready } { .. }`) are not read as its body. - A raw identifier (`r#try! { }`) is read as one word, so its macro braces end the test-only statement. --- crates/xtask/README.md | 13 +- crates/xtask/src/names.rs | 314 +++++++++++++++++++++++++++++++------- 2 files changed, 264 insertions(+), 63 deletions(-) diff --git a/crates/xtask/README.md b/crates/xtask/README.md index c6a74f1488..95394832a7 100644 --- a/crates/xtask/README.md +++ b/crates/xtask/README.md @@ -106,13 +106,17 @@ fn production_display_names_stay_neutral() { character, byte and lifetime tokens, and the rest of a module after `#![cfg(test)]` are skipped. So is the code under `#[cfg(test)]`, comments in the attribute included: a `const`, `static` or `let` up to its `;`, another item up to the `}` that closes it or the `;` that ends it, and a - field, argument, arm or statement up to its `,` or `;`. An item keyword counts only when nothing + field, argument, arm or statement up to its `,` or `;`. A `,` in an expression's generic + arguments (`f::`) or in the parameters of a closure that starts the expression does not + end it. An item keyword counts only when nothing but qualifiers (`pub`, `unsafe`, `extern "C"` and the like) comes before it, so the `fn` in `f::(…)` or in a field's type does not start an item, and the braces of a macro in an item's header (`-> ty! { u8 }`) are not its body. A block expression (a block, a macro's braces, a labeled block, or `if`, `match`, `loop`, `while`, `for`, `unsafe`, `async` or `const`), as the whole statement or as an arm's expression after `=>`, ends at its `}`, or at the last `}` of an `if` with `else` - branches. When a method call, field or `?` follows that `}`, it runs up to its `,` or `;` + branches. Braces in the condition of an `if`, `while`, `match` or `for` (a macro's, or a block + operand such as `if { ready } { … }`) are not its block. When a method call, field or `?` follows + that `}`, it runs up to its `,` or `;` instead. Other `cfg` expressions and macros are not evaluated. A source the lexer cannot read (an unterminated literal or comment, an unknown escape, unbalanced delimiters) is an error. - A word matches as a whole word: the characters next to it are not letters, digits or `_`, in any @@ -122,8 +126,9 @@ fn production_display_names_stay_neutral() { - In Rust, the marker counts only inside a comment on a line the literal spans. In a manifest it counts anywhere on a line the description spans, and in a text file anywhere on the line. - `scan` identifies a file by its input path joined to the base folder, and joins a relative base - folder to the current folder first, so a relative and an absolute input for one file are read - once; `..` and links are not resolved. + folder to the current folder before it lists any input (on Windows, a drive-relative base such as + `C:src` to the current folder of its drive), so a relative and an absolute input for one file are + read once; `..` and links are not resolved. - `scan` reads each file once, in path order, and returns the findings in path order, then source order, then the order of `banned`. A `Finding` has the path, the first line of the literal or description, the word, the decoded text or text line and the kind; its `Display` text is diff --git a/crates/xtask/src/names.rs b/crates/xtask/src/names.rs index 0cbb1a0b20..2969b83a3a 100644 --- a/crates/xtask/src/names.rs +++ b/crates/xtask/src/names.rs @@ -16,7 +16,7 @@ use std::collections::HashSet; use std::fmt; use std::fs::{self, File}; use std::io::{self, Read}; -use std::path::{Path, PathBuf}; +use std::path::{Component, Path, PathBuf}; /// The limits [`Policy::new`] sets: 8 MiB per file, 512 MiB in all, folders 64 deep. pub const DEFAULT_LIMITS: Limits = Limits { max_file_bytes: 8 << 20, max_total_bytes: 512 << 20, max_depth: 64 }; @@ -293,8 +293,9 @@ impl From for String { /// findings come in path order, then in source order, then in the order of [`Policy::banned`]. /// /// A file is identified by its path joined to `base`, and a relative `base` is joined to the -/// current folder first, so a relative and an absolute path to it are one file, read once and -/// reported by the input path that sorts first; `..` and links are not resolved. +/// current folder (on Windows, a drive-relative `base` such as `C:src` to the current folder of +/// its drive) before any input is listed, so a relative and an absolute path to it are one file, +/// read once and reported by the input path that sorts first; `..` and links are not resolved. /// /// A [`Kind::Rust`] folder is read recursively for `.rs` files that [`Policy::skip`] keeps; links to /// folders are not followed, also when the link is an input. A file input is read whatever its @@ -302,21 +303,17 @@ impl From for String { /// is over a limit, and a Rust source the lexer cannot read. Inputs must not include binary files. pub fn scan(base: &Path, inputs: &[Input<'_>], policy: &Policy<'_>) -> Result, Error> { policy.validate()?; + // The base is made absolute once, so that every input is listed and read under one folder. + let root = absolute_base(base)?; let mut files = Vec::new(); for input in inputs { if input.kind == Kind::Rust { - collect_rust(base, input.path, policy, &mut files)?; + collect_rust(&root, input.path, policy, &mut files)?; } else { files.push((input.path.to_path_buf(), input.kind)); } } // A file is known by the path that is opened, so a relative and an absolute input for it are one. - // A relative base is joined to the current folder first, so the keys of both inputs are absolute. - let root = if base.is_absolute() { - base.to_path_buf() - } else { - std::env::current_dir().map_err(|source| Error::Io { path: base.to_path_buf(), source })?.join(base) - }; let mut files: Vec<(PathBuf, Kind, PathBuf)> = files.into_iter().map(|(path, kind)| (root.join(&path), kind, path)).collect(); files.sort(); files.dedup_by(|later, first| later.0 == first.0 && later.1 == first.1); @@ -333,6 +330,20 @@ pub fn scan(base: &Path, inputs: &[Input<'_>], policy: &Policy<'_>) -> Result Result { + if base.is_absolute() { + return Ok(base.to_path_buf()); + } + let error = |source: io::Error| Error::Io { path: base.to_path_buf(), source }; + let mut parts = base.components(); + match parts.next() { + Some(Component::Prefix(drive)) => Ok(std::path::absolute(drive.as_os_str()).map_err(error)?.join(parts.as_path())), + _ => Ok(std::env::current_dir().map_err(error)?.join(base)), + } +} + /// The findings in one source that is already in memory. `path` is only reported. A /// [`Kind::Rust`] source is a single file here. pub fn scan_source(path: &Path, source: &str, kind: Kind, policy: &Policy<'_>) -> Result, Error> { @@ -574,9 +585,12 @@ pub struct Literal { /// Test-only code is the code under `#[cfg(test)]` and the rest of a module that starts with /// `#![cfg(test)]`. Under `#[cfg(test)]`, a `const`, `static` or `let` runs up to its `;`, another /// item up to its closing `}` or its `;`, and a field, argument, arm or statement up to its `,` or -/// `;`. A block expression (a block, a macro's braces, a labeled block, or `if`, `match`, `loop`, -/// `while`, `for`, `unsafe`, `async` or `const`), as the whole statement or as an arm's expression -/// after `=>`, ends at its `}`, or at the last `}` of an `if` with `else` branches. When a method +/// `;`; a `,` in an expression's generic arguments (`f::`) or in the parameters of a closure +/// that starts the expression does not end it. A block expression (a block, a macro's braces, a +/// labeled block, or `if`, `match`, `loop`, `while`, `for`, `unsafe`, `async` or `const`), as the +/// whole statement or as an arm's expression after `=>`, ends at its `}`, or at the last `}` of an +/// `if` with `else` branches; braces in the condition of an `if`, `while`, `match` or `for` (a +/// macro's, or a block operand such as `if { ready } { .. }`) are not its block. When a method /// call, field or `?` follows that `}`, it runs up to its `,` or `;` instead. Character, byte and /// lifetime tokens are read, so a quote in them does not start a string. A shebang first line is /// skipped. @@ -726,9 +740,11 @@ enum TestRegion { /// expression (a block, a macro's braces, or `if`, `match`, `loop` and the like), which ends at /// its `}` unless an `else` follows. A `.` or `?` after that `}` clears `block`, so the expression /// ends at its `;` or `,`. A `{` does not count as the first while `pattern` is set (between the - /// `let` of an `if` or `while` condition and its `=`, or a `for` and its `in`) or while `angle` - /// generic brackets of an item's header are open. - Item { level: usize, start: usize, opened: bool, item: bool, semi: bool, block: bool, pattern: bool, angle: usize }, + /// `let` of an `if` or `while` condition and its `=`, or a `for` and its `in`), while `angle` + /// generic brackets are open (of an item's header, or after a `::` in an expression), or while + /// `params` is set (between the `|`s of a closure that starts the expression). A `,` does not + /// end it while `angle` or `params` is set either. + Item { level: usize, start: usize, opened: bool, item: bool, semi: bool, block: bool, pattern: bool, angle: usize, params: bool }, /// The rest of the module that `#![cfg(test)]` starts, at this delimiter depth. Inner { level: usize }, } @@ -736,7 +752,7 @@ enum TestRegion { impl TestRegion { /// Test-only code at `level` whose code starts at `start`. fn item(level: usize, start: usize) -> Self { - TestRegion::Item { level, start, opened: false, item: false, semi: false, block: false, pattern: false, angle: 0 } + TestRegion::Item { level, start, opened: false, item: false, semi: false, block: false, pattern: false, angle: 0, params: false } } } @@ -758,6 +774,12 @@ struct Lexer<'s> { /// Whether the last token read, past whitespace and comments, is a macro's `!`: a `!` after a /// word, not the `!` of a never type (`-> !`). after_bang: bool, + /// The first character of the last token read, past whitespace and comments. + prev: Option, + /// The first character of the token before that one. + prev_prev: Option, + /// The index where the last word or number read starts. + last_word: usize, out: Lexed, } @@ -772,6 +794,9 @@ fn lex(source: &str, kinds: Literals, marker: Option<&str>) -> Result { } fn run(&mut self) -> Result<(), SyntaxError> { - // A first line that starts with `#!` is a shebang, unless a `[` follows (an inner attribute). + // A first line that starts with `#!` is a shebang, unless a `[` follows past whitespace and + // comments that are not doc comments (an inner attribute). let start = usize::from(self.peek(0) == Some('\u{feff}')); - if self.chars.get(start..start + 2) == Some(&['#', '!'][..]) && self.chars.get(self.skip_space(start + 2)) != Some(&'[') { + if self.chars.get(start..start + 2) == Some(&['#', '!'][..]) && self.chars.get(self.skip_plain_space(start + 2)) != Some(&'[') { while self.peek(0).is_some_and(|c| c != '\n') { self.at += 1; } } while let Some(c) = self.peek(0) { + let begin = self.at; let token = !c.is_whitespace() && !(c == '/' && matches!(self.peek(1), Some('/' | '*'))); match c { '\n' => { @@ -823,6 +850,7 @@ impl Lexer<'_> { '=' if self.peek(1) == Some('>') => self.arrow(), '=' => self.equals(), '<' | '>' => self.angle(c), + '|' => self.pipe(), c if c.is_ascii_digit() => self.number(), c if is_word(c) => self.word()?, _ => self.at += 1, @@ -830,6 +858,11 @@ impl Lexer<'_> { if token { self.after_bang = c == '!' && self.after_word; self.after_word = is_word(c); + self.prev_prev = self.prev; + self.prev = Some(c); + if is_word(c) { + self.last_word = begin; + } } } match self.delims.last() { @@ -1080,19 +1113,40 @@ impl Lexer<'_> { self.at += 1; } - /// A `<` or `>` (not the `>` of `->`) in an item's header before its body opens or closes a - /// generic bracket. + /// A `<` or `>` (not the `>` of `->`) opens or closes a generic bracket in an item's header + /// before its body, and in an expression from a `::<` until its brackets close + /// (`pair::`), so that a comparison does not. fn angle(&mut self, c: char) { let arrow = c == '>' && self.at.checked_sub(1).and_then(|i| self.chars.get(i)) == Some(&'-'); - if let Some(TestRegion::Item { level, opened: false, item: true, angle, .. }) = &mut self.test + let turbofish = c == '<' && self.prev == Some(':') && self.prev_prev == Some(':'); + if let Some(TestRegion::Item { level, opened, item, angle, .. }) = &mut self.test && *level == self.delims.len() && !arrow + && if *item { !*opened } else { turbofish || *angle > 0 } { *angle = if c == '<' { *angle + 1 } else { angle.saturating_sub(1) }; } self.at += 1; } + /// A `|` at the start of a test-only expression, after any `async` and `move`, opens the + /// parameters of a closure (`|a, b| ..`), and the next `|` at its level closes them. + fn pipe(&mut self) { + if let Some(TestRegion::Item { level, start, item: false, params, .. }) = self.test + && level == self.delims.len() + { + let mut code = self.code_start(start); + while matches!(self.word_at(code).as_str(), "async" | "move") { + code = self.skip_space(code + self.word_at(code).chars().count()); + } + let opens = !params && code == self.at; + if let Some(TestRegion::Item { params, .. }) = &mut self.test { + *params = opens; + } + } + self.at += 1; + } + /// A raw literal after its `r`, or a raw identifier (`r#name`), which is left to the caller. fn raw(&mut self, form: Form) -> Result<(), SyntaxError> { let hashes = self.chars.get(self.at..).unwrap_or_default().iter().take_while(|&&c| c == '#').count(); @@ -1121,38 +1175,74 @@ impl Lexer<'_> { /// The index after the whitespace and comments at `at`. fn skip_space(&self, mut at: usize) -> usize { loop { - match (self.chars.get(at), self.chars.get(at + 1)) { - (Some(c), _) if c.is_whitespace() => at += 1, - (Some('/'), Some('/')) => { - while self.chars.get(at).is_some_and(|&c| c != '\n') { - at += 1; - } - } - (Some('/'), Some('*')) => { - let mut depth = 0_usize; - loop { - match (self.chars.get(at), self.chars.get(at + 1)) { - (None, _) => return at, - (Some('/'), Some('*')) => { - depth += 1; - at += 2; - } - (Some('*'), Some('/')) => { - depth -= 1; - at += 2; - if depth == 0 { - break; - } - } - _ => at += 1, - } - } - } - _ => return at, + if self.chars.get(at).is_some_and(|c| c.is_whitespace()) { + at += 1; + } else if let Some(end) = self.comment_end(at) { + at = end; + } else { + return at; } } } + /// The index after the whitespace and the comments that are not doc comments at `at`. + fn skip_plain_space(&self, mut at: usize) -> usize { + loop { + if self.chars.get(at).is_some_and(|c| c.is_whitespace()) { + at += 1; + } else if let Some(end) = self.comment_end(at).filter(|_| !self.doc_comment(at)) { + at = end; + } else { + return at; + } + } + } + + /// The index after the comment that starts at `at`, or `None` when none starts there. + fn comment_end(&self, mut at: usize) -> Option { + match (self.chars.get(at), self.chars.get(at + 1)) { + (Some('/'), Some('/')) => { + while self.chars.get(at).is_some_and(|&c| c != '\n') { + at += 1; + } + } + (Some('/'), Some('*')) => { + let mut depth = 0_usize; + loop { + match (self.chars.get(at), self.chars.get(at + 1)) { + (None, _) => break, + (Some('/'), Some('*')) => { + depth += 1; + at += 2; + } + (Some('*'), Some('/')) => { + depth -= 1; + at += 2; + if depth == 0 { + break; + } + } + _ => at += 1, + } + } + } + _ => return None, + } + Some(at) + } + + /// Whether the comment at `at` is a doc comment: `//!`, `/*!`, `///` but not `////`, or `/**` + /// but not `/***` or `/**/`. + fn doc_comment(&self, at: usize) -> bool { + let c = |n: usize| self.chars.get(at + n).copied(); + match (c(0), c(1), c(2)) { + (Some('/'), Some('/' | '*'), Some('!')) => true, + (Some('/'), Some('/'), Some('/')) => c(3) != Some('/'), + (Some('/'), Some('*'), Some('*')) => !matches!(c(3), Some('*' | '/')), + _ => false, + } + } + /// The end of a `#[cfg(test)]` or `#![cfg(test)]` attribute at the current `#`, and whether it /// is the inner form. fn cfg_test_end(&self) -> Option<(usize, bool)> { @@ -1334,20 +1424,24 @@ impl Lexer<'_> { } } + /// The word at `at`, with the `r#` of a raw identifier (`r#try`), which is never a keyword. fn word_at(&self, at: usize) -> String { - self.chars.get(at..).unwrap_or_default().iter().take_while(|&&c| is_word(c)).collect() + let raw = self.chars.get(at..at + 2) == Some(&['r', '#'][..]) && self.chars.get(at + 2).is_some_and(|&c| is_word(c) && !c.is_ascii_digit()); + let from = if raw { at + 2 } else { at }; + let word: String = self.chars.get(from..).unwrap_or_default().iter().take_while(|&&c| is_word(c)).collect(); + if raw { format!("r#{word}") } else { word } } fn open(&mut self, c: char) { - if let Some(TestRegion::Item { level, start, opened: false, item, pattern: false, angle: 0, .. }) = self.test + if let Some(TestRegion::Item { level, start, opened: false, item, pattern: false, angle: 0, params: false, .. }) = self.test && level == self.delims.len() && c == '{' { let starts = !item && self.starts_block(start); - // In an item's header, the braces of a macro (`fn f() -> ty! { u8 } { .. }`) are not its body. - if !(item && self.after_bang) - && let Some(TestRegion::Item { opened, block, .. }) = &mut self.test - { + // In an item's header, the braces of a macro (`fn f() -> ty! { u8 } { .. }`) are not its + // body, and neither are braces in the condition of an `if` and the like. + let header = if item { self.after_bang } else { self.in_condition(start) }; + if !header && let Some(TestRegion::Item { opened, block, .. }) = &mut self.test { *opened = true; *block = starts; } @@ -1356,6 +1450,26 @@ impl Lexer<'_> { self.at += 1; } + /// Whether the `{` at the current index is in the condition of the test-only `if`, `while`, + /// `match` or `for` whose code starts at `start`, not its body: the braces of a macro + /// (`if ready! { } { .. }`), or a block where an operand starts, after the keyword, an `in`, an + /// operator or a block keyword (`if { ready } { .. }`, `while !{ done } { .. }`, + /// `if const { true } { .. }`). + fn in_condition(&self, start: usize) -> bool { + if !matches!(self.word_at(self.code_start(start)).as_str(), "if" | "while" | "match" | "for") { + return false; + } + if self.after_bang { + return true; + } + if self.after_word { + let raw = self.last_word.checked_sub(1).and_then(|i| self.chars.get(i)) == Some(&'#'); + let word = self.word_at(self.last_word); + return !raw && matches!(word.as_str(), "if" | "while" | "match" | "in" | "const" | "unsafe" | "loop" | "async" | "move"); + } + matches!(self.prev, Some('!' | '&' | '|' | '=' | '<' | '+' | '-' | '*' | '/' | '%' | '^')) + } + fn close(&mut self, c: char) -> Result<(), SyntaxError> { let expected = match self.delims.pop() { Some(('{', _)) => '}', @@ -1409,11 +1523,12 @@ impl Lexer<'_> { /// A `;` ends a test-only statement, or an item that has no body or ends at its `;`, and a `,` /// inside a delimiter ends a test-only field, argument or arm. A `,` in an item's header - /// (`impl`, a `where` clause) does not. + /// (`impl`, a `where` clause) does not, and neither does one in an expression's generic + /// arguments (`f::`) or in the parameters of a closure that starts it. fn item_end(&mut self, comma: bool) { - if let Some(TestRegion::Item { level, opened, item, semi, .. }) = self.test + if let Some(TestRegion::Item { level, opened, item, semi, angle, params, .. }) = self.test && level == self.delims.len() - && if comma { level > 0 && !item } else { !item || !opened || semi } + && if comma { level > 0 && !item && angle == 0 && !params } else { !item || !opened || semi } { self.test = None; } @@ -2026,6 +2141,75 @@ fn after() { let _ = "seven"; } assert_eq!(literals("\"one\" #!x"), ["one"]); } + #[test] + fn a_doc_comment_after_a_shebang_does_not_make_it_an_attribute() { + assert_eq!(literals("#! /*! doc */ [allow(unused)] \"AzureKit\"\n\"live\""), ["live"]); + assert_eq!(literals("#! /** doc */ [allow(unused)] \"AzureKit\"\n\"live\""), ["live"]); + assert_eq!(literals("#! /**/ /*** note */ [cfg(test)]\nfn a() { \"AzureKit\"; }\n"), Vec::::new()); + } + + #[test] + fn test_only_generic_arguments_and_closure_parameters_keep_their_commas() { + let source = r#"fn arms(x: u8) { + match x { + #[cfg(test)] + 0 => pair::("AzureKit"), + #[cfg(test)] + 1 => pair::, u16>("AzureKit"), + #[cfg(test)] + 2 => |_: u8, _: u8| "AzureKit", + #[cfg(test)] + 3 => move |P { a, b }: P, _: u8| "AzureKit", + #[cfg(test)] + 4 => pair:: /* spaced */ ("AzureKit"), + _ => pair::("one"), + } + call(#[cfg(test)] pair::("AzureKit"), "two"); + call(#[cfg(test)] |a, b| a | b, "three"); + call(#[cfg(test)] a < b, "four"); +} +"#; + assert_eq!(literals(source), ["one", "two", "three", "four"]); + } + + #[test] + fn braces_in_a_test_only_condition_are_not_its_body() { + let source = r#"fn statements(ready: bool) { + #[cfg(test)] if ready! { } { let _ = "AzureKit"; } + let _ = "one"; + #[cfg(test)] if { ready } { "AzureKit"; } + let _ = "two"; + #[cfg(test)] while !{ ready } { "AzureKit"; } + let _ = "three"; + #[cfg(test)] if ready { "AzureKit"; } else if { ready } { "AzureKit"; } else { "AzureKit"; } + let _ = "four"; + #[cfg(test)] match { 1 } { _ => "AzureKit" } + let _ = "five"; + #[cfg(test)] if ready() { "AzureKit"; } + let _ = "six"; + #[cfg(test)] for x in { [1] } { "AzureKit"; } + let _ = "seven"; + #[cfg(test)] if const { true } { "AzureKit"; } + let _ = "eight"; + #[cfg(test)] while unsafe { ready } { "AzureKit"; } + let _ = "nine"; +} +"#; + assert_eq!(literals(source), ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]); + } + + #[test] + fn a_raw_identifier_macro_is_read_as_one_word() { + let source = r#"fn statements() { + #[cfg(test)] r#try! {} + let _ = "one"; + #[cfg(test)] r#try! { "AzureKit" } + let _ = "two"; +} +"#; + assert_eq!(literals(source), ["one", "two"]); + } + #[test] fn test_only_item_headers_do_not_end_items_early() { let source = r#"#[cfg(test)] fn generic() {} @@ -2143,6 +2327,18 @@ fn seven() { let _ = "seven"; } scan(Path::new("."), &same, &fits).unwrap(); } + #[cfg(windows)] + #[test] + fn aliases_under_a_drive_relative_base_are_read_once() { + let current = std::env::current_dir().unwrap(); + let Some(Component::Prefix(drive)) = current.components().next() else { panic!("no drive in {}", current.display()) }; + let absolute = current.join("Cargo.toml"); + let size = std::fs::metadata(&absolute).unwrap().len(); + let fits = Policy { limits: Limits { max_total_bytes: size, ..DEFAULT_LIMITS }, ..POLICY }; + let same = [Input::manifest(Path::new("Cargo.toml")), Input::manifest(&absolute)]; + scan(Path::new(drive.as_os_str()), &same, &fits).unwrap(); + } + #[test] fn the_total_budget_is_checked_before_reading() { let dir = tempfile::tempdir().unwrap(); From 2e30cc9522fe966318439bca3137ea66b51a9550 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 24 Nov 2026 11:37:33 -0800 Subject: [PATCH 04/12] craft-xtask: keep generic commas, borrowed closures and raw identifiers in test-only code - A Windows drive-relative input (`C:a.rs`) is keyed by the file it opens under the current folder of its drive, so it deduplicates with the absolute path and takes part in the mixed-kind conflict check. - A `<` opens generic brackets where an operand starts (` as Fixture>::LABEL`, also after `return` and the like), in the type of a declared field or parameter (`field: Pair`), and in a tuple field, so their commas do not end the test-only region. Fields and parameters count as declared only in the braces or parentheses of a struct, union, enum variant or function, so a comparison in a struct expression (`probe: a < b`) stays one. - A closure after a borrow (`&|_, _|`, `&mut move |..|`) keeps its parameter commas. - A raw identifier (`r#let`) is read as one word and is never taken for a keyword. --- crates/xtask/src/names.rs | 356 +++++++++++++++++++++++++++++++++++--- 1 file changed, 333 insertions(+), 23 deletions(-) diff --git a/crates/xtask/src/names.rs b/crates/xtask/src/names.rs index 2969b83a3a..2d4518d2ff 100644 --- a/crates/xtask/src/names.rs +++ b/crates/xtask/src/names.rs @@ -294,7 +294,8 @@ impl From for String { /// /// A file is identified by its path joined to `base`, and a relative `base` is joined to the /// current folder (on Windows, a drive-relative `base` such as `C:src` to the current folder of -/// its drive) before any input is listed, so a relative and an absolute path to it are one file, +/// its drive) before any input is listed. A drive-relative input (`C:a.rs`) is joined to the +/// current folder of its drive instead. A relative and an absolute path to a file are one file, /// read once and reported by the input path that sorts first; `..` and links are not resolved. /// /// A [`Kind::Rust`] folder is read recursively for `.rs` files that [`Policy::skip`] keeps; links to @@ -314,7 +315,7 @@ pub fn scan(base: &Path, inputs: &[Input<'_>], policy: &Policy<'_>) -> Result = files.into_iter().map(|(path, kind)| (root.join(&path), kind, path)).collect(); + let mut files = files.into_iter().map(|(path, kind)| Ok((input_path(&root, &path)?, kind, path))).collect::, Error>>()?; files.sort(); files.dedup_by(|later, first| later.0 == first.0 && later.1 == first.1); if let Some(pair) = files.windows(2).find(|pair| pair[0].0 == pair[1].0) { @@ -344,6 +345,15 @@ fn absolute_base(base: &Path) -> Result { } } +/// The absolute path that `path` opens under the absolute `root`: `path` joined to `root`, or for a +/// Windows drive-relative path (`C:a.rs`), joined to the current folder of its drive. +fn input_path(root: &Path, path: &Path) -> Result { + match path.components().next() { + Some(Component::Prefix(_)) if !path.has_root() => absolute_base(path), + _ => Ok(root.join(path)), + } +} + /// The findings in one source that is already in memory. `path` is only reported. A /// [`Kind::Rust`] source is a single file here. pub fn scan_source(path: &Path, source: &str, kind: Kind, policy: &Policy<'_>) -> Result, Error> { @@ -585,8 +595,9 @@ pub struct Literal { /// Test-only code is the code under `#[cfg(test)]` and the rest of a module that starts with /// `#![cfg(test)]`. Under `#[cfg(test)]`, a `const`, `static` or `let` runs up to its `;`, another /// item up to its closing `}` or its `;`, and a field, argument, arm or statement up to its `,` or -/// `;`; a `,` in an expression's generic arguments (`f::`) or in the parameters of a closure -/// that starts the expression does not end it. A block expression (a block, a macro's braces, a +/// `;`; a `,` in generic arguments (`f::`, ` as Fixture>::LABEL`, or a field's +/// `field: Pair`) or in the parameters of a closure that starts the expression (after any +/// `move` or `&`) does not end it. A raw identifier (`r#let`) is never a keyword. A block expression (a block, a macro's braces, a /// labeled block, or `if`, `match`, `loop`, `while`, `for`, `unsafe`, `async` or `const`), as the /// whole statement or as an arm's expression after `=>`, ends at its `}`, or at the last `}` of an /// `if` with `else` branches; braces in the condition of an `if`, `while`, `match` or `for` (a @@ -741,10 +752,11 @@ enum TestRegion { /// its `}` unless an `else` follows. A `.` or `?` after that `}` clears `block`, so the expression /// ends at its `;` or `,`. A `{` does not count as the first while `pattern` is set (between the /// `let` of an `if` or `while` condition and its `=`, or a `for` and its `in`), while `angle` - /// generic brackets are open (of an item's header, or after a `::` in an expression), or while - /// `params` is set (between the `|`s of a closure that starts the expression). A `,` does not - /// end it while `angle` or `params` is set either. - Item { level: usize, start: usize, opened: bool, item: bool, semi: bool, block: bool, pattern: bool, angle: usize, params: bool }, + /// generic brackets are open (of an item's header, after a `::` or where an operand starts in an + /// expression, or in the type after `typed` is set), or while `params` is set (between the `|`s + /// of a closure that starts the expression). A `,` does not end it while `angle` or `params` is + /// set either. `typed` is set at the `:` after the name of a field or parameter (`name: Type`). + Item { level: usize, start: usize, opened: bool, item: bool, semi: bool, block: bool, pattern: bool, angle: usize, params: bool, typed: bool }, /// The rest of the module that `#![cfg(test)]` starts, at this delimiter depth. Inner { level: usize }, } @@ -752,7 +764,18 @@ enum TestRegion { impl TestRegion { /// Test-only code at `level` whose code starts at `start`. fn item(level: usize, start: usize) -> Self { - TestRegion::Item { level, start, opened: false, item: false, semi: false, block: false, pattern: false, angle: 0, params: false } + TestRegion::Item { + level, + start, + opened: false, + item: false, + semi: false, + block: false, + pattern: false, + angle: 0, + params: false, + typed: false, + } } } @@ -760,14 +783,44 @@ impl TestRegion { /// an item (`if let`, `for … in`). const HEADER_KEYWORDS: &[&str] = &["for", "if", "loop", "match", "while"]; +/// What the fields or parameters in a delimiter are. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Fields { + /// None are declared there: the delimiter is an expression's, a pattern's or a block's. + Untyped, + /// They are declared with their types: a struct's, union's or variant's named fields, or a + /// function's parameters. + Typed, + /// The types of a tuple struct's or tuple variant's fields. + Tuple, + /// An enum's variants, whose `{ .. }` and `( .. )` hold typed fields. + Variants, +} + +/// A declaration whose fields or parameters have not opened yet. +#[derive(Debug, Clone, Copy)] +struct Declares { + /// The delimiter depth of its keyword. + level: usize, + /// Its keyword: `struct`, `union`, `enum` or `fn`. + keyword: &'static str, + /// The generic brackets of its header that are open (`fn f`). + angle: usize, + /// Whether its `where` clause has started, after which only a `{` opens its fields. + after_where: bool, +} + struct Lexer<'s> { chars: Vec, at: usize, line: usize, kinds: Literals, marker: Option<&'s str>, - /// Open delimiters and the line of each. - delims: Vec<(char, usize)>, + /// Open delimiters, the line of each, and what the fields or parameters in each are. + delims: Vec<(char, usize, Fields)>, + /// The next `{` or `(` that declares fields or parameters, after a `struct`, `union`, `enum` or + /// `fn`. + declares: Option, test: Option, /// Whether the last token read, past whitespace and comments, is a word. after_word: bool, @@ -791,6 +844,7 @@ fn lex(source: &str, kinds: Literals, marker: Option<&str>) -> Result { '=' => self.equals(), '<' | '>' => self.angle(c), '|' => self.pipe(), + ':' => self.colon(), c if c.is_ascii_digit() => self.number(), c if is_word(c) => self.word()?, _ => self.at += 1, @@ -866,7 +921,7 @@ impl Lexer<'_> { } } match self.delims.last() { - Some(&(_, line)) => Err(Self::error(line, "unclosed delimiter")), + Some(&(_, line, _)) => Err(Self::error(line, "unclosed delimiter")), None => Ok(()), } } @@ -1046,12 +1101,31 @@ impl Lexer<'_> { } } - /// An identifier, or the prefix of a byte, C or raw literal. + /// An identifier, or the prefix of a byte, C or raw literal. A raw identifier (`r#let`) is read + /// whole, so its name is not taken for a keyword. fn word(&mut self) -> Result<(), SyntaxError> { let start = self.at; self.number(); + let raw_name = self.peek(1).is_some_and(|c| is_word(c) && !c.is_ascii_digit()); + if self.at == start + 1 && self.chars.get(start) == Some(&'r') && self.peek(0) == Some('#') && raw_name { + self.at += 1; + self.number(); + } let word: String = self.chars.get(start..self.at).unwrap_or_default().iter().collect(); self.header_word(start, &word); + // `union` is a keyword only before the union's name. + let named = word != "union" || !self.word_at(self.skip_space(self.at)).is_empty(); + let keyword = ["struct", "union", "enum", "fn"].into_iter().find(|&k| k == word); + // A `fn` type in a declaration's header (`struct S`) does not replace it. + let in_header = self.declares.is_some_and(|d| d.level == self.delims.len() && (d.angle > 0 || d.after_where)); + if let Some(keyword) = keyword.filter(|_| named && !in_header) { + self.declares = Some(Declares { level: self.delims.len(), keyword, angle: 0, after_where: false }); + } else if word == "where" + && let Some(declares) = &mut self.declares + && declares.level == self.delims.len() + { + declares.after_where = true; + } match (word.as_str(), self.peek(0)) { ("b", Some('\'')) => self.quote(Form::Byte), ("b", Some('"')) => { @@ -1114,30 +1188,140 @@ impl Lexer<'_> { } /// A `<` or `>` (not the `>` of `->`) opens or closes a generic bracket in an item's header - /// before its body, and in an expression from a `::<` until its brackets close - /// (`pair::`), so that a comparison does not. + /// before its body. In an expression, a `<` opens one after a `::` (`pair::`), where an + /// operand starts (` as Fixture>::LABEL`), and after a word in the type of a field + /// or parameter when its `>` follows (`field: Pair`), and the brackets stay open until + /// they close, so that a comparison (`a < b`) does not. fn angle(&mut self, c: char) { let arrow = c == '>' && self.at.checked_sub(1).and_then(|i| self.chars.get(i)) == Some(&'-'); let turbofish = c == '<' && self.prev == Some(':') && self.prev_prev == Some(':'); + let level_now = self.delims.len(); + if let Some(declares) = &mut self.declares + && declares.level == level_now + && !arrow + { + declares.angle = if c == '<' { declares.angle + 1 } else { declares.angle.saturating_sub(1) }; + } + // A tuple field is a type from its start. + let tuple = self.delims.last().is_some_and(|&(_, _, fields)| fields == Fields::Tuple); + let generic = c == '<' + && match self.test { + Some(TestRegion::Item { level, start, item: false, angle: 0, typed, .. }) if level == level_now => { + self.operand_starts(start) || ((typed || tuple) && self.after_word && self.angle_closes()) + } + _ => false, + }; if let Some(TestRegion::Item { level, opened, item, angle, .. }) = &mut self.test && *level == self.delims.len() && !arrow - && if *item { !*opened } else { turbofish || *angle > 0 } + && if *item { !*opened } else { turbofish || generic || *angle > 0 } { *angle = if c == '<' { *angle + 1 } else { angle.saturating_sub(1) }; } self.at += 1; } - /// A `|` at the start of a test-only expression, after any `async` and `move`, opens the - /// parameters of a closure (`|a, b| ..`), and the next `|` at its level closes them. + /// Whether an operand starts at the current index in the test-only expression whose code starts + /// at `start`: at that code, or after an operator or a keyword that takes one. + fn operand_starts(&self, start: usize) -> bool { + if self.after_word { + return matches!(self.word_at(self.last_word).as_str(), "as" | "break" | "if" | "in" | "match" | "return" | "while"); + } + self.code_start(start) == self.at || matches!(self.prev, Some('=' | '!' | '&' | '|' | '+' | '-' | '*' | '/' | '%' | '^' | ':')) + } + + /// Whether the `<` at the current index has its `>` before a `;`, a `=>`, a closing delimiter of + /// its level, or a string or `.` that a type does not hold. Whitespace, comments, groups in + /// delimiters and character literals are passed over whole. + fn angle_closes(&self) -> bool { + let mut depth = 0_usize; + let mut at = self.at + 1; + loop { + at = self.skip_space(at); + let Some(&c) = self.chars.get(at) else { return false }; + match c { + '(' | '[' | '{' => { + let Some(end) = self.group_end(at) else { return false }; + at = end; + continue; + } + ')' | ']' | '}' | ';' | '"' | '.' => return false, + '=' if self.chars.get(at + 1) == Some(&'>') => return false, + '-' if self.chars.get(at + 1) == Some(&'>') => at += 1, + '<' => depth += 1, + '>' if depth == 0 => return true, + '>' => depth -= 1, + '\'' => { + at += match (self.chars.get(at + 1), self.chars.get(at + 2)) { + (Some('\\'), _) => self.chars.get(at + 3..).and_then(|rest| rest.iter().position(|&q| q == '\'')).map_or(1, |end| end + 4), + (Some(_), Some('\'')) => 3, + _ => 1, + }; + continue; + } + c if is_word(c) => { + at += self.word_at(at).chars().count(); + continue; + } + _ => {} + } + at += 1; + } + } + + /// A `:` (not part of a `::`) right after the name that starts a test-only field or parameter, + /// after any `pub`, starts its type (`name: Type`) in the fields of a struct, union or variant + /// and in a function's parameters, not in a struct expression. + fn colon(&mut self) { + let single = self.peek(1) != Some(':') && self.at.checked_sub(1).and_then(|i| self.chars.get(i)) != Some(&':'); + let declared = self.delims.last().is_some_and(|&(_, _, fields)| fields == Fields::Typed); + if single + && declared + && let Some(TestRegion::Item { level, start, item: false, typed: false, .. }) = self.test + && level == self.delims.len() + && self.name_before(start) + && let Some(TestRegion::Item { typed, .. }) = &mut self.test + { + *typed = true; + } + self.at += 1; + } + + /// Whether the code at `start` is one name, after any `pub` and its `(..)`, that ends at the + /// current index. + fn name_before(&self, start: usize) -> bool { + let mut at = self.code_start(start); + if self.word_at(at) == "pub" { + at = self.skip_space(at + 3); + if self.chars.get(at) == Some(&'(') { + let Some(end) = self.group_end(at) else { return false }; + at = self.skip_space(end); + } + } + let len = self.word_at(at).chars().count(); + len > 0 && self.skip_space(at + len) == self.at + } + + /// A `|` at the start of a test-only expression, after any `async`, `move` and borrow (`&`, + /// `&mut`), opens the parameters of a closure (`|a, b| ..`), and the next `|` at its level + /// closes them. fn pipe(&mut self) { if let Some(TestRegion::Item { level, start, item: false, params, .. }) = self.test && level == self.delims.len() { let mut code = self.code_start(start); - while matches!(self.word_at(code).as_str(), "async" | "move") { - code = self.skip_space(code + self.word_at(code).chars().count()); + loop { + let word = self.word_at(code); + if matches!(word.as_str(), "async" | "move") { + code = self.skip_space(code + word.chars().count()); + } else if self.chars.get(code) == Some(&'&') { + code = self.skip_space(code + 1); + if self.word_at(code) == "mut" { + code = self.skip_space(code + 3); + } + } else { + break; + } } let opens = !params && code == self.at; if let Some(TestRegion::Item { params, .. }) = &mut self.test { @@ -1446,7 +1630,25 @@ impl Lexer<'_> { *block = starts; } } - self.delims.push((c, self.line)); + let level = self.delims.len(); + let fields = match self.declares { + Some(Declares { level: at, keyword, angle: 0, after_where }) if at == level && (c == '{' || (c == '(' && !after_where)) => { + self.declares = None; + match (keyword, c) { + ("enum", '{') => Fields::Variants, + ("fn", _) | (_, '{') => Fields::Typed, + _ => Fields::Tuple, + } + } + // A variant's `{ .. }` or `( .. )` after its name. + _ if self.after_word && self.delims.last().is_some_and(|&(_, _, fields)| fields == Fields::Variants) => match c { + '{' => Fields::Typed, + '(' => Fields::Tuple, + _ => Fields::Untyped, + }, + _ => Fields::Untyped, + }; + self.delims.push((c, self.line, fields)); self.at += 1; } @@ -1472,8 +1674,8 @@ impl Lexer<'_> { fn close(&mut self, c: char) -> Result<(), SyntaxError> { let expected = match self.delims.pop() { - Some(('{', _)) => '}', - Some(('(', _)) => ')', + Some(('{', _, _)) => '}', + Some(('(', _, _)) => ')', Some(_) => ']', None => return Err(Self::error(self.line, "unmatched closing delimiter")), }; @@ -1482,6 +1684,9 @@ impl Lexer<'_> { } self.at += 1; let depth = self.delims.len(); + if self.declares.is_some_and(|declares| declares.level > depth) { + self.declares = None; + } let ends = match self.test { Some(TestRegion::Item { level, opened, item, semi, block, .. }) => { let block_end = opened && depth == level && c == '}'; @@ -1526,6 +1731,10 @@ impl Lexer<'_> { /// (`impl`, a `where` clause) does not, and neither does one in an expression's generic /// arguments (`f::`) or in the parameters of a closure that starts it. fn item_end(&mut self, comma: bool) { + // A unit struct (`struct S;`) declares no fields. + if !comma && self.declares.is_some_and(|declares| declares.level == self.delims.len()) { + self.declares = None; + } if let Some(TestRegion::Item { level, opened, item, semi, angle, params, .. }) = self.test && level == self.delims.len() && if comma { level > 0 && !item && angle == 0 && !params } else { !item || !opened || semi } @@ -2172,6 +2381,96 @@ fn after() { let _ = "seven"; } assert_eq!(literals(source), ["one", "two", "three", "four"]); } + #[test] + fn test_only_types_and_qualified_paths_keep_their_generic_commas() { + let source = r#"struct S { + #[cfg(test)] + field: Pair>, + live: Tag<{ "one".len() }>, +} +fn arms(x: u8, a: u8, b: u8) { + match x { + #[cfg(test)] + 0 => > as Fixture>::LABEL, + #[cfg(test)] + 1 => a < b, + _ => "two", + } + call(#[cfg(test)] a < b, "three"); + let _ = S { #[cfg(test)] probe: a < b, label: "four" }; + let _ = S { #[cfg(test)] probe: a < b, live: "six".len() > 0 }; + let _ = S { #[cfg(test)] probe: a < b, live: x("seven") > d }; + let _ = S { #[cfg(test)] probe: a < /* > */ b, live: "eight" }; +} +fn typed(#[cfg(test)] p: Pair>, q: Tag<{ "five".len() }>) {} +struct Bounds<'a> { + #[cfg(test)] + field: &'a dyn Foo>, + live: Tag<{ "nine".len() }>, +} +enum E { + V { + #[cfg(test)] + field: Pair>, + live: Tag<{ "ten".len() }>, + }, + W(#[cfg(test)] Pair>, Tag<{ "eleven".len() }>), +} +fn bounded(#[cfg(test)] p: Pair>, f: F) -> Tag<{ "twelve".len() }> {} +struct Tuple(#[cfg(test)] pub Pair>, Tag<{ "thirteen".len() }>); +struct Where where F: Fn(u8, u16) { + #[cfg(test)] + field: Pair>, + live: Tag<{ "fourteen".len() }>, +} +struct Defaulted { + #[cfg(test)] + field: Pair>, + live: Tag<{ "fifteen".len() }>, +} +fn returns(x: u8) -> &'static str { + match x { + #[cfg(test)] + 0 => return > as Fixture>::LABEL, + _ => "sixteen", + } +} +"#; + let expected = [ + "one", "two", "three", "four", "six", "seven", "eight", "five", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", + "sixteen", + ]; + assert_eq!(literals(source), expected); + } + + #[test] + fn test_only_borrowed_closures_keep_their_parameter_commas() { + let source = r#"fn arms(x: u8) { + match x { + #[cfg(test)] + 0 => &|_, _| "AzureKit", + #[cfg(test)] + 1 => &mut move |_, _| "AzureKit", + 2 => &|_, _| "one", + _ => &|_, _| "two", + } +} +"#; + assert_eq!(literals(source), ["one", "two"]); + } + + #[test] + fn a_raw_identifier_is_not_read_as_a_keyword() { + let source = r##"fn f(r#let: bool) { + #[cfg(test)] if r#let {} + let _ = "one"; + #[cfg(test)] if r#let { "AzureKit"; } + let _ = r#"two"#; +} +"##; + assert_eq!(literals(source), ["one", "two"]); + } + #[test] fn braces_in_a_test_only_condition_are_not_its_body() { let source = r#"fn statements(ready: bool) { @@ -2337,6 +2636,17 @@ fn seven() { let _ = "seven"; } let fits = Policy { limits: Limits { max_total_bytes: size, ..DEFAULT_LIMITS }, ..POLICY }; let same = [Input::manifest(Path::new("Cargo.toml")), Input::manifest(&absolute)]; scan(Path::new(drive.as_os_str()), &same, &fits).unwrap(); + // A drive-relative input names the file under the current folder of its drive. + let mut spelled = drive.as_os_str().to_os_string(); + spelled.push("Cargo.toml"); + let relative = PathBuf::from(spelled); + for base in [Path::new(drive.as_os_str()), current.as_path()] { + let same = [Input::manifest(&relative), Input::manifest(&absolute)]; + scan(base, &same, &fits).unwrap(); + let mixed = [Input::text(&relative), Input::manifest(&absolute)]; + let err = scan(base, &mixed, &POLICY).unwrap_err(); + assert!(matches!(&err, Error::Conflict { path } if path.ends_with("Cargo.toml")), "{err}"); + } } #[test] From a2854ae0fbd3fbc27b597d957fa8e26e5b33405a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 24 Nov 2026 19:09:47 -0800 Subject: [PATCH 05/12] craft-xtask: read the type after a test-only parameter pattern A `:` at the level of a test-only parameter now starts its type after any pattern (`mut p`, `ref mut p`, `(a, b)`, `&(a, b)`, `P { x, .. }`, `m::P(x)`), not only after a bare name, so a generic comma in that type no longer ends the test-only code. --- crates/xtask/src/names.rs | 41 +++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/crates/xtask/src/names.rs b/crates/xtask/src/names.rs index 2d4518d2ff..a40ea73a5c 100644 --- a/crates/xtask/src/names.rs +++ b/crates/xtask/src/names.rs @@ -755,7 +755,8 @@ enum TestRegion { /// generic brackets are open (of an item's header, after a `::` or where an operand starts in an /// expression, or in the type after `typed` is set), or while `params` is set (between the `|`s /// of a closure that starts the expression). A `,` does not end it while `angle` or `params` is - /// set either. `typed` is set at the `:` after the name of a field or parameter (`name: Type`). + /// set either. `typed` is set at the `:` after the name of a field or the pattern of a parameter + /// (`name: Type`, `mut p: Type`). Item { level: usize, start: usize, opened: bool, item: bool, semi: bool, block: bool, pattern: bool, angle: usize, params: bool, typed: bool }, /// The rest of the module that `#![cfg(test)]` starts, at this delimiter depth. Inner { level: usize }, @@ -1269,17 +1270,18 @@ impl Lexer<'_> { } } - /// A `:` (not part of a `::`) right after the name that starts a test-only field or parameter, - /// after any `pub`, starts its type (`name: Type`) in the fields of a struct, union or variant - /// and in a function's parameters, not in a struct expression. + /// A `:` (not part of a `::`) at the level of a test-only field or parameter, after its name + /// (and any `pub`) or its pattern (`mut p`, `(a, b)`, `P { x, .. }`), starts its type + /// (`name: Type`) in the fields of a struct, union or variant and in a function's parameters, + /// not in a struct expression. A `:` inside the pattern's own delimiters is at a deeper level. fn colon(&mut self) { let single = self.peek(1) != Some(':') && self.at.checked_sub(1).and_then(|i| self.chars.get(i)) != Some(&':'); let declared = self.delims.last().is_some_and(|&(_, _, fields)| fields == Fields::Typed); if single && declared - && let Some(TestRegion::Item { level, start, item: false, typed: false, .. }) = self.test + && let Some(TestRegion::Item { level, start, item: false, typed: false, angle: 0, .. }) = self.test && level == self.delims.len() - && self.name_before(start) + && self.code_start(start) < self.at && let Some(TestRegion::Item { typed, .. }) = &mut self.test { *typed = true; @@ -1287,21 +1289,6 @@ impl Lexer<'_> { self.at += 1; } - /// Whether the code at `start` is one name, after any `pub` and its `(..)`, that ends at the - /// current index. - fn name_before(&self, start: usize) -> bool { - let mut at = self.code_start(start); - if self.word_at(at) == "pub" { - at = self.skip_space(at + 3); - if self.chars.get(at) == Some(&'(') { - let Some(end) = self.group_end(at) else { return false }; - at = self.skip_space(end); - } - } - let len = self.word_at(at).chars().count(); - len > 0 && self.skip_space(at + len) == self.at - } - /// A `|` at the start of a test-only expression, after any `async`, `move` and borrow (`&`, /// `&mut`), opens the parameters of a closure (`|a, b| ..`), and the next `|` at its level /// closes them. @@ -2443,6 +2430,18 @@ fn returns(x: u8) -> &'static str { assert_eq!(literals(source), expected); } + #[test] + fn test_only_parameter_patterns_keep_their_generic_commas() { + let source = r#"fn mutable(#[cfg(test)] mut p: Pair>, q: Tag<{ "one".len() }>) {} +fn tuple(#[cfg(test)] (a, b): Pair>, q: Tag<{ "two".len() }>) {} +fn borrowed(#[cfg(test)] &(a, b): &Pair>, q: Tag<{ "three".len() }>) {} +fn fields(#[cfg(test)] P { x, .. }: Pair>, q: Tag<{ "four".len() }>) {} +fn by_ref(#[cfg(test)] ref mut p: Pair>, q: Tag<{ "five".len() }>) {} +fn paths(#[cfg(test)] m::P(x): Pair>, q: Tag<{ "six".len() }>) {} +"#; + assert_eq!(literals(source), ["one", "two", "three", "four", "five", "six"]); + } + #[test] fn test_only_borrowed_closures_keep_their_parameter_commas() { let source = r#"fn arms(x: u8) { From 3e48c109c578e9d485cb6b0813231bd331425ce6 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 24 Nov 2026 19:13:46 -0800 Subject: [PATCH 06/12] craft-xtask: keep a declaration pending across nested declarations in its header The pending declarations are now a stack, innermost last. A `fn` pointer type or a local item inside a declaration's header (`struct S`, `Box<[fn(u8, u16); 2]>`, a const generic default block that declares a struct) is consumed by its own delimiter and leaves the outer declaration pending, so the outer body still opens as typed fields. --- crates/xtask/src/names.rs | 63 ++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 17 deletions(-) diff --git a/crates/xtask/src/names.rs b/crates/xtask/src/names.rs index a40ea73a5c..0d86fb887e 100644 --- a/crates/xtask/src/names.rs +++ b/crates/xtask/src/names.rs @@ -819,9 +819,10 @@ struct Lexer<'s> { marker: Option<&'s str>, /// Open delimiters, the line of each, and what the fields or parameters in each are. delims: Vec<(char, usize, Fields)>, - /// The next `{` or `(` that declares fields or parameters, after a `struct`, `union`, `enum` or - /// `fn`. - declares: Option, + /// The declarations after a `struct`, `union`, `enum` or `fn` whose `{` or `(` of fields or + /// parameters has not opened yet, innermost last: a declaration in another's header + /// (`struct S`) is pending until its own delimiter opens, and the outer one after. + declares: Vec, test: Option, /// Whether the last token read, past whitespace and comments, is a word. after_word: bool, @@ -845,7 +846,7 @@ fn lex(source: &str, kinds: Literals, marker: Option<&str>) -> Result { // `union` is a keyword only before the union's name. let named = word != "union" || !self.word_at(self.skip_space(self.at)).is_empty(); let keyword = ["struct", "union", "enum", "fn"].into_iter().find(|&k| k == word); - // A `fn` type in a declaration's header (`struct S`) does not replace it. - let in_header = self.declares.is_some_and(|d| d.level == self.delims.len() && (d.angle > 0 || d.after_where)); - if let Some(keyword) = keyword.filter(|_| named && !in_header) { - self.declares = Some(Declares { level: self.delims.len(), keyword, angle: 0, after_where: false }); + // A `fn` type in a declaration's header (`struct S`, `struct S`) + // is pending on top of it, and its `(` opens before the declaration's fields. + if let Some(keyword) = keyword.filter(|_| named) { + self.declares.push(Declares { level: self.delims.len(), keyword, angle: 0, after_where: false }); } else if word == "where" - && let Some(declares) = &mut self.declares + && let Some(declares) = self.declares.last_mut() && declares.level == self.delims.len() { declares.after_where = true; @@ -1197,7 +1198,7 @@ impl Lexer<'_> { let arrow = c == '>' && self.at.checked_sub(1).and_then(|i| self.chars.get(i)) == Some(&'-'); let turbofish = c == '<' && self.prev == Some(':') && self.prev_prev == Some(':'); let level_now = self.delims.len(); - if let Some(declares) = &mut self.declares + if let Some(declares) = self.declares.last_mut() && declares.level == level_now && !arrow { @@ -1618,9 +1619,9 @@ impl Lexer<'_> { } } let level = self.delims.len(); - let fields = match self.declares { + let fields = match self.declares.last().copied() { Some(Declares { level: at, keyword, angle: 0, after_where }) if at == level && (c == '{' || (c == '(' && !after_where)) => { - self.declares = None; + self.declares.pop(); match (keyword, c) { ("enum", '{') => Fields::Variants, ("fn", _) | (_, '{') => Fields::Typed, @@ -1671,9 +1672,7 @@ impl Lexer<'_> { } self.at += 1; let depth = self.delims.len(); - if self.declares.is_some_and(|declares| declares.level > depth) { - self.declares = None; - } + self.declares.retain(|declares| declares.level <= depth); let ends = match self.test { Some(TestRegion::Item { level, opened, item, semi, block, .. }) => { let block_end = opened && depth == level && c == '}'; @@ -1719,8 +1718,9 @@ impl Lexer<'_> { /// arguments (`f::`) or in the parameters of a closure that starts it. fn item_end(&mut self, comma: bool) { // A unit struct (`struct S;`) declares no fields. - if !comma && self.declares.is_some_and(|declares| declares.level == self.delims.len()) { - self.declares = None; + if !comma { + let level = self.delims.len(); + self.declares.retain(|declares| declares.level < level); } if let Some(TestRegion::Item { level, opened, item, semi, angle, params, .. }) = self.test && level == self.delims.len() @@ -2442,6 +2442,35 @@ fn paths(#[cfg(test)] m::P(x): Pair>, q: Tag<{ "si assert_eq!(literals(source), ["one", "two", "three", "four", "five", "six"]); } + #[test] + fn nested_function_pointer_types_keep_the_declaration() { + let source = r#"struct Nested { + #[cfg(test)] + field: Pair>, + live: Tag<{ "one".len() }>, +} +struct Boxed>(#[cfg(test)] Pair>, Tag<{ "two".len() }>); +enum Listed { + V { + #[cfg(test)] + field: Pair>, + live: Tag<{ "three".len() }>, + }, +} +struct Bounded where F: Into<(fn(u8),)> { + #[cfg(test)] + field: Pair>, + live: Tag<{ "four".len() }>, +} +struct Outer>, live: Tag<{ "five".len() }> } 0 }> { + #[cfg(test)] + field: Pair>, + live: Tag<{ "six".len() }>, +} +"#; + assert_eq!(literals(source), ["one", "two", "three", "four", "five", "six"]); + } + #[test] fn test_only_borrowed_closures_keep_their_parameter_commas() { let source = r#"fn arms(x: u8) { From a83685686a6c953799e3bc50c1e2e743a0709f9e Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 24 Nov 2026 19:23:22 -0800 Subject: [PATCH 07/12] craft-xtask: keep generic commas in test-only cast types An `as` in test-only code now starts a cast type that runs until a token a type does not hold. A `<` after a word in that type opens generic brackets, so a comma in `ptr as *const Pair`, in a qualified or generic path (`::Assoc`, `Pair::Assoc`) or in a function pointer's return type (`f as fn() -> Pair`) no longer ends the test-only code. A comparison after the cast's parentheses (`(a as u8) < b`) or after an operator (`a as u8 + a < b`) is still read as a comparison. --- crates/xtask/src/names.rs | 102 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 4 deletions(-) diff --git a/crates/xtask/src/names.rs b/crates/xtask/src/names.rs index 0d86fb887e..f4c9c1c281 100644 --- a/crates/xtask/src/names.rs +++ b/crates/xtask/src/names.rs @@ -756,8 +756,21 @@ enum TestRegion { /// expression, or in the type after `typed` is set), or while `params` is set (between the `|`s /// of a closure that starts the expression). A `,` does not end it while `angle` or `params` is /// set either. `typed` is set at the `:` after the name of a field or the pattern of a parameter - /// (`name: Type`, `mut p: Type`). - Item { level: usize, start: usize, opened: bool, item: bool, semi: bool, block: bool, pattern: bool, angle: usize, params: bool, typed: bool }, + /// (`name: Type`, `mut p: Type`), and `cast` at an `as` until the token that ends its type + /// (`ptr as *const Pair`). + Item { + level: usize, + start: usize, + opened: bool, + item: bool, + semi: bool, + block: bool, + pattern: bool, + angle: usize, + params: bool, + typed: bool, + cast: bool, + }, /// The rest of the module that `#![cfg(test)]` starts, at this delimiter depth. Inner { level: usize }, } @@ -776,6 +789,7 @@ impl TestRegion { angle: 0, params: false, typed: false, + cast: false, } } } @@ -886,6 +900,9 @@ impl Lexer<'_> { while let Some(c) = self.peek(0) { let begin = self.at; let token = !c.is_whitespace() && !(c == '/' && matches!(self.peek(1), Some('/' | '*'))); + if token { + self.cast_token(c); + } match c { '\n' => { self.line += 1; @@ -1115,6 +1132,13 @@ impl Lexer<'_> { } let word: String = self.chars.get(start..self.at).unwrap_or_default().iter().collect(); self.header_word(start, &word); + let level = self.delims.len(); + if word == "as" + && let Some(TestRegion::Item { level: at, item: false, angle: 0, cast, .. }) = &mut self.test + && *at == level + { + *cast = true; + } // `union` is a keyword only before the union's name. let named = word != "union" || !self.word_at(self.skip_space(self.at)).is_empty(); let keyword = ["struct", "union", "enum", "fn"].into_iter().find(|&k| k == word); @@ -1208,8 +1232,8 @@ impl Lexer<'_> { let tuple = self.delims.last().is_some_and(|&(_, _, fields)| fields == Fields::Tuple); let generic = c == '<' && match self.test { - Some(TestRegion::Item { level, start, item: false, angle: 0, typed, .. }) if level == level_now => { - self.operand_starts(start) || ((typed || tuple) && self.after_word && self.angle_closes()) + Some(TestRegion::Item { level, start, item: false, angle: 0, typed, cast, .. }) if level == level_now => { + self.operand_starts(start) || ((typed || tuple || cast) && self.after_word && self.angle_closes()) } _ => false, }; @@ -1232,6 +1256,25 @@ impl Lexer<'_> { self.code_start(start) == self.at || matches!(self.prev, Some('=' | '!' | '&' | '|' | '+' | '-' | '*' | '/' | '%' | '^' | ':')) } + /// Reads the token that starts with `c` at the current index against the type of a cast in + /// test-only code: outside its generic brackets and other delimiters, the type holds words (with + /// `::` and lifetimes), `&`, `*`, `(` and `[` after `as`, `const`, `mut`, `dyn` or `fn` (or + /// another of them), the `->` of a function pointer's return type, and `<` and the `>` that close + /// them, and any other token ends it. + fn cast_token(&mut self, c: char) { + let level = self.delims.len(); + let Some(TestRegion::Item { level: at, angle: 0, cast: true, .. }) = self.test else { return }; + if at != level { + return; + } + let prefix = !self.after_word || matches!(self.word_at(self.last_word).as_str(), "as" | "const" | "mut" | "dyn" | "fn"); + let arrow = (c == '-' && self.peek(1) == Some('>')) || (c == '>' && self.at.checked_sub(1).and_then(|i| self.chars.get(i)) == Some(&'-')); + let keeps = is_word(c) || arrow || matches!(c, ':' | '\'' | '<') || (matches!(c, '&' | '*' | '(' | '[') && prefix); + if !keeps && let Some(TestRegion::Item { cast, .. }) = &mut self.test { + *cast = false; + } + } + /// Whether the `<` at the current index has its `>` before a `;`, a `=>`, a closing delimiter of /// its level, or a string or `.` that a type does not hold. Whitespace, comments, groups in /// delimiters and character literals are passed over whole. @@ -2471,6 +2514,57 @@ struct Outer ptr as *const Pair>, + #[cfg(test)] + 1 => ptr as Pair>, + #[cfg(test)] + 2 => ptr as &'static dyn Foo>, + #[cfg(test)] + 3 => ptr as ::std::ptr::NonNull>, + #[cfg(test)] + 4 => ptr as *mut m::Pair>, + #[cfg(test)] + 6 => ptr as *const ::Assoc>, + #[cfg(test)] + 7 => ptr as >::Assoc>, + #[cfg(test)] + 8 => ptr as m::Pair u8>::Assoc>, + #[cfg(test)] + 9 => ptr as Vec::::Assoc>, + #[cfg(test)] + 10 => ptr as Pair<{ 1 > 0 }>::Assoc>, + #[cfg(test)] + 11 => ptr as m::r#Pair::Assoc>, + #[cfg(test)] + 12 => ptr as /* type */ Pair>, + #[cfg(test)] + 13 => f as fn() -> Pair>, + #[cfg(test)] + 14 => f as unsafe fn(u8, &[u8]) -> &'static Pair>, + #[cfg(test)] + 15 => f as fn() -> (u8, Pair), + #[cfg(test)] + 16 => f as fn() -> m::Pair>, + #[cfg(test)] + 5 => a < b, + _ => "one", + } + call(#[cfg(test)] (a as u8) < b, "two"); + call(#[cfg(test)] a < b, "three"); + call(#[cfg(test)] a as u8 + a < b, "four"); + call(#[cfg(test)] a as u8 > b && a < b, "five"); + call(#[cfg(test)] a as u8 == b || a < b, "six"); + call(#[cfg(test)] a as u8 - b < b, "seven"); +} +"#; + assert_eq!(literals(source), ["one", "two", "three", "four", "five", "six", "seven"]); + } + #[test] fn test_only_borrowed_closures_keep_their_parameter_commas() { let source = r#"fn arms(x: u8) { From 3641b5c8bc09f52ebc699c21b63d2ffb0a312e79 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 24 Nov 2026 19:24:46 -0800 Subject: [PATCH 08/12] craft-xtask: keep a declaration pending across a macro in its header A `{` or `(` right after a macro's `!` in a struct, union or enum header (`struct S where ty!{}: Copy { .. }`) is the macro's group, not the declaration's fields. The declaration stays pending until its own body brace, so the body still opens as typed fields. --- crates/xtask/src/names.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/crates/xtask/src/names.rs b/crates/xtask/src/names.rs index f4c9c1c281..3eec539238 100644 --- a/crates/xtask/src/names.rs +++ b/crates/xtask/src/names.rs @@ -1662,8 +1662,12 @@ impl Lexer<'_> { } } let level = self.delims.len(); + // A macro's delimiters in the header (`struct S where ty!{}: Copy { .. }`) are not the + // fields; the declaration stays pending across them. let fields = match self.declares.last().copied() { - Some(Declares { level: at, keyword, angle: 0, after_where }) if at == level && (c == '{' || (c == '(' && !after_where)) => { + Some(Declares { level: at, keyword, angle: 0, after_where }) + if at == level && !self.after_bang && (c == '{' || (c == '(' && !after_where)) => + { self.declares.pop(); match (keyword, c) { ("enum", '{') => Fields::Variants, @@ -2514,6 +2518,30 @@ struct Outer>, + live: Tag<{ "one".len() }>, +} +enum E where m::ty! { u8 }: Copy, F: Fn(u8, u16) { + V { + #[cfg(test)] + field: Pair>, + live: Tag<{ "two".len() }>, + }, +} +union U where ty! {}: Copy { + #[cfg(test)] + field: Pair>, + live: Tag<{ "three".len() }>, +} +fn f() where ty!{}: Copy { let _ = S { #[cfg(test)] probe: a < b, live: "four" }; } +"#; + assert_eq!(literals(source), ["one", "two", "three", "four"]); + } + #[test] fn test_only_cast_types_keep_their_generic_commas() { let source = r#"fn arms(x: u8, a: u8, b: u8, ptr: *const u8) { From 6f34164a3704599fa1d40a5873408f01b77e8551 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 24 Nov 2026 19:32:34 -0800 Subject: [PATCH 09/12] craft-xtask: read enum discriminants as expressions A `=` among an enum's variants starts a discriminant that runs to the next `,`, and its `( .. )` and `{ .. }` are an expression's, so a call such as `V = f(..)` is no longer read as a tuple variant's fields. A `(` after a path, a method or an operator is not a variant's either, which covers a discriminant whose generic arguments hold a comma (`V = m::f::::call(..)`). Variants named by a macro's `$name` or after repeated macro attributes keep their fields. --- crates/xtask/src/names.rs | 62 ++++++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/crates/xtask/src/names.rs b/crates/xtask/src/names.rs index 3eec539238..caa2ac585a 100644 --- a/crates/xtask/src/names.rs +++ b/crates/xtask/src/names.rs @@ -810,6 +810,9 @@ enum Fields { Tuple, /// An enum's variants, whose `{ .. }` and `( .. )` hold typed fields. Variants, + /// An enum's variants, in the discriminant (`V = f(..)`) that runs from a variant's `=` to the + /// next `,`: its delimiters are an expression's. + Discriminant, } /// A declaration whose fields or parameters have not opened yet. @@ -1200,10 +1203,16 @@ impl Lexer<'_> { } } - /// A `=` that is not part of `==`, `!=`, `<=`, `>=` or `..=` ends a `let` pattern. + /// A `=` that is not part of `==`, `!=`, `<=`, `>=` or `..=` ends a `let` pattern, and among an + /// enum's variants starts a discriminant. fn equals(&mut self) { let before = self.at.checked_sub(1).and_then(|i| self.chars.get(i)).copied(); let compares = matches!(before, Some('=' | '!' | '<' | '>' | '.')) || self.peek(1) == Some('='); + if let Some((_, _, fields @ Fields::Variants)) = self.delims.last_mut() + && !compares + { + *fields = Fields::Discriminant; + } if let Some(TestRegion::Item { level, pattern, .. }) = &mut self.test && *level == self.delims.len() && !compares @@ -1675,12 +1684,19 @@ impl Lexer<'_> { _ => Fields::Tuple, } } - // A variant's `{ .. }` or `( .. )` after its name. - _ if self.after_word && self.delims.last().is_some_and(|&(_, _, fields)| fields == Fields::Variants) => match c { - '{' => Fields::Typed, - '(' => Fields::Tuple, - _ => Fields::Untyped, - }, + // A variant's `{ .. }` or `( .. )` after its name, not in its discriminant, and not after + // a path, a method or an operator (`m::f::::call(..)` after a comma in the + // discriminant's generic arguments). + _ if self.after_word + && !matches!(self.prev_prev, Some(':' | '.' | '=' | '+' | '-' | '/' | '%' | '&' | '|' | '^' | '<' | '>')) + && self.delims.last().is_some_and(|&(_, _, fields)| fields == Fields::Variants) => + { + match c { + '{' => Fields::Typed, + '(' => Fields::Tuple, + _ => Fields::Untyped, + } + } _ => Fields::Untyped, }; self.delims.push((c, self.line, fields)); @@ -1769,6 +1785,12 @@ impl Lexer<'_> { let level = self.delims.len(); self.declares.retain(|declares| declares.level < level); } + // A `,` among an enum's variants ends a discriminant. + if let Some((_, _, fields @ Fields::Discriminant)) = self.delims.last_mut() + && comma + { + *fields = Fields::Variants; + } if let Some(TestRegion::Item { level, opened, item, semi, angle, params, .. }) = self.test && level == self.delims.len() && if comma { level > 0 && !item && angle == 0 && !params } else { !item || !opened || semi } @@ -2542,6 +2564,32 @@ fn f() where ty!{}: Copy { let _ = S { #[cfg(test)] probe: a < b, live: "four" } assert_eq!(literals(source), ["one", "two", "three", "four"]); } + #[test] + fn enum_discriminants_are_expressions() { + let source = r#"enum E { + V = f(#[cfg(test)] if 1 < 2 { true } else { false }, live("one") > 0), + W = m::f(#[cfg(test)] a < b, live("two") > 0), + X = g(1)(#[cfg(test)] a < b, live("three") > 0), + T = m::f::::call(#[cfg(test)] a < b, live("ten") > 0), + S = >::call(#[cfg(test)] a < b, live("eleven") > 0), + #[doc = "four"] + Y(#[cfg(test)] Pair>, Tag<{ "five".len() }>), + Z { #[cfg(test)] field: Pair>, live: Tag<{ "six".len() }> }, +} +macro_rules! variants { + ($a:ident, $b:ident, $c:ident) => { + enum M { + $a = f(#[cfg(test)] a < b, live("seven") > 0), + $b(#[cfg(test)] Pair>, Tag<{ "eight".len() }>), + $c { #[cfg(test)] field: Pair>, live: Tag<{ "nine".len() }> }, + $(#[$meta])* V(#[cfg(test)] Pair>, Tag<{ "twelve".len() }>), + } + }; +} +"#; + assert_eq!(literals(source), ["one", "two", "three", "ten", "eleven", "four", "five", "six", "seven", "eight", "nine", "twelve"]); + } + #[test] fn test_only_cast_types_keep_their_generic_commas() { let source = r#"fn arms(x: u8, a: u8, b: u8, ptr: *const u8) { From f84b8eec3dccaca9c4e8cc873616535bfa4c4060 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 24 Nov 2026 19:34:27 -0800 Subject: [PATCH 10/12] craft-xtask: resolve each input once before it is listed Each input is now joined to the base, or a Windows drive-relative input (`C:src`) to the current folder of its drive, before its folder is listed. The listing and the reads use that path, and findings keep the input's spelling, so a change of the drive's current folder during the walk does not change which files are read. --- crates/xtask/src/names.rs | 53 ++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/crates/xtask/src/names.rs b/crates/xtask/src/names.rs index caa2ac585a..9f507d1d1e 100644 --- a/crates/xtask/src/names.rs +++ b/crates/xtask/src/names.rs @@ -294,8 +294,9 @@ impl From for String { /// /// A file is identified by its path joined to `base`, and a relative `base` is joined to the /// current folder (on Windows, a drive-relative `base` such as `C:src` to the current folder of -/// its drive) before any input is listed. A drive-relative input (`C:a.rs`) is joined to the -/// current folder of its drive instead. A relative and an absolute path to a file are one file, +/// its drive) before any input is listed. A drive-relative input (`C:a.rs`, `C:src`) is joined to +/// the current folder of its drive instead, once, before it is listed, and the files of a folder +/// input are opened under that path. A relative and an absolute path to a file are one file, /// read once and reported by the input path that sorts first; `..` and links are not resolved. /// /// A [`Kind::Rust`] folder is read recursively for `.rs` files that [`Policy::skip`] keeps; links to @@ -306,16 +307,17 @@ pub fn scan(base: &Path, inputs: &[Input<'_>], policy: &Policy<'_>) -> Result, Error>>()?; files.sort(); files.dedup_by(|later, first| later.0 == first.0 && later.1 == first.1); if let Some(pair) = files.windows(2).find(|pair| pair[0].0 == pair[1].0) { @@ -648,39 +650,43 @@ fn io_error(path: &Path) -> impl FnOnce(io::Error) -> Error + '_ { move |source| Error::Io { path: path.to_path_buf(), source } } -fn collect_rust(base: &Path, path: &Path, policy: &Policy<'_>, out: &mut Vec<(PathBuf, Kind)>) -> Result<(), Error> { - let full = base.join(path); +/// The Rust files of the input `path` under the absolute `root`: the path that is opened, the kind, +/// and the path that is reported. `path` is resolved once, so the folder that is listed is the +/// folder whose files are read. +fn collect_rust(root: &Path, path: &Path, policy: &Policy<'_>, out: &mut Vec<(PathBuf, Kind, PathBuf)>) -> Result<(), Error> { + let full = input_path(root, path)?; let link = fs::symlink_metadata(&full).map_err(io_error(path))?.file_type().is_symlink(); let meta = fs::metadata(&full).map_err(io_error(path))?; if meta.is_dir() { // A link to a folder is not followed, also when it is an input. - if link { Ok(()) } else { walk(base, path, path, policy, 0, out) } + if link { Ok(()) } else { walk(&full, path, path, policy, 0, out) } } else { - out.push((path.to_path_buf(), Kind::Rust)); + out.push((full, Kind::Rust, path.to_path_buf())); Ok(()) } } -fn walk(base: &Path, input: &Path, dir: &Path, policy: &Policy<'_>, depth: usize, out: &mut Vec<(PathBuf, Kind)>) -> Result<(), Error> { +/// The Rust files in the folder `full`, which is reported as `dir`, under the input `input`. +fn walk(full: &Path, input: &Path, dir: &Path, policy: &Policy<'_>, depth: usize, out: &mut Vec<(PathBuf, Kind, PathBuf)>) -> Result<(), Error> { if depth > policy.limits.max_depth { return Err(Error::TooDeep { path: dir.to_path_buf(), limit: policy.limits.max_depth }); } - let mut entries = fs::read_dir(base.join(dir)).and_then(|entries| entries.collect::>>()).map_err(io_error(dir))?; + let mut entries = fs::read_dir(full).and_then(|entries| entries.collect::>>()).map_err(io_error(dir))?; entries.sort_by_key(fs::DirEntry::file_name); for entry in entries { - let path = dir.join(entry.file_name()); + let (opened, path) = (full.join(entry.file_name()), dir.join(entry.file_name())); let file_type = entry.file_type().map_err(io_error(&path))?; if file_type.is_dir() { - walk(base, input, &path, policy, depth + 1, out)?; + walk(&opened, input, &path, policy, depth + 1, out)?; continue; } if !path.extension().is_some_and(|ext| ext == "rs") || (policy.skip)(path.strip_prefix(input).unwrap_or(&path)) { continue; } // A link to a folder is not followed; a link to a file is read like the file. - let is_file = if file_type.is_symlink() { fs::metadata(base.join(&path)).map_err(io_error(&path))?.is_file() } else { file_type.is_file() }; + let is_file = if file_type.is_symlink() { fs::metadata(&opened).map_err(io_error(&path))?.is_file() } else { file_type.is_file() }; if is_file { - out.push((path, Kind::Rust)); + out.push((opened, Kind::Rust, path)); } } Ok(()) @@ -2847,6 +2853,25 @@ fn seven() { let _ = "seven"; } } } + #[cfg(windows)] + #[test] + fn a_drive_relative_rust_folder_is_resolved_before_it_is_listed() { + let current = std::env::current_dir().unwrap(); + let Some(Component::Prefix(drive)) = current.components().next() else { panic!("no drive in {}", current.display()) }; + let mut spelled = drive.as_os_str().to_os_string(); + spelled.push("src"); + let input = PathBuf::from(spelled); + let mut files = Vec::new(); + collect_rust(¤t, &input, &POLICY, &mut files).unwrap(); + assert!(!files.is_empty()); + for (opened, kind, path) in &files { + assert_eq!(*kind, Kind::Rust); + // The file is opened by the path that was listed, and reported by the input's spelling. + let rest = path.strip_prefix(&input).unwrap(); + assert_eq!(opened, ¤t.join("src").join(rest), "{}", path.display()); + } + } + #[test] fn the_total_budget_is_checked_before_reading() { let dir = tempfile::tempdir().unwrap(); From 08c0d99732f291e8dc53d13d9e871e07fdf74442 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 24 Nov 2026 19:35:40 -0800 Subject: [PATCH 11/12] craft-xtask: describe parameter, cast and drive-relative input rules The README and the `literals` docs now say that a comma in the type of a parameter after its pattern, or of a cast, does not end test-only code, and that a drive-relative input is resolved once before it is listed. --- crates/xtask/README.md | 8 +++++--- crates/xtask/src/names.rs | 5 +++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/crates/xtask/README.md b/crates/xtask/README.md index 95394832a7..982ee0ec28 100644 --- a/crates/xtask/README.md +++ b/crates/xtask/README.md @@ -107,8 +107,9 @@ fn production_display_names_stay_neutral() { So is the code under `#[cfg(test)]`, comments in the attribute included: a `const`, `static` or `let` up to its `;`, another item up to the `}` that closes it or the `;` that ends it, and a field, argument, arm or statement up to its `,` or `;`. A `,` in an expression's generic - arguments (`f::`) or in the parameters of a closure that starts the expression does not - end it. An item keyword counts only when nothing + arguments (`f::`), in the type of a field, a parameter (after its name or pattern) or a + cast (`mut p: Pair`, `ptr as *const Pair`), or in the parameters of a closure that + starts the expression does not end it. An item keyword counts only when nothing but qualifiers (`pub`, `unsafe`, `extern "C"` and the like) comes before it, so the `fn` in `f::(…)` or in a field's type does not start an item, and the braces of a macro in an item's header (`-> ty! { u8 }`) are not its body. A block expression (a block, a macro's braces, a labeled block, or @@ -128,7 +129,8 @@ fn production_display_names_stay_neutral() { - `scan` identifies a file by its input path joined to the base folder, and joins a relative base folder to the current folder before it lists any input (on Windows, a drive-relative base such as `C:src` to the current folder of its drive), so a relative and an absolute input for one file are - read once; `..` and links are not resolved. + read once; `..` and links are not resolved. A drive-relative input is joined to the current + folder of its drive once, before it is listed, and its files are opened under that path. - `scan` reads each file once, in path order, and returns the findings in path order, then source order, then the order of `banned`. A `Finding` has the path, the first line of the literal or description, the word, the decoded text or text line and the kind; its `Display` text is diff --git a/crates/xtask/src/names.rs b/crates/xtask/src/names.rs index 9f507d1d1e..0f05da1e85 100644 --- a/crates/xtask/src/names.rs +++ b/crates/xtask/src/names.rs @@ -597,8 +597,9 @@ pub struct Literal { /// Test-only code is the code under `#[cfg(test)]` and the rest of a module that starts with /// `#![cfg(test)]`. Under `#[cfg(test)]`, a `const`, `static` or `let` runs up to its `;`, another /// item up to its closing `}` or its `;`, and a field, argument, arm or statement up to its `,` or -/// `;`; a `,` in generic arguments (`f::`, ` as Fixture>::LABEL`, or a field's -/// `field: Pair`) or in the parameters of a closure that starts the expression (after any +/// `;`; a `,` in generic arguments (`f::`, ` as Fixture>::LABEL`, a field's +/// `field: Pair`, a parameter's `mut p: Pair`, or a cast's `ptr as *const Pair`) +/// or in the parameters of a closure that starts the expression (after any /// `move` or `&`) does not end it. A raw identifier (`r#let`) is never a keyword. A block expression (a block, a macro's braces, a /// labeled block, or `if`, `match`, `loop`, `while`, `for`, `unsafe`, `async` or `const`), as the /// whole statement or as an arm's expression after `=>`, ends at its `}`, or at the last `}` of an From 994d03d02f3396d88c5536f2665093f5790f59d2 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 24 Nov 2026 23:17:10 -0800 Subject: [PATCH 12/12] craft-xtask: drop only the out-of-scope pending declarations Pending declarations are kept innermost last, so a closing delimiter or an item's end now pops the ones above the current depth from the end instead of scanning the whole list. A source with 200,000 pending `fn`s and 200,000 `[]` groups took 216 s in a debug test before and reads at once now. --- crates/xtask/src/names.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/crates/xtask/src/names.rs b/crates/xtask/src/names.rs index 0f05da1e85..5cd3d65c77 100644 --- a/crates/xtask/src/names.rs +++ b/crates/xtask/src/names.rs @@ -1742,7 +1742,10 @@ impl Lexer<'_> { } self.at += 1; let depth = self.delims.len(); - self.declares.retain(|declares| declares.level <= depth); + // `declares` is in ascending order of level, so the ones out of scope are at its end. + while self.declares.last().is_some_and(|declares| declares.level > depth) { + self.declares.pop(); + } let ends = match self.test { Some(TestRegion::Item { level, opened, item, semi, block, .. }) => { let block_end = opened && depth == level && c == '}'; @@ -1790,7 +1793,9 @@ impl Lexer<'_> { // A unit struct (`struct S;`) declares no fields. if !comma { let level = self.delims.len(); - self.declares.retain(|declares| declares.level < level); + while self.declares.last().is_some_and(|declares| declares.level >= level) { + self.declares.pop(); + } } // A `,` among an enum's variants ends a discriminant. if let Some((_, _, fields @ Fields::Discriminant)) = self.delims.last_mut() @@ -2518,6 +2523,15 @@ fn paths(#[cfg(test)] m::P(x): Pair>, q: Tag<{ "si assert_eq!(literals(source), ["one", "two", "three", "four", "five", "six"]); } + #[test] + fn closing_delimiters_keep_pending_declarations_in_linear_time() { + // 200,000 pending `fn`s and 200,000 `[]` groups: scanning every pending declaration at + // each `]` makes 4 * 10^10 comparisons, which takes minutes. + let count = 200_000; + let source = format!("m! {{ {}{}}} \"done\"", "fn ".repeat(count), "[] ".repeat(count)); + assert_eq!(literals(&source), ["done"]); + } + #[test] fn nested_function_pointer_types_keep_the_declaration() { let source = r#"struct Nested {