11158: fix: Enable completions for `<_>::$0` r=Veykril a=Veykril

Fixes https://github.com/rust-analyzer/rust-analyzer/issues/10462
bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2022-01-01 21:20:55 +00:00 committed by GitHub
commit 8da5f46fac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,7 +2,7 @@
use std::iter;
use hir::ScopeDef;
use hir::{ScopeDef, Trait};
use rustc_hash::FxHashSet;
use syntax::{ast, AstNode};
@ -27,6 +27,25 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
_ => return,
};
// special case `<_>::$0` as this doesn't resolve to anything.
if path.qualifier().is_none() {
if matches!(
path.segment().and_then(|it| it.kind()),
Some(ast::PathSegmentKind::Type {
type_ref: Some(ast::Type::InferType(_)),
trait_ref: None,
})
) {
cov_mark::hit!(completion_type_anchor_empty);
ctx.scope
.visible_traits()
.into_iter()
.flat_map(|it| Trait::from(it).items(ctx.sema.db))
.for_each(|item| add_assoc_item(acc, ctx, item));
return;
}
}
let resolution = match ctx.sema.resolve_path(path) {
Some(res) => res,
None => return,
@ -707,4 +726,28 @@ pub mod m {}
expect![[r#""#]],
)
}
#[test]
fn type_anchor_empty() {
cov_mark::check!(completion_type_anchor_empty);
check(
r#"
trait Foo {
fn foo() -> Self;
}
struct Bar;
impl Foo for Bar {
fn foo() -> {
Bar
}
}
fn bar() -> Bar {
<_>::$0
}
"#,
expect![[r#"
fn foo() (as Foo) fn() -> Self
"#]],
)
}
}