From 9ad1c2f9220b1214bb229231e3e642448840d107 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 13 Mar 2023 11:05:56 -0300 Subject: [PATCH] checker: fix missing option function field checking (#17601) --- vlib/v/checker/fn.v | 3 +++ vlib/v/checker/tests/option_fn_field_err.out | 6 ++++++ vlib/v/checker/tests/option_fn_field_err.vv | 8 ++++++++ 3 files changed, 17 insertions(+) create mode 100644 vlib/v/checker/tests/option_fn_field_err.out create mode 100644 vlib/v/checker/tests/option_fn_field_err.vv 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