From 530c69bbc2c84b3def01a748722331ce9fa52c55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 8 May 2026 22:43:48 +0000 Subject: [PATCH] Do not index past end of buffer when checking heuristic in error index syntax highlighter When checking whether the current token is a function indentifier by inspecting the syntax, do not attempt to access past the end of the code buffer. --- compiler/rustc_driver_impl/src/highlighter.rs | 2 +- .../explanation-code-rendering-edge-case-regression.rs | 3 +++ .../explanation-code-rendering-edge-case-regression.stdout | 7 +++++++ 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 tests/ui/explain/explanation-code-rendering-edge-case-regression.rs create mode 100644 tests/ui/explain/explanation-code-rendering-edge-case-regression.stdout diff --git a/compiler/rustc_driver_impl/src/highlighter.rs b/compiler/rustc_driver_impl/src/highlighter.rs index 70b73cc29b723..16b8f58bca1e6 100644 --- a/compiler/rustc_driver_impl/src/highlighter.rs +++ b/compiler/rustc_driver_impl/src/highlighter.rs @@ -106,7 +106,7 @@ impl Highlighter { // This heuristic test is to detect if the identifier is // a function call. If it is, then the function identifier is // colored differently. - if code[*len_accum..*len_accum + len + 1].ends_with('(') { + if code[*len_accum + len..].starts_with('(') { style = style.fg_color(Some(Color::Ansi(FUNCTION_COLOR))); } // The `derive` keyword is colored differently. diff --git a/tests/ui/explain/explanation-code-rendering-edge-case-regression.rs b/tests/ui/explain/explanation-code-rendering-edge-case-regression.rs new file mode 100644 index 0000000000000..0d12c71bb8736 --- /dev/null +++ b/tests/ui/explain/explanation-code-rendering-edge-case-regression.rs @@ -0,0 +1,3 @@ +//@ compile-flags: --explain E0602 --color always --error-format=human +//@ check-pass +// Ensure that we trigger the condition for #156326 without ICEing. diff --git a/tests/ui/explain/explanation-code-rendering-edge-case-regression.stdout b/tests/ui/explain/explanation-code-rendering-edge-case-regression.stdout new file mode 100644 index 0000000000000..962d129214349 --- /dev/null +++ b/tests/ui/explain/explanation-code-rendering-edge-case-regression.stdout @@ -0,0 +1,7 @@ +An unknown or invalid lint was used on the command line. + +Erroneous code example: + +rustc -D bogus rust_file.rs + +Maybe you just misspelled the lint name or the lint doesn't exist anymore. Either way, try to update/remove it in order to fix the error.