1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: handle operator overloading type errors

This commit is contained in:
Alexander Medvednikov 2019-12-17 00:50:41 +03:00
parent 560d1383d1
commit 5a56ca0892

View File

@ -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()