Skip to content

Commit

Permalink
Auto merge of #15044 - lowr:fix/deduplicate-compl-fields, r=lnicola
Browse files Browse the repository at this point in the history
Deduplicate tuple indices for completion

Follow-up to #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.
  • Loading branch information
bors committed Jun 13, 2023
2 parents 07bc6cb + d01283b commit 25f1c72
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions crates/ide-completion/src/completions/dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,12 @@ fn complete_fields(
}
}
for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() {
// Tuple fields are always public (tuple struct fields are handled above).
tuple_index(acc, i, ty);
// Tuples are always the last type in a deref chain, so just check if the name is
// already seen without inserting into the hashset.
if !seen_names.contains(&hir::Name::new_tuple_field(i)) {
// Tuple fields are always public (tuple struct fields are handled above).
tuple_index(acc, i, ty);
}
}
}
}
Expand Down Expand Up @@ -720,6 +724,28 @@ fn test(a: A) {
);
}

#[test]
fn test_tuple_struct_deref_to_tuple_no_same_index() {
check(
r#"
//- minicore: deref
struct A(u8);
impl core::ops::Deref for A {
type Target = (u16, u32);
fn deref(&self) -> &Self::Target { loop {} }
}
fn test(a: A) {
a.$0
}
"#,
expect![[r#"
fd 0 u8
fd 1 u32
me deref() (use core::ops::Deref) fn(&self) -> &<Self as Deref>::Target
"#]],
);
}

#[test]
fn test_completion_works_in_consts() {
check(
Expand Down

0 comments on commit 25f1c72

Please sign in to comment.