Return correct type for builtin functions

This commit is contained in:
Anton Medvedev
2023-08-17 18:00:31 +02:00
parent c8b485f6a8
commit 3fe12039de
2 changed files with 22 additions and 4 deletions
+1 -4
View File
@@ -139,10 +139,7 @@ func (v *visitor) IdentifierNode(node *ast.IdentifierNode) (reflect.Type, info)
return mapType, info{}
}
if fn, ok := v.config.Functions[node.Value]; ok {
// Return anyType instead of func type as we don't know the arguments yet.
// The func type can be one of the fn.Types. The type will be resolved
// when the arguments are known in CallNode.
return anyType, info{fn: fn}
return functionType, info{fn: fn}
}
if t, ok := v.config.Types[node.Value]; ok {
if t.Ambiguous {
+21
View File
@@ -962,3 +962,24 @@ func TestCheck_env_keyword(t *testing.T) {
})
}
}
func TestCheck_builtin_without_call(t *testing.T) {
tests := []struct {
input string
err string
}{
{`len + 1`, "invalid operation: + (mismatched types func(...interface {}) (interface {}, error) and int) (1:5)\n | len + 1\n | ....^"},
{`string.A`, "type func(...interface {}) (interface {}, error)[string] is undefined (1:8)\n | string.A\n | .......^"},
}
for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
tree, err := parser.Parse(test.input)
require.NoError(t, err)
_, err = checker.Check(tree, conf.New(nil))
require.Error(t, err)
require.Equal(t, test.err, err.Error())
})
}
}