From f45042fa09e5bef9f5d85a38d91de28896019e5b Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 27 Aug 2022 03:38:53 +0800 Subject: [PATCH] checker: improve error message of fn args mismatch (#15550) --- vlib/v/checker/check_types.v | 7 ++++++- vlib/v/checker/tests/array_sort_with_compare_err.out | 2 +- vlib/v/checker/tests/fn_args.out | 2 +- vlib/v/checker/tests/non_matching_functional_args.out | 4 ++-- vlib/v/checker/tests/struct_field_type_err.out | 6 +++--- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index 8a8ce530d..222921873 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -389,7 +389,12 @@ pub fn (mut c Checker) check_matching_function_symbols(got_type_sym &ast.TypeSym if exp_arg_is_ptr != got_arg_is_ptr { exp_arg_pointedness := if exp_arg_is_ptr { 'a pointer' } else { 'NOT a pointer' } got_arg_pointedness := if got_arg_is_ptr { 'a pointer' } else { 'NOT a pointer' } - c.add_error_detail('`$exp_fn.name`\'s expected fn argument: `$exp_arg.name` is $exp_arg_pointedness, but the passed fn argument: `$got_arg.name` is $got_arg_pointedness') + if exp_fn.name.len == 0 { + c.add_error_detail('expected argument ${i + 1} to be $exp_arg_pointedness, but the passed argument ${ + i + 1} is $got_arg_pointedness') + } else { + c.add_error_detail('`$exp_fn.name`\'s expected argument `$exp_arg.name` to be $exp_arg_pointedness, but the passed argument `$got_arg.name` is $got_arg_pointedness') + } return false } else if exp_arg_is_ptr && got_arg_is_ptr { continue diff --git a/vlib/v/checker/tests/array_sort_with_compare_err.out b/vlib/v/checker/tests/array_sort_with_compare_err.out index 47b67389c..3e8febe9e 100644 --- a/vlib/v/checker/tests/array_sort_with_compare_err.out +++ b/vlib/v/checker/tests/array_sort_with_compare_err.out @@ -12,7 +12,7 @@ vlib/v/checker/tests/array_sort_with_compare_err.vv:4:26: error: cannot use `fn | ~~~~~~~~~~~~~~~~~ 5 | println(names) 6 | -Details: ``'s expected fn argument: `` is a pointer, but the passed fn argument: `a` is NOT a pointer +Details: expected argument 1 to be a pointer, but the passed argument 1 is NOT a pointer vlib/v/checker/tests/array_sort_with_compare_err.vv:7:26: error: cannot use `int literal` as `fn (voidptr, voidptr) int` in argument 1 to `[]string.sort_with_compare` 5 | println(names) 6 | diff --git a/vlib/v/checker/tests/fn_args.out b/vlib/v/checker/tests/fn_args.out index 532174da3..0f9b093a6 100644 --- a/vlib/v/checker/tests/fn_args.out +++ b/vlib/v/checker/tests/fn_args.out @@ -19,7 +19,7 @@ vlib/v/checker/tests/fn_args.vv:11:6: error: cannot use `fn (&int)` as `fn (int) | ~~~~~~~~~~~~~~ 12 | fun(fn (ii ...int) {}) 13 | } -Details: ``'s expected fn argument: `` is NOT a pointer, but the passed fn argument: `i` is a pointer +Details: expected argument 1 to be NOT a pointer, but the passed argument 1 is a pointer vlib/v/checker/tests/fn_args.vv:12:6: error: cannot use `fn (...int)` as `fn (int)` in argument 1 to `fun` 10 | arr([5]!) 11 | fun(fn (i &int) {}) diff --git a/vlib/v/checker/tests/non_matching_functional_args.out b/vlib/v/checker/tests/non_matching_functional_args.out index 7d155d033..4de37fb0d 100644 --- a/vlib/v/checker/tests/non_matching_functional_args.out +++ b/vlib/v/checker/tests/non_matching_functional_args.out @@ -5,7 +5,7 @@ vlib/v/checker/tests/non_matching_functional_args.vv:27:6: error: cannot use `fn | ~~~~~~~~~~~~~~~~~~ 28 | t.rename() 29 | println(t.name) -Details: `main.MyFn`'s expected fn argument: `zzzz` is NOT a pointer, but the passed fn argument: `t` is a pointer +Details: `main.MyFn`'s expected argument `zzzz` to be NOT a pointer, but the passed argument `t` is a pointer vlib/v/checker/tests/non_matching_functional_args.vv:31:6: error: cannot use `fn (mut Table)` as `fn (Table)` in argument 1 to `sum` 29 | println(t.name) 30 | }) @@ -13,4 +13,4 @@ vlib/v/checker/tests/non_matching_functional_args.vv:31:6: error: cannot use `fn | ~~~ 32 | sum(yyy) 33 | } -Details: `main.MyFn`'s expected fn argument: `zzzz` is NOT a pointer, but the passed fn argument: `mytable` is a pointer +Details: `main.MyFn`'s expected argument `zzzz` to be NOT a pointer, but the passed argument `mytable` is a pointer diff --git a/vlib/v/checker/tests/struct_field_type_err.out b/vlib/v/checker/tests/struct_field_type_err.out index 6c8e6df75..53da8cf6f 100644 --- a/vlib/v/checker/tests/struct_field_type_err.out +++ b/vlib/v/checker/tests/struct_field_type_err.out @@ -19,7 +19,7 @@ vlib/v/checker/tests/struct_field_type_err.vv:14:3: error: cannot assign to fiel | ~~~~~~~~~~~~~~~~~~~~~~~~ 15 | f2: fn (v voidptr) {} 16 | data: true -Details: ``'s expected fn argument: `` is a pointer, but the passed fn argument: `v` is NOT a pointer +Details: expected argument 1 to be a pointer, but the passed argument 1 is NOT a pointer vlib/v/checker/tests/struct_field_type_err.vv:15:3: error: cannot assign to field `f2`: expected `fn (...voidptr)`, not `fn (voidptr)` 13 | b: 0 14 | f1: fn (v ...voidptr) {} @@ -27,7 +27,7 @@ vlib/v/checker/tests/struct_field_type_err.vv:15:3: error: cannot assign to fiel | ~~~~~~~~~~~~~~~~~~~~~ 16 | data: true 17 | } -Details: ``'s expected fn argument: `` is NOT a pointer, but the passed fn argument: `v` is a pointer +Details: expected argument 1 to be NOT a pointer, but the passed argument 1 is a pointer vlib/v/checker/tests/struct_field_type_err.vv:16:3: error: cannot assign to field `data`: expected `&Data`, not `bool` 14 | f1: fn (v ...voidptr) {} 15 | f2: fn (v voidptr) {} @@ -37,7 +37,7 @@ vlib/v/checker/tests/struct_field_type_err.vv:16:3: error: cannot assign to fiel 18 | vlib/v/checker/tests/struct_field_type_err.vv:19:11: error: cannot assign to `data.n`: expected `int`, not `bool` 17 | } - 18 | + 18 | 19 | data.n = true | ~~~~ 20 | } -- 2.30.2