diff --git a/Cargo.lock b/Cargo.lock index f4957a3a8e..d3c4e0343a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3125,7 +3125,6 @@ name = "craft-testkit" version = "0.1.0" dependencies = [ "rustix 1.1.5", - "unicode-ident", ] [[package]] diff --git a/crates/testkit/Cargo.toml b/crates/testkit/Cargo.toml index 5e58b0168b..bfb89fb742 100644 --- a/crates/testkit/Cargo.toml +++ b/crates/testkit/Cargo.toml @@ -9,7 +9,6 @@ description = "Source-level platform regression guards and verification utilitie publish = false [dependencies] -unicode-ident = "1" [target.'cfg(unix)'.dependencies] rustix = { version = "1.1", default-features = false, features = ["std", "fs"] } diff --git a/crates/testkit/src/guards.rs b/crates/testkit/src/guards.rs index db5de5b35d..988f102e60 100644 --- a/crates/testkit/src/guards.rs +++ b/crates/testkit/src/guards.rs @@ -151,15 +151,22 @@ impl Token { } } -/// Whether `c` can start a Rust identifier: `_` or a character with the Unicode `XID_Start` property. +/// Whether `c` can start a Rust identifier: `_`, a letter, or another non-ASCII character that is not white +/// space (see [`non_ascii_word`]). fn is_ident_start(c: char) -> bool { - c == '_' || unicode_ident::is_xid_start(c) + c == '_' || c.is_alphabetic() || non_ascii_word(c) } -/// Whether `c` can continue a Rust identifier: a character with the Unicode `XID_Continue` property, which -/// includes `_`, digits and combining marks such as U+0301. +/// Whether `c` can continue a Rust identifier: `_`, a letter or digit, or another non-ASCII character that is +/// not white space, such as the combining mark U+0301. fn is_ident_continue(c: char) -> bool { - unicode_ident::is_xid_continue(c) + c == '_' || c.is_alphanumeric() || non_ascii_word(c) +} + +/// Whether `c` is a non-ASCII character other than Rust's white space and the byte order mark U+FEFF. Outside +/// literals and comments, Rust allows such a character only in an identifier, so it is read as part of one. +fn non_ascii_word(c: char) -> bool { + !c.is_ascii() && !c.is_whitespace() && !matches!(c, '\u{200E}' | '\u{200F}' | '\u{FEFF}') } /// Split Rust source into tokens with 1-based line numbers. @@ -181,7 +188,9 @@ fn lex_rust(code: &str) -> Vec { if c == '\n' { line += 1; i += 1; - } else if c.is_whitespace() { + } else if c.is_whitespace() || matches!(c, '\u{200E}' | '\u{200F}' | '\u{FEFF}') { + // Rust also reads the left-to-right and right-to-left marks as white space, and skips a byte + // order mark at the start of a file. i += 1; } else if c == '/' && at(i + 1) == Some('/') { while i < len && chars[i] != '\n' { diff --git a/crates/testkit/tests/guards.rs b/crates/testkit/tests/guards.rs index 5014703a46..b820b07272 100644 --- a/crates/testkit/tests/guards.rs +++ b/crates/testkit/tests/guards.rs @@ -1446,6 +1446,10 @@ fn guards_read_names_that_hold_combining_marks() { let menu = "use muda::{PredefinedMenuItem as Ite\u{301}m};\nfn menu() {\n let a = Ite\u{301}m::quit(None);\n}\n"; assert_eq!(lines(&predefined_quit([("src/menu.rs", menu)])), vec![3]); + + // A byte order mark and the left-to-right and right-to-left marks are not part of a name. + let marked = "\u{FEFF}use std::time::Instant;\nuse std::time as\u{200E}t;\nfn run() { t\u{200F}::Instant::now(); }\n"; + assert_eq!(lines(&std_instant([("src/marked.rs", marked)])), vec![1, 3]); } #[test]