Skip to content

Commit f9106a8

Browse files
authored
cgen: fix option var nested unwrapping from sumtype (fix #23478) (#23485)
1 parent 12d8f17 commit f9106a8

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

‎vlib/v/gen/c/cgen.v‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5025,23 +5025,31 @@ fn (mut g Gen) ident(node ast.Ident) {
50255025
if is_auto_heap {
50265026
g.write('(*(')
50275027
}
5028+
is_option = is_option || node.obj.orig_type.has_flag(.option)
50285029
if node.obj.smartcasts.len > 0 {
50295030
obj_sym := g.table.sym(g.unwrap_generic(node.obj.typ))
50305031
if !prevent_sum_type_unwrapping_once {
5032+
nested_unwrap := node.obj.smartcasts.len > 1
5033+
if is_option && nested_unwrap && obj_sym.kind == .sum_type {
5034+
g.write('*(')
5035+
}
50315036
for i, typ in node.obj.smartcasts {
5032-
is_option_unwrap := is_option && typ == node.obj.typ.clear_flag(.option)
5037+
is_option_unwrap := i == 0 && is_option
5038+
&& typ == node.obj.orig_type.clear_flag(.option)
50335039
g.write('(')
50345040
if i == 0 && node.obj.is_unwrapped && node.obj.ct_type_var == .smartcast {
50355041
ctyp := g.unwrap_generic(g.type_resolver.get_type(node))
50365042
g.write('*(${g.base_type(ctyp)}*)(')
50375043
}
50385044
if obj_sym.kind == .sum_type && !is_auto_heap {
50395045
if is_option {
5040-
if !is_option_unwrap {
5041-
g.write('*(')
5046+
if i == 0 {
5047+
if !is_option_unwrap {
5048+
g.write('*(')
5049+
}
5050+
styp := g.base_type(node.obj.typ)
5051+
g.write('*(${styp}*)')
50425052
}
5043-
styp := g.base_type(node.obj.typ)
5044-
g.write('*(${styp}*)')
50455053
} else if !g.arg_no_auto_deref {
50465054
g.write('*')
50475055
}
@@ -5082,7 +5090,9 @@ fn (mut g Gen) ident(node ast.Ident) {
50825090
g.write('${dot}_${sym.cname}')
50835091
} else {
50845092
if is_option && !node.obj.is_unwrapped {
5085-
g.write('.data')
5093+
if i == 0 {
5094+
g.write('.data')
5095+
}
50865096
if !is_option_unwrap {
50875097
g.write(')')
50885098
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
type Foo = string | int | f32
2+
3+
fn bar(log ?Foo) {
4+
if log != none {
5+
dump(log)
6+
assert typeof(log).name == 'Foo'
7+
if log is string {
8+
dump(log)
9+
assert true
10+
return
11+
} else {
12+
assert false
13+
}
14+
}
15+
assert false
16+
}
17+
18+
fn test_main() {
19+
bar('foo')
20+
}

0 commit comments

Comments
 (0)