1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: unwrap and c-mangle field selectors in or blocks (#17495)

This commit is contained in:
Tim Marston 2023-03-04 09:52:08 +00:00 committed by GitHub
parent 6944d54257
commit 3f8821b8d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View File

@ -3448,8 +3448,11 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
g.checker_bug('unexpected SelectorExpr.expr_type = 0', node.pos) g.checker_bug('unexpected SelectorExpr.expr_type = 0', node.pos)
} }
sym := g.table.sym(g.unwrap_generic(node.expr_type))
field_name := if sym.language == .v { c_name(node.field_name) } else { node.field_name }
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] is_ptr := sym.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
@ -3459,7 +3462,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
g.write('*(') g.write('*(')
} }
g.expr(node.expr) g.expr(node.expr)
g.write('.${node.field_name}') g.write('.${field_name}')
if is_ptr { if is_ptr {
g.write(')') g.write(')')
} }
@ -3472,7 +3475,6 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
return return
} }
sym := g.table.sym(g.unwrap_generic(node.expr_type))
// if node expr is a root ident and an optional // if node expr is a root ident and an optional
mut is_opt_or_res := node.expr is ast.Ident mut is_opt_or_res := node.expr is ast.Ident
&& (node.expr_type.has_flag(.option) || node.expr_type.has_flag(.result)) && (node.expr_type.has_flag(.option) || node.expr_type.has_flag(.result))
@ -3640,7 +3642,6 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
if node.expr_type == 0 { if node.expr_type == 0 {
verror('cgen: SelectorExpr | expr_type: 0 | it.expr: `${node.expr}` | field: `${node.field_name}` | file: ${g.file.path} | line: ${node.pos.line_nr}') verror('cgen: SelectorExpr | expr_type: 0 | it.expr: `${node.expr}` | field: `${node.field_name}` | file: ${g.file.path} | line: ${node.pos.line_nr}')
} }
field_name := if sym.language == .v { c_name(node.field_name) } else { node.field_name }
g.write(field_name) g.write(field_name)
if sum_type_deref_field != '' { if sum_type_deref_field != '' {
g.write('${sum_type_dot}${sum_type_deref_field})') g.write('${sum_type_dot}${sum_type_deref_field})')

View File

@ -22,3 +22,16 @@ fn test_main() {
assert greet(Hello{}) == 'UNKNOWN' assert greet(Hello{}) == 'UNKNOWN'
assert greet(Hello{'cool', ''}) == 'cool' assert greet(Hello{'cool', ''}) == 'cool'
} }
struct CnameTest {
long ?string
short ?string
}
fn test_cname_opt_field_selecor() {
x := CnameTest{
short: 'xyz'
}
assert (x.long or { 'NOPE' }) == 'NOPE'
assert (x.short or { 'NOPE' }) == 'xyz'
}