Skip to content

Commit

Permalink
Merge branch 'vlang:master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
lv37 authored Sep 24, 2023
2 parents fb97bd4 + 28234c7 commit 025b4de
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 23 deletions.
17 changes: 16 additions & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -3637,7 +3637,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
39 changes: 17 additions & 22 deletions vlib/v/gen/c/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -501,10 +501,9 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) (bool, bool) {
return if cond.op == .and { l && r } else { l || r }, d1 && d1 == d2
}
.key_is, .not_is {
left := cond.left
if left in [ast.TypeNode, ast.Ident, ast.SelectorExpr]
if cond.left in [ast.TypeNode, ast.Ident, ast.SelectorExpr]
&& cond.right in [ast.ComptimeType, ast.TypeNode] {
exp_type := g.get_expr_type(left)
exp_type := g.get_expr_type(cond.left)
if cond.right is ast.ComptimeType {
is_true := g.table.is_comptime_type(exp_type, cond.right)
if cond.op == .key_is {
Expand Down Expand Up @@ -652,28 +651,24 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) (bool, bool) {
}
}
.gt, .lt, .ge, .le {
if cond.left is ast.SelectorExpr && cond.right is ast.IntegerLiteral {
if g.is_comptime_selector_field_name(cond.left as ast.SelectorExpr,
'indirections')
{
is_true := match cond.op {
.gt { g.comptime_for_field_type.nr_muls() > cond.right.val.i64() }
.lt { g.comptime_for_field_type.nr_muls() < cond.right.val.i64() }
.ge { g.comptime_for_field_type.nr_muls() >= cond.right.val.i64() }
.le { g.comptime_for_field_type.nr_muls() <= cond.right.val.i64() }
else { false }
}
if is_true {
g.write('1')
} else {
g.write('0')
}
return is_true, true
if cond.left is ast.SelectorExpr && cond.right is ast.IntegerLiteral
&& g.is_comptime_selector_field_name(cond.left, 'indirections') {
is_true := match cond.op {
.gt { g.comptime_for_field_type.nr_muls() > cond.right.val.i64() }
.lt { g.comptime_for_field_type.nr_muls() < cond.right.val.i64() }
.ge { g.comptime_for_field_type.nr_muls() >= cond.right.val.i64() }
.le { g.comptime_for_field_type.nr_muls() <= cond.right.val.i64() }
else { false }
}
if is_true {
g.write('1')
} else {
return true, false
g.write('0')
}
return is_true, true
} else {
return true, false
}
return true, false
}
else {
return true, false
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 025b4de

Please sign in to comment.