cgen: fix multi return with option type (#24144)

This commit is contained in:
Felipe Pena 2025-04-06 22:19:40 -03:00 committed by GitHub
parent 1e6fdbf815
commit a69b0b7224
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 5 deletions

View File

@ -5517,10 +5517,9 @@ fn (mut g Gen) concat_expr(node ast.ConcatExpr) {
g.write('(${styp}){') g.write('(${styp}){')
for i, expr in node.vals { for i, expr in node.vals {
g.write('.arg${i}=') g.write('.arg${i}=')
if types[i].has_flag(.option) && expr.is_literal() { expr_typ := g.get_expr_type(expr)
g.write('{.data=') if expr_typ != ast.void_type && types[i].has_flag(.option) {
g.expr(expr) g.expr_with_opt(expr, expr_typ, types[i])
g.write('}')
} else { } else {
old_left_is_opt := g.left_is_opt old_left_is_opt := g.left_is_opt
g.left_is_opt = true g.left_is_opt = true
@ -5942,7 +5941,7 @@ fn (mut g Gen) return_stmt(node ast.Return) {
multi_unpack += g.go_before_last_stmt() multi_unpack += g.go_before_last_stmt()
g.write(line) g.write(line)
expr_styp := g.base_type(call_expr.return_type) 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 expr_types := expr_sym.mr_info().types
for j, _ in expr_types { for j, _ in expr_types {

View File

@ -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 { else {
return ast.void_type return ast.void_type
} }

View File

@ -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
}