checker: improve errors for `return` count mismatch (#8686)

pull/8692/head
Nick Treleaven 2021-02-11 23:47:11 +00:00 committed by GitHub
parent 9d5243a410
commit 375efb0953
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 7 deletions

View File

@ -1301,7 +1301,7 @@ pub fn (mut c Checker) call_expr(mut call_expr ast.CallExpr) table.Type {
fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ table.Type, call_expr ast.CallExpr) {
if call_expr.args.len != 1 {
c.error('expected 1 arguments, but got $call_expr.args.len', call_expr.pos)
c.error('expected 1 argument, but got $call_expr.args.len', call_expr.pos)
// Finish early so that it doesn't fail later
return
}
@ -2359,12 +2359,13 @@ pub fn (mut c Checker) return_stmt(mut return_stmt ast.Return) {
expected_type := c.unwrap_generic(c.expected_type)
expected_type_sym := c.table.get_type_symbol(expected_type)
if return_stmt.exprs.len > 0 && c.cur_fn.return_type == table.void_type {
c.error('too many arguments to return, current function does not return anything',
return_stmt.pos)
c.error('unexpected argument, current function does not return anything', return_stmt.exprs[0].position())
return
} else if return_stmt.exprs.len == 0 && !(c.expected_type == table.void_type
|| expected_type_sym.kind == .void) {
c.error('too few arguments to return', return_stmt.pos)
stype := c.table.type_to_str(expected_type)
arg := if expected_type_sym.kind == .multi_return { 'arguments' } else { 'argument' }
c.error('expected `$stype` $arg', return_stmt.pos)
return
}
if return_stmt.exprs.len == 0 {
@ -2397,7 +2398,8 @@ pub fn (mut c Checker) return_stmt(mut return_stmt ast.Return) {
return
}
if expected_types.len > 0 && expected_types.len != got_types.len {
c.error('wrong number of return arguments', return_stmt.pos)
arg := if expected_types.len == 1 { 'argument' } else { 'arguments' }
c.error('expected $expected_types.len $arg, but got $got_types.len', return_stmt.pos)
return
}
for i, exp_type in expected_types {

View File

@ -1,11 +1,11 @@
vlib/v/checker/tests/array_map_arg_mismatch.vv:3:4: error: expected 1 arguments, but got 0
vlib/v/checker/tests/array_map_arg_mismatch.vv:3:4: error: expected 1 argument, but got 0
1 | fn main() {
2 | a := [1, 2, 3]
3 | a.map()
| ~~~~~
4 | a.map(it * 2, 3)
5 | }
vlib/v/checker/tests/array_map_arg_mismatch.vv:4:4: error: expected 1 arguments, but got 2
vlib/v/checker/tests/array_map_arg_mismatch.vv:4:4: error: expected 1 argument, but got 2
2 | a := [1, 2, 3]
3 | a.map()
4 | a.map(it * 2, 3)

View File

@ -0,0 +1,34 @@
vlib/v/checker/tests/return_count_mismatch.vv:2:9: error: unexpected argument, current function does not return anything
1 | fn v() {
2 | return 3
| ^
3 | }
4 |
vlib/v/checker/tests/return_count_mismatch.vv:7:2: error: expected `int` argument
5 | fn f() int
6 | {
7 | return
| ~~~~~~
8 | }
9 |
vlib/v/checker/tests/return_count_mismatch.vv:12:2: error: expected `(int, string)` arguments
10 | fn g() (int, string)
11 | {
12 | return
| ~~~~~~
13 | }
14 |
vlib/v/checker/tests/return_count_mismatch.vv:17:2: error: expected 1 argument, but got 2
15 | fn ff() int
16 | {
17 | return 2, ''
| ~~~~~~~~~~~~
18 | }
19 |
vlib/v/checker/tests/return_count_mismatch.vv:22:2: error: expected 2 arguments, but got 1
20 | fn gg() (int, string)
21 | {
22 | return 3
| ~~~~~~~~
23 | }
24 |

View File

@ -0,0 +1,24 @@
fn v() {
return 3
}
fn f() int
{
return
}
fn g() (int, string)
{
return
}
fn ff() int
{
return 2, ''
}
fn gg() (int, string)
{
return 3
}