Skip to content

Commit

Permalink
[analyzer] fix anonymous functions are self-invoking type mismatch (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lycs-D authored Mar 21, 2024
1 parent 366d30c commit 3439620
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
14 changes: 9 additions & 5 deletions analyzer/psi/TypeInferer.v
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub fn (t &TypeInferer) infer_type(elem ?PsiElement) types.Type {

pub fn (t &TypeInferer) infer_type_impl(elem ?PsiElement) types.Type {
element := elem or { return types.unknown_type }

mut visited := map[string]types.Type{}

if element.node.type_name in [
.in_expression,
.not_in_expression,
Expand Down Expand Up @@ -179,7 +182,6 @@ pub fn (t &TypeInferer) infer_type_impl(elem ?PsiElement) types.Type {

if element is TypeInitializer {
type_element := element.find_child_by_type(.plain_type) or { return types.unknown_type }
mut visited := map[string]types.Type{}
return t.convert_type(type_element, mut visited)
}

Expand Down Expand Up @@ -252,6 +254,12 @@ pub fn (t &TypeInferer) infer_type_impl(elem ?PsiElement) types.Type {
}

if element is CallExpression {
if grand := element.expression() {
if grand is FunctionLiteral {
signature := grand.signature() or { return types.unknown_type }
return t.convert_type(signature.result(), mut visited)
}
}
return t.infer_call_expr_type(element)
}

Expand Down Expand Up @@ -347,21 +355,17 @@ pub fn (t &TypeInferer) infer_type_impl(elem ?PsiElement) types.Type {
}

if element is TypeReferenceExpression {
mut visited := map[string]types.Type{}
return t.infer_type_reference_type(element, mut visited)
}

if element is GlobalVarDefinition {
type_element := element.find_child_by_type_or_stub(.plain_type) or {
return types.unknown_type
}
mut visited := map[string]types.Type{}
return t.convert_type(type_element, mut visited)
}

if element is EmbeddedDefinition {
mut visited := map[string]types.Type{}

if qualified_type := element.find_child_by_type_or_stub(.qualified_type) {
return t.convert_type_inner(qualified_type, mut visited)
}
Expand Down
3 changes: 3 additions & 0 deletions tests/testdata/types/function_literal.v
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ fn calls() {

func1 := fn (i int) int {}
expr_type(func1(), 'int')

func2 := fn (i int) int {}()
expr_type(func2, 'int')
}

0 comments on commit 3439620

Please sign in to comment.