From 49c322f120c421240e0d6409a715dcc4d683c9a2 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Tue, 8 Sep 2020 18:34:17 +0100 Subject: [PATCH] checker: simplify argument qualifier errors (#6321) --- vlib/v/ast/ast.v | 2 +- vlib/v/checker/checker.v | 25 +++++-------------------- vlib/v/checker/tests/mut_arg.out | 25 +++++++++++++++++++++++++ vlib/v/checker/tests/mut_arg.vv | 11 +++++++++++ vlib/v/table/table.v | 3 ++- 5 files changed, 44 insertions(+), 22 deletions(-) create mode 100644 vlib/v/checker/tests/mut_arg.out create mode 100644 vlib/v/checker/tests/mut_arg.vv diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index ddc3340046..72ae6e5822 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -242,7 +242,7 @@ pub struct FnDecl { pub: name string mod string - args []table.Arg + args []table.Arg // parameters is_deprecated bool is_pub bool is_variadic bool diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index a4435718b4..87d80c97b9 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1331,31 +1331,16 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type { if call_arg.is_mut { c.fail_if_immutable(call_arg.expr) if !arg.is_mut { - mut words := 'mutable' - mut tok := 'mut' - if call_arg.share == .shared_t { - words = 'shared' - tok = 'shared' - } else if call_arg.share == .atomic_t { - words = 'atomic' - tok = 'atomic' - } - c.error('`$arg.name` argument is not $words, `$tok` is not needed`', call_arg.expr.position()) + tok := call_arg.share.str() + c.error('`$call_expr.name` parameter `$arg.name` is not `$tok`, `$tok` is not needed`', + call_arg.expr.position()) } else if arg.typ.share() != call_arg.share { c.error('wrong shared type', call_arg.expr.position()) } } else { if arg.is_mut && (!call_arg.is_mut || arg.typ.share() != call_arg.share) { - mut words := ' mutable' - mut tok := 'mut' - if arg.typ.share() == .shared_t { - words = ' shared' - tok = 'shared' - } else if arg.typ.share() == .atomic_t { - words = 'n atomic' - tok = 'atomic' - } - c.error('`$arg.name` is a$words argument, you need to provide `$tok`: `${call_expr.name}($tok ...)`', + tok := call_arg.share.str() + c.error('`$call_expr.name` parameter `$arg.name` is `$tok`, you need to provide `$tok` e.g. `$tok arg${i+1}`', call_arg.expr.position()) } } diff --git a/vlib/v/checker/tests/mut_arg.out b/vlib/v/checker/tests/mut_arg.out new file mode 100644 index 0000000000..c3d2f1b3b3 --- /dev/null +++ b/vlib/v/checker/tests/mut_arg.out @@ -0,0 +1,25 @@ +vlib/v/checker/tests/mut_arg.vv:6:3: error: `f` parameter `par` is `mut`, you need to provide `mut` e.g. `mut arg1` + 4 | } + 5 | + 6 | f([3,4]) + | ~~~~~ + 7 | mut a := [1,2] + 8 | f(a) +vlib/v/checker/tests/mut_arg.vv:8:3: error: `f` parameter `par` is `mut`, you need to provide `mut` e.g. `mut arg1` + 6 | f([3,4]) + 7 | mut a := [1,2] + 8 | f(a) + | ^ + 9 | + 10 | g(mut [3,4]) +vlib/v/checker/tests/mut_arg.vv:10:7: error: `g` parameter `par` is not `mut`, `mut` is not needed` + 8 | f(a) + 9 | + 10 | g(mut [3,4]) + | ~~~~~ + 11 | g(mut a) +vlib/v/checker/tests/mut_arg.vv:11:7: error: `g` parameter `par` is not `mut`, `mut` is not needed` + 9 | + 10 | g(mut [3,4]) + 11 | g(mut a) + | ^ diff --git a/vlib/v/checker/tests/mut_arg.vv b/vlib/v/checker/tests/mut_arg.vv new file mode 100644 index 0000000000..0a2afdd844 --- /dev/null +++ b/vlib/v/checker/tests/mut_arg.vv @@ -0,0 +1,11 @@ +fn f(mut par []int) { +} +fn g(par []int) { +} + +f([3,4]) +mut a := [1,2] +f(a) + +g(mut [3,4]) +g(mut a) diff --git a/vlib/v/table/table.v b/vlib/v/table/table.v index b2f5ac84e0..230bd4e3fa 100644 --- a/vlib/v/table/table.v +++ b/vlib/v/table/table.v @@ -22,7 +22,7 @@ pub mut: pub struct Fn { pub: - args []Arg + args []Arg // parameters return_type Type return_type_source_name string is_variadic bool @@ -39,6 +39,7 @@ pub mut: name string } +// parameter pub struct Arg { pub: pos token.Position