From 560d1383d1c28fec3642a576479c5e29a8735e23 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 17 Dec 2019 00:36:28 +0300 Subject: [PATCH] fix optional struct field assignment --- vlib/compiler/parser.v | 2 +- vlib/compiler/tests/option_test.v | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index 83739b1d2a..c25a4fc9ea 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -1441,7 +1441,7 @@ fn ($v.name mut $v.typ) ${p.cur_fn.name}(...) { expr := p.cgen.cur_line[pos..] left := p.cgen.cur_line[..pos] typ := expr_type.replace('Option_', '') - p.cgen.resetln(left + 'opt_ok($expr, sizeof($typ))') + p.cgen.resetln(left + 'opt_ok(($typ[]){ $expr }, sizeof($typ))') } else if expr_type.starts_with('Option_') && p.assigned_type == expr_type['Option_'.len..] && p.tok == .key_orelse diff --git a/vlib/compiler/tests/option_test.v b/vlib/compiler/tests/option_test.v index 40e1399bc6..fdebb08903 100644 --- a/vlib/compiler/tests/option_test.v +++ b/vlib/compiler/tests/option_test.v @@ -73,7 +73,7 @@ fn test_opt_default() { fn foo_ok() ?int { return 777 -} +} fn foo_str() ?string { return 'something' @@ -81,7 +81,7 @@ fn foo_str() ?string { fn test_q() { //assert foo_ok()? == true -} +} struct Person { mut: @@ -112,3 +112,15 @@ fn test_field_or() { } assert mytitle == 'default' } + +struct Thing { +mut: + opt ?int +} + +fn test_opt_field() { + mut t := Thing{} + t.opt = 5 + val := t.opt or { return } + assert val == 5 +}