From 5a56ca08926bcbcd3b37538166790e6c08a4d005 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 17 Dec 2019 00:50:41 +0300 Subject: [PATCH] parser: handle operator overloading type errors --- vlib/compiler/fn.v | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/vlib/compiler/fn.v b/vlib/compiler/fn.v index eed86f6416..1ae6b719c1 100644 --- a/vlib/compiler/fn.v +++ b/vlib/compiler/fn.v @@ -284,10 +284,12 @@ fn (p mut Parser) fn_decl() { f.args << receiver p.register_var(receiver) } - // +-/* methods + // +-/* methods (operator overloading) + mut is_op := false if p.tok in [.plus, .minus, .mul, .div, .mod] { f.name = p.tok.str() p.next() + is_op = true } else { f.name = p.check_name() @@ -364,6 +366,14 @@ fn (p mut Parser) fn_decl() { } // Args (...) p.fn_args(mut f) + if is_op { + if f.args.len != 1 + 1 { // +1 is for the receiver + p.error('operator overloading methods must have only 1 argument') + } + if f.args[0].typ != f.args[1].typ { + p.error('operators must have the same types on both sides') + } + } // Returns an error? if p.tok == .not { p.next()