Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Nov 12, 2024
1 parent e433d7c commit 7b6d0e9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,8 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool {
}
}
}
} else if stmt is ast.Return {
g.stmt(stmt)
}
} else {
g.set_current_pos_as_last_stmt_pos()
Expand Down
10 changes: 10 additions & 0 deletions vlib/v/gen/c/testdata/if_else_return.c.must_have
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
VV_LOCAL_SYMBOL _result_string main__empty(string s) {
_result_string _t2; /* if prepend */
if ((s).len != 0) {
_result_ok(&(string[]) { s }, (_result*)(&_t2), sizeof(string));
} else {
return (_result_string){ .is_error=true, .err=_v_error(_SLIT("empty")), .data={EMPTY_STRUCT_INITIALIZATION} };
}
_result_string _t1 = _t2;
return _t1;
}
21 changes: 21 additions & 0 deletions vlib/v/gen/c/testdata/if_else_return.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
fn empty(s string) !string {
return if s != '' {
s
} else {
return error('empty')
}
}

fn main() {
str_1 := empty('something') or {
assert false, 'something is not empty!'
return
}
assert str_1 == 'something'

str_2 := empty('') or {
println('expected error ${err}')
return
}
assert false, 'invalid accepted ${str_2}'
}

0 comments on commit 7b6d0e9

Please sign in to comment.