Skip to content

Commit ad6df5b

Browse files
committed
Auto merge of rust-lang#12199 - jonas-schievink:no-invalid-assoc-ty-completions, r=jonas-schievink
fix: Don't show assoc. type binding completions when invalid Fixes rust-lang/rust-analyzer#12165
2 parents ab55805 + 5a2398d commit ad6df5b

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

crates/ide-completion/src/completions/type.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,17 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext)
165165
if let Some(ImmediateLocation::GenericArgList(arg_list)) = &ctx.completion_location {
166166
if let Some(path_seg) = arg_list.syntax().parent().and_then(ast::PathSegment::cast)
167167
{
168-
if let Some(hir::PathResolution::Def(hir::ModuleDef::Trait(trait_))) =
169-
ctx.sema.resolve_path(&path_seg.parent_path())
170-
{
171-
trait_.items_with_supertraits(ctx.sema.db).into_iter().for_each(|it| {
172-
if let hir::AssocItem::TypeAlias(alias) = it {
173-
cov_mark::hit!(complete_assoc_type_in_generics_list);
174-
acc.add_type_alias_with_eq(ctx, alias)
175-
}
176-
});
168+
if path_seg.syntax().ancestors().find_map(ast::TypeBound::cast).is_some() {
169+
if let Some(hir::PathResolution::Def(hir::ModuleDef::Trait(trait_))) =
170+
ctx.sema.resolve_path(&path_seg.parent_path())
171+
{
172+
trait_.items_with_supertraits(ctx.sema.db).into_iter().for_each(|it| {
173+
if let hir::AssocItem::TypeAlias(alias) = it {
174+
cov_mark::hit!(complete_assoc_type_in_generics_list);
175+
acc.add_type_alias_with_eq(ctx, alias)
176+
}
177+
});
178+
}
177179
}
178180
}
179181
}

crates/ide-completion/src/tests/type_pos.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,38 @@ fn foo<'lt, T: Trait2<self::$0>, const CONST_PARAM: usize>(_: T) {}
393393
);
394394
}
395395

396+
#[test]
397+
fn no_assoc_completion_outside_type_bounds() {
398+
check(
399+
r#"
400+
struct S;
401+
trait Tr<T> {
402+
type Ty;
403+
}
404+
405+
impl Tr<$0
406+
"#,
407+
expect![[r#"
408+
ct CONST
409+
en Enum
410+
ma makro!(…) macro_rules! makro
411+
md module
412+
sp Self
413+
st Record
414+
st S
415+
st Tuple
416+
st Unit
417+
tt Tr
418+
tt Trait
419+
un Union
420+
bt u32
421+
kw crate::
422+
kw self::
423+
kw super::
424+
"#]],
425+
);
426+
}
427+
396428
#[test]
397429
fn enum_qualified() {
398430
check(

0 commit comments

Comments
 (0)