1 //! Completion of paths, i.e. `some::prefix::$0`.
3 use hir::{Adt, HasVisibility, PathResolution, ScopeDef};
4 use rustc_hash::FxHashSet;
7 use crate::{CompletionContext, Completions};
9 pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) {
10 if ctx.is_path_disallowed() {
13 let path = match &ctx.path_qual {
14 Some(path) => path.clone(),
18 let resolution = match ctx.sema.resolve_path(&path) {
22 let context_module = ctx.scope.module();
24 // Add associated types on type parameters and `Self`.
25 resolution.assoc_type_shorthand_candidates(ctx.db, |_, alias| {
26 acc.add_type_alias(ctx, alias);
31 PathResolution::Def(hir::ModuleDef::Module(module)) => {
32 let module_scope = module.scope(ctx.db, context_module);
33 for (name, def) in module_scope {
34 if ctx.use_item_syntax.is_some() {
35 if let ScopeDef::Unknown = def {
36 if let Some(name_ref) = ctx.name_ref_syntax.as_ref() {
37 if name_ref.syntax().text() == name.to_string().as_str() {
38 // for `use self::foo$0`, don't suggest `foo` as a completion
39 cov_mark::hit!(dont_complete_current_use);
46 acc.add_resolution(ctx, name.to_string(), &def);
49 PathResolution::Def(def @ hir::ModuleDef::Adt(_))
50 | PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_))
51 | PathResolution::Def(def @ hir::ModuleDef::BuiltinType(_)) => {
52 if let hir::ModuleDef::Adt(Adt::Enum(e)) = def {
53 add_enum_variants(ctx, acc, e);
56 hir::ModuleDef::Adt(adt) => adt.ty(ctx.db),
57 hir::ModuleDef::TypeAlias(a) => {
58 let ty = a.ty(ctx.db);
59 if let Some(Adt::Enum(e)) = ty.as_adt() {
60 cov_mark::hit!(completes_variant_through_alias);
61 add_enum_variants(ctx, acc, e);
65 hir::ModuleDef::BuiltinType(builtin) => {
66 let module = match ctx.scope.module() {
70 cov_mark::hit!(completes_primitive_assoc_const);
71 builtin.ty(ctx.db, module)
76 // XXX: For parity with Rust bug #22519, this does not complete Ty::AssocType.
77 // (where AssocType is defined on a trait, not an inherent impl)
79 let krate = ctx.krate;
80 if let Some(krate) = krate {
81 let traits_in_scope = ctx.scope.traits_in_scope();
82 ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
83 if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
87 hir::AssocItem::Function(func) => acc.add_function(ctx, func, None),
88 hir::AssocItem::Const(ct) => acc.add_const(ctx, ct),
89 hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
94 // Iterate assoc types separately
95 ty.iterate_assoc_items(ctx.db, krate, |item| {
96 if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
99 if let hir::AssocItem::TypeAlias(ty) = item {
100 acc.add_type_alias(ctx, ty)
106 PathResolution::Def(hir::ModuleDef::Trait(t)) => {
107 // Handles `Trait::assoc` as well as `<Ty as Trait>::assoc`.
108 for item in t.items(ctx.db) {
109 if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
113 hir::AssocItem::Function(func) => acc.add_function(ctx, func, None),
114 hir::AssocItem::Const(ct) => acc.add_const(ctx, ct),
115 hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
119 PathResolution::TypeParam(_) | PathResolution::SelfType(_) => {
120 if let Some(krate) = ctx.krate {
121 let ty = match resolution {
122 PathResolution::TypeParam(param) => param.ty(ctx.db),
123 PathResolution::SelfType(impl_def) => impl_def.self_ty(ctx.db),
127 if let Some(Adt::Enum(e)) = ty.as_adt() {
128 add_enum_variants(ctx, acc, e);
131 let traits_in_scope = ctx.scope.traits_in_scope();
132 let mut seen = FxHashSet::default();
133 ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
134 if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
138 // We might iterate candidates of a trait multiple times here, so deduplicate
140 if seen.insert(item) {
142 hir::AssocItem::Function(func) => acc.add_function(ctx, func, None),
143 hir::AssocItem::Const(ct) => acc.add_const(ctx, ct),
144 hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
155 fn add_enum_variants(ctx: &CompletionContext, acc: &mut Completions, e: hir::Enum) {
156 for variant in e.variants(ctx.db) {
157 acc.add_enum_variant(ctx, variant, None);
163 use expect_test::{expect, Expect};
166 test_utils::{check_edit, completion_list},
170 fn check(ra_fixture: &str, expect: Expect) {
171 let actual = completion_list(ra_fixture, CompletionKind::Reference);
172 expect.assert_eq(&actual);
175 fn check_builtin(ra_fixture: &str, expect: Expect) {
176 let actual = completion_list(ra_fixture, CompletionKind::BuiltinType);
177 expect.assert_eq(&actual);
181 fn dont_complete_current_use() {
182 cov_mark::check!(dont_complete_current_use);
183 check(r#"use self::foo$0;"#, expect![[""]]);
187 fn dont_complete_current_use_in_braces_with_glob() {
190 mod foo { pub struct S; }
191 use self::{foo::*, bar$0};
201 fn dont_complete_primitive_in_use() {
202 check_builtin(r#"use self::$0;"#, expect![[""]]);
206 fn dont_complete_primitive_in_module_scope() {
207 check_builtin(r#"fn foo() { self::$0 }"#, expect![[""]]);
211 fn completes_primitives() {
213 r#"fn main() { let _: $0 = 92; }"#,
237 fn completes_mod_with_same_name_as_function() {
242 mod my { pub struct Bar; }
252 fn filters_visibility() {
260 pub use Bar as PublicBar;
271 fn completes_use_item_starting_with_self() {
276 mod m { pub struct Bar; }
285 fn completes_use_item_starting_with_crate() {
302 fn completes_nested_use_tree() {
319 fn completes_deeply_nested_use_tree() {
330 use crate::{bar::{baz::Sp$0}};
339 fn completes_enum_variant() {
342 enum E { Foo, Bar(i32) }
343 fn foo() { let _ = E::$0 }
353 fn completes_struct_associated_items() {
366 fn foo() { let _ = S::$0 }
371 ct C const C: i32 = 42;
378 fn associated_item_visibility() {
385 pub(crate) fn public_method() { }
386 fn private_method() { }
387 pub(crate) type PublicType = u32;
388 type PrivateType = u32;
389 pub(crate) const PUBLIC_CONST: u32 = 1;
390 const PRIVATE_CONST: u32 = 1;
394 fn foo() { let _ = S::$0 }
397 fn public_method() fn()
398 ct PUBLIC_CONST pub(crate) const PUBLIC_CONST: u32 = 1;
399 ta PublicType pub(crate) type PublicType = u32;
405 fn completes_enum_associated_method() {
409 impl E { fn m() { } }
411 fn foo() { let _ = E::$0 }
420 fn completes_union_associated_method() {
424 impl U { fn m() { } }
426 fn foo() { let _ = U::$0 }
435 fn completes_use_paths_across_crates() {
438 //- /main.rs crate:main deps:foo
441 //- /foo/lib.rs crate:foo
442 pub mod bar { pub struct S; }
451 fn completes_trait_associated_method_1() {
454 trait Trait { fn m(); }
456 fn foo() { let _ = Trait::$0 }
465 fn completes_trait_associated_method_2() {
468 trait Trait { fn m(); }
473 fn foo() { let _ = S::$0 }
482 fn completes_trait_associated_method_3() {
485 trait Trait { fn m(); }
490 fn foo() { let _ = <S as Trait>::$0 }
499 fn completes_ty_param_assoc_ty() {
513 fn submethod(&self) {}
516 fn foo<T: Sub>() { T::$0 }
523 me submethod(…) fn(&self)
524 ct CONST const CONST: u8;
526 me method(…) fn(&self)
532 fn completes_self_param_assoc_ty() {
546 fn submethod(&self) {}
550 impl<T> Super for Wrap<T> {}
551 impl<T> Sub for Wrap<T> {
553 // Should be able to assume `Self: Sub + Super`
561 ct CONST const CONST: u8 = 0;
563 me method(…) fn(&self)
564 ct C2 const C2: () = ();
566 me submethod(…) fn(&self)
572 fn completes_type_alias() {
576 impl S { fn foo() {} }
578 impl T { fn bar() {} }
590 fn completes_qualified_macros() {
594 macro_rules! foo { () => {} }
596 fn main() { let _ = crate::$0 }
600 ma foo!(…) #[macro_export] macro_rules! foo
606 fn test_super_super_completion() {
613 mod c { use super::super::$0 }
625 fn completes_reexported_items_under_correct_name() {
628 fn foo() { self::m::$0 }
631 pub use super::p::wrong_fn as right_fn;
632 pub use super::p::WRONG_CONST as RIGHT_CONST;
633 pub use super::p::WrongType as RightType;
637 const WRONG_CONST: u32 = 1;
651 fn foo() { self::m::$0 }
654 pub use super::p::wrong_fn as right_fn;
655 pub use super::p::WRONG_CONST as RIGHT_CONST;
656 pub use super::p::WrongType as RightType;
660 const WRONG_CONST: u32 = 1;
665 fn foo() { self::m::RightType }
668 pub use super::p::wrong_fn as right_fn;
669 pub use super::p::WRONG_CONST as RIGHT_CONST;
670 pub use super::p::WrongType as RightType;
674 const WRONG_CONST: u32 = 1;
682 fn completes_in_simple_macro_call() {
685 macro_rules! m { ($e:expr) => { $e } }
686 fn main() { m!(self::f$0); }
697 fn function_mod_share_name() {
700 fn foo() { self::m::$0 }
715 fn completes_hashmap_new() {
719 struct HashMap<K, V, S = RandomState> {}
721 impl<K, V> HashMap<K, V, RandomState> {
722 pub fn new() -> HashMap<K, V, RandomState> { }
729 fn new() fn() -> HashMap<K, V, RandomState>
735 fn dont_complete_attr() {
738 mod foo { pub struct Foo; }
747 fn completes_variant_through_self() {
770 fn completes_primitive_assoc_const() {
771 cov_mark::check!(completes_primitive_assoc_const);
774 //- /lib.rs crate:lib deps:core
779 //- /core.rs crate:core
782 pub const MAX: Self = 255;
788 ct MAX pub const MAX: Self = 255;
795 fn completes_variant_through_alias() {
796 cov_mark::check!(completes_variant_through_alias);