Repository: rust-analyzer/rust-analyzer (rust)

Issue:
`#[macro_use] extern crate self` causes a cycle
```rust
#[macro_use]
extern crate self as bla;
```

```
thread '<unnamed>' panicked at 'Internal error, cycle detected:

DatabaseKeyIndex { group_index: 6, query_index: 1, key_index: 12 }
', /home/jonas/.cargo/registry/src/github.com-1ecc6299db9ec823/salsa-0.16.1/src/lib.rs:490:48
stack backtrace:
   0: rust_begin_unwind
   1: std::panicking::begin_panic_fmt
   2: salsa::QueryTable<Q>::get::{{closure}}
   3: <DB as hir_def::db::DefDatabase>::crate_def_map_query::__shim
   4: hir_def::db::crate_def_map_wait
   5: hir_def::nameres::collector::DefCollector::import_all_macros_exported
   6: hir_def::nameres::collector::ModCollector::collect
   7: hir_def::nameres::collector::collect_defs
...
```

Pull request:
Ignore macro import from `extern crate self`
Closes #8834

Patch:
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs
index adfb78c942..19db6cc59a 100644
--- a/crates/hir_def/src/nameres/collector.rs
+++ b/crates/hir_def/src/nameres/collector.rs
@@ -481,6 +481,11 @@ impl DefCollector<'_> {
         let res = self.def_map.resolve_name_in_extern_prelude(self.db, &extern_crate.name);
 
         if let Some(ModuleDefId::ModuleId(m)) = res.take_types() {
+            if m == self.def_map.module_id(current_module_id) {
+                cov_mark::hit!(ignore_macro_use_extern_crate_self);
+                return;
+            }
+
             cov_mark::hit!(macro_rules_from_other_crates_are_visible_with_macro_use);
             self.import_all_macros_exported(current_module_id, m.krate);
         }
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs
index 73dcba6940..8ce4aa2423 100644
--- a/crates/rust-analyzer/src/to_proto.rs
+++ b/crates/rust-analyzer/src/to_proto.rs
@@ -422,7 +422,7 @@ fn semantic_token_type_and_modifiers(
     let type_ = match highlight.tag {
         HlTag::Symbol(symbol) => match symbol {
             SymbolKind::Module => lsp_types::SemanticTokenType::NAMESPACE,
-            SymbolKind::Impl => semantic_tokens::TYPE_ALIAS,
+            SymbolKind::Impl => lsp_types::SemanticTokenType::TYPE,
             SymbolKind::Field => lsp_types::SemanticTokenType::PROPERTY,
             SymbolKind::TypeParam => lsp_types::SemanticTokenType::TYPE_PARAMETER,
             SymbolKind::ConstParam => semantic_tokens::CONST_PARAMETER,


Test patch:
diff --git a/crates/hir_def/src/nameres/tests.rs b/crates/hir_def/src/nameres/tests.rs
index 4f2e7a2f96..9f652731d9 100644
--- a/crates/hir_def/src/nameres/tests.rs
+++ b/crates/hir_def/src/nameres/tests.rs
@@ -410,6 +410,22 @@ struct Arc;
     );
 }
 
+#[test]
+fn macro_use_extern_crate_self() {
+    cov_mark::check!(ignore_macro_use_extern_crate_self);
+    check(
+        r#"
+//- /main.rs crate:main
+#[macro_use]
+extern crate self as bla;
+"#,
+        expect![[r#"
+            crate
+            bla: t
+        "#]],
+    );
+}
+
 #[test]
 fn reexport_across_crates() {
     check(
diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html
index 0d325f3f3d..33bc6b0f3b 100644
--- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html
+++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html
@@ -66,11 +66,11 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
     <span class="keyword">pub</span> <span class="field declaration">y</span><span class="colon">:</span> <span class="builtin_type">i32</span><span class="comma">,</span>
 <span class="brace">}</span>
 
-<span class="keyword">trait</span> <span class="trait declaration">Bar</span> <span class="keyword">where</span> <span class="type_param">Self</span><span class="colon">:</span> <span class="brace">{</span>
+<span class="keyword">trait</span> <span class="trait declaration">Bar</span> <span class="brace">{</span>
     <span class="keyword">fn</span> <span class="function associated declaration trait">bar</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="operator">-&gt;</span> <span class="builtin_type">i32</span><span class="semicolon">;</span>
 <span class="brace">}</span>
 
-<span class="keyword">impl</span> <span class="trait">Bar</span> <span class="keyword">for</span> <span class="struct">Foo</span> <span class="keyword">where</span> <span class="self_type">Self</span><span class="colon">:</span> <span class="brace">{</span>
+<span class="keyword">impl</span> <span class="trait">Bar</span> <span class="keyword">for</span> <span class="struct">Foo</span> <span class="brace">{</span>
     <span class="keyword">fn</span> <span class="function associated declaration trait">bar</span><span class="parenthesis">(</span><span class="operator">&</span><span class="self_keyword declaration">self</span><span class="parenthesis">)</span> <span class="operator">-&gt;</span> <span class="builtin_type">i32</span> <span class="brace">{</span>
         <span class="self_keyword">self</span><span class="operator">.</span><span class="field">x</span>
     <span class="brace">}</span>
diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs
index 8c8878d367..95408dfb2e 100644
--- a/crates/ide/src/syntax_highlighting/tests.rs
+++ b/crates/ide/src/syntax_highlighting/tests.rs
@@ -39,11 +39,11 @@ struct Foo {
     pub y: i32,
 }
 
-trait Bar where Self: {
+trait Bar {
     fn bar(&self) -> i32;
 }
 
-impl Bar for Foo where Self: {
+impl Bar for Foo {
     fn bar(&self) -> i32 {
         self.x
     }