checker: ambiguous function call error

* checker: ambiguous function call error

* checker: ambiguous function call error wording

* checker: ambiguous functions tests newline
pull/4639/head
Henrixounez 2020-04-28 15:27:49 +02:00 committed by GitHub
parent 8a81aa5c93
commit 0afaef5818
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 51 additions and 0 deletions

View File

@ -691,6 +691,7 @@ pub fn (mut c Checker) call_fn(call_expr mut ast.CallExpr) table.Type {
// look for function in format `mod.fn` or `fn` (main/builtin)
mut f := table.Fn{}
mut found := false
mut found_in_args := false
// try prefix with current module as it would have never gotten prefixed
if !fn_name.contains('.') && call_expr.mod !in ['builtin', 'main'] {
name_prefixed := '${call_expr.mod}.$fn_name'
@ -717,6 +718,7 @@ pub fn (mut c Checker) call_fn(call_expr mut ast.CallExpr) table.Type {
info := vts.info as table.FnType
f = info.func
found = true
found_in_args = true
}
}
}
@ -725,6 +727,12 @@ pub fn (mut c Checker) call_fn(call_expr mut ast.CallExpr) table.Type {
c.error('unknown function: $fn_name', call_expr.pos)
return table.void_type
}
if !found_in_args && call_expr.mod in ['builtin', 'main'] {
scope := c.file.scope.innermost(call_expr.pos.pos)
if _ := scope.find_var(fn_name) {
c.error('ambiguous call to: `$fn_name`, may refer to fn `$fn_name` or variable `$fn_name`', call_expr.pos)
}
}
call_expr.return_type = f.return_type
if f.return_type == table.void_type && f.ctdefine.len > 0 && f.ctdefine !in c.pref.compile_defines {
call_expr.should_be_skipped = true

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/ambiguous_function_call_a.v:2:2: error: ambiguous call to: `foo`, may refer to fn `foo` or variable `foo`
1| fn foo(foo int) {
2| foo(foo + 1)
~~~~~~~~~~~~
3| }
4|

View File

@ -0,0 +1,7 @@
fn foo(foo int) {
foo(foo + 1)
}
fn main() {
foo(5)
}

View File

@ -0,0 +1,7 @@
fn foo(foo int) {
foo(foo + 1)
}
fn main() {
foo(5)
}

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/ambiguous_function_call_b.v:3:2: error: ambiguous call to: `foo`, may refer to fn `foo` or variable `foo`
1| fn foo() {
2| foo := 1
3| foo(foo)
~~~~~~~~
4| }
5|

View File

@ -0,0 +1,8 @@
fn foo() {
foo := 1
foo(foo)
}
fn main() {
foo()
}

View File

@ -0,0 +1,8 @@
fn foo() {
foo := 1
foo(foo)
}
fn main() {
foo()
}