Skip to content

Commit 0023f19

Browse files
Merge pull request #21277 from A4-Tacks/strip-deref
Fix expected type no strip deref
2 parents 87cfc88 + d359992 commit 0023f19

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

crates/ide-completion/src/context/analysis.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,10 +600,26 @@ fn expected_type_and_name<'db>(
600600
Some(it) => it,
601601
None => return ty,
602602
};
603-
for _ in top_syn.ancestors().skip(1).map_while(ast::RefExpr::cast) {
603+
let refs_level = top_syn
604+
.ancestors()
605+
.skip(1)
606+
.map_while(Either::<ast::RefExpr, ast::PrefixExpr>::cast)
607+
.take_while(|it| match it {
608+
Either::Left(_) => true,
609+
Either::Right(prefix) => prefix.op_kind() == Some(ast::UnaryOp::Deref),
610+
})
611+
.fold(0i32, |level, expr| match expr {
612+
Either::Left(_) => level + 1,
613+
Either::Right(_) => level - 1,
614+
});
615+
for _ in 0..refs_level {
604616
cov_mark::hit!(expected_type_fn_param_ref);
605617
ty = ty.strip_reference();
606618
}
619+
for _ in refs_level..0 {
620+
cov_mark::hit!(expected_type_fn_param_deref);
621+
ty = ty.add_reference(hir::Mutability::Shared);
622+
}
607623
ty
608624
}
609625
_ => ty,

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ fn bar(x: &u32) {}
146146
);
147147
}
148148

149+
#[test]
150+
fn expected_type_fn_param_deref() {
151+
cov_mark::check!(expected_type_fn_param_deref);
152+
check_expected_type_and_name(
153+
r#"
154+
fn foo() { bar(*$0); }
155+
fn bar(x: &u32) {}
156+
"#,
157+
expect!["ty: &'_ &'_ u32, name: x"],
158+
);
159+
}
160+
149161
#[test]
150162
fn expected_type_struct_field_without_leading_char() {
151163
cov_mark::check!(expected_type_struct_field_without_leading_char);

0 commit comments

Comments
 (0)