-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Complete assoc. types on paths without a trait #4149
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,12 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon | |
Some(path) => path.clone(), | ||
_ => return, | ||
}; | ||
let def = match ctx.scope().resolve_hir_path(&path) { | ||
let scope = ctx.scope(); | ||
let def = match scope.resolve_hir_path(&path) { | ||
Some(PathResolution::Def(def)) => def, | ||
_ => return, | ||
}; | ||
let context_module = ctx.scope().module(); | ||
let context_module = scope.module(); | ||
match def { | ||
hir::ModuleDef::Module(module) => { | ||
let module_scope = module.scope(ctx.db, context_module); | ||
|
@@ -46,11 +47,8 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon | |
hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db), | ||
_ => unreachable!(), | ||
}; | ||
// Iterate assoc types separately | ||
// FIXME: complete T::AssocType | ||
let krate = ctx.krate; | ||
if let Some(krate) = krate { | ||
let traits_in_scope = ctx.scope().traits_in_scope(); | ||
if let Some(krate) = ctx.krate { | ||
let traits_in_scope = scope.traits_in_scope(); | ||
ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| { | ||
if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) { | ||
return None; | ||
|
@@ -64,20 +62,10 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon | |
} | ||
None::<()> | ||
}); | ||
|
||
ty.iterate_impl_items(ctx.db, krate, |item| { | ||
if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) { | ||
return None; | ||
} | ||
match item { | ||
hir::AssocItem::Function(_) | hir::AssocItem::Const(_) => {} | ||
hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty), | ||
} | ||
None::<()> | ||
}); | ||
} | ||
} | ||
hir::ModuleDef::Trait(t) => { | ||
// Handles `Trait::assoc` as well as `<Ty as Trait>::assoc`. | ||
for item in t.items(ctx.db) { | ||
if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) { | ||
continue; | ||
|
@@ -584,6 +572,39 @@ mod tests { | |
); | ||
} | ||
|
||
#[test] | ||
fn completes_trait_associated_type() { | ||
assert_debug_snapshot!( | ||
do_reference_completion( | ||
r" | ||
mod foo { | ||
pub struct S; | ||
} | ||
trait Tr { | ||
type Assoc; | ||
} | ||
impl Tr for foo::S {} | ||
|
||
fn f() { | ||
foo::S::<|> | ||
} | ||
", | ||
), | ||
@r###" | ||
[ | ||
CompletionItem { | ||
label: "Assoc", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... so this completion is actually wrong, rustc will complain about this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh great, not sure how I missed that :/ I think this is actually a bug in rustc, namely rust-lang/rust#22519. But yeah, rust-analyzer shouldn't suggest something that doesn't work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I can see, that issue doesn't propose allowing (Edit: Ok, eddyb's last comment does refer to allowing this, but still it doesn't seem like this will change anytime soon.) |
||
source_range: 248..248, | ||
delete: 248..248, | ||
insert: "Assoc", | ||
kind: TypeAlias, | ||
detail: "type Assoc;", | ||
}, | ||
] | ||
"### | ||
); | ||
} | ||
|
||
#[test] | ||
fn associated_item_visibility() { | ||
assert_debug_snapshot!( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
r? @flodiebold
From the look of it, the old code looks correct, as it only uses the values namespace. I would expect two similar fuctions, one for consttants & methods, and one for types. The caller can then select the appropriate namespace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah,
method_resolution
is for values only, and associated types resolution has different rules. (You can't just writeS::Item
ifS
is a struct implementingIterator
, the only situation where you can writeT::AssociatedType
whereT
is a type is whenT
is a type parameter. I thought you could do this as well before I actually implemented it!)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might still be a good idea to replace
_
with explicit match & comment