Skip to content

Commit

Permalink
cgen: fix option var nested unwrapping from sumtype (fix #23478) (#23485
Browse files Browse the repository at this point in the history
)
  • Loading branch information
felipensp authored Jan 16, 2025
1 parent 12d8f17 commit f9106a8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
22 changes: 16 additions & 6 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -5025,23 +5025,31 @@ fn (mut g Gen) ident(node ast.Ident) {
if is_auto_heap {
g.write('(*(')
}
is_option = is_option || node.obj.orig_type.has_flag(.option)
if node.obj.smartcasts.len > 0 {
obj_sym := g.table.sym(g.unwrap_generic(node.obj.typ))
if !prevent_sum_type_unwrapping_once {
nested_unwrap := node.obj.smartcasts.len > 1
if is_option && nested_unwrap && obj_sym.kind == .sum_type {
g.write('*(')
}
for i, typ in node.obj.smartcasts {
is_option_unwrap := is_option && typ == node.obj.typ.clear_flag(.option)
is_option_unwrap := i == 0 && is_option
&& typ == node.obj.orig_type.clear_flag(.option)
g.write('(')
if i == 0 && node.obj.is_unwrapped && node.obj.ct_type_var == .smartcast {
ctyp := g.unwrap_generic(g.type_resolver.get_type(node))
g.write('*(${g.base_type(ctyp)}*)(')
}
if obj_sym.kind == .sum_type && !is_auto_heap {
if is_option {
if !is_option_unwrap {
g.write('*(')
if i == 0 {
if !is_option_unwrap {
g.write('*(')
}
styp := g.base_type(node.obj.typ)
g.write('*(${styp}*)')
}
styp := g.base_type(node.obj.typ)
g.write('*(${styp}*)')
} else if !g.arg_no_auto_deref {
g.write('*')
}
Expand Down Expand Up @@ -5082,7 +5090,9 @@ fn (mut g Gen) ident(node ast.Ident) {
g.write('${dot}_${sym.cname}')
} else {
if is_option && !node.obj.is_unwrapped {
g.write('.data')
if i == 0 {
g.write('.data')
}
if !is_option_unwrap {
g.write(')')
}
Expand Down
20 changes: 20 additions & 0 deletions vlib/v/tests/options/option_nested_unwrapping_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
type Foo = string | int | f32

fn bar(log ?Foo) {
if log != none {
dump(log)
assert typeof(log).name == 'Foo'
if log is string {
dump(log)
assert true
return
} else {
assert false
}
}
assert false
}

fn test_main() {
bar('foo')
}

0 comments on commit f9106a8

Please sign in to comment.