Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -3447,8 +3447,11 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
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 {
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()
styp := g.typ(node.typ)
g.empty_line = true
Expand All @@ -3458,7 +3461,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
g.write('*(')
}
g.expr(node.expr)
g.write('.${node.field_name}')
g.write('.${field_name}')
if is_ptr {
g.write(')')
}
Expand All @@ -3471,7 +3474,6 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
return
}

sym := g.table.sym(g.unwrap_generic(node.expr_type))
// if node expr is a root ident and an optional
mut is_opt_or_res := node.expr is ast.Ident
&& (node.expr_type.has_flag(.option) || node.expr_type.has_flag(.result))
Expand Down Expand Up @@ -3639,7 +3641,6 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
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}')
}
field_name := if sym.language == .v { c_name(node.field_name) } else { node.field_name }
g.write(field_name)
if sum_type_deref_field != '' {
g.write('${sum_type_dot}${sum_type_deref_field})')
Expand Down
13 changes: 13 additions & 0 deletions vlib/v/tests/struct_selector_or_block_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@ fn test_main() {
assert greet(Hello{}) == 'UNKNOWN'
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'
}