From e8ca2e62a779205ba6a24bf37158f0445c129dd3 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 4 Feb 2023 17:38:07 +0800 Subject: [PATCH] checker: fix if guard with struct option fntype field (#17220) --- vlib/v/checker/fn.v | 6 +++++- ...ard_with_struct_option_fntype_field_test.v | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/if_guard_with_struct_option_fntype_field_test.v diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 5d026e14c8..eea915efa4 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -800,7 +800,11 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast. typ = obj.smartcasts.last() } else { if obj.typ == 0 { - typ = c.expr(obj.expr) + if obj.expr is ast.IfGuardExpr { + typ = c.expr(obj.expr.expr) + } else { + typ = c.expr(obj.expr) + } } else { typ = obj.typ } diff --git a/vlib/v/tests/if_guard_with_struct_option_fntype_field_test.v b/vlib/v/tests/if_guard_with_struct_option_fntype_field_test.v new file mode 100644 index 0000000000..0320bd26c1 --- /dev/null +++ b/vlib/v/tests/if_guard_with_struct_option_fntype_field_test.v @@ -0,0 +1,19 @@ +struct Foo { + f ?fn (int) int +} + +fn t1(a int) int { + println(a) + return a +} + +fn test_if_guard_with_struct_option_fntype_field() { + foo := Foo{t1} + + if ff := foo.f { + ret := ff(22) + assert ret == 22 + } else { + assert false + } +}