cgen: fix array fixed initialization from map indexing (fix #22133) (#22149)

This commit is contained in:
Felipe Pena 2024-09-02 05:38:43 -03:00 committed by GitHub
parent ace4e93576
commit 07e0370ece
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 2 deletions

View File

@ -491,7 +491,7 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
else { else {
if mut left is ast.IndexExpr { if mut left is ast.IndexExpr {
// eprintln('>>> left.is_setter: ${left.is_setter:10} | left.is_map: ${left.is_map:10} | left.is_array: ${left.is_array:10}') // eprintln('>>> left.is_setter: ${left.is_setter:10} | left.is_map: ${left.is_map:10} | left.is_array: ${left.is_array:10}')
if left.is_map && left.is_setter { if (left.is_map || left.is_farray) && left.is_setter {
left.recursive_mapset_is_setter(true) left.recursive_mapset_is_setter(true)
} }
} }

View File

@ -342,6 +342,16 @@ fn (mut g Gen) index_of_fixed_array(node ast.IndexExpr, sym ast.TypeSymbol) {
g.expr(node.left) g.expr(node.left)
g.writeln(';') g.writeln(';')
g.past_tmp_var_done(past) g.past_tmp_var_done(past)
} else if node.left is ast.IndexExpr && node.left.is_setter {
past := g.past_tmp_var_new()
styp := g.typ(node.left_type)
println(node.left)
g.write('${styp}* ${past.tmp_var} = &')
g.expr(node.left)
g.writeln(';')
g.write('(*')
g.past_tmp_var_done(past)
g.write(')')
} else { } else {
if is_fn_index_call { if is_fn_index_call {
g.write('(*') g.write('(*')
@ -385,7 +395,7 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) {
g.typ(val_type.clear_flag(.result)) g.typ(val_type.clear_flag(.result))
} }
} }
get_and_set_types := val_sym.kind in [.struct_, .map, .array] get_and_set_types := val_sym.kind in [.struct_, .map, .array, .array_fixed]
if g.is_assign_lhs && !g.is_arraymap_set && !get_and_set_types { if g.is_assign_lhs && !g.is_arraymap_set && !get_and_set_types {
if g.assign_op == .assign || info.value_type == ast.string_type { if g.assign_op == .assign || info.value_type == ast.string_type {
g.is_arraymap_set = true g.is_arraymap_set = true

View File

@ -0,0 +1,26 @@
module main
struct File {
root string
path string
}
struct Test {
mut:
mod_files map[string][5]File
}
fn test_main() {
mut test := Test{}
for i in 0 .. 4 {
test.mod_files['main'][i] = File{}
}
test.mod_files['main'][3] = File{
root: 'foo'
path: 'bar'
}
assert test.mod_files['main'][3] == File{
root: 'foo'
path: 'bar'
}
}