Skip to content

Commit 0646d5c

Browse files
Merge #10413
10413: fix: Fix format highlighting with `concat!` and `unreachable!` r=jonas-schievink a=jonas-schievink Last item in #10394, closes #10394 Also documents why/how `is_format_string` even works, since I found that nontrivial. bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents 47c079a + 3bc0d89 commit 0646d5c

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

crates/ide/src/syntax_highlighting/format.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,18 @@ pub(super) fn highlight_format_string(
2828
}
2929

3030
fn is_format_string(string: &ast::String) -> Option<()> {
31-
let parent = string.syntax().parent()?;
31+
// Check if `string` is a format string argument of a macro invocation.
32+
// `string` is a string literal, mapped down into the innermost macro expansion.
33+
// Since `format_args!` etc. remove the format string when expanding, but place all arguments
34+
// in the expanded output, we know that the string token is (part of) the format string if it
35+
// appears in `format_args!` (otherwise it would have been mapped down further).
36+
//
37+
// This setup lets us correctly highlight the components of `concat!("{}", "bla")` format
38+
// strings. It still fails for `concat!("{", "}")`, but that is rare.
39+
40+
let macro_call = string.syntax().ancestors().find_map(ast::MacroCall::cast)?;
41+
let name = macro_call.path()?.segment()?.name_ref()?;
3242

33-
let name = parent.parent().and_then(ast::MacroCall::cast)?.path()?.segment()?.name_ref()?;
3443
if !matches!(
3544
name.text().as_str(),
3645
"format_args" | "format_args_nl" | "const_format_args" | "panic_2015" | "panic_2021"
@@ -41,13 +50,6 @@ fn is_format_string(string: &ast::String) -> Option<()> {
4150
// NB: we match against `panic_2015`/`panic_2021` here because they have a special-cased arm for
4251
// `"{}"`, which otherwise wouldn't get highlighted.
4352

44-
let first_literal = parent
45-
.children_with_tokens()
46-
.find_map(|it| it.as_token().cloned().and_then(ast::String::cast))?;
47-
if &first_literal != string {
48-
return None;
49-
}
50-
5153
Some(())
5254
}
5355

crates/ide/src/syntax_highlighting/test_data/highlight_strings.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,5 @@
145145
<span class="macro">assert!</span><span class="parenthesis">(</span><span class="bool_literal">true</span><span class="comma">,</span> <span class="string_literal">"</span><span class="format_specifier">{</span><span class="format_specifier">}</span><span class="string_literal"> asdasd"</span><span class="comma">,</span> <span class="numeric_literal">1</span><span class="parenthesis">)</span><span class="semicolon">;</span>
146146
<span class="macro">toho!</span><span class="parenthesis">(</span><span class="string_literal">"</span><span class="format_specifier">{</span><span class="format_specifier">}</span><span class="string_literal">fmt"</span><span class="comma">,</span> <span class="numeric_literal">0</span><span class="parenthesis">)</span><span class="semicolon">;</span>
147147
<span class="macro">asm!</span><span class="parenthesis">(</span><span class="string_literal">"mov eax, </span><span class="format_specifier">{</span><span class="numeric_literal">0</span><span class="format_specifier">}</span><span class="string_literal">"</span><span class="parenthesis">)</span><span class="semicolon">;</span>
148+
<span class="macro">format_args!</span><span class="parenthesis">(</span>concat<span class="punctuation">!</span><span class="parenthesis">(</span><span class="string_literal">"</span><span class="format_specifier">{</span><span class="format_specifier">}</span><span class="string_literal">"</span><span class="parenthesis">)</span><span class="comma">,</span> <span class="string_literal">"{}"</span><span class="parenthesis">)</span><span class="semicolon">;</span>
148149
<span class="brace">}</span></code></pre>

crates/ide/src/syntax_highlighting/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ fn main() {
540540
assert!(true, "{} asdasd", 1);
541541
toho!("{}fmt", 0);
542542
asm!("mov eax, {0}");
543+
format_args!(concat!("{}"), "{}");
543544
}"#
544545
.trim(),
545546
expect_file!["./test_data/highlight_strings.html"],

0 commit comments

Comments
 (0)