Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Feb 15, 2025
1 parent e9c2314 commit 748d78f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
5 changes: 3 additions & 2 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ mut:
inside_global_decl bool
inside_interface_deref bool
last_tmp_call_var []string
last_if_option_type ast.Type // stores the expected if type on nested if expr
loop_depth int
ternary_names map[string]string
ternary_level_names map[string][]string
Expand Down Expand Up @@ -2068,7 +2069,8 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool {
styp = 'f64'
}
}
if stmt.typ.has_flag(.option) {
if stmt.typ.has_flag(.option)
|| (g.inside_if_option && stmt.expr is ast.IfExpr) {
g.writeln('')
g.write('${tmp_var} = ')
g.expr(stmt.expr)
Expand All @@ -2082,7 +2084,6 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool {
}
styp = g.base_type(ret_typ)
if stmt.expr is ast.CallExpr && stmt.expr.is_noreturn {
g.expr(stmt.expr)
g.writeln(';')
} else {
g.write('_option_ok(&(${styp}[]) { ')
Expand Down
25 changes: 21 additions & 4 deletions vlib/v/gen/c/if.v
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,26 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
// easier to use a temp var, than do C tricks with commas, introduce special vars etc
// (as it used to be done).
// Always use this in -autofree, since ?: can have tmp expressions that have to be freed.
needs_tmp_var := g.need_tmp_var_in_if(node)
needs_tmp_var := g.inside_if_option || g.need_tmp_var_in_if(node)
needs_conds_order := g.needs_conds_order(node)
tmp := if node.typ != ast.void_type && needs_tmp_var { g.new_tmp_var() } else { '' }
tmp := if g.inside_if_option || (node.typ != ast.void_type && needs_tmp_var) {
g.new_tmp_var()
} else {
''
}
mut cur_line := ''
mut raw_state := false
if needs_tmp_var {
mut styp := g.styp(node.typ)
if node.typ.has_flag(.option) {
if g.inside_if_option || node.typ.has_flag(.option) {
raw_state = g.inside_if_option
if node.typ != ast.void_type {
tmp_if_option_type := g.last_if_option_type
g.last_if_option_type = node.typ
defer {
g.last_if_option_type = tmp_if_option_type
}
}
defer {
g.inside_if_option = raw_state
}
Expand All @@ -210,7 +221,13 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
cur_line = g.go_before_last_stmt()
g.empty_line = true
if tmp != '' {
g.writeln('${styp} ${tmp}; /* if prepend */')
if node.typ == ast.void_type && g.last_if_option_type != 0 {
// nested if on return stmt
g.write2(g.styp(g.last_if_option_type), ' ')
} else {
g.write('${styp} ')
}
g.writeln('${tmp}; /* if prepend */')
}
if g.infix_left_var_name.len > 0 {
g.writeln('if (${g.infix_left_var_name}) {')
Expand Down
41 changes: 41 additions & 0 deletions vlib/v/tests/nested_if_return_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
struct Message {
prio bool
}

struct Throttler[T] {
max_wait int
mut:
wait_counter int
low_prio_requests []T
high_prio_requests []T
}

fn new_throttler[T](max_wait int) Throttler[T] {
return Throttler[T]{max_wait, 0, []T{len: 1, init: T{}}, []T{len: 1, init: T{}}}
}

fn pop[T](mut t Throttler[T]) ?T {
if t.wait_counter <= t.max_wait {
return if t.high_prio_requests.len > 10 {
t.high_prio_requests.pop()
} else {
t.wait_counter = 0
if t.low_prio_requests.len > 0 {
t.low_prio_requests.pop()
} else {
none
}
}
} else {
return none
}
}

fn test_main() {
c := chan Message{}
mut throttler := new_throttler[Message](10)
msg := pop(mut throttler) or { return }
assert msg.str() == 'Message{
prio: false
}'
}

0 comments on commit 748d78f

Please sign in to comment.