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 option sumtype with option variant #23656

Merged
merged 1 commit into from
Feb 5, 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
14 changes: 11 additions & 3 deletions vlib/v/gen/c/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,17 @@ fn (mut g Gen) expr_opt_with_cast(expr ast.Expr, expr_typ ast.Type, ret_typ ast.
g.write('_option_ok(&(${styp}[]) {')
}
if expr is ast.CastExpr && expr_typ.has_flag(.option) {
g.write('*((${g.base_type(expr_typ)}*)')
g.expr(expr)
g.write('.data)')
ret_sym := g.table.sym(ret_typ)
if ret_sym.kind == .sum_type {
exp_sym := g.table.sym(expr_typ)
fname := g.get_sumtype_casting_fn(expr_typ, ret_typ)
g.call_cfn_for_casting_expr(fname, expr, ret_typ.is_ptr(), ret_sym.cname,
expr_typ.is_ptr(), exp_sym.kind == .function, g.styp(expr_typ))
} else {
g.write('*((${g.base_type(expr_typ)}*)')
g.expr(expr)
g.write('.data)')
}
} else {
old_inside_opt_or_res := g.inside_opt_or_res
g.inside_opt_or_res = false
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -2585,7 +2585,7 @@ struct SumtypeCastingFn {

fn (mut g Gen) get_sumtype_casting_fn(got_ ast.Type, exp_ ast.Type) string {
mut got, exp := got_.idx_type(), exp_.idx_type()
i := int(got) | int(u32(exp) << 17) | int(u32(exp_.has_flag(.option)) << 16)
i := int(got) | int(u32(exp) << 18) | int(u32(exp_.has_flag(.option)) << 17) | int(u32(got_.has_flag(.option)) << 16)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be better commented.

It took me 10 minutes to figure out what it actually does, and why the shifts are as they are.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we have:

vlib/v/gen/c/cgen.v:194:sumtype_definitions       map[int]bool    // `_TypeA_to_sumtype_TypeB()` fns that have been generated

can we just make the map map[u64]bool instead?

exp_sym := g.table.sym(exp)
mut got_sym := g.table.sym(got)
cname := if exp == ast.int_type_idx {
Expand Down
12 changes: 12 additions & 0 deletions vlib/v/tests/options/option_sumtype_option_variant_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type Any = ?int | string | ?string

fn test_main() {
b := ?Any(?string('bar'))
assert b?.str() == "Any(Option('bar'))"

c := ?Any(string('baz'))
assert c?.str() == "Any('baz')"

d := ?Any('baz')
assert d?.str() == "Any('baz')"
}
Loading