From 3fc4ab9b9e09e19caf98d355395a213f54ea3af4 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 6 Jan 2025 07:39:16 -0300 Subject: [PATCH 1/2] fix --- vlib/v/gen/c/cgen.v | 4 ++++ vlib/v/tests/selector_as_cast_test.v | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 vlib/v/tests/selector_as_cast_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index d67a4c6ec1b183..1519fa9c70f74c 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2713,6 +2713,10 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp_is_ptr // Note: the `_to_sumtype_` family of functions do call memdup internally, making // another duplicate with the HEAP macro is redundant, so use ADDR instead: if expr.is_as_cast() { + if got_is_ptr && expr is ast.SelectorExpr { + // (var as Type).field_non_ptr + // g.write('&') + } old_inside_smartcast := g.inside_smartcast g.inside_smartcast = true defer { diff --git a/vlib/v/tests/selector_as_cast_test.v b/vlib/v/tests/selector_as_cast_test.v new file mode 100644 index 00000000000000..d56b03c9ff9219 --- /dev/null +++ b/vlib/v/tests/selector_as_cast_test.v @@ -0,0 +1,26 @@ +struct Foo { + expr SumType +} + +struct Bar { + expr SumType +} + +type SumType = Foo | string | Bar +type SumType2 = SumType | int + +struct Gen {} + +fn (g Gen) t(arg SumType2) { +} + +fn test_main() { + gen := Gen{} + s := Bar{ + expr: Foo{ + expr: 'foobar' + } + } + gen.t((s.expr as Foo).expr) + assert true +} From 3d970888f57e5ba6158d13efd6e1def8566dfb90 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 6 Jan 2025 07:40:24 -0300 Subject: [PATCH 2/2] fix --- vlib/v/gen/c/cgen.v | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 1519fa9c70f74c..03e560de8866a9 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2713,9 +2713,9 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp_is_ptr // Note: the `_to_sumtype_` family of functions do call memdup internally, making // another duplicate with the HEAP macro is redundant, so use ADDR instead: if expr.is_as_cast() { - if got_is_ptr && expr is ast.SelectorExpr { + if !got_is_ptr && expr is ast.SelectorExpr { // (var as Type).field_non_ptr - // g.write('&') + g.write('&') } old_inside_smartcast := g.inside_smartcast g.inside_smartcast = true