1
0
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:
Felipe Pena 2022-12-18 09:22:06 -03:00 committed by GitHub
parent de5ae63401
commit 04936b00c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View File

@ -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 { 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() stmt_str := g.go_before_stmt(0).trim_space()
styp := g.typ(node.typ) styp := g.typ(node.typ)
g.empty_line = true g.empty_line = true
tmp_var := g.new_tmp_var() tmp_var := g.new_tmp_var()
g.write('${styp} ${tmp_var} = ') g.write('${styp} ${tmp_var} = ')
if is_ptr {
g.write('*(')
}
g.expr(node.expr) g.expr(node.expr)
g.write('.${node.field_name}') g.write('.${node.field_name}')
if is_ptr {
g.write(')')
}
g.or_block(tmp_var, node.or_block, node.typ) g.or_block(tmp_var, node.or_block, node.typ)
g.write(stmt_str) g.write(stmt_str)
g.write(' ') g.write(' ')

View 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'
}