Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: fix codegen for sumtype casting on selector on as cast with field non ptr #23391

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions vlib/v/tests/selector_as_cast_test.v
Original file line number Diff line number Diff line change
@@ -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
}
Loading