From 532e5c45bfb4371f7eacdabf7d41f15631f3457c Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Fri, 25 Mar 2022 15:55:55 +0000 Subject: [PATCH] v/parser: error if parameter name starts with a capital Before the error was confusing for `fn g(T v)`: error: cannot use `int literal` as `v` in argument 1 to `g` --- vlib/v/parser/fn.v | 3 +++ vlib/v/parser/tests/fn_param_name_cap.out | 7 +++++++ vlib/v/parser/tests/fn_param_name_cap.vv | 10 ++++++++++ 3 files changed, 20 insertions(+) create mode 100644 vlib/v/parser/tests/fn_param_name_cap.out create mode 100644 vlib/v/parser/tests/fn_param_name_cap.vv diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 4f154503a8..3bb22921aa 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -833,6 +833,9 @@ fn (mut p Parser) fn_args() ([]ast.Param, bool, bool) { } mut arg_pos := [p.tok.pos()] mut arg_names := [p.check_name()] + if arg_names[0][0].is_capital() { + p.error_with_pos('parameter name must not begin with upper case letter (`${arg_names[0]}`)', p.prev_tok.pos()) + } mut type_pos := [p.tok.pos()] // `a, b, c int` for p.tok.kind == .comma { diff --git a/vlib/v/parser/tests/fn_param_name_cap.out b/vlib/v/parser/tests/fn_param_name_cap.out new file mode 100644 index 0000000000..1a3f7ccf70 --- /dev/null +++ b/vlib/v/parser/tests/fn_param_name_cap.out @@ -0,0 +1,7 @@ +vlib/v/parser/tests/fn_param_name_cap.vv:6:9: error: parameter name must not begin with upper case letter (`T`) + 4 | fn f(Type) + 5 | + 6 | fn g(T v) { + | ^ + 7 | + 8 | } diff --git a/vlib/v/parser/tests/fn_param_name_cap.vv b/vlib/v/parser/tests/fn_param_name_cap.vv new file mode 100644 index 0000000000..e436179df5 --- /dev/null +++ b/vlib/v/parser/tests/fn_param_name_cap.vv @@ -0,0 +1,10 @@ +type Type = int + +// OK +fn f(Type) + +fn g(T v) { + +} + +g(5)