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

parser: disallow defining methods on option receivers - fn (x ?Type) method() { (#17351)

This commit is contained in:
Felipe Pena 2023-02-19 10:00:29 -03:00 committed by GitHub
parent d971d93066
commit ce1978ecde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 0 deletions

View File

@ -146,6 +146,9 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
}
}
if node.is_method {
if node.receiver.typ.has_flag(.option) {
c.error('option types cannot have methods', node.receiver_pos)
}
mut sym := c.table.sym(node.receiver.typ)
if sym.kind == .array && !c.is_builtin_mod && node.name == 'map' {
// TODO `node.map in array_builtin_methods`

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/option_in_receiver_err.vv:10:9: error: option types cannot have methods
8 | }
9 |
10 | fn (mut f ?Foo) t1(arr ?[]int) ?string {
| ~~~
11 | return arr?.len.str()
12 | }

View File

@ -0,0 +1,12 @@
struct Foo {
mut:
name string
}
fn (mut f Foo) t0(arr ?[]int) ?string {
return arr?.len.str()
}
fn (mut f ?Foo) t1(arr ?[]int) ?string {
return arr?.len.str()
}