diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index a3f6954ae4..a7bf394ed5 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -1899,6 +1899,9 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type { // call struct field fn type // TODO: can we use SelectorExpr for all? this dosent really belong here if field := c.table.find_field_with_embeds(left_sym, method_name) { + if field.typ.has_flag(.option) { + c.error('Option function field must be unwrapped first', node.pos) + } field_sym := c.table.sym(c.unwrap_generic(field.typ)) if field_sym.kind == .function { node.is_method = false diff --git a/vlib/v/checker/tests/option_fn_field_err.out b/vlib/v/checker/tests/option_fn_field_err.out new file mode 100644 index 0000000000..6b857c501c --- /dev/null +++ b/vlib/v/checker/tests/option_fn_field_err.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/option_fn_field_err.vv:7:6: error: Option function field must be unwrapped first + 5 | fn main() { + 6 | foo := Foo{} + 7 | foo.f(1) + | ~~~~ + 8 | } diff --git a/vlib/v/checker/tests/option_fn_field_err.vv b/vlib/v/checker/tests/option_fn_field_err.vv new file mode 100644 index 0000000000..d14019dae3 --- /dev/null +++ b/vlib/v/checker/tests/option_fn_field_err.vv @@ -0,0 +1,8 @@ +struct Foo { + f ?fn(int) +} + +fn main() { + foo := Foo{} + foo.f(1) +} \ No newline at end of file