From 10b0432ecafc42088998e7b582fac768cb737037 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 12 Nov 2019 22:35:53 +0300 Subject: [PATCH] parser: fix mutable map args --- vlib/builtin/map_test.v | 10 ++++++++++ vlib/compiler/gen_c.v | 6 +++++- vlib/compiler/parser.v | 8 ++++---- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/vlib/builtin/map_test.v b/vlib/builtin/map_test.v index 8675762582..fa6a4d294a 100644 --- a/vlib/builtin/map_test.v +++ b/vlib/builtin/map_test.v @@ -158,6 +158,16 @@ fn test_string_arr() { assert m['a'][1] == 'two' } +fn mut_map(m mut map[string]int) { + m['a'] = 10 +} + +fn test_mut_arg() { + mut m := map[string]int + mut_map(mut m) + assert m['a'] == 10 +} + /* fn test_ref() { m := { 'one': 1 } diff --git a/vlib/compiler/gen_c.v b/vlib/compiler/gen_c.v index 654bda8cb6..59f4b1424a 100644 --- a/vlib/compiler/gen_c.v +++ b/vlib/compiler/gen_c.v @@ -400,7 +400,11 @@ fn (p mut Parser) gen_array_set(typ string, is_ptr, is_map bool,fn_ph, assign_po mut cao_tmp := p.cgen.cur_line mut func := '' if is_map { - func = 'map_set(&' + if is_ptr { + func = 'map_set(' + } else { + func = 'map_set(&' + } // CAO on map is a bit more complicated as it loads // the value inside a pointer instead of returning it. } diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index 252de6de3c..659fee0746 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -1831,7 +1831,7 @@ fn (p mut Parser) index_expr(typ_ string, fn_ph int) string { if is_indexer { is_fixed_arr := typ[0] == `[` if !is_str && !is_arr && !is_map && !is_ptr && !is_fixed_arr && !is_variadic_arg { - p.error('Cant [] non-array/string/map. Got type "$typ"') + p.error('invalid operation: type `$typ` does not support indexing') } p.check(.lsbr) // Get element type (set `typ` to it) @@ -1844,7 +1844,7 @@ fn (p mut Parser) index_expr(typ_ string, fn_ph int) string { } else { // Bounds check everywhere else - p.gen(',') + p.gen(', ') } } if is_variadic_arg { typ = typ[5..] } @@ -1864,8 +1864,8 @@ fn (p mut Parser) index_expr(typ_ string, fn_ph int) string { // typ = 'byte' typ = typ.replace('*', '') // modify(mut []string) fix - if !is_arr { - p.gen('[/*ptr*/') + if !is_arr && !is_map { + p.gen('[/*ptr!*/') close_bracket = true } }