Skip to content

Commit a69b0b7

Browse files
authored
cgen: fix multi return with option type (#24144)
1 parent 1e6fdbf commit a69b0b7

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

vlib/v/gen/c/cgen.v

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5517,10 +5517,9 @@ fn (mut g Gen) concat_expr(node ast.ConcatExpr) {
55175517
g.write('(${styp}){')
55185518
for i, expr in node.vals {
55195519
g.write('.arg${i}=')
5520-
if types[i].has_flag(.option) && expr.is_literal() {
5521-
g.write('{.data=')
5522-
g.expr(expr)
5523-
g.write('}')
5520+
expr_typ := g.get_expr_type(expr)
5521+
if expr_typ != ast.void_type && types[i].has_flag(.option) {
5522+
g.expr_with_opt(expr, expr_typ, types[i])
55245523
} else {
55255524
old_left_is_opt := g.left_is_opt
55265525
g.left_is_opt = true
@@ -5942,7 +5941,7 @@ fn (mut g Gen) return_stmt(node ast.Return) {
59425941
multi_unpack += g.go_before_last_stmt()
59435942
g.write(line)
59445943
expr_styp := g.base_type(call_expr.return_type)
5945-
tmp = ('(*(${expr_styp}*)${tmp}.data)')
5944+
tmp = '(*(${expr_styp}*)${tmp}.data)'
59465945
}
59475946
expr_types := expr_sym.mr_info().types
59485947
for j, _ in expr_types {

vlib/v/gen/c/comptime.v

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,21 @@ fn (mut g Gen) get_expr_type(cond ast.Expr) ast.Type {
472472
}
473473
}
474474
}
475+
ast.IntegerLiteral {
476+
return ast.int_type
477+
}
478+
ast.BoolLiteral {
479+
return ast.bool_type
480+
}
481+
ast.StringLiteral {
482+
return ast.string_type
483+
}
484+
ast.CharLiteral {
485+
return ast.char_type
486+
}
487+
ast.FloatLiteral {
488+
return ast.f64_type
489+
}
475490
else {
476491
return ast.void_type
477492
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn fails(i int) !(int, ?int) {
2+
return error('fails')
3+
}
4+
5+
fn test_main() {
6+
a2, b2 := fails(2) or { 22, 22 }
7+
c2 := b2? as int
8+
assert b2 != none
9+
assert a2 == c2
10+
}

0 commit comments

Comments
 (0)