parser: error if operators are used as function names (#7532)

pull/7540/head
Lukas Neubert 2020-12-24 12:38:11 +01:00 committed by GitHub
parent 691e6f9d3f
commit 0caf668e73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 0 deletions

View File

@ -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()
}
// <T>

View File

@ -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 | }

View File

@ -0,0 +1,3 @@
fn +(x int) int {
return 5 + x
}