Skip to content

Commit 25f1c72

Browse files
committed
Auto merge of rust-lang#15044 - lowr:fix/deduplicate-compl-fields, r=lnicola
Deduplicate tuple indices for completion Follow-up to rust-lang#15026 A tuple struct may dereference to a primitive tuple (though unusual, which is why I previously overlooked this case). We should not show the same tuple index in completion in such cases. Deduplication of indices among multiple tuple structs is already handled in the previous PR.
2 parents 07bc6cb + d01283b commit 25f1c72

File tree

1 file changed

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

1 file changed

+28
-2
lines changed

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,12 @@ fn complete_fields(
113113
}
114114
}
115115
for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() {
116-
// Tuple fields are always public (tuple struct fields are handled above).
117-
tuple_index(acc, i, ty);
116+
// Tuples are always the last type in a deref chain, so just check if the name is
117+
// already seen without inserting into the hashset.
118+
if !seen_names.contains(&hir::Name::new_tuple_field(i)) {
119+
// Tuple fields are always public (tuple struct fields are handled above).
120+
tuple_index(acc, i, ty);
121+
}
118122
}
119123
}
120124
}
@@ -720,6 +724,28 @@ fn test(a: A) {
720724
);
721725
}
722726

727+
#[test]
728+
fn test_tuple_struct_deref_to_tuple_no_same_index() {
729+
check(
730+
r#"
731+
//- minicore: deref
732+
struct A(u8);
733+
impl core::ops::Deref for A {
734+
type Target = (u16, u32);
735+
fn deref(&self) -> &Self::Target { loop {} }
736+
}
737+
fn test(a: A) {
738+
a.$0
739+
}
740+
"#,
741+
expect![[r#"
742+
fd 0 u8
743+
fd 1 u32
744+
me deref() (use core::ops::Deref) fn(&self) -> &<Self as Deref>::Target
745+
"#]],
746+
);
747+
}
748+
723749
#[test]
724750
fn test_completion_works_in_consts() {
725751
check(

0 commit comments

Comments
 (0)