Skip to content

Commit 42eab5e

Browse files
committed
Deduplicate field names for completion
1 parent b479550 commit 42eab5e

File tree

1 file changed

+50
-2
lines changed
  • crates/ide-completion/src/completions

1 file changed

+50
-2
lines changed

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

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,12 @@ fn complete_fields(
105105
mut named_field: impl FnMut(&mut Completions, hir::Field, hir::Type),
106106
mut tuple_index: impl FnMut(&mut Completions, usize, hir::Type),
107107
) {
108+
let mut seen_names = FxHashSet::default();
108109
for receiver in receiver.autoderef(ctx.db) {
109110
for (field, ty) in receiver.fields(ctx.db) {
110-
named_field(acc, field, ty);
111+
if seen_names.insert(field.name(ctx.db)) {
112+
named_field(acc, field, ty);
113+
}
111114
}
112115
for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() {
113116
// Tuple fields are always public (tuple struct fields are handled above).
@@ -671,6 +674,52 @@ impl T {
671674
);
672675
}
673676

677+
#[test]
678+
fn test_field_no_same_name() {
679+
check(
680+
r#"
681+
//- minicore: deref
682+
struct A { field: u8 }
683+
struct B { field: u16, another: u32 }
684+
impl core::ops::Deref for A {
685+
type Target = B;
686+
fn deref(&self) -> &Self::Target { loop {} }
687+
}
688+
fn test(a: A) {
689+
a.$0
690+
}
691+
"#,
692+
expect![[r#"
693+
fd another u32
694+
fd field u8
695+
me deref() (use core::ops::Deref) fn(&self) -> &<Self as Deref>::Target
696+
"#]],
697+
);
698+
}
699+
700+
#[test]
701+
fn test_tuple_field_no_same_index() {
702+
check(
703+
r#"
704+
//- minicore: deref
705+
struct A(u8);
706+
struct B(u16, u32);
707+
impl core::ops::Deref for A {
708+
type Target = B;
709+
fn deref(&self) -> &Self::Target { loop {} }
710+
}
711+
fn test(a: A) {
712+
a.$0
713+
}
714+
"#,
715+
expect![[r#"
716+
fd 0 u8
717+
fd 1 u32
718+
me deref() (use core::ops::Deref) fn(&self) -> &<Self as Deref>::Target
719+
"#]],
720+
);
721+
}
722+
674723
#[test]
675724
fn test_completion_works_in_consts() {
676725
check(
@@ -1000,7 +1049,6 @@ fn test(a: A) {
10001049
}
10011050
"#,
10021051
expect![[r#"
1003-
fd 0 u16
10041052
fd 0 u8
10051053
me deref() (use core::ops::Deref) fn(&self) -> &<Self as Deref>::Target
10061054
"#]],

0 commit comments

Comments
 (0)