Repository: rust-analyzer/rust-analyzer
Pull request #10413 (2021-10-01 11:31:18)

fix: Fix format highlighting with `concat!` and `unreachable!`
Last item in https://github.com/rust-analyzer/rust-analyzer/issues/10394, closes https://github.com/rust-analyzer/rust-analyzer/issues/10394

Also documents why/how `is_format_string` even works, since I found that nontrivial.

bors r+


Patch:
diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs
index 03d513fe4d..cb23fec3c7 100644
--- a/crates/ide/src/syntax_highlighting.rs
+++ b/crates/ide/src/syntax_highlighting.rs
@@ -133,7 +133,6 @@ pub struct HlRange {
 // constant:: Emitted for consts.
 // consuming:: Emitted for locals that are being consumed when use in a function call.
 // controlFlow:: Emitted for control-flow related tokens, this includes the `?` operator.
-// crateRoot:: Emitted for crate names, like `serde` and `crate`.
 // declaration:: Emitted for names of definitions, like `foo` in `fn foo() {}`.
 // defaultLibrary:: Emitted for items from built-in crates (std, core, alloc, test and proc_macro).
 // documentation:: Emitted for documentation comments.
diff --git a/crates/ide/src/syntax_highlighting/format.rs b/crates/ide/src/syntax_highlighting/format.rs
index b755a945cd..6f6671f8ca 100644
--- a/crates/ide/src/syntax_highlighting/format.rs
+++ b/crates/ide/src/syntax_highlighting/format.rs
@@ -28,9 +28,18 @@ pub(super) fn highlight_format_string(
 }
 
 fn is_format_string(string: &ast::String) -> Option<()> {
-    let parent = string.syntax().parent()?;
+    // Check if `string` is a format string argument of a macro invocation.
+    // `string` is a string literal, mapped down into the innermost macro expansion.
+    // Since `format_args!` etc. remove the format string when expanding, but place all arguments
+    // in the expanded output, we know that the string token is (part of) the format string if it
+    // appears in `format_args!` (otherwise it would have been mapped down further).
+    //
+    // This setup lets us correctly highlight the components of `concat!("{}", "bla")` format
+    // strings. It still fails for `concat!("{", "}")`, but that is rare.
+
+    let macro_call = string.syntax().ancestors().find_map(ast::MacroCall::cast)?;
+    let name = macro_call.path()?.segment()?.name_ref()?;
 
-    let name = parent.parent().and_then(ast::MacroCall::cast)?.path()?.segment()?.name_ref()?;
     if !matches!(
         name.text().as_str(),
         "format_args" | "format_args_nl" | "const_format_args" | "panic_2015" | "panic_2021"
@@ -41,13 +50,6 @@ fn is_format_string(string: &ast::String) -> Option<()> {
     // NB: we match against `panic_2015`/`panic_2021` here because they have a special-cased arm for
     // `"{}"`, which otherwise wouldn't get highlighted.
 
-    let first_literal = parent
-        .children_with_tokens()
-        .find_map(|it| it.as_token().cloned().and_then(ast::String::cast))?;
-    if &first_literal != string {
-        return None;
-    }
-
     Some(())
 }
 
diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs
index d2ec3ca939..65705d303c 100644
--- a/crates/ide/src/syntax_highlighting/highlight.rs
+++ b/crates/ide/src/syntax_highlighting/highlight.rs
@@ -164,10 +164,8 @@ fn token(
                     T![for] if !is_child_of_impl(&token) => h | HlMod::ControlFlow,
                     T![unsafe] => h | HlMod::Unsafe,
                     T![true] | T![false] => HlTag::BoolLiteral.into(),
-                    // crate is handled just as a token if it's in an `extern crate`
-                    T![crate] if parent_matches::<ast::ExternCrate>(&token) => h,
-                    // self, crate and super are handled as either a Name or NameRef already
-                    T![self] | T![crate] | T![super] => return None,
+                    // self is handled as either a Name or NameRef already
+                    T![self] => return None,
                     T![ref] => token
                         .parent()
                         .and_then(ast::IdentPat::cast)
@@ -285,7 +283,7 @@ fn highlight_name_ref(
                 }
             }
         };
-        let mut h = match name_class {
+        let h = match name_class {
             NameRefClass::Definition(def) => {
                 if let Definition::Local(local) = &def {
                     if let Some(name) = local.name(db) {
@@ -327,15 +325,11 @@ fn highlight_name_ref(
             }
             NameRefClass::FieldShorthand { .. } => SymbolKind::Field.into(),
         };
-        if h.tag == HlTag::Symbol(SymbolKind::Module) {
-            if name_ref.self_token().is_some() {
-                return SymbolKind::SelfParam.into();
-            }
-            if name_ref.crate_token().is_some() || name_ref.super_token().is_some() {
-                h.tag = HlTag::Keyword;
-            }
+        if h.tag == HlTag::Symbol(SymbolKind::Module) && name_ref.self_token().is_some() {
+            SymbolKind::SelfParam.into()
+        } else {
+            h
         }
-        h
     })
 }
 
@@ -399,13 +393,7 @@ fn highlight_def(
         Definition::Macro(_) => Highlight::new(HlTag::Symbol(SymbolKind::Macro)),
         Definition::Field(_) => Highlight::new(HlTag::Symbol(SymbolKind::Field)),
         Definition::ModuleDef(def) => match def {
-            hir::ModuleDef::Module(module) => {
-                let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Module));
-                if module.parent(db).is_none() {
-                    h |= HlMod::CrateRoot
-                }
-                h
-            }
+            hir::ModuleDef::Module(_) => Highlight::new(HlTag::Symbol(SymbolKind::Module)),
             hir::ModuleDef::Function(func) => {
                 let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Function));
                 if let Some(item) = func.as_assoc_item(db) {
diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs
index 92c7fcab76..0f9a99efac 100644
--- a/crates/ide/src/syntax_highlighting/tags.rs
+++ b/crates/ide/src/syntax_highlighting/tags.rs
@@ -55,8 +55,6 @@ pub enum HlMod {
     Consuming,
     /// Used with keywords like `if` and `break`.
     ControlFlow,
-    /// Used for crate names, like `serde`.
-    CrateRoot,
     /// Used for items from built-in crates (std, core, alloc, test and proc_macro).
     DefaultLibrary,
     /// `foo` in `fn foo(x: i32)` is a definition, `foo` in `foo(90 + 2)` is
@@ -196,7 +194,6 @@ impl HlMod {
         HlMod::Callable,
         HlMod::Consuming,
         HlMod::ControlFlow,
-        HlMod::CrateRoot,
         HlMod::DefaultLibrary,
         HlMod::Definition,
         HlMod::Documentation,
@@ -219,7 +216,6 @@ impl HlMod {
             HlMod::Callable => "callable",
             HlMod::Consuming => "consuming",
             HlMod::ControlFlow => "control",
-            HlMod::CrateRoot => "crate_root",
             HlMod::DefaultLibrary => "default_library",
             HlMod::Definition => "declaration",
             HlMod::Documentation => "documentation",
diff --git a/crates/rust-analyzer/src/semantic_tokens.rs b/crates/rust-analyzer/src/semantic_tokens.rs
index 3117b75728..d5f579f11d 100644
--- a/crates/rust-analyzer/src/semantic_tokens.rs
+++ b/crates/rust-analyzer/src/semantic_tokens.rs
@@ -94,7 +94,6 @@ define_semantic_token_modifiers![
     (CONSTANT, "constant"),
     (CONSUMING, "consuming"),
     (CONTROL_FLOW, "controlFlow"),
-    (CRATE_ROOT, "crateRoot"),
     (INJECTED, "injected"),
     (INTRA_DOC_LINK, "intraDocLink"),
     (LIBRARY, "library"),
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs
index e18298fade..3508fcc189 100644
--- a/crates/rust-analyzer/src/to_proto.rs
+++ b/crates/rust-analyzer/src/to_proto.rs
@@ -535,7 +535,6 @@ fn semantic_token_type_and_modifiers(
             HlMod::Callable => semantic_tokens::CALLABLE,
             HlMod::Consuming => semantic_tokens::CONSUMING,
             HlMod::ControlFlow => semantic_tokens::CONTROL_FLOW,
-            HlMod::CrateRoot => semantic_tokens::CRATE_ROOT,
             HlMod::DefaultLibrary => lsp_types::SemanticTokenModifier::DEFAULT_LIBRARY,
             HlMod::Definition => lsp_types::SemanticTokenModifier::DECLARATION,
             HlMod::Documentation => lsp_types::SemanticTokenModifier::DOCUMENTATION,


Test patch:
diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_crate_root.html b/crates/ide/src/syntax_highlighting/test_data/highlight_crate_root.html
deleted file mode 100644
index 8b4a76453b..0000000000
--- a/crates/ide/src/syntax_highlighting/test_data/highlight_crate_root.html
+++ /dev/null
@@ -1,64 +0,0 @@
-
-<style>
-body                { margin: 0; }
-pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padding: 0.4em; }
-
-.lifetime           { color: #DFAF8F; font-style: italic; }
-.label              { color: #DFAF8F; font-style: italic; }
-.comment            { color: #7F9F7F; }
-.documentation      { color: #629755; }
-.intra_doc_link     { font-style: italic; }
-.injected           { opacity: 0.65 ; }
-.struct, .enum      { color: #7CB8BB; }
-.enum_variant       { color: #BDE0F3; }
-.string_literal     { color: #CC9393; }
-.field              { color: #94BFF3; }
-.function           { color: #93E0E3; }
-.function.unsafe    { color: #BC8383; }
-.trait.unsafe       { color: #BC8383; }
-.operator.unsafe    { color: #BC8383; }
-.mutable.unsafe     { color: #BC8383; text-decoration: underline; }
-.keyword.unsafe     { color: #BC8383; font-weight: bold; }
-.parameter          { color: #94BFF3; }
-.text               { color: #DCDCCC; }
-.type               { color: #7CB8BB; }
-.builtin_type       { color: #8CD0D3; }
-.type_param         { color: #DFAF8F; }
-.attribute          { color: #94BFF3; }
-.numeric_literal    { color: #BFEBBF; }
-.bool_literal       { color: #BFE6EB; }
-.macro              { color: #94BFF3; }
-.module             { color: #AFD8AF; }
-.value_param        { color: #DCDCCC; }
-.variable           { color: #DCDCCC; }
-.format_specifier   { color: #CC696B; }
-.mutable            { text-decoration: underline; }
-.escape_sequence    { color: #94BFF3; }
-.keyword            { color: #F0DFAF; font-weight: bold; }
-.control            { font-style: italic; }
-.reference          { font-style: italic; font-weight: bold; }
-
-.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
-</style>
-<pre><code><span class="keyword">extern</span> <span class="keyword">crate</span> <span class="module crate_root library">foo</span><span class="semicolon">;</span>
-<span class="keyword">use</span> <span class="module crate_root default_library library">core</span><span class="operator">::</span><span class="module default_library library">iter</span><span class="semicolon">;</span>
-
-<span class="keyword">pub</span> <span class="keyword">const</span> <span class="constant declaration public">NINETY_TWO</span><span class="colon">:</span> <span class="builtin_type">u8</span> <span class="operator">=</span> <span class="numeric_literal">92</span><span class="semicolon">;</span>
-
-<span class="keyword">use</span> <span class="module crate_root library">foo</span> <span class="keyword">as</span> <span class="module crate_root declaration library">foooo</span><span class="semicolon">;</span>
-
-<span class="keyword">pub</span><span class="parenthesis">(</span><span class="keyword crate_root">crate</span><span class="parenthesis">)</span> <span class="keyword">fn</span> <span class="function declaration">main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
-    <span class="keyword">let</span> <span class="variable declaration">baz</span> <span class="operator">=</span> <span class="module default_library library">iter</span><span class="operator">::</span><span class="function default_library library">repeat</span><span class="parenthesis">(</span><span class="numeric_literal">92</span><span class="parenthesis">)</span><span class="semicolon">;</span>
-<span class="brace">}</span>
-
-<span class="keyword">mod</span> <span class="module declaration">bar</span> <span class="brace">{</span>
-    <span class="keyword">pub</span><span class="parenthesis">(</span><span class="keyword control">in</span> <span class="keyword crate_root">super</span><span class="parenthesis">)</span> <span class="keyword">const</span> <span class="constant declaration">FORTY_TWO</span><span class="colon">:</span> <span class="builtin_type">u8</span> <span class="operator">=</span> <span class="numeric_literal">42</span><span class="semicolon">;</span>
-
-    <span class="keyword">mod</span> <span class="module declaration">baz</span> <span class="brace">{</span>
-        <span class="keyword">use</span> <span class="keyword">super</span><span class="operator">::</span><span class="keyword crate_root">super</span><span class="operator">::</span><span class="constant public">NINETY_TWO</span><span class="semicolon">;</span>
-        <span class="keyword">use</span> <span class="keyword crate_root">crate</span><span class="operator">::</span><span class="module crate_root library">foooo</span><span class="operator">::</span><span class="struct library">Point</span><span class="semicolon">;</span>
-
-        <span class="keyword">pub</span><span class="parenthesis">(</span><span class="keyword control">in</span> <span class="keyword">super</span><span class="operator">::</span><span class="keyword crate_root">super</span><span class="parenthesis">)</span> <span class="keyword">const</span> <span class="constant declaration">TWENTY_NINE</span><span class="colon">:</span> <span class="builtin_type">u8</span> <span class="operator">=</span> <span class="numeric_literal">29</span><span class="semicolon">;</span>
-    <span class="brace">}</span>
-<span class="brace">}</span>
-</code></pre>
\ No newline at end of file
diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_default_library.html b/crates/ide/src/syntax_highlighting/test_data/highlight_default_library.html
index ea68f5f14a..33d3196cdb 100644
--- a/crates/ide/src/syntax_highlighting/test_data/highlight_default_library.html
+++ b/crates/ide/src/syntax_highlighting/test_data/highlight_default_library.html
@@ -40,7 +40,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 
 .unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
 </style>
-<pre><code><span class="keyword">use</span> <span class="module crate_root default_library library">core</span><span class="operator">::</span><span class="module default_library library">iter</span><span class="semicolon">;</span>
+<pre><code><span class="keyword">use</span> <span class="module default_library library">core</span><span class="operator">::</span><span class="module default_library library">iter</span><span class="semicolon">;</span>
 
 <span class="keyword">fn</span> <span class="function declaration">main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
     <span class="keyword">let</span> <span class="variable declaration">foo</span> <span class="operator">=</span> <span class="enum_variant default_library library">Some</span><span class="parenthesis">(</span><span class="numeric_literal">92</span><span class="parenthesis">)</span><span class="semicolon">;</span>
diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html b/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html
index ca869c78be..92cfa0d0a8 100644
--- a/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html
+++ b/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html
@@ -40,6 +40,6 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 
 .unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
 </style>
-<pre><code><span class="keyword">extern</span> <span class="keyword">crate</span> <span class="module crate_root default_library library">std</span><span class="semicolon">;</span>
-<span class="keyword">extern</span> <span class="keyword">crate</span> <span class="module crate_root default_library library">alloc</span> <span class="keyword">as</span> <span class="module crate_root default_library declaration library">abc</span><span class="semicolon">;</span>
+<pre><code><span class="keyword">extern</span> <span class="keyword">crate</span> <span class="module default_library library">std</span><span class="semicolon">;</span>
+<span class="keyword">extern</span> <span class="keyword">crate</span> <span class="module default_library library">alloc</span> <span class="keyword">as</span> <span class="module default_library declaration library">abc</span><span class="semicolon">;</span>
 </code></pre>
\ No newline at end of file
diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html
index 4f67c4316a..893094b24a 100644
--- a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html
+++ b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html
@@ -145,4 +145,5 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
     <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>
     <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>
     <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>
+    <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>
 <span class="brace">}</span></code></pre>
\ No newline at end of file
diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html
index 05e6739830..7918a96275 100644
--- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html
+++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html
@@ -253,12 +253,12 @@ proc_macros::<span class="macro">mirror!</span> <span class="brace">{</span>
 <span class="brace">}</span>
 
 <span class="keyword">fn</span> <span class="function declaration">use_foo_items</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
-    <span class="keyword">let</span> <span class="variable declaration">bob</span> <span class="operator">=</span> <span class="module crate_root library">foo</span><span class="operator">::</span><span class="struct library">Person</span> <span class="brace">{</span>
+    <span class="keyword">let</span> <span class="variable declaration">bob</span> <span class="operator">=</span> <span class="module library">foo</span><span class="operator">::</span><span class="struct library">Person</span> <span class="brace">{</span>
         <span class="field library">name</span><span class="colon">:</span> <span class="string_literal">"Bob"</span><span class="comma">,</span>
-        <span class="field library">age</span><span class="colon">:</span> <span class="module crate_root library">foo</span><span class="operator">::</span><span class="module library">consts</span><span class="operator">::</span><span class="constant library">NUMBER</span><span class="comma">,</span>
+        <span class="field library">age</span><span class="colon">:</span> <span class="module library">foo</span><span class="operator">::</span><span class="module library">consts</span><span class="operator">::</span><span class="constant library">NUMBER</span><span class="comma">,</span>
     <span class="brace">}</span><span class="semicolon">;</span>
 
-    <span class="keyword">let</span> <span class="variable declaration">control_flow</span> <span class="operator">=</span> <span class="module crate_root library">foo</span><span class="operator">::</span><span class="function library">identity</span><span class="parenthesis">(</span><span class="module crate_root library">foo</span><span class="operator">::</span><span class="enum library">ControlFlow</span><span class="operator">::</span><span class="enum_variant library">Continue</span><span class="parenthesis">)</span><span class="semicolon">;</span>
+    <span class="keyword">let</span> <span class="variable declaration">control_flow</span> <span class="operator">=</span> <span class="module library">foo</span><span class="operator">::</span><span class="function library">identity</span><span class="parenthesis">(</span><span class="module library">foo</span><span class="operator">::</span><span class="enum library">ControlFlow</span><span class="operator">::</span><span class="enum_variant library">Continue</span><span class="parenthesis">)</span><span class="semicolon">;</span>
 
     <span class="keyword control">if</span> <span class="variable">control_flow</span><span class="operator">.</span><span class="function associated consuming library">should_die</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
         foo::<span class="macro">die!</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs
index 6972d431d2..18f562f0b1 100644
--- a/crates/ide/src/syntax_highlighting/tests.rs
+++ b/crates/ide/src/syntax_highlighting/tests.rs
@@ -540,6 +540,7 @@ fn main() {
     assert!(true, "{} asdasd", 1);
     toho!("{}fmt", 0);
     asm!("mov eax, {0}");
+    format_args!(concat!("{}"), "{}");
 }"#
         .trim(),
         expect_file!["./test_data/highlight_strings.html"],
@@ -779,50 +780,6 @@ fn test_extern_crate() {
     );
 }
 
-#[test]
-fn test_crate_root() {
-    check_highlighting(
-        r#"
-        //- minicore: iterators
-        //- /main.rs crate:main deps:foo
-        extern crate foo;
-        use core::iter;
-
-        pub const NINETY_TWO: u8 = 92;
-
-        use foo as foooo;
-
-        pub(crate) fn main() {
-            let baz = iter::repeat(92);
-        }
-
-        mod bar {
-            pub(in super) const FORTY_TWO: u8 = 42;
-
-            mod baz {
-                use super::super::NINETY_TWO;
-                use crate::foooo::Point;
-
-                pub(in super::super) const TWENTY_NINE: u8 = 29;
-            }
-        }
-        //- /foo.rs crate:foo
-        struct Point {
-            x: u8,
-            y: u8,
-        }
-
-        mod inner {
-            pub(super) fn swap(p: crate::Point) -> crate::Point {
-                crate::Point { x: p.y, y: p.x }
-            }
-        }
-        "#,
-        expect_file!["./test_data/highlight_crate_root.html"],
-        false,
-    );
-}
-
 #[test]
 fn test_default_library() {
     check_highlighting(