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:
parent
d971d93066
commit
ce1978ecde
@ -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`
|
||||
|
7
vlib/v/checker/tests/option_in_receiver_err.out
Normal file
7
vlib/v/checker/tests/option_in_receiver_err.out
Normal 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 | }
|
12
vlib/v/checker/tests/option_in_receiver_err.vv
Normal file
12
vlib/v/checker/tests/option_in_receiver_err.vv
Normal 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()
|
||||
}
|
Loading…
Reference in New Issue
Block a user