1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: fix mutable map args

This commit is contained in:
Alexander Medvednikov 2019-11-12 22:35:53 +03:00
parent 19c9e226a3
commit 10b0432eca
3 changed files with 19 additions and 5 deletions

View File

@ -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 }

View File

@ -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.
}

View File

@ -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
}
}