Skip to content

cgen: fix multi return with option type #24144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -5517,10 +5517,9 @@ fn (mut g Gen) concat_expr(node ast.ConcatExpr) {
g.write('(${styp}){')
for i, expr in node.vals {
g.write('.arg${i}=')
if types[i].has_flag(.option) && expr.is_literal() {
g.write('{.data=')
g.expr(expr)
g.write('}')
expr_typ := g.get_expr_type(expr)
if expr_typ != ast.void_type && types[i].has_flag(.option) {
g.expr_with_opt(expr, expr_typ, types[i])
} else {
old_left_is_opt := g.left_is_opt
g.left_is_opt = true
Expand Down Expand Up @@ -5942,7 +5941,7 @@ fn (mut g Gen) return_stmt(node ast.Return) {
multi_unpack += g.go_before_last_stmt()
g.write(line)
expr_styp := g.base_type(call_expr.return_type)
tmp = ('(*(${expr_styp}*)${tmp}.data)')
tmp = '(*(${expr_styp}*)${tmp}.data)'
}
expr_types := expr_sym.mr_info().types
for j, _ in expr_types {
Expand Down
15 changes: 15 additions & 0 deletions vlib/v/gen/c/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,21 @@ fn (mut g Gen) get_expr_type(cond ast.Expr) ast.Type {
}
}
}
ast.IntegerLiteral {
return ast.int_type
}
ast.BoolLiteral {
return ast.bool_type
}
ast.StringLiteral {
return ast.string_type
}
ast.CharLiteral {
return ast.char_type
}
ast.FloatLiteral {
return ast.f64_type
}
else {
return ast.void_type
}
Expand Down
10 changes: 10 additions & 0 deletions vlib/v/tests/options/option_multi_return_opt_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn fails(i int) !(int, ?int) {
return error('fails')
}

fn test_main() {
a2, b2 := fails(2) or { 22, 22 }
c2 := b2? as int
assert b2 != none
assert a2 == c2
}
Loading