Implement function type matching

This commit is contained in:
Jessie Chatham Spencer 2023-11-04 12:31:42 +00:00 committed by Lukas Wirth
parent 14a7a614c1
commit 6abba17a5b
3 changed files with 95 additions and 101 deletions

View file

@ -4567,8 +4567,8 @@ impl Type {
// FIXME: Document this // FIXME: Document this
#[derive(Debug)] #[derive(Debug)]
pub struct Callable { pub struct Callable {
pub ty: Type, ty: Type,
pub sig: CallableSig, sig: CallableSig,
callee: Callee, callee: Callee,
/// Whether this is a method that was called with method call syntax. /// Whether this is a method that was called with method call syntax.
pub(crate) is_bound_method: bool, pub(crate) is_bound_method: bool,

View file

@ -10,9 +10,7 @@ pub(crate) mod variant;
pub(crate) mod union_literal; pub(crate) mod union_literal;
pub(crate) mod literal; pub(crate) mod literal;
use core::panic; use hir::{AsAssocItem, Function, HasAttrs, HirDisplay, ModuleDef, ScopeDef, Type};
use hir::{AsAssocItem, HasAttrs, HirDisplay, ModuleDef, ScopeDef, Type};
use ide_db::{ use ide_db::{
documentation::{Documentation, HasDocs}, documentation::{Documentation, HasDocs},
helpers::item_name, helpers::item_name,
@ -395,17 +393,14 @@ fn render_resolution_path(
ScopeDef::ModuleDef(ModuleDef::Adt(adt)) | ScopeDef::AdtSelfType(adt) => { ScopeDef::ModuleDef(ModuleDef::Adt(adt)) | ScopeDef::AdtSelfType(adt) => {
set_item_relevance(adt.ty(db)) set_item_relevance(adt.ty(db))
} }
ScopeDef::ModuleDef(ModuleDef::Function(func)) => { // Functions are handled at the start of the function.
set_item_relevance(func.ty(db).as_callable(db).unwrap().ty) ScopeDef::ModuleDef(ModuleDef::Function(_)) => (), // TODO: Should merge with the match case earlier in the function?
} // Enum variants are handled at the start of the function.
ScopeDef::ModuleDef(ModuleDef::Variant(variant)) => { ScopeDef::ModuleDef(ModuleDef::Variant(_)) => (),
set_item_relevance(variant.parent_enum(db).ty(db))
}
ScopeDef::ModuleDef(ModuleDef::Const(konst)) => set_item_relevance(konst.ty(db)), ScopeDef::ModuleDef(ModuleDef::Const(konst)) => set_item_relevance(konst.ty(db)),
ScopeDef::ModuleDef(ModuleDef::Static(stat)) => set_item_relevance(stat.ty(db)), ScopeDef::ModuleDef(ModuleDef::Static(stat)) => set_item_relevance(stat.ty(db)),
ScopeDef::ModuleDef(ModuleDef::BuiltinType(bt)) => set_item_relevance(bt.ty(db)), ScopeDef::ModuleDef(ModuleDef::BuiltinType(bt)) => set_item_relevance(bt.ty(db)),
ScopeDef::ImplSelfType(imp) => set_item_relevance(imp.self_ty(db)), ScopeDef::ImplSelfType(imp) => set_item_relevance(imp.self_ty(db)),
ScopeDef::GenericParam(_) ScopeDef::GenericParam(_)
| ScopeDef::Label(_) | ScopeDef::Label(_)
| ScopeDef::Unknown | ScopeDef::Unknown
@ -502,6 +497,20 @@ fn scope_def_is_deprecated(ctx: &RenderContext<'_>, resolution: ScopeDef) -> boo
} }
} }
fn match_types(
ctx: &CompletionContext<'_>,
ty1: &hir::Type,
ty2: &hir::Type,
) -> Option<CompletionRelevanceTypeMatch> {
if ty1 == ty2 {
Some(CompletionRelevanceTypeMatch::Exact)
} else if ty1.could_unify_with(ctx.db, ty2) {
Some(CompletionRelevanceTypeMatch::CouldUnify)
} else {
None
}
}
fn compute_type_match( fn compute_type_match(
ctx: &CompletionContext<'_>, ctx: &CompletionContext<'_>,
completion_ty: &hir::Type, completion_ty: &hir::Type,
@ -514,35 +523,42 @@ fn compute_type_match(
return None; return None;
} }
if completion_ty == expected_type { match_types(ctx, expected_type, completion_ty)
Some(CompletionRelevanceTypeMatch::Exact)
} else if expected_type.could_unify_with(ctx.db, completion_ty) {
Some(CompletionRelevanceTypeMatch::CouldUnify)
} else {
None
}
} }
fn compute_type_match2( fn compute_function_type_match(
ctx: &CompletionContext<'_>, ctx: &CompletionContext<'_>,
completion_ty1: &hir::Type, func: &Function,
completion_ty2: &hir::Type,
) -> Option<CompletionRelevanceTypeMatch> { ) -> Option<CompletionRelevanceTypeMatch> {
let expected_type = completion_ty1; // We compute a vec of function parameters + the return type for the expected
// type as well as the function we are matching with. Doing this allows for
// matching all of the types in one iterator.
// We don't ever consider unit type to be an exact type match, since let expected_callable = ctx.expected_type.as_ref()?.as_callable(ctx.db)?;
// nearly always this is not meaningful to the user. let expected_types = expected_callable.params(ctx.db).into_iter().map(|param| param.1);
if expected_type.is_unit() { let actual_types =
func.ty(ctx.db).as_callable(ctx.db)?.params(ctx.db).into_iter().map(|param| param.1);
if expected_types.len() != actual_types.len() {
return None; return None;
} }
if completion_ty2 == expected_type { let mut matches = expected_types
Some(CompletionRelevanceTypeMatch::Exact) .zip(actual_types)
} else if expected_type.could_unify_with(ctx.db, completion_ty2) { .chain([(expected_callable.return_type(), func.ret_type(ctx.db))])
Some(CompletionRelevanceTypeMatch::CouldUnify) .map(|(expected_type, actual_type)| match_types(ctx, &expected_type, &actual_type));
} else {
None // Any missing type match indicates that these types can not be unified.
if matches.any(|type_match| type_match.is_none()) {
return None;
} }
// If any of the types are unifiable but not exact we consider the function types as a whole
// to be unifiable. Otherwise if every pair of types is an exact match the functions are an
// exact type match.
matches
.find(|type_match| matches!(type_match, Some(CompletionRelevanceTypeMatch::CouldUnify)))
.unwrap_or(Some(CompletionRelevanceTypeMatch::Exact))
} }
fn compute_exact_name_match(ctx: &CompletionContext<'_>, completion_name: &str) -> bool { fn compute_exact_name_match(ctx: &CompletionContext<'_>, completion_name: &str) -> bool {
@ -796,7 +812,7 @@ fn main() {
); );
} }
// TODO: does this test even make sense? // TODO: How dowe test ModuleDef::Variant(Variant?)
#[test] #[test]
fn set_enum_variant_type_completion_info() { fn set_enum_variant_type_completion_info() {
check_relevance( check_relevance(
@ -820,7 +836,7 @@ pub mod test_mod_a {
fn test(input: dep::test_mod_b::Enum) { } fn test(input: dep::test_mod_b::Enum) { }
fn main() { fn main() {
test(Enum$0); test(Enum::Variant$0);
} }
"#, "#,
expect![[r#" expect![[r#"
@ -859,7 +875,7 @@ fn main() {
} }
"#, "#,
expect![[r#" expect![[r#"
fn Function (use dep::test_mod_a::Function) [type_could_unify+requires_import] fn Function (use dep::test_mod_a::Function) [type+requires_import]
fn main [] fn main []
fn test [] fn test []
md dep [] md dep []
@ -868,7 +884,6 @@ fn main() {
); );
} }
// TODO This test does not trigger the const case
#[test] #[test]
fn set_const_type_completion_info() { fn set_const_type_completion_info() {
check_relevance( check_relevance(
@ -933,8 +948,38 @@ fn main() {
); );
} }
// TODO: seems like something is going wrong here. Exapt type match has no effect #[test]
// EDIT: maybe it is actually working fn set_self_type_completion_info_with_params() {
check_relevance(
r#"
//- /lib.rs crate:dep
pub struct Struct;
impl Struct {
pub fn Function(&self, input: i32) -> bool {
false
}
}
//- /main.rs crate:main deps:dep
use dep::Struct;
fn test(input: fn(&dep::Struct, i32) -> bool) { }
fn main() {
test(Struct::Function$0);
}
"#,
expect![[r#"
me Function [type]
"#]],
);
}
#[test] #[test]
fn set_self_type_completion_info() { fn set_self_type_completion_info() {
check_relevance( check_relevance(
@ -964,34 +1009,26 @@ fn func(input: Struct) { }
); );
} }
// TODO: how do we actually test builtins?
#[test] #[test]
fn set_builtin_type_completion_info() { fn set_builtin_type_completion_info() {
check_relevance( check_relevance(
r#" r#"
//- /lib.rs crate:dep //- /main.rs crate:main
pub mod test_mod_b { fn test(input: bool) { }
static STATIC: i32 = 5; pub Input: bool = false;
}
pub mod test_mod_a {
static STATIC: &str = "test";
}
//- /main.rs crate:main deps:dep
fn test(input: i32) { }
fn main() { fn main() {
test(STATIC$0); let input = false;
let inputbad = 3;
test(inp$0);
} }
"#, "#,
expect![[r#" expect![[r#"
lc input [type+name+local]
lc inputbad [local]
fn main() [] fn main() []
fn test() [] fn test() []
md dep []
"#]], "#]],
); );
} }

View file

@ -1,6 +1,6 @@
//! Renderer for function calls. //! Renderer for function calls.
use hir::{db::HirDatabase, AsAssocItem, Callable, HirDisplay, Type}; use hir::{db::HirDatabase, AsAssocItem, HirDisplay};
use ide_db::{SnippetCap, SymbolKind}; use ide_db::{SnippetCap, SymbolKind};
use itertools::Itertools; use itertools::Itertools;
use stdx::{format_to, to_lower_snake_case}; use stdx::{format_to, to_lower_snake_case};
@ -8,13 +8,9 @@ use syntax::{AstNode, SmolStr};
use crate::{ use crate::{
context::{CompletionContext, DotAccess, DotAccessKind, PathCompletionCtx, PathKind}, context::{CompletionContext, DotAccess, DotAccessKind, PathCompletionCtx, PathKind},
item::{ item::{Builder, CompletionItem, CompletionItemKind, CompletionRelevance},
Builder, CompletionItem, CompletionItemKind, CompletionRelevance,
CompletionRelevanceTypeMatch,
},
render::{ render::{
compute_exact_name_match, compute_ref_match, compute_type_match, compute_type_match2, compute_exact_name_match, compute_function_type_match, compute_ref_match, RenderContext,
RenderContext,
}, },
CallableSnippets, CallableSnippets,
}; };
@ -85,47 +81,8 @@ fn render(
.and_then(|trait_| trait_.containing_trait_or_trait_impl(ctx.db())) .and_then(|trait_| trait_.containing_trait_or_trait_impl(ctx.db()))
.map_or(false, |trait_| completion.is_ops_trait(trait_)); .map_or(false, |trait_| completion.is_ops_trait(trait_));
// TODO next step figure out how to unify function typesk, we need to convert fndef to actual callable type
let type_match = if let Some(ref t) = completion.expected_type {
if let Some(t) = t.as_callable(db) {
let (mut param_types_exp, ret_type_exp) = (
t.params(db).into_iter().map(|(_, ty)| ty).collect::<Vec<Type>>(),
t.return_type(),
);
param_types_exp.push(ret_type_exp);
let mut param_types = func
.ty(db)
.as_callable(db)
.unwrap()
.params(db)
.into_iter()
.map(|(_, ty)| ty)
.collect::<Vec<Type>>();
param_types.push(ret_type.clone());
if param_types.len() != param_types_exp.len() {
None
} else {
if param_types_exp.iter().zip(param_types).all(|(expected_type, item_type)| {
compute_type_match2(completion, &expected_type, &item_type).is_some()
}) {
Some(CompletionRelevanceTypeMatch::CouldUnify)
} else {
None
}
}
} else {
None
}
} else {
None
};
item.set_relevance(CompletionRelevance { item.set_relevance(CompletionRelevance {
type_match, type_match: compute_function_type_match(completion, &func),
exact_name_match: compute_exact_name_match(completion, &call), exact_name_match: compute_exact_name_match(completion, &call),
is_op_method, is_op_method,
..ctx.completion_relevance() ..ctx.completion_relevance()