v.gen.c: fix assigning a fixed array variable to a map value (#10523)
parent
79879128b6
commit
afc81277be
|
@ -932,3 +932,16 @@ fn test_u64_keys() {
|
||||||
}
|
}
|
||||||
assert m.len == 0
|
assert m.len == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_map_set_fixed_array_variable() {
|
||||||
|
mut m := map[string][2]f64{}
|
||||||
|
m['A'] = [1.1, 2.2]!
|
||||||
|
println(m)
|
||||||
|
assert '$m' == "{'A': [1.1, 2.2]}"
|
||||||
|
|
||||||
|
mut m2 := map[string][2]f64{}
|
||||||
|
arr := [1.1, 2.2]!
|
||||||
|
m2['A'] = arr
|
||||||
|
println(m2)
|
||||||
|
assert '$m2' == "{'A': [1.1, 2.2]}"
|
||||||
|
}
|
||||||
|
|
|
@ -2474,7 +2474,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
right_sym := g.table.get_type_symbol(g.unwrap_generic(val_type))
|
right_sym := g.table.get_type_symbol(g.unwrap_generic(val_type))
|
||||||
is_fixed_array_copy := right_sym.kind == .array_fixed && val is ast.Ident
|
is_fixed_array_var := right_sym.kind == .array_fixed && val is ast.Ident
|
||||||
g.is_assign_lhs = true
|
g.is_assign_lhs = true
|
||||||
g.assign_op = assign_stmt.op
|
g.assign_op = assign_stmt.op
|
||||||
if val_type.has_flag(.optional) {
|
if val_type.has_flag(.optional) {
|
||||||
|
@ -2491,13 +2491,19 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
|
||||||
g.expr(val)
|
g.expr(val)
|
||||||
g.writeln(';}')
|
g.writeln(';}')
|
||||||
}
|
}
|
||||||
} else if is_fixed_array_init && assign_stmt.op == .assign {
|
} else if assign_stmt.op == .assign && (is_fixed_array_init || is_fixed_array_var) {
|
||||||
right := val as ast.ArrayInit
|
mut v_var := ''
|
||||||
v_var := g.new_tmp_var()
|
|
||||||
arr_typ := styp.trim('*')
|
arr_typ := styp.trim('*')
|
||||||
|
if is_fixed_array_init {
|
||||||
|
right := val as ast.ArrayInit
|
||||||
|
v_var = g.new_tmp_var()
|
||||||
g.write('$arr_typ $v_var = ')
|
g.write('$arr_typ $v_var = ')
|
||||||
g.expr(right)
|
g.expr(right)
|
||||||
g.writeln(';')
|
g.writeln(';')
|
||||||
|
} else {
|
||||||
|
right := val as ast.Ident
|
||||||
|
v_var = right.name
|
||||||
|
}
|
||||||
pos := g.out.len
|
pos := g.out.len
|
||||||
g.expr(left)
|
g.expr(left)
|
||||||
|
|
||||||
|
@ -2580,7 +2586,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
|
||||||
if left is ast.Ident || left is ast.SelectorExpr {
|
if left is ast.Ident || left is ast.SelectorExpr {
|
||||||
g.prevent_sum_type_unwrapping_once = true
|
g.prevent_sum_type_unwrapping_once = true
|
||||||
}
|
}
|
||||||
if !is_fixed_array_copy || is_decl {
|
if !is_fixed_array_var || is_decl {
|
||||||
if !is_decl && left.is_auto_deref_var() {
|
if !is_decl && left.is_auto_deref_var() {
|
||||||
g.write('*')
|
g.write('*')
|
||||||
}
|
}
|
||||||
|
@ -2593,7 +2599,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
|
||||||
g.expr(left)
|
g.expr(left)
|
||||||
}
|
}
|
||||||
g.is_assign_lhs = false
|
g.is_assign_lhs = false
|
||||||
if is_fixed_array_copy {
|
if is_fixed_array_var {
|
||||||
if is_decl {
|
if is_decl {
|
||||||
g.writeln(';')
|
g.writeln(';')
|
||||||
}
|
}
|
||||||
|
@ -2628,7 +2634,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
|
||||||
}
|
}
|
||||||
g.is_shared = var_type.has_flag(.shared_f)
|
g.is_shared = var_type.has_flag(.shared_f)
|
||||||
if !cloned {
|
if !cloned {
|
||||||
if is_fixed_array_copy {
|
if is_fixed_array_var {
|
||||||
typ_str := g.typ(val_type).trim('*')
|
typ_str := g.typ(val_type).trim('*')
|
||||||
ref_str := if val_type.is_ptr() { '' } else { '&' }
|
ref_str := if val_type.is_ptr() { '' } else { '&' }
|
||||||
g.write('memcpy(($typ_str*)')
|
g.write('memcpy(($typ_str*)')
|
||||||
|
|
Loading…
Reference in New Issue