Repository: rust-analyzer/rust-analyzer
Pull request #8035 (2021-03-15 16:51:39)

unqualfied_path completions aren't responsible for variant pattern completions
bors r+


Patch:
diff --git a/crates/ide_completion/src/completions/pattern.rs b/crates/ide_completion/src/completions/pattern.rs
index 9282c38279..46cef58f01 100644
--- a/crates/ide_completion/src/completions/pattern.rs
+++ b/crates/ide_completion/src/completions/pattern.rs
@@ -11,10 +11,13 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
         return;
     }
 
-    if let Some(ty) = &ctx.expected_type {
-        super::complete_enum_variants(acc, ctx, ty, |acc, ctx, variant, path| {
-            acc.add_qualified_variant_pat(ctx, variant, path)
-        });
+    if !ctx.is_irrefutable_pat_binding {
+        if let Some(ty) = ctx.expected_type.as_ref() {
+            super::complete_enum_variants(acc, ctx, ty, |acc, ctx, variant, path| {
+                acc.add_qualified_variant_pat(ctx, variant, path.clone());
+                acc.add_qualified_enum_variant(ctx, variant, path);
+            });
+        }
     }
 
     // FIXME: ideally, we should look at the type we are matching against and
@@ -85,7 +88,7 @@ static FOO: E = E::X;
 struct Bar { f: u32 }
 
 fn foo() {
-   match E::X { $0 }
+   match E::X { a$0 }
 }
 "#,
             expect![[r#"
@@ -106,10 +109,11 @@ macro_rules! m { ($e:expr) => { $e } }
 enum E { X }
 
 fn foo() {
-   m!(match E::X { $0 })
+   m!(match E::X { a$0 })
 }
 "#,
             expect![[r#"
+                ev E::X  ()
                 en E
                 ma m!(…) macro_rules! m
             "#]],
@@ -129,7 +133,7 @@ static FOO: E = E::X;
 struct Bar { f: u32 }
 
 fn foo() {
-   let $0
+   let a$0
 }
 "#,
             expect![[r#"
@@ -147,7 +151,7 @@ enum E { X }
 static FOO: E = E::X;
 struct Bar { f: u32 }
 
-fn foo($0) {
+fn foo(a$0) {
 }
 "#,
             expect![[r#"
@@ -163,7 +167,7 @@ fn foo($0) {
 struct Bar { f: u32 }
 
 fn foo() {
-   let $0
+   let a$0
 }
 "#,
             expect![[r#"
@@ -179,7 +183,7 @@ fn foo() {
 struct Foo { bar: String, baz: String }
 struct Bar(String, String);
 struct Baz;
-fn outer($0) {}
+fn outer(a$0) {}
 "#,
             expect![[r#"
                 bn Foo Foo { bar$1, baz$2 }: Foo$0
@@ -196,7 +200,7 @@ struct Foo { bar: String, baz: String }
 struct Bar(String, String);
 struct Baz;
 fn outer() {
-    let $0
+    let a$0
 }
 "#,
             expect![[r#"
@@ -215,7 +219,7 @@ struct Bar(String, String);
 struct Baz;
 fn outer() {
     match () {
-        $0
+        a$0
     }
 }
 "#,
@@ -239,7 +243,7 @@ use foo::*;
 
 fn outer() {
     match () {
-        $0
+        a$0
     }
 }
 "#,
@@ -258,7 +262,7 @@ fn outer() {
 struct Foo(i32);
 fn main() {
     match Foo(92) {
-        $0(92) => (),
+        a$0(92) => (),
     }
 }
 "#,
@@ -281,7 +285,7 @@ struct Foo(i32);
 impl Foo {
     fn foo() {
         match () {
-            $0
+            a$0
         }
     }
 }
@@ -314,4 +318,86 @@ impl Foo {
             "#]],
         )
     }
+
+    #[test]
+    fn completes_enum_variant_matcharm() {
+        check(
+            r#"
+enum Foo { Bar, Baz, Quux }
+
+fn main() {
+    let foo = Foo::Quux;
+    match foo { Qu$0 }
+}
+"#,
+            expect![[r#"
+                ev Foo::Bar  ()
+                ev Foo::Baz  ()
+                ev Foo::Quux ()
+                en Foo
+            "#]],
+        )
+    }
+
+    #[test]
+    fn completes_enum_variant_matcharm_ref() {
+        check(
+            r#"
+enum Foo { Bar, Baz, Quux }
+
+fn main() {
+    let foo = Foo::Quux;
+    match &foo { Qu$0 }
+}
+"#,
+            expect![[r#"
+                ev Foo::Bar  ()
+                ev Foo::Baz  ()
+                ev Foo::Quux ()
+                en Foo
+            "#]],
+        )
+    }
+
+    #[test]
+    fn completes_enum_variant_iflet() {
+        check(
+            r#"
+enum Foo { Bar, Baz, Quux }
+
+fn main() {
+    let foo = Foo::Quux;
+    if let Qu$0 = foo { }
+}
+"#,
+            expect![[r#"
+                ev Foo::Bar  ()
+                ev Foo::Baz  ()
+                ev Foo::Quux ()
+                en Foo
+            "#]],
+        )
+    }
+
+    #[test]
+    fn completes_enum_variant_impl() {
+        check(
+            r#"
+enum Foo { Bar, Baz, Quux }
+impl Foo {
+    fn foo() { match Foo::Bar { Q$0 } }
+}
+"#,
+            expect![[r#"
+                ev Self::Bar  ()
+                ev Self::Baz  ()
+                ev Self::Quux ()
+                ev Foo::Bar   ()
+                ev Foo::Baz   ()
+                ev Foo::Quux  ()
+                sp Self
+                en Foo
+            "#]],
+        )
+    }
 }
diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs
index 044dfd1602..5ef80f6a72 100644
--- a/crates/ide_completion/src/completions/unqualified_path.rs
+++ b/crates/ide_completion/src/completions/unqualified_path.rs
@@ -6,7 +6,7 @@ use syntax::AstNode;
 use crate::{CompletionContext, Completions};
 
 pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) {
-    if !(ctx.is_trivial_path || ctx.is_pat_binding_or_const) {
+    if !ctx.is_trivial_path {
         return;
     }
     if ctx.record_lit_syntax.is_some()
@@ -23,10 +23,6 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
         });
     }
 
-    if ctx.is_pat_binding_or_const {
-        return;
-    }
-
     ctx.scope.process_all_names(&mut |name, res| {
         if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
             cov_mark::hit!(skip_lifetime_completion);
@@ -608,66 +604,6 @@ fn main() { $0 }
         );
     }
 
-    #[test]
-    fn completes_enum_variant_matcharm() {
-        check(
-            r#"
-enum Foo { Bar, Baz, Quux }
-
-fn main() {
-    let foo = Foo::Quux;
-    match foo { Qu$0 }
-}
-"#,
-            expect![[r#"
-                ev Foo::Bar  ()
-                ev Foo::Baz  ()
-                ev Foo::Quux ()
-                en Foo
-            "#]],
-        )
-    }
-
-    #[test]
-    fn completes_enum_variant_matcharm_ref() {
-        check(
-            r#"
-enum Foo { Bar, Baz, Quux }
-
-fn main() {
-    let foo = Foo::Quux;
-    match &foo { Qu$0 }
-}
-"#,
-            expect![[r#"
-                ev Foo::Bar  ()
-                ev Foo::Baz  ()
-                ev Foo::Quux ()
-                en Foo
-            "#]],
-        )
-    }
-
-    #[test]
-    fn completes_enum_variant_iflet() {
-        check(
-            r#"
-enum Foo { Bar, Baz, Quux }
-
-fn main() {
-    let foo = Foo::Quux;
-    if let Qu$0 = foo { }
-}
-"#,
-            expect![[r#"
-                ev Foo::Bar  ()
-                ev Foo::Baz  ()
-                ev Foo::Quux ()
-                en Foo
-            "#]],
-        )
-    }
-
     #[test]
     fn completes_enum_variant_basic_expr() {
         check(
@@ -700,28 +636,6 @@ fn f() -> m::E { V$0 }
         )
     }
 
-    #[test]
-    fn completes_enum_variant_impl() {
-        check(
-            r#"
-enum Foo { Bar, Baz, Quux }
-impl Foo {
-    fn foo() { match Foo::Bar { Q$0 } }
-}
-"#,
-            expect![[r#"
-                ev Self::Bar  ()
-                ev Self::Baz  ()
-                ev Self::Quux ()
-                ev Foo::Bar   ()
-                ev Foo::Baz   ()
-                ev Foo::Quux  ()
-                sp Self
-                en Foo
-            "#]],
-        )
-    }
-
     #[test]
     fn dont_complete_attr() {
         check(
diff --git a/crates/ide_completion/src/render.rs b/crates/ide_completion/src/render.rs
index 2514dda7c7..670563e509 100644
--- a/crates/ide_completion/src/render.rs
+++ b/crates/ide_completion/src/render.rs
@@ -927,66 +927,6 @@ fn f(foo: &Foo) { f(foo, w$0) }
         );
     }
 
-    #[test]
-    fn score_fn_type_and_name_match() {
-        check_relevance(
-            r#"
-struct A { bar: u8 }
-fn baz() -> u8 { 0 }
-fn bar() -> u8 { 0 }
-fn f() { A { bar: b$0 }; }
-"#,
-            expect![[r#"
-                fn baz() [type]
-                st A []
-                fn bar() [type+name]
-                fn f() []
-            "#]],
-        );
-    }
-
-    #[test]
-    fn score_method_type_and_name_match() {
-        check_relevance(
-            r#"
-fn baz(aaa: u32){}
-struct Foo;
-impl Foo {
-fn aaa(&self) -> u32 { 0 }
-fn bbb(&self) -> u32 { 0 }
-fn ccc(&self) -> u64 { 0 }
-}
-fn f() {
-    baz(Foo.$0
-}
-"#,
-            expect![[r#"
-                me aaa() [type+name]
-                me bbb() [type]
-                me ccc() []
-            "#]],
-        );
-    }
-
-    #[test]
-    fn score_method_name_match_only() {
-        check_relevance(
-            r#"
-fn baz(aaa: u32){}
-struct Foo;
-impl Foo {
-fn aaa(&self) -> u64 { 0 }
-}
-fn f() {
-    baz(Foo.$0
-}
-"#,
-            expect![[r#"
-                me aaa() [name]
-            "#]],
-        );
-    }
-
     #[test]
     fn suggest_ref_mut() {
         cov_mark::check!(suggest_ref);
diff --git a/crates/ide_completion/src/render/function.rs b/crates/ide_completion/src/render/function.rs
index 47e26a5d8d..f4dabe3d10 100644
--- a/crates/ide_completion/src/render/function.rs
+++ b/crates/ide_completion/src/render/function.rs
@@ -5,7 +5,7 @@ use ide_db::SymbolKind;
 use syntax::ast::Fn;
 
 use crate::{
-    item::{CompletionItem, CompletionItemKind, CompletionKind, CompletionRelevance, ImportEdit},
+    item::{CompletionItem, CompletionItemKind, CompletionKind, ImportEdit},
     render::{builder_ext::Params, RenderContext},
 };
 
@@ -55,21 +55,6 @@ impl<'a> FunctionRender<'a> {
             .add_call_parens(self.ctx.completion, self.name, params)
             .add_import(import_to_add);
 
-        let mut relevance = CompletionRelevance::default();
-        if let Some(expected_type) = &self.ctx.completion.expected_type {
-            let ret_ty = self.func.ret_type(self.ctx.db());
-
-            // We don't ever consider a function which returns unit type to be an
-            // exact type match, since nearly always this is not meaningful to the
-            // user.
-            relevance.exact_type_match = &ret_ty == expected_type && !ret_ty.is_unit();
-        }
-        if let Some(expected_name) = &self.ctx.completion.expected_name {
-            relevance.exact_name_match =
-                expected_name == &self.func.name(self.ctx.db()).to_string();
-        }
-        item.set_relevance(relevance);
-
         item.build()
     }
 
diff --git a/crates/ide_completion/src/render/pattern.rs b/crates/ide_completion/src/render/pattern.rs
index ca29261254..b4e80f424a 100644
--- a/crates/ide_completion/src/render/pattern.rs
+++ b/crates/ide_completion/src/render/pattern.rs
@@ -6,24 +6,6 @@ use itertools::Itertools;
 
 use crate::{item::CompletionKind, render::RenderContext, CompletionItem, CompletionItemKind};
 
-fn visible_fields(
-    ctx: &RenderContext<'_>,
-    fields: &[hir::Field],
-    item: impl HasAttrs,
-) -> Option<(Vec<hir::Field>, bool)> {
-    let module = ctx.completion.scope.module()?;
-    let n_fields = fields.len();
-    let fields = fields
-        .into_iter()
-        .filter(|field| field.is_visible_from(ctx.db(), module))
-        .copied()
-        .collect::<Vec<_>>();
-
-    let fields_omitted =
-        n_fields - fields.len() > 0 || item.attrs(ctx.db()).by_key("non_exhaustive").exists();
-    Some((fields, fields_omitted))
-}
-
 pub(crate) fn render_struct_pat(
     ctx: RenderContext<'_>,
     strukt: hir::Struct,
@@ -148,3 +130,21 @@ fn render_tuple_as_pat(fields: &[hir::Field], name: &str, fields_omitted: bool)
         name = name
     )
 }
+
+fn visible_fields(
+    ctx: &RenderContext<'_>,
+    fields: &[hir::Field],
+    item: impl HasAttrs,
+) -> Option<(Vec<hir::Field>, bool)> {
+    let module = ctx.completion.scope.module()?;
+    let n_fields = fields.len();
+    let fields = fields
+        .into_iter()
+        .filter(|field| field.is_visible_from(ctx.db(), module))
+        .copied()
+        .collect::<Vec<_>>();
+
+    let fields_omitted =
+        n_fields - fields.len() > 0 || item.attrs(ctx.db()).by_key("non_exhaustive").exists();
+    Some((fields, fields_omitted))
+}
diff --git a/crates/proc_macro_api/src/lib.rs b/crates/proc_macro_api/src/lib.rs
index 2dd2a85411..941d0fe9e2 100644
--- a/crates/proc_macro_api/src/lib.rs
+++ b/crates/proc_macro_api/src/lib.rs
@@ -23,7 +23,6 @@ use tt::{SmolStr, Subtree};
 use crate::process::{ProcMacroProcessSrv, ProcMacroProcessThread};
 
 pub use rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind};
-pub use version::{read_dylib_info, RustCInfo};
 
 #[derive(Debug, Clone)]
 struct ProcMacroProcessExpander {
@@ -77,7 +76,7 @@ impl ProcMacroClient {
     }
 
     pub fn by_dylib_path(&self, dylib_path: &Path) -> Vec<ProcMacro> {
-        match version::read_dylib_info(dylib_path) {
+        match version::read_info(dylib_path) {
             Ok(info) => {
                 if info.version.0 < 1 || info.version.1 < 47 {
                     eprintln!("proc-macro {} built by {:#?} is not supported by Rust Analyzer, please update your rust version.", dylib_path.to_string_lossy(), info);
diff --git a/crates/proc_macro_api/src/version.rs b/crates/proc_macro_api/src/version.rs
index dcf8fae8f5..b903658fb9 100644
--- a/crates/proc_macro_api/src/version.rs
+++ b/crates/proc_macro_api/src/version.rs
@@ -11,15 +11,14 @@ use object::read::{File as BinaryFile, Object, ObjectSection};
 use snap::read::FrameDecoder as SnapDecoder;
 
 #[derive(Debug)]
-pub struct RustCInfo {
-    pub version: (usize, usize, usize),
-    pub channel: String,
-    pub commit: String,
-    pub date: String,
+pub(crate) struct RustCInfo {
+    pub(crate) version: (usize, usize, usize),
+    pub(crate) channel: String,
+    pub(crate) commit: String,
+    pub(crate) date: String,
 }
 
-/// Read rustc dylib information
-pub fn read_dylib_info(dylib_path: &Path) -> io::Result<RustCInfo> {
+pub(crate) fn read_info(dylib_path: &Path) -> io::Result<RustCInfo> {
     macro_rules! err {
         ($e:literal) => {
             io::Error::new(io::ErrorKind::InvalidData, $e)
diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs
index 8f541976ec..8af7871ac4 100644
--- a/crates/rust-analyzer/src/config.rs
+++ b/crates/rust-analyzer/src/config.rs
@@ -172,7 +172,7 @@ config_data! {
         notifications_cargoTomlNotFound: bool      = "true",
 
         /// Enable support for procedural macros, implies `#rust-analyzer.cargo.runBuildScripts#`.
-        procMacro_enable: bool                     = "true",
+        procMacro_enable: bool                     = "false",
         /// Internal config, path to proc-macro server executable (typically,
         /// this is rust-analyzer itself, but we override this in tests).
         procMacro_server: Option<PathBuf>          = "null",
diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc
index c2521289c0..042ba2d54c 100644
--- a/docs/user/generated_config.adoc
+++ b/docs/user/generated_config.adoc
@@ -276,7 +276,7 @@ Number of syntax trees rust-analyzer keeps in memory. Defaults to 128.
 --
 Whether to show `can't find Cargo.toml` error message.
 --
-[[rust-analyzer.procMacro.enable]]rust-analyzer.procMacro.enable (default: `true`)::
+[[rust-analyzer.procMacro.enable]]rust-analyzer.procMacro.enable (default: `false`)::
 +
 --
 Enable support for procedural macros, implies `#rust-analyzer.cargo.runBuildScripts#`.
diff --git a/editors/code/package.json b/editors/code/package.json
index a2ed9b2d58..923e9b35a0 100644
--- a/editors/code/package.json
+++ b/editors/code/package.json
@@ -690,7 +690,7 @@
                 },
                 "rust-analyzer.procMacro.enable": {
                     "markdownDescription": "Enable support for procedural macros, implies `#rust-analyzer.cargo.runBuildScripts#`.",
-                    "default": true,
+                    "default": false,
                     "type": "boolean"
                 },
                 "rust-analyzer.procMacro.server": {


Test patch:
diff --git a/crates/proc_macro_srv/src/tests/mod.rs b/crates/proc_macro_srv/src/tests/mod.rs
index b4ab4c0776..fd54f8dfda 100644
--- a/crates/proc_macro_srv/src/tests/mod.rs
+++ b/crates/proc_macro_srv/src/tests/mod.rs
@@ -56,10 +56,3 @@ DummyTrait [CustomDerive]"#,
         &res
     );
 }
-
-#[test]
-fn test_version_check() {
-    let path = fixtures::dylib_path("proc_macro_test", "0.0.0");
-    let info = proc_macro_api::read_dylib_info(&path).unwrap();
-    assert!(info.version.1 >= 50);
-}
diff --git a/crates/proc_macro_srv/src/tests/utils.rs b/crates/proc_macro_srv/src/tests/utils.rs
index f15381f0f4..0484c3af44 100644
--- a/crates/proc_macro_srv/src/tests/utils.rs
+++ b/crates/proc_macro_srv/src/tests/utils.rs
@@ -6,7 +6,7 @@ use proc_macro_api::ListMacrosTask;
 use std::str::FromStr;
 use test_utils::assert_eq_text;
 
-pub mod fixtures {
+mod fixtures {
     use cargo_metadata::Message;
     use std::path::PathBuf;
     use std::process::Command;