cgen: fix assigning ArrayInit to fixed array: `a = [3]int` (#6115)
parent
078ab47a9a
commit
6016f28171
|
@ -735,11 +735,11 @@ pub:
|
||||||
elem_type_pos token.Position
|
elem_type_pos token.Position
|
||||||
exprs []Expr
|
exprs []Expr
|
||||||
is_fixed bool
|
is_fixed bool
|
||||||
has_val bool
|
has_val bool // fixed size literal `[expr, expr]!!`
|
||||||
mod string
|
mod string
|
||||||
len_expr Expr
|
len_expr Expr
|
||||||
cap_expr Expr
|
cap_expr Expr
|
||||||
default_expr Expr
|
default_expr Expr // init: expr
|
||||||
has_len bool
|
has_len bool
|
||||||
has_cap bool
|
has_cap bool
|
||||||
has_default bool
|
has_default bool
|
||||||
|
|
|
@ -177,7 +177,7 @@ pub fn (lit &StringInterLiteral) get_fspec_braces(i int) (string, bool) {
|
||||||
return res.join(''), needs_braces
|
return res.join(''), needs_braces
|
||||||
}
|
}
|
||||||
|
|
||||||
// string representaiton of expr
|
// string representation of expr
|
||||||
pub fn (x Expr) str() string {
|
pub fn (x Expr) str() string {
|
||||||
match x {
|
match x {
|
||||||
BoolLiteral {
|
BoolLiteral {
|
||||||
|
|
|
@ -1917,7 +1917,7 @@ pub fn (mut c Checker) array_init(mut array_init ast.ArrayInit) table.Type {
|
||||||
fixed_size = cint
|
fixed_size = cint
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
c.error('non existant integer const $full_const_name while initializing the size of a static array',
|
c.error('non existent integer const $full_const_name while initializing the size of a static array',
|
||||||
array_init.pos)
|
array_init.pos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1401,12 +1401,26 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
|
||||||
}
|
}
|
||||||
} else if right_sym.kind == .array_fixed && assign_stmt.op == .assign {
|
} else if right_sym.kind == .array_fixed && assign_stmt.op == .assign {
|
||||||
right := val as ast.ArrayInit
|
right := val as ast.ArrayInit
|
||||||
|
if right.has_val {
|
||||||
for j, expr in right.exprs {
|
for j, expr in right.exprs {
|
||||||
g.expr(left)
|
g.expr(left)
|
||||||
g.write('[$j] = ')
|
g.write('[$j] = ')
|
||||||
g.expr(expr)
|
g.expr(expr)
|
||||||
g.writeln(';')
|
g.writeln(';')
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
fixed_array := right_sym.info as table.ArrayFixed
|
||||||
|
for j in 0 .. fixed_array.size {
|
||||||
|
g.expr(left)
|
||||||
|
g.write('[$j] = ')
|
||||||
|
if right.has_default {
|
||||||
|
g.expr(right.default_expr)
|
||||||
|
} else {
|
||||||
|
g.write(g.type_default(right.elem_type))
|
||||||
|
}
|
||||||
|
g.writeln(';')
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
g.assign_op = assign_stmt.op
|
g.assign_op = assign_stmt.op
|
||||||
is_inside_ternary := g.inside_ternary != 0
|
is_inside_ternary := g.inside_ternary != 0
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
fn test_fixed_array_can_be_assigned() {
|
fn test_fixed_array_can_be_assigned() {
|
||||||
x := 2.32
|
x := 2.32
|
||||||
mut v := [8]f64
|
mut v := [8]f64
|
||||||
|
assert v[1] == 0
|
||||||
v = [1.0, x, 3.0,4.0,5.0,6.0,7.0,8.0]!!
|
v = [1.0, x, 3.0,4.0,5.0,6.0,7.0,8.0]!!
|
||||||
assert v[1] == x
|
assert v[1] == x
|
||||||
|
v = [8]f64
|
||||||
|
assert v[1] == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_fixed_array_can_be_used_in_declaration() {
|
fn test_fixed_array_can_be_used_in_declaration() {
|
||||||
|
|
Loading…
Reference in New Issue