From 0caf668e7326a9389e7abfc8df25914505a287d1 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Thu, 24 Dec 2020 12:38:11 +0100 Subject: [PATCH] parser: error if operators are used as function names (#7532) --- vlib/v/parser/fn.v | 4 ++++ vlib/v/parser/tests/operator_normal_fn.out | 5 +++++ vlib/v/parser/tests/operator_normal_fn.vv | 3 +++ 3 files changed, 12 insertions(+) create mode 100644 vlib/v/parser/tests/operator_normal_fn.out create mode 100644 vlib/v/parser/tests/operator_normal_fn.vv diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 3fd2ec11ba..7ad8f3b090 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -257,6 +257,10 @@ fn (mut p Parser) fn_decl() ast.FnDecl { } if p.tok.kind in [.plus, .minus, .mul, .div, .mod] { name = p.tok.kind.str() // op_to_fn_name() + if rec_type == table.void_type { + p.error_with_pos('cannot use operator overloading with normal functions', + p.tok.position()) + } p.next() } // diff --git a/vlib/v/parser/tests/operator_normal_fn.out b/vlib/v/parser/tests/operator_normal_fn.out new file mode 100644 index 0000000000..9313c0a635 --- /dev/null +++ b/vlib/v/parser/tests/operator_normal_fn.out @@ -0,0 +1,5 @@ +vlib/v/parser/tests/operator_normal_fn.vv:1:4: error: cannot use operator overloading with normal functions + 1 | fn +(x int) int { + | ^ + 2 | return 5 + x + 3 | } diff --git a/vlib/v/parser/tests/operator_normal_fn.vv b/vlib/v/parser/tests/operator_normal_fn.vv new file mode 100644 index 0000000000..5a82b31bd5 --- /dev/null +++ b/vlib/v/parser/tests/operator_normal_fn.vv @@ -0,0 +1,3 @@ +fn +(x int) int { + return 5 + x +}