map: wrap up delete fix (#9601)
parent
7d5c1c2ddb
commit
cbcc0d34b5
|
@ -637,16 +637,9 @@ fn (mut d DenseArray) delete(i int) {
|
|||
}
|
||||
}
|
||||
|
||||
// delete this
|
||||
pub fn (mut m map) delete(key string) {
|
||||
unsafe {
|
||||
m.delete_1(&key)
|
||||
}
|
||||
}
|
||||
|
||||
// Removes the mapping of a particular key from the map.
|
||||
[unsafe]
|
||||
pub fn (mut m map) delete_1(key voidptr) {
|
||||
pub fn (mut m map) delete(key voidptr) {
|
||||
mut index, mut meta := m.key_to_index(key)
|
||||
index, meta = m.meta_less(index, meta)
|
||||
// Perform backwards shifting
|
||||
|
|
|
@ -568,9 +568,9 @@ fn test_int_keys() {
|
|||
}
|
||||
|
||||
four := 4
|
||||
m2.delete_1(3)
|
||||
m2.delete_1(four)
|
||||
m2.delete_1(5)
|
||||
m2.delete(3)
|
||||
m2.delete(four)
|
||||
m2.delete(5)
|
||||
assert m2.len == 0
|
||||
assert m2[3] == 0
|
||||
assert m2[4] == 0
|
||||
|
@ -603,7 +603,7 @@ fn test_int_keys() {
|
|||
2: 'two'
|
||||
}
|
||||
assert m3[1] == 'one'
|
||||
m3.delete_1(1)
|
||||
m3.delete(1)
|
||||
}
|
||||
|
||||
fn test_voidptr_keys() {
|
||||
|
|
|
@ -1465,7 +1465,7 @@ pub fn (mut c Checker) method_call(mut call_expr ast.CallExpr) ast.Type {
|
|||
// FIXME: Argument count != 1 will break these
|
||||
if left_type_sym.kind == .array && method_name in checker.array_builtin_methods {
|
||||
return c.array_builtin_method_call(mut call_expr, left_type, left_type_sym)
|
||||
} else if left_type_sym.kind == .map && method_name in ['clone', 'keys', 'move', 'delete_1'] {
|
||||
} else if left_type_sym.kind == .map && method_name in ['clone', 'keys', 'move', 'delete'] {
|
||||
return c.map_builtin_method_call(mut call_expr, left_type, left_type_sym)
|
||||
} else if left_type_sym.kind == .array && method_name in ['insert', 'prepend'] {
|
||||
info := left_type_sym.info as ast.Array
|
||||
|
@ -1746,6 +1746,17 @@ fn (mut c Checker) map_builtin_method_call(mut call_expr ast.CallExpr, left_type
|
|||
typ := c.table.find_or_register_array(info.key_type)
|
||||
ret_type = ast.Type(typ)
|
||||
}
|
||||
'delete' {
|
||||
c.fail_if_immutable(call_expr.left)
|
||||
if call_expr.args.len != 1 {
|
||||
c.error('expected 1 argument, but got $call_expr.args.len', call_expr.pos)
|
||||
}
|
||||
info := left_type_sym.info as ast.Map
|
||||
arg_type := c.expr(call_expr.args[0].expr)
|
||||
c.check_expected_call_arg(arg_type, info.key_type, call_expr.language) or {
|
||||
c.error('$err.msg in argument 1 to `Map.delete`', call_expr.args[0].pos)
|
||||
}
|
||||
}
|
||||
else {}
|
||||
}
|
||||
call_expr.receiver_type = left_type.to_ptr()
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
vlib/v/checker/tests/map_delete.vv:5:11: error: cannot use `int literal` as `string` in argument 1 to `Map.delete`
|
||||
3 | '1': 1
|
||||
4 | }
|
||||
5 | m.delete(1)
|
||||
| ^
|
||||
6 | m.delete(1, 2)
|
||||
7 | m2 := {
|
||||
vlib/v/checker/tests/map_delete.vv:6:4: error: expected 1 argument, but got 2
|
||||
4 | }
|
||||
5 | m.delete(1)
|
||||
6 | m.delete(1, 2)
|
||||
| ~~~~~~~~~~~~
|
||||
7 | m2 := {
|
||||
8 | '1': 1
|
||||
vlib/v/checker/tests/map_delete.vv:10:2: error: `m2` is immutable, declare it with `mut` to make it mutable
|
||||
8 | '1': 1
|
||||
9 | }
|
||||
10 | m2.delete('1')
|
||||
| ~~
|
||||
11 | }
|
|
@ -0,0 +1,11 @@
|
|||
fn main() {
|
||||
mut m := {
|
||||
'1': 1
|
||||
}
|
||||
m.delete(1)
|
||||
m.delete(1, 2)
|
||||
m2 := {
|
||||
'1': 1
|
||||
}
|
||||
m2.delete('1')
|
||||
}
|
|
@ -551,10 +551,10 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
|
|||
}
|
||||
}
|
||||
|
||||
if left_sym.kind == .map && node.name == 'delete_1' {
|
||||
if left_sym.kind == .map && node.name == 'delete' {
|
||||
left_info := left_sym.info as ast.Map
|
||||
elem_type_str := g.typ(left_info.key_type)
|
||||
g.write('map_delete_1(&')
|
||||
g.write('map_delete(&')
|
||||
g.expr(node.left)
|
||||
g.write(', &($elem_type_str[]){')
|
||||
g.expr(node.args[0].expr)
|
||||
|
|
Loading…
Reference in New Issue