diff --git a/compiler/parser.v b/compiler/parser.v index 0e4ccaf0d8..64cade9538 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -2000,6 +2000,7 @@ fn (p mut Parser) index_expr(typ_ string, fn_ph int) string { p.error('strings are immutable') } assign_pos := p.cgen.cur_line.len + is_cao := p.tok != .assign p.assigned_type = typ p.expected_type = typ p.assign_statement(v, fn_ph, is_indexer && (is_map || is_arr)) @@ -2013,19 +2014,33 @@ fn (p mut Parser) index_expr(typ_ string, fn_ph int) string { tmp_val := p.cgen.cur_line.right(assign_pos) p.cgen.resetln(p.cgen.cur_line.left(assign_pos)) // val := p.cgen.end_tmp() + mut cao_tmp := p.cgen.cur_line if is_map { p.cgen.set_placeholder(fn_ph, 'map__set(&') + // CAO on map is a bit more complicated as it loads + // the value inside a pointer instead of returning it. } else { if is_ptr { p.cgen.set_placeholder(fn_ph, 'array_set(') + if is_cao { + cao_tmp = '*($p.expected_type *) array__get(*$cao_tmp)' + } } else { p.cgen.set_placeholder(fn_ph, 'array_set(&/*q*/') + if is_cao { + cao_tmp = '*($p.expected_type *) array__get($cao_tmp)' + } } } p.gen(', & $tmp)') - p.cgen.insert_before('$typ $tmp = $tmp_val;') + if !is_cao { + p.cgen.insert_before('$typ $tmp = $tmp_val;') + } + else { + p.cgen.insert_before('$typ $tmp = $cao_tmp ' + tmp_val.all_before('=') + tmp_val.all_after('=') + ';') + } } return typ } diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index 888b9b04a0..516d6b653b 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -199,3 +199,10 @@ fn test_clone() { assert nums.slice(1, 3).str() == '[2, 3]' } +fn test_doubling() { + mut nums := [1, 2, 3, 4, 5] + for i := 0; i < nums.len; i++ { + nums[i] *= 2 + } + assert nums.str() == '[2, 4, 6, 8, 10]' +} \ No newline at end of file