9181: Don't complete values in type position r=jonas-schievink a=Veykril

Will add some proper tests in a bit

9182: fix: don't complete derive macros as fn-like macros r=jonas-schievink a=jonas-schievink

Part of https://github.com/rust-analyzer/rust-analyzer/issues/8518

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
This commit is contained in:
bors[bot] 2021-06-08 19:09:13 +00:00 committed by GitHub
commit b6199de706
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 219 additions and 59 deletions

View File

@ -1351,6 +1351,13 @@ pub fn kind(&self) -> MacroKind {
MacroDefKind::ProcMacro(_, base_db::ProcMacroKind::FuncLike, _) => MacroKind::ProcMacro,
}
}
pub fn is_fn_like(&self) -> bool {
match self.kind() {
MacroKind::Declarative | MacroKind::BuiltIn | MacroKind::ProcMacro => true,
MacroKind::Attr | MacroKind::Derive => false,
}
}
}
/// Invariant: `inner.as_assoc_item(db).is_some()`
@ -2496,6 +2503,18 @@ pub fn all_items(def: PerNs) -> ArrayVec<Self, 3> {
items
}
pub fn is_value_def(&self) -> bool {
matches!(
self,
ScopeDef::ModuleDef(ModuleDef::Function(_))
| ScopeDef::ModuleDef(ModuleDef::Variant(_))
| ScopeDef::ModuleDef(ModuleDef::Const(_))
| ScopeDef::ModuleDef(ModuleDef::Static(_))
| ScopeDef::GenericParam(GenericParam::ConstParam(_))
| ScopeDef::Local(_)
)
}
}
impl From<ItemInNs> for ScopeDef {

View File

@ -35,8 +35,9 @@ pub enum PathResolution {
Def(ModuleDef),
/// A local binding (only value namespace)
Local(Local),
/// A generic parameter
/// A type parameter
TypeParam(TypeParam),
/// A const parameter
ConstParam(ConstParam),
SelfType(Impl),
Macro(MacroDef),

View File

@ -56,10 +56,16 @@ pub(crate) fn add_to(self, acc: &mut Completions) {
}
impl Completions {
pub(crate) fn add(&mut self, item: CompletionItem) {
fn add(&mut self, item: CompletionItem) {
self.buf.push(item)
}
fn add_opt(&mut self, item: Option<CompletionItem>) {
if let Some(item) = item {
self.buf.push(item)
}
}
pub(crate) fn add_all<I>(&mut self, items: I)
where
I: IntoIterator,
@ -103,9 +109,10 @@ pub(crate) fn add_resolution(
local_name: hir::Name,
resolution: &hir::ScopeDef,
) {
if let Some(item) = render_resolution(RenderContext::new(ctx), local_name, resolution) {
self.add(item);
if ctx.expects_type() && resolution.is_value_def() {
return;
}
self.add_opt(render_resolution(RenderContext::new(ctx), local_name, resolution));
}
pub(crate) fn add_macro(
@ -118,9 +125,7 @@ pub(crate) fn add_macro(
Some(it) => it,
None => return,
};
if let Some(item) = render_macro(RenderContext::new(ctx), None, name, macro_) {
self.add(item);
}
self.add_opt(render_macro(RenderContext::new(ctx), None, name, macro_));
}
pub(crate) fn add_function(
@ -129,9 +134,10 @@ pub(crate) fn add_function(
func: hir::Function,
local_name: Option<hir::Name>,
) {
if let Some(item) = render_fn(RenderContext::new(ctx), None, local_name, func) {
self.add(item)
if ctx.expects_type() {
return;
}
self.add_opt(render_fn(RenderContext::new(ctx), None, local_name, func));
}
pub(crate) fn add_method(
@ -141,10 +147,7 @@ pub(crate) fn add_method(
receiver: Option<hir::Name>,
local_name: Option<hir::Name>,
) {
if let Some(item) = render_method(RenderContext::new(ctx), None, receiver, local_name, func)
{
self.add(item)
}
self.add_opt(render_method(RenderContext::new(ctx), None, receiver, local_name, func));
}
pub(crate) fn add_variant_pat(
@ -153,9 +156,7 @@ pub(crate) fn add_variant_pat(
variant: hir::Variant,
local_name: Option<hir::Name>,
) {
if let Some(item) = render_variant_pat(RenderContext::new(ctx), variant, local_name, None) {
self.add(item);
}
self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, local_name, None));
}
pub(crate) fn add_qualified_variant_pat(
@ -164,9 +165,7 @@ pub(crate) fn add_qualified_variant_pat(
variant: hir::Variant,
path: hir::ModPath,
) {
if let Some(item) = render_variant_pat(RenderContext::new(ctx), variant, None, Some(path)) {
self.add(item);
}
self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, None, Some(path)));
}
pub(crate) fn add_struct_pat(
@ -175,21 +174,18 @@ pub(crate) fn add_struct_pat(
strukt: hir::Struct,
local_name: Option<hir::Name>,
) {
if let Some(item) = render_struct_pat(RenderContext::new(ctx), strukt, local_name) {
self.add(item);
}
self.add_opt(render_struct_pat(RenderContext::new(ctx), strukt, local_name));
}
pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
if let Some(item) = render_const(RenderContext::new(ctx), constant) {
self.add(item);
if ctx.expects_type() {
return;
}
self.add_opt(render_const(RenderContext::new(ctx), constant));
}
pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
if let Some(item) = render_type_alias(RenderContext::new(ctx), type_alias) {
self.add(item)
}
self.add_opt(render_type_alias(RenderContext::new(ctx), type_alias));
}
pub(crate) fn add_qualified_enum_variant(
@ -208,6 +204,9 @@ pub(crate) fn add_enum_variant(
variant: hir::Variant,
local_name: Option<hir::Name>,
) {
if ctx.expects_type() {
return;
}
let item = render_variant(RenderContext::new(ctx), None, local_name, variant, None);
self.add(item);
}

View File

@ -69,7 +69,7 @@ fn complete_new_attribute(acc: &mut Completions, ctx: &CompletionContext, attrib
}
if is_inner || !attr_completion.prefer_inner {
acc.add(item.build());
item.add_to(acc);
}
};
@ -96,7 +96,7 @@ fn complete_new_attribute(acc: &mut Completions, ctx: &CompletionContext, attrib
if let Some(docs) = mac.docs(ctx.sema.db) {
item.documentation(docs);
}
acc.add(item.build());
item.add_to(acc);
}
}
});

View File

@ -90,7 +90,6 @@
//! Note that having this flag set to `true` does not guarantee that the feature is enabled: your client needs to have the corredponding
//! capability enabled.
use hir::ModPath;
use ide_db::helpers::{
import_assets::{ImportAssets, ImportCandidate},
insert_use::ImportScope,
@ -208,7 +207,7 @@ fn import_assets(ctx: &CompletionContext, fuzzy_name: String) -> Option<ImportAs
}
fn compute_fuzzy_completion_order_key(
proposed_mod_path: &ModPath,
proposed_mod_path: &hir::ModPath,
user_input_lowercased: &str,
) -> usize {
cov_mark::hit!(certain_fuzzy_order_test);

View File

@ -39,7 +39,7 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
| hir::ModuleDef::Module(..) => refutable,
_ => false,
},
hir::ScopeDef::MacroDef(_) => true,
hir::ScopeDef::MacroDef(mac) => mac.is_fn_like(),
hir::ScopeDef::ImplSelfType(impl_) => match impl_.self_ty(ctx.db).as_adt() {
Some(hir::Adt::Struct(strukt)) => {
acc.add_struct_pat(ctx, strukt, Some(name.clone()));
@ -101,6 +101,28 @@ fn foo() {
);
}
#[test]
fn does_not_complete_non_fn_macros() {
check(
r#"
macro_rules! m { ($e:expr) => { $e } }
enum E { X }
#[rustc_builtin_macro]
macro Clone {}
fn foo() {
match E::X { $0 }
}
"#,
expect![[r#"
ev E::X ()
en E
ma m!() macro_rules! m
"#]],
);
}
#[test]
fn completes_in_simple_macro_call() {
check(

View File

@ -26,7 +26,9 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
let module_scope = module.scope(ctx.db, context_module);
for (name, def) in module_scope {
if let hir::ScopeDef::MacroDef(macro_def) = def {
acc.add_macro(ctx, Some(name.clone()), macro_def);
if macro_def.is_fn_like() {
acc.add_macro(ctx, Some(name.clone()), macro_def);
}
}
if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = def {
acc.add_resolution(ctx, name, &def);
@ -58,6 +60,13 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
}
}
if let hir::ScopeDef::MacroDef(macro_def) = def {
if !macro_def.is_fn_like() {
// Don't suggest attribute macros and derives.
continue;
}
}
acc.add_resolution(ctx, name, &def);
}
}
@ -198,6 +207,36 @@ fn dont_complete_current_use() {
check(r#"use self::foo$0;"#, expect![[""]]);
}
#[test]
fn dont_complete_values_in_type_pos() {
check(
r#"
const FOO: () = ();
static BAR: () = ();
struct Baz;
fn foo() {
let _: self::$0;
}
"#,
expect![[r#"
st Baz
"#]],
);
}
#[test]
fn dont_complete_enum_variants_in_type_pos() {
check(
r#"
enum Foo { Bar }
fn foo() {
let _: Foo::$0;
}
"#,
expect![[r#""#]],
);
}
#[test]
fn dont_complete_current_use_in_braces_with_glob() {
check(

View File

@ -13,7 +13,9 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
// only show macros in {Assoc}ItemList
ctx.scope.process_all_names(&mut |name, res| {
if let hir::ScopeDef::MacroDef(mac) = res {
acc.add_macro(ctx, Some(name.clone()), mac);
if mac.is_fn_like() {
acc.add_macro(ctx, Some(name.clone()), mac);
}
}
if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res {
acc.add_resolution(ctx, name, &res);
@ -46,7 +48,13 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
cov_mark::hit!(skip_lifetime_completion);
return;
}
acc.add_resolution(ctx, name, &res);
let add_resolution = match res {
ScopeDef::MacroDef(mac) => mac.is_fn_like(),
_ => true,
};
if add_resolution {
acc.add_resolution(ctx, name, &res);
}
});
}
@ -68,6 +76,28 @@ fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect)
expect.assert_eq(&actual)
}
#[test]
fn dont_complete_values_in_type_pos() {
check(
r#"
const FOO: () = ();
static BAR: () = ();
enum Foo {
Bar
}
struct Baz;
fn foo() {
let local = ();
let _: $0;
}
"#,
expect![[r#"
en Foo
st Baz
"#]],
);
}
#[test]
fn only_completes_modules_in_import() {
cov_mark::check!(only_completes_modules_in_import);
@ -339,7 +369,6 @@ fn x() -> $0
"#,
expect![[r#"
st Foo
fn x() fn()
"#]],
);
}
@ -391,7 +420,6 @@ pub mod rust_2018 {
}
"#,
expect![[r#"
fn foo() fn()
md std
st Option
"#]],
@ -426,6 +454,44 @@ fn f() fn()
);
}
#[test]
fn does_not_complete_non_fn_macros() {
check(
r#"
#[rustc_builtin_macro]
pub macro Clone {}
fn f() {$0}
"#,
expect![[r#"
fn f() fn()
"#]],
);
check(
r#"
#[rustc_builtin_macro]
pub macro Clone {}
struct S;
impl S {
$0
}
"#,
expect![[r#""#]],
);
check(
r#"
mod m {
#[rustc_builtin_macro]
pub macro Clone {}
}
fn f() {m::$0}
"#,
expect![[r#""#]],
);
}
#[test]
fn completes_std_prelude_if_core_is_defined() {
check(
@ -448,7 +514,6 @@ pub mod rust_2018 {
}
"#,
expect![[r#"
fn foo() fn()
md std
md core
st String
@ -509,7 +574,6 @@ macro_rules! foo { () => {} }
fn main() { let x: $0 }
"#,
expect![[r#"
fn main() fn()
ma foo!() macro_rules! foo
"#]],
);

View File

@ -29,6 +29,12 @@ pub(crate) enum PatternRefutability {
Irrefutable,
}
#[derive(Debug)]
pub(super) enum PathKind {
Expr,
Type,
}
#[derive(Debug)]
pub(crate) struct PathCompletionContext {
/// If this is a call with () already there
@ -36,13 +42,12 @@ pub(crate) struct PathCompletionContext {
/// A single-indent path, like `foo`. `::foo` should not be considered a trivial path.
pub(super) is_trivial_path: bool,
/// If not a trivial path, the prefix (qualifier).
pub(super) path_qual: Option<ast::Path>,
pub(super) is_path_type: bool,
pub(super) qualifier: Option<ast::Path>,
pub(super) kind: Option<PathKind>,
/// Whether the path segment has type args or not.
pub(super) has_type_args: bool,
/// `true` if we are a statement or a last expr in the block.
pub(super) can_be_stmt: bool,
/// `true` if we expect an expression at the cursor position.
pub(super) is_expr: bool,
pub(super) in_loop_body: bool,
}
@ -308,7 +313,11 @@ pub(crate) fn is_path_disallowed(&self) -> bool {
}
pub(crate) fn expects_expression(&self) -> bool {
self.path_context.as_ref().map_or(false, |it| it.is_expr)
matches!(self.path_context, Some(PathCompletionContext { kind: Some(PathKind::Expr), .. }))
}
pub(crate) fn expects_type(&self) -> bool {
matches!(self.path_context, Some(PathCompletionContext { kind: Some(PathKind::Type), .. }))
}
pub(crate) fn path_call_kind(&self) -> Option<CallKind> {
@ -316,11 +325,11 @@ pub(crate) fn path_call_kind(&self) -> Option<CallKind> {
}
pub(crate) fn is_trivial_path(&self) -> bool {
self.path_context.as_ref().map_or(false, |it| it.is_trivial_path)
matches!(self.path_context, Some(PathCompletionContext { is_trivial_path: true, .. }))
}
pub(crate) fn path_qual(&self) -> Option<&ast::Path> {
self.path_context.as_ref().and_then(|it| it.path_qual.as_ref())
self.path_context.as_ref().and_then(|it| it.qualifier.as_ref())
}
fn fill_impl_def(&mut self) {
@ -573,12 +582,11 @@ fn classify_name_ref(&mut self, original_file: &SyntaxNode, name_ref: ast::NameR
let path_ctx = self.path_context.get_or_insert(PathCompletionContext {
call_kind: None,
is_trivial_path: false,
path_qual: None,
qualifier: None,
has_type_args: false,
is_path_type: false,
can_be_stmt: false,
is_expr: false,
in_loop_body: false,
kind: None,
});
path_ctx.in_loop_body = is_in_loop_body(name_ref.syntax());
let path = segment.parent_path();
@ -593,11 +601,20 @@ fn classify_name_ref(&mut self, original_file: &SyntaxNode, name_ref: ast::NameR
}
};
}
path_ctx.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some();
if let Some(parent) = path.syntax().parent() {
path_ctx.kind = match_ast! {
match parent {
ast::PathType(_it) => Some(PathKind::Type),
ast::PathExpr(_it) => Some(PathKind::Expr),
_ => None,
}
};
}
path_ctx.has_type_args = segment.generic_arg_list().is_some();
if let Some(path) = path_or_use_tree_qualifier(&path) {
path_ctx.path_qual = path
path_ctx.qualifier = path
.segment()
.and_then(|it| {
find_node_with_range::<ast::PathSegment>(
@ -635,7 +652,6 @@ fn classify_name_ref(&mut self, original_file: &SyntaxNode, name_ref: ast::NameR
None
})
.unwrap_or(false);
path_ctx.is_expr = path.syntax().parent().and_then(ast::PathExpr::cast).is_some();
}
}
}

View File

@ -18,6 +18,7 @@
use syntax::TextRange;
use crate::{
context::{PathCompletionContext, PathKind},
item::{CompletionRelevanceTypeMatch, ImportEdit},
render::{enum_variant::render_variant, function::render_fn, macro_::render_macro},
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, CompletionRelevance,
@ -54,6 +55,9 @@ pub(crate) fn render_resolution_with_import<'a>(
import_edit: ImportEdit,
) -> Option<CompletionItem> {
let resolution = hir::ScopeDef::from(import_edit.import.original_item);
if ctx.completion.expects_type() && resolution.is_value_def() {
return None;
}
let local_name = match resolution {
hir::ScopeDef::ModuleDef(hir::ModuleDef::Function(f)) => f.name(ctx.completion.db),
hir::ScopeDef::ModuleDef(hir::ModuleDef::Const(c)) => c.name(ctx.completion.db)?,
@ -275,13 +279,10 @@ fn render_resolution(
};
// Add `<>` for generic types
if self
.ctx
.completion
.path_context
.as_ref()
.map_or(false, |it| it.is_path_type && !it.has_type_args)
&& self.ctx.completion.config.add_call_parenthesis
if matches!(
self.ctx.completion.path_context,
Some(PathCompletionContext { kind: Some(PathKind::Type), has_type_args: false, .. })
) && self.ctx.completion.config.add_call_parenthesis
{
if let Some(cap) = self.ctx.snippet_cap() {
let has_non_default_type_params = match resolution {