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

cgen: fix selector accessing field (#17567)

This commit is contained in:
Felipe Pena 2023-03-09 10:24:43 -03:00 committed by GitHub
parent 737e7f6410
commit d353dd6e8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 1 deletions

View File

@ -3461,7 +3461,12 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
g.write('*(')
}
g.expr(node.expr)
g.write('.${field_name}')
if node.expr_type.is_ptr() {
g.write('->')
} else {
g.write('.')
}
g.write(field_name)
if is_ptr {
g.write(')')
}

View File

@ -0,0 +1,27 @@
module main
struct JsonParser {
mut:
js string
tokens []string
pos int
tok_next int
tok_super ?int
}
fn new_parser(qty int) JsonParser {
return JsonParser{
tokens: []string{cap: qty}
}
}
fn (mut p JsonParser) parse() int {
j := p.tok_super or { 999 }
return j
}
fn test_main() {
mut x := new_parser(100)
i := x.parse()
assert i == 999
}