fix(completion): make the expected type a tad smarter with Fns

This commit changes how the expected type is calculated when working
with Fn pointers, making the parenthesis stop vanishing when completing
the function name.

I've been bugged by the behaviour on parenthesis completion for a long
while now. R-a assumes that the `LetStmt` type is the same as the
function type I've just written. Worse is that all parenthesis vanish,
even from functions that have completely different signatures. It will
now verify if the signature is the same.

While working on this, I noticed that record fields behave the same, so
I also made it prioritize the field type instead of the current
expression when possible, but I'm unsure if this is OK, so input is
appreciated.

ImplTraits as return types will still behave weirdly because lowering is
disallowed at the time it resolves the function types.
This commit is contained in:
Luiz Carvalho 2023-12-16 11:49:26 -03:00
parent 34df29620a
commit ed216e285d
No known key found for this signature in database
3 changed files with 24 additions and 17 deletions

View file

@ -4657,6 +4657,9 @@ impl Callable {
pub fn return_type(&self) -> Type {
self.ty.derived(self.sig.ret().clone())
}
pub fn sig(&self) -> &CallableSig {
&self.sig
}
}
fn closure_source(db: &dyn HirDatabase, closure: ClosureId) -> Option<ast::ClosureExpr> {

View file

@ -361,7 +361,12 @@ fn expected_type_and_name(
let ty = it.pat()
.and_then(|pat| sema.type_of_pat(&pat))
.or_else(|| it.initializer().and_then(|it| sema.type_of_expr(&it)))
.map(TypeInfo::original);
.map(TypeInfo::original)
.filter(|ty| {
// don't infer the let type if the expr is a function,
// preventing parenthesis from vanishing
it.ty().is_some() || !ty.is_fn()
});
let name = match it.pat() {
Some(ast::Pat::IdentPat(ident)) => ident.name().map(NameOrNameRef::Name),
Some(_) | None => None,
@ -415,20 +420,16 @@ fn expected_type_and_name(
})().unwrap_or((None, None))
},
ast::RecordExprField(it) => {
let field_ty = sema.resolve_record_field(&it).map(|(_, _, ty)| ty);
let field_name = it.field_name().map(NameOrNameRef::NameRef);
if let Some(expr) = it.expr() {
cov_mark::hit!(expected_type_struct_field_with_leading_char);
(
sema.type_of_expr(&expr).map(TypeInfo::original),
it.field_name().map(NameOrNameRef::NameRef),
)
let ty = field_ty
.or_else(|| sema.type_of_expr(&expr).map(TypeInfo::original));
(ty, field_name)
} else {
cov_mark::hit!(expected_type_struct_field_followed_by_comma);
let ty = sema.resolve_record_field(&it)
.map(|(_, _, ty)| ty);
(
ty,
it.field_name().map(NameOrNameRef::NameRef),
)
(field_ty, field_name)
}
},
// match foo { $0 }

View file

@ -305,14 +305,17 @@ fn params(
return None;
}
// Don't add parentheses if the expected type is some function reference.
if let Some(ty) = &ctx.expected_type {
// FIXME: check signature matches?
if ty.is_fn() {
// Don't add parentheses if the expected type is a function reference with the same signature.
if let Some(expected) = ctx.expected_type.as_ref().filter(|e| e.is_fn()) {
if let Some(expected) = expected.as_callable(ctx.db) {
if let Some(completed) = func.ty(ctx.db).as_callable(ctx.db) {
if expected.sig() == completed.sig() {
cov_mark::hit!(no_call_parens_if_fn_ptr_needed);
return None;
}
}
}
}
let self_param = if has_dot_receiver || matches!(func_kind, FuncKind::Method(_, Some(_))) {
None