diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 0fccc21111..2a3e2610d7 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -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` diff --git a/vlib/v/checker/tests/option_in_receiver_err.out b/vlib/v/checker/tests/option_in_receiver_err.out new file mode 100644 index 0000000000..111e0ddf90 --- /dev/null +++ b/vlib/v/checker/tests/option_in_receiver_err.out @@ -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 | } diff --git a/vlib/v/checker/tests/option_in_receiver_err.vv b/vlib/v/checker/tests/option_in_receiver_err.vv new file mode 100644 index 0000000000..0c1eb3c762 --- /dev/null +++ b/vlib/v/checker/tests/option_in_receiver_err.vv @@ -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() +} \ No newline at end of file