From 9de852160a266c2ec8f5272dc699d2bd62a9efc2 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 10 Aug 2023 22:24:03 +0800 Subject: [PATCH] parser: fix struct field fn type with default value --- vlib/v/parser/parse_type.v | 2 +- ...ct_field_default_fn_type_value_init_test.v | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/struct_field_default_fn_type_value_init_test.v diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index a165616197..3b9ad7d259 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -424,7 +424,7 @@ fn (mut p Parser) parse_type() ast.Type { is_required_field := p.inside_struct_field_decl && p.tok.kind == .lsbr && p.peek_tok.kind == .name && p.peek_tok.lit == 'required' - if p.tok.line_nr > line_nr || p.tok.kind in [.comma, .rpar] || is_required_field { + if p.tok.line_nr > line_nr || p.tok.kind in [.comma, .rpar, .assign] || is_required_field { mut typ := ast.void_type if is_option { typ = typ.set_flag(.option) diff --git a/vlib/v/tests/struct_field_default_fn_type_value_init_test.v b/vlib/v/tests/struct_field_default_fn_type_value_init_test.v new file mode 100644 index 0000000000..380de6250f --- /dev/null +++ b/vlib/v/tests/struct_field_default_fn_type_value_init_test.v @@ -0,0 +1,25 @@ +struct Flip { + name string = 'NULL' + execute fn () ! = unsafe { nil } +} + +fn (flip Flip) exec() ! { + if isnil(flip.execute) { + return + } + + println('Executing ${flip.name}') + flip.execute()! +} + +fn test_struct_field_default_fn_type_value() { + fl := Flip{ + name: 'a function' + execute: fn () ! { + println('Hello, World!') + } + } + + fl.exec()! + assert true +}