From 0d4207f2c5b7bea9683a938bc3c40fd4452c7d8d Mon Sep 17 00:00:00 2001 From: Frances Wingerter <91758128+fw-immunant@users.noreply.github.com> Date: Thu, 3 Jul 2025 17:07:22 +0000 Subject: [PATCH 1/3] rust: `className` -> `scope` Co-authored-by: Josh Goebel --- src/languages/rust.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/languages/rust.js b/src/languages/rust.js index 13e7be19f2..c0795b8736 100644 --- a/src/languages/rust.js +++ b/src/languages/rust.js @@ -17,7 +17,7 @@ export default function(hljs) { const IDENT_RE = regex.concat(RAW_IDENTIFIER, hljs.IDENT_RE); // ============================================ const FUNCTION_INVOKE = { - className: "title.function.invoke", + scope: "title.function.invoke", relevance: 0, begin: regex.concat( /\b/, @@ -199,7 +199,7 @@ export default function(hljs) { illegal: null }), { - className: 'symbol', + scope: 'symbol', // negative lookahead to avoid matching `'` begin: /'[a-zA-Z_][a-zA-Z0-9_]*(?!')/ }, @@ -220,7 +220,7 @@ export default function(hljs) { ] }, { - className: 'number', + scope: 'number', variants: [ { begin: '\\b0b([01_]+)' + NUMBER_SUFFIX }, { begin: '\\b0o([0-7_]+)' + NUMBER_SUFFIX }, @@ -236,18 +236,18 @@ export default function(hljs) { /\s+/, UNDERSCORE_IDENT_RE ], - className: { + scope: { 1: "keyword", 3: "title.function" } }, { - className: 'meta', + scope: 'meta', begin: '#!?\\[', end: '\\]', contains: [ { - className: 'string', + scope: 'string', begin: /"/, end: /"/, contains: [ @@ -263,7 +263,7 @@ export default function(hljs) { /(?:mut\s+)?/, UNDERSCORE_IDENT_RE ], - className: { + scope: { 1: "keyword", 3: "keyword", 4: "variable" @@ -278,7 +278,7 @@ export default function(hljs) { /\s+/, /in/ ], - className: { + scope: { 1: "keyword", 3: "variable", 5: "keyword" @@ -290,7 +290,7 @@ export default function(hljs) { /\s+/, UNDERSCORE_IDENT_RE ], - className: { + scope: { 1: "keyword", 3: "title.class" } @@ -301,7 +301,7 @@ export default function(hljs) { /\s+/, UNDERSCORE_IDENT_RE ], - className: { + scope: { 1: "keyword", 3: "title.class" } @@ -315,7 +315,7 @@ export default function(hljs) { } }, { - className: "punctuation", + scope: "punctuation", begin: '->' }, FUNCTION_INVOKE From 98d2a772388565b04b4396fc5220d805a652447d Mon Sep 17 00:00:00 2001 From: Frances Wingerter <91758128+fw-immunant@users.noreply.github.com> Date: Thu, 3 Jul 2025 16:58:34 +0000 Subject: [PATCH 2/3] rust: add `safe` keyword This is a "weak" keyword, only applicable in Rust 2024+ `unsafe extern` blocks, immediately prior to the "extern" keyword: https://doc.rust-lang.org/reference/keywords.html#r-lex.keywords.weak.safe --- src/languages/rust.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/languages/rust.js b/src/languages/rust.js index c0795b8736..ca3ba627c8 100644 --- a/src/languages/rust.js +++ b/src/languages/rust.js @@ -230,6 +230,17 @@ export default function(hljs) { ], relevance: 0 }, + { + begin: [ + /\bsafe/, + /\s+/, + /extern/, + ], + scope: { + 1: "keyword", + 3: "keyword", + } + }, { begin: [ /fn/, From 438e403c3e65fa79c47ee451267f888c58940c0d Mon Sep 17 00:00:00 2001 From: Frances Wingerter Date: Thu, 3 Jul 2025 18:50:24 +0000 Subject: [PATCH 3/3] rust: add tests for safe keyword in unsafe extern block --- test/markup/rust/extern.expect.txt | 4 ++++ test/markup/rust/extern.txt | 4 ++++ 2 files changed, 8 insertions(+) create mode 100644 test/markup/rust/extern.expect.txt create mode 100644 test/markup/rust/extern.txt diff --git a/test/markup/rust/extern.expect.txt b/test/markup/rust/extern.expect.txt new file mode 100644 index 0000000000..ea26fda242 --- /dev/null +++ b/test/markup/rust/extern.expect.txt @@ -0,0 +1,4 @@ +unsafe extern "C" { +unsafe fn foo(); +safe fn foo(); +} diff --git a/test/markup/rust/extern.txt b/test/markup/rust/extern.txt new file mode 100644 index 0000000000..6ebb795631 --- /dev/null +++ b/test/markup/rust/extern.txt @@ -0,0 +1,4 @@ +unsafe extern "C" { +unsafe fn foo(); +safe fn foo(); +}