Repository: rust-analyzer/rust-analyzer
Pull request #5311 (2020-07-11 10:46:10)

Fix goto definition for type alias type parameters
closes https://github.com/rust-analyzer/rust-analyzer/issues/5042



bors r+
🤖


Patch:
diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs
index 97125b32a3..155b666d7f 100644
--- a/crates/ra_hir/src/semantics.rs
+++ b/crates/ra_hir/src/semantics.rs
@@ -485,6 +485,7 @@ impl<'db> SemanticsImpl<'db> {
             ChildContainer::ModuleId(it) => it.resolver(self.db.upcast()),
             ChildContainer::EnumId(it) => it.resolver(self.db.upcast()),
             ChildContainer::VariantId(it) => it.resolver(self.db.upcast()),
+            ChildContainer::TypeAliasId(it) => it.resolver(self.db.upcast()),
             ChildContainer::GenericDefId(it) => it.resolver(self.db.upcast()),
         };
         SourceAnalyzer::new_for_resolver(resolver, src)
diff --git a/crates/ra_hir/src/semantics/source_to_def.rs b/crates/ra_hir/src/semantics/source_to_def.rs
index 8af64fdc1b..0e1d92fb39 100644
--- a/crates/ra_hir/src/semantics/source_to_def.rs
+++ b/crates/ra_hir/src/semantics/source_to_def.rs
@@ -194,6 +194,10 @@ impl SourceToDefCtx<'_, '_> {
                         let def = self.const_to_def(container.with_value(it))?;
                         DefWithBodyId::from(def).into()
                     },
+                    ast::TypeAliasDef(it) => {
+                        let def = self.type_alias_to_def(container.with_value(it))?;
+                        def.into()
+                    },
                     _ => continue,
                 }
             };
@@ -246,6 +250,7 @@ pub(crate) enum ChildContainer {
     ImplId(ImplId),
     EnumId(EnumId),
     VariantId(VariantId),
+    TypeAliasId(TypeAliasId),
     /// XXX: this might be the same def as, for example an `EnumId`. However,
     /// here the children generic parameters, and not, eg enum variants.
     GenericDefId(GenericDefId),
@@ -258,6 +263,7 @@ impl_froms! {
     ImplId,
     EnumId,
     VariantId,
+    TypeAliasId,
     GenericDefId
 }
 
@@ -271,6 +277,7 @@ impl ChildContainer {
             ChildContainer::ImplId(it) => it.child_by_source(db),
             ChildContainer::EnumId(it) => it.child_by_source(db),
             ChildContainer::VariantId(it) => it.child_by_source(db),
+            ChildContainer::TypeAliasId(_) => DynMap::default(),
             ChildContainer::GenericDefId(it) => it.child_by_source(db),
         }
     }
diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs
index 8fc33d0311..f575d738fe 100644
--- a/crates/ra_ide/src/goto_definition.rs
+++ b/crates/ra_ide/src/goto_definition.rs
@@ -856,4 +856,14 @@ impl Foo {
 "#,
         );
     }
+
+    #[test]
+    fn goto_def_for_type_alias_generic_parameter() {
+        check(
+            r#"
+type Alias<T> = T<|>;
+         //^
+"#,
+        )
+    }
 }
diff --git a/crates/rust-analyzer/src/diagnostics/to_proto.rs b/crates/rust-analyzer/src/diagnostics/to_proto.rs
index 1b1e670c1b..b1b222deb8 100644
--- a/crates/rust-analyzer/src/diagnostics/to_proto.rs
+++ b/crates/rust-analyzer/src/diagnostics/to_proto.rs
@@ -118,6 +118,7 @@ fn map_rust_child_diagnostic(
                 changes: Some(edit_map),
                 document_changes: None,
             }),
+            command: None,
         })
     }
 }
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs
index d39dceae04..8ce6e1c711 100644
--- a/crates/rust-analyzer/src/handlers.rs
+++ b/crates/rust-analyzer/src/handlers.rs
@@ -762,6 +762,7 @@ fn handle_fixes(
             group: None,
             kind: Some(lsp_types::code_action_kind::QUICKFIX.into()),
             edit: Some(edit),
+            command: None,
         };
         res.push(action);
     }
diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs
index a3e12c046b..ba8a0231fa 100644
--- a/crates/rust-analyzer/src/lsp_ext.rs
+++ b/crates/rust-analyzer/src/lsp_ext.rs
@@ -249,9 +249,8 @@ pub struct CodeAction {
     pub group: Option<String>,
     #[serde(skip_serializing_if = "Option::is_none")]
     pub kind: Option<String>,
-    // We don't handle commands on the client-side
-    // #[serde(skip_serializing_if = "Option::is_none")]
-    // pub command: Option<lsp_types::Command>,
+    #[serde(skip_serializing_if = "Option::is_none")]
+    pub command: Option<lsp_types::Command>,
     #[serde(skip_serializing_if = "Option::is_none")]
     pub edit: Option<SnippetWorkspaceEdit>,
 }
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs
index f6dff16844..263f58a00e 100644
--- a/crates/rust-analyzer/src/to_proto.rs
+++ b/crates/rust-analyzer/src/to_proto.rs
@@ -651,6 +651,7 @@ pub(crate) fn unresolved_code_action(
         group: assist.group.filter(|_| snap.config.client_caps.code_action_group).map(|gr| gr.0),
         kind: Some(code_action_kind(assist.id.1)),
         edit: None,
+        command: None,
     };
     Ok(res)
 }


Test patch:
diff --git a/crates/rust-analyzer/test_data/rustc_unused_variable.txt b/crates/rust-analyzer/test_data/rustc_unused_variable.txt
index 92aea088b9..81f180a861 100644
--- a/crates/rust-analyzer/test_data/rustc_unused_variable.txt
+++ b/crates/rust-analyzer/test_data/rustc_unused_variable.txt
@@ -39,6 +39,7 @@
                 kind: Some(
                     "quickfix",
                 ),
+                command: None,
                 edit: Some(
                     SnippetWorkspaceEdit {
                         changes: Some(
diff --git a/crates/rust-analyzer/test_data/rustc_unused_variable_as_hint.txt b/crates/rust-analyzer/test_data/rustc_unused_variable_as_hint.txt
index 80cf0d8a54..d5d78fd800 100644
--- a/crates/rust-analyzer/test_data/rustc_unused_variable_as_hint.txt
+++ b/crates/rust-analyzer/test_data/rustc_unused_variable_as_hint.txt
@@ -39,6 +39,7 @@
                 kind: Some(
                     "quickfix",
                 ),
+                command: None,
                 edit: Some(
                     SnippetWorkspaceEdit {
                         changes: Some(
diff --git a/crates/rust-analyzer/test_data/rustc_unused_variable_as_info.txt b/crates/rust-analyzer/test_data/rustc_unused_variable_as_info.txt
index afe3cb6d8a..2a7505c59f 100644
--- a/crates/rust-analyzer/test_data/rustc_unused_variable_as_info.txt
+++ b/crates/rust-analyzer/test_data/rustc_unused_variable_as_info.txt
@@ -39,6 +39,7 @@
                 kind: Some(
                     "quickfix",
                 ),
+                command: None,
                 edit: Some(
                     SnippetWorkspaceEdit {
                         changes: Some(
diff --git a/crates/rust-analyzer/test_data/snap_multi_line_fix.txt b/crates/rust-analyzer/test_data/snap_multi_line_fix.txt
index 7814d35435..6eca3ae2f2 100644
--- a/crates/rust-analyzer/test_data/snap_multi_line_fix.txt
+++ b/crates/rust-analyzer/test_data/snap_multi_line_fix.txt
@@ -54,6 +54,7 @@
                 kind: Some(
                     "quickfix",
                 ),
+                command: None,
                 edit: Some(
                     SnippetWorkspaceEdit {
                         changes: Some(