From 9de852160a266c2ec8f5272dc699d2bd62a9efc2 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 10 Aug 2023 22:24:03 +0800 Subject: [PATCH 1/2] 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 +} From fa21ac80468faf05a165e93f73f6f07cc00ec1a5 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 10 Aug 2023 22:36:07 +0800 Subject: [PATCH 2/2] fmt struct_field_default_fn_type_value_init_test.v --- vlib/v/tests/struct_field_default_fn_type_value_init_test.v | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 index 380de6250f..77371f5f78 100644 --- 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 @@ -1,5 +1,5 @@ struct Flip { - name string = 'NULL' + name string = 'NULL' execute fn () ! = unsafe { nil } } @@ -7,7 +7,7 @@ fn (flip Flip) exec() ! { if isnil(flip.execute) { return } - + println('Executing ${flip.name}') flip.execute()! } @@ -19,7 +19,7 @@ fn test_struct_field_default_fn_type_value() { println('Hello, World!') } } - + fl.exec()! assert true }