1 //! Completion of paths, i.e. `some::prefix::$0`.
3 use hir::{Adt, HasVisibility, PathResolution, ScopeDef};
4 use rustc_hash::FxHashSet;
8 use crate::{CompletionContext, Completions};
10 pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) {
11 let path = match &ctx.path_qual {
12 Some(path) => path.clone(),
16 if ctx.attribute_under_caret.is_some() || ctx.mod_declaration_under_caret.is_some() {
20 let context_module = ctx.scope.module();
22 let resolution = match ctx.sema.resolve_path(&path) {
27 // Add associated types on type parameters and `Self`.
28 resolution.assoc_type_shorthand_candidates(ctx.db, |alias| {
29 acc.add_type_alias(ctx, alias);
34 PathResolution::Def(hir::ModuleDef::Module(module)) => {
35 let module_scope = module.scope(ctx.db, context_module);
36 for (name, def) in module_scope {
37 if ctx.use_item_syntax.is_some() {
38 if let ScopeDef::Unknown = def {
39 if let Some(name_ref) = ctx.name_ref_syntax.as_ref() {
40 if name_ref.syntax().text() == name.to_string().as_str() {
41 // for `use self::foo$0`, don't suggest `foo` as a completion
42 mark::hit!(dont_complete_current_use);
49 acc.add_resolution(ctx, name.to_string(), &def);
52 PathResolution::Def(def @ hir::ModuleDef::Adt(_))
53 | PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_))
54 | PathResolution::Def(def @ hir::ModuleDef::BuiltinType(_)) => {
55 if let hir::ModuleDef::Adt(Adt::Enum(e)) = def {
56 for variant in e.variants(ctx.db) {
57 acc.add_enum_variant(ctx, variant, None);
61 hir::ModuleDef::Adt(adt) => adt.ty(ctx.db),
62 hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db),
63 hir::ModuleDef::BuiltinType(builtin) => {
64 let module = match ctx.scope.module() {
68 builtin.ty(ctx.db, module)
73 // XXX: For parity with Rust bug #22519, this does not complete Ty::AssocType.
74 // (where AssocType is defined on a trait, not an inherent impl)
76 let krate = ctx.krate;
77 if let Some(krate) = krate {
78 let traits_in_scope = ctx.scope.traits_in_scope();
79 ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
80 if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
84 hir::AssocItem::Function(func) => {
85 acc.add_function(ctx, func, None);
87 hir::AssocItem::Const(ct) => acc.add_const(ctx, ct),
88 hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
93 // Iterate assoc types separately
94 ty.iterate_assoc_items(ctx.db, krate, |item| {
95 if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
99 hir::AssocItem::Function(_) | hir::AssocItem::Const(_) => {}
100 hir::AssocItem::TypeAlias(ty) => 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) => {
114 acc.add_function(ctx, func, None);
116 hir::AssocItem::Const(ct) => acc.add_const(ctx, ct),
117 hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
121 PathResolution::TypeParam(_) | PathResolution::SelfType(_) => {
122 if let Some(krate) = ctx.krate {
123 let ty = match resolution {
124 PathResolution::TypeParam(param) => param.ty(ctx.db),
125 PathResolution::SelfType(impl_def) => impl_def.target_ty(ctx.db),
129 if let Some(Adt::Enum(e)) = ty.as_adt() {
130 for variant in e.variants(ctx.db) {
131 acc.add_enum_variant(ctx, variant, None);
135 let traits_in_scope = ctx.scope.traits_in_scope();
136 let mut seen = FxHashSet::default();
137 ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
138 if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
142 // We might iterate candidates of a trait multiple times here, so deduplicate
144 if seen.insert(item) {
146 hir::AssocItem::Function(func) => {
147 acc.add_function(ctx, func, None);
149 hir::AssocItem::Const(ct) => acc.add_const(ctx, ct),
150 hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
163 use expect_test::{expect, Expect};
164 use test_utils::mark;
167 test_utils::{check_edit, completion_list},
171 fn check(ra_fixture: &str, expect: Expect) {
172 let actual = completion_list(ra_fixture, CompletionKind::Reference);
173 expect.assert_eq(&actual);
176 fn check_builtin(ra_fixture: &str, expect: Expect) {
177 let actual = completion_list(ra_fixture, CompletionKind::BuiltinType);
178 expect.assert_eq(&actual);
182 fn dont_complete_current_use() {
183 mark::check!(dont_complete_current_use);
184 check(r#"use self::foo$0;"#, expect![[""]]);
188 fn dont_complete_current_use_in_braces_with_glob() {
191 mod foo { pub struct S; }
192 use self::{foo::*, bar$0};
202 fn dont_complete_primitive_in_use() {
203 check_builtin(r#"use self::$0;"#, expect![[""]]);
207 fn dont_complete_primitive_in_module_scope() {
208 check_builtin(r#"fn foo() { self::$0 }"#, expect![[""]]);
212 fn completes_primitives() {
214 r#"fn main() { let _: $0 = 92; }"#,
238 fn completes_mod_with_same_name_as_function() {
243 mod my { pub struct Bar; }
253 fn filters_visibility() {
261 pub use Bar as PublicBar;
272 fn completes_use_item_starting_with_self() {
277 mod m { pub struct Bar; }
286 fn completes_use_item_starting_with_crate() {
303 fn completes_nested_use_tree() {
320 fn completes_deeply_nested_use_tree() {
331 use crate::{bar::{baz::Sp$0}};
340 fn completes_enum_variant() {
343 enum E { Foo, Bar(i32) }
344 fn foo() { let _ = E::$0 }
354 fn completes_struct_associated_items() {
367 fn foo() { let _ = S::$0 }
372 ct C const C: i32 = 42;
379 fn associated_item_visibility() {
386 pub(crate) fn public_method() { }
387 fn private_method() { }
388 pub(crate) type PublicType = u32;
389 type PrivateType = u32;
390 pub(crate) const PUBLIC_CONST: u32 = 1;
391 const PRIVATE_CONST: u32 = 1;
395 fn foo() { let _ = S::$0 }
398 fn public_method() -> ()
399 ct PUBLIC_CONST pub(crate) const PUBLIC_CONST: u32 = 1;
400 ta PublicType pub(crate) type PublicType = u32;
406 fn completes_enum_associated_method() {
410 impl E { fn m() { } }
412 fn foo() { let _ = E::$0 }
421 fn completes_union_associated_method() {
425 impl U { fn m() { } }
427 fn foo() { let _ = U::$0 }
436 fn completes_use_paths_across_crates() {
439 //- /main.rs crate:main deps:foo
442 //- /foo/lib.rs crate:foo
443 pub mod bar { pub struct S; }
452 fn completes_trait_associated_method_1() {
455 trait Trait { fn m(); }
457 fn foo() { let _ = Trait::$0 }
466 fn completes_trait_associated_method_2() {
469 trait Trait { fn m(); }
474 fn foo() { let _ = S::$0 }
483 fn completes_trait_associated_method_3() {
486 trait Trait { fn m(); }
491 fn foo() { let _ = <S as Trait>::$0 }
500 fn completes_ty_param_assoc_ty() {
514 fn submethod(&self) {}
517 fn foo<T: Sub>() { T::$0 }
524 me submethod(…) -> ()
525 ct CONST const CONST: u8;
533 fn completes_self_param_assoc_ty() {
547 fn submethod(&self) {}
551 impl<T> Super for Wrap<T> {}
552 impl<T> Sub for Wrap<T> {
554 // Should be able to assume `Self: Sub + Super`
562 ct CONST const CONST: u8 = 0;
565 ct C2 const C2: () = ();
567 me submethod(…) -> ()
573 fn completes_type_alias() {
577 impl S { fn foo() {} }
579 impl T { fn bar() {} }
591 fn completes_qualified_macros() {
595 macro_rules! foo { () => {} }
597 fn main() { let _ = crate::$0 }
601 ma foo!(…) #[macro_export] macro_rules! foo
607 fn test_super_super_completion() {
614 mod c { use super::super::$0 }
626 fn completes_reexported_items_under_correct_name() {
629 fn foo() { self::m::$0 }
632 pub use super::p::wrong_fn as right_fn;
633 pub use super::p::WRONG_CONST as RIGHT_CONST;
634 pub use super::p::WrongType as RightType;
638 const WRONG_CONST: u32 = 1;
652 fn foo() { self::m::$0 }
655 pub use super::p::wrong_fn as right_fn;
656 pub use super::p::WRONG_CONST as RIGHT_CONST;
657 pub use super::p::WrongType as RightType;
661 const WRONG_CONST: u32 = 1;
666 fn foo() { self::m::RightType }
669 pub use super::p::wrong_fn as right_fn;
670 pub use super::p::WRONG_CONST as RIGHT_CONST;
671 pub use super::p::WrongType as RightType;
675 const WRONG_CONST: u32 = 1;
683 fn completes_in_simple_macro_call() {
686 macro_rules! m { ($e:expr) => { $e } }
687 fn main() { m!(self::f$0); }
698 fn function_mod_share_name() {
701 fn foo() { self::m::$0 }
716 fn completes_hashmap_new() {
720 struct HashMap<K, V, S = RandomState> {}
722 impl<K, V> HashMap<K, V, RandomState> {
723 pub fn new() -> HashMap<K, V, RandomState> { }
730 fn new() -> HashMap<K, V, RandomState>
736 fn dont_complete_attr() {
739 mod foo { pub struct Foo; }
748 fn completes_function() {
770 fn completes_self_enum() {
793 fn completes_primitive_assoc_const() {
796 //- /lib.rs crate:lib deps:core
801 //- /core.rs crate:core
804 pub const MAX: Self = 255;
810 ct MAX pub const MAX: Self = 255;