mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix struct selector with or block (#16695)
This commit is contained in:
parent
de5ae63401
commit
04936b00c5
@ -3591,13 +3591,20 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
|
||||
}
|
||||
|
||||
if node.or_block.kind != .absent && !g.is_assign_lhs && g.table.sym(node.typ).kind != .chan {
|
||||
is_ptr := g.table.sym(g.unwrap_generic(node.expr_type)).kind in [.interface_, .sum_type]
|
||||
stmt_str := g.go_before_stmt(0).trim_space()
|
||||
styp := g.typ(node.typ)
|
||||
g.empty_line = true
|
||||
tmp_var := g.new_tmp_var()
|
||||
g.write('${styp} ${tmp_var} = ')
|
||||
if is_ptr {
|
||||
g.write('*(')
|
||||
}
|
||||
g.expr(node.expr)
|
||||
g.write('.${node.field_name}')
|
||||
if is_ptr {
|
||||
g.write(')')
|
||||
}
|
||||
g.or_block(tmp_var, node.or_block, node.typ)
|
||||
g.write(stmt_str)
|
||||
g.write(' ')
|
||||
|
26
vlib/v/tests/struct_selector_or_block_test.v
Normal file
26
vlib/v/tests/struct_selector_or_block_test.v
Normal file
@ -0,0 +1,26 @@
|
||||
module main
|
||||
|
||||
interface Greeting {
|
||||
tt ?string
|
||||
}
|
||||
|
||||
struct Hello {
|
||||
tt ?string = none
|
||||
value string
|
||||
}
|
||||
|
||||
struct Hi {
|
||||
tt ?string = none
|
||||
}
|
||||
|
||||
fn greet(g Greeting) string {
|
||||
// Problematic piece of code here
|
||||
// If I leave it unchecked, the compiler complains (as expected)
|
||||
t := g.tt or { 'UNKNOWN' }
|
||||
return t
|
||||
}
|
||||
|
||||
fn test_main() {
|
||||
assert greet(Hello{}) == 'UNKNOWN'
|
||||
assert greet(Hello{'cool', ''}) == 'cool'
|
||||
}
|
Loading…
Reference in New Issue
Block a user