mirror of
https://github.com/vlang/v.git
synced 2025-08-04 02:07:28 -04:00
This commit is contained in:
parent
12d8f173a4
commit
f9106a86fb
@ -5025,11 +5025,17 @@ fn (mut g Gen) ident(node ast.Ident) {
|
|||||||
if is_auto_heap {
|
if is_auto_heap {
|
||||||
g.write('(*(')
|
g.write('(*(')
|
||||||
}
|
}
|
||||||
|
is_option = is_option || node.obj.orig_type.has_flag(.option)
|
||||||
if node.obj.smartcasts.len > 0 {
|
if node.obj.smartcasts.len > 0 {
|
||||||
obj_sym := g.table.sym(g.unwrap_generic(node.obj.typ))
|
obj_sym := g.table.sym(g.unwrap_generic(node.obj.typ))
|
||||||
if !prevent_sum_type_unwrapping_once {
|
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 {
|
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('(')
|
g.write('(')
|
||||||
if i == 0 && node.obj.is_unwrapped && node.obj.ct_type_var == .smartcast {
|
if i == 0 && node.obj.is_unwrapped && node.obj.ct_type_var == .smartcast {
|
||||||
ctyp := g.unwrap_generic(g.type_resolver.get_type(node))
|
ctyp := g.unwrap_generic(g.type_resolver.get_type(node))
|
||||||
@ -5037,11 +5043,13 @@ fn (mut g Gen) ident(node ast.Ident) {
|
|||||||
}
|
}
|
||||||
if obj_sym.kind == .sum_type && !is_auto_heap {
|
if obj_sym.kind == .sum_type && !is_auto_heap {
|
||||||
if is_option {
|
if is_option {
|
||||||
|
if i == 0 {
|
||||||
if !is_option_unwrap {
|
if !is_option_unwrap {
|
||||||
g.write('*(')
|
g.write('*(')
|
||||||
}
|
}
|
||||||
styp := g.base_type(node.obj.typ)
|
styp := g.base_type(node.obj.typ)
|
||||||
g.write('*(${styp}*)')
|
g.write('*(${styp}*)')
|
||||||
|
}
|
||||||
} else if !g.arg_no_auto_deref {
|
} else if !g.arg_no_auto_deref {
|
||||||
g.write('*')
|
g.write('*')
|
||||||
}
|
}
|
||||||
@ -5082,7 +5090,9 @@ fn (mut g Gen) ident(node ast.Ident) {
|
|||||||
g.write('${dot}_${sym.cname}')
|
g.write('${dot}_${sym.cname}')
|
||||||
} else {
|
} else {
|
||||||
if is_option && !node.obj.is_unwrapped {
|
if is_option && !node.obj.is_unwrapped {
|
||||||
|
if i == 0 {
|
||||||
g.write('.data')
|
g.write('.data')
|
||||||
|
}
|
||||||
if !is_option_unwrap {
|
if !is_option_unwrap {
|
||||||
g.write(')')
|
g.write(')')
|
||||||
}
|
}
|
||||||
|
20
vlib/v/tests/options/option_nested_unwrapping_test.v
Normal file
20
vlib/v/tests/options/option_nested_unwrapping_test.v
Normal file
@ -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')
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user