From 7f8f571ed23e81a07fc7f44eef18f5e1d8d77e65 Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 2 Mar 2022 01:21:55 +0800 Subject: [PATCH] cgen: fix error for optional cast to interface (#13623) --- vlib/v/gen/c/cgen.v | 2 +- .../v/tests/cast_optional_to_interface_test.v | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/cast_optional_to_interface_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 3b2ed301a7..4f2f504c16 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2001,7 +2001,7 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp_is_ptr if !got_is_ptr { if !expr.is_lvalue() || (expr is ast.Ident && (expr as ast.Ident).obj.is_simple_define_const()) { - g.write('ADDR($got_styp, (') + g.write('HEAP($got_styp, (') rparen_n += 2 } else { g.write('&') diff --git a/vlib/v/tests/cast_optional_to_interface_test.v b/vlib/v/tests/cast_optional_to_interface_test.v new file mode 100644 index 0000000000..9d869da7fe --- /dev/null +++ b/vlib/v/tests/cast_optional_to_interface_test.v @@ -0,0 +1,40 @@ +[heap] +struct Package { + str string +} + +interface Parser { + main &Package +} + +struct ParserV1 { +mut: + main &Package +} + +fn new_0_parser() ?ParserV1 { + return ParserV1{ + main: &Package{ + str: 'test' + } + } +} + +fn new_parser() ?Parser { + return Parser(new_0_parser() ?) +} + +struct Engine { + parser Parser +} + +fn test_cast_optional_to_interface() ? { + parser := new_parser() ? + assert parser.main.str == 'test' + eprintln(voidptr(parser.main)) + e := Engine{ + parser: parser + } + assert e.parser.main.str == 'test' + eprintln(voidptr(e.parser.main)) +}