Skip to content

Commit

Permalink
cgen: fix interface with option field
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Sep 24, 2023
1 parent 7e08a50 commit 46a06bf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
17 changes: 16 additions & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -3633,7 +3633,22 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
g.write('*(')
}
g.expr(node.expr)
if node.expr_type.is_ptr() {
for i, embed in node.from_embed_types {
embed_sym := g.table.sym(embed)
embed_name := embed_sym.embed_name()
is_left_ptr := if i == 0 {
node.expr_type.is_ptr()
} else {
node.from_embed_types[i - 1].is_ptr()
}
if is_left_ptr {
g.write('->')
} else {
g.write('.')
}
g.write(embed_name)
}
if node.expr_type.is_ptr() && node.from_embed_types.len == 0 {
g.write('->')
} else {
g.write('.')
Expand Down
32 changes: 32 additions & 0 deletions vlib/v/tests/interface_with_option_field_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import time

interface InterfaceObject {
mut:
duration ?time.Duration
update()
}

struct Object {
mut:
duration ?time.Duration
}

fn (mut obj Object) update() {
duration := obj.duration or { time.second }
println(duration)
}

struct FooObject {
Object
}

fn (mut obj FooObject) update() {
duration := obj.duration or { time.millisecond * 500 }
println(duration)
}

fn test_interface_with_option_field() {
mut object := InterfaceObject(FooObject{})
println(object)
assert '${object.duration}' == 'Option(none)'
}

0 comments on commit 46a06bf

Please sign in to comment.