From 7b6d0e91fd371e4d8ab3cecc0787ea9237ba1229 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 11 Nov 2024 23:41:29 -0300 Subject: [PATCH] fix --- vlib/v/gen/c/cgen.v | 2 ++ .../gen/c/testdata/if_else_return.c.must_have | 10 +++++++++ vlib/v/gen/c/testdata/if_else_return.vv | 21 +++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 vlib/v/gen/c/testdata/if_else_return.c.must_have create mode 100644 vlib/v/gen/c/testdata/if_else_return.vv diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 59a50eb67c3b5c..03b587ae7eb695 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -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() diff --git a/vlib/v/gen/c/testdata/if_else_return.c.must_have b/vlib/v/gen/c/testdata/if_else_return.c.must_have new file mode 100644 index 00000000000000..03926cc7337b1d --- /dev/null +++ b/vlib/v/gen/c/testdata/if_else_return.c.must_have @@ -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; +} \ No newline at end of file diff --git a/vlib/v/gen/c/testdata/if_else_return.vv b/vlib/v/gen/c/testdata/if_else_return.vv new file mode 100644 index 00000000000000..ae29cdea50d75c --- /dev/null +++ b/vlib/v/gen/c/testdata/if_else_return.vv @@ -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}' +}