cgen: fix fixed array option cast with none (fix #23164) (#23168)

This commit is contained in:
Felipe Pena 2024-12-15 17:44:56 -03:00 committed by GitHub
parent ca1e23abdd
commit 066f825dea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 3 deletions

View File

@ -5364,10 +5364,14 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) {
g.expr(node.expr)
g.write('))')
} else if sym.kind == .alias && g.table.final_sym(node.typ).kind == .array_fixed {
if node.expr is ast.ArrayInit && g.assign_op != .decl_assign {
g.write('(${g.styp(node.expr.typ)})')
if node.typ.has_flag(.option) {
g.expr_with_opt(node.expr, expr_type, node.typ)
} else {
if node.expr is ast.ArrayInit && g.assign_op != .decl_assign {
g.write('(${g.styp(node.expr.typ)})')
}
g.expr(node.expr)
}
g.expr(node.expr)
} else if expr_type == ast.bool_type && node.typ.is_int() {
styp := g.styp(node_typ)
g.write('(${styp}[]){(')

View File

@ -0,0 +1,24 @@
type Bytes = [3]u8
type Strs = [3]string
fn test_none() {
b := ?Bytes(none)
println(b)
assert b == none
c := ?Strs(none)
println(c)
assert c == none
}
fn test_non_none() {
b := ?Bytes([u8(1), 2, 3]!)
println(b)
assert b != none
assert b?[2] == 3
c := ?Strs(['a', 'b', 'c']!)
println(c)
assert c != none
assert c?[2] == 'c'
}