From cbcc0d34b5688dc8bb5d41a634754dada21173d1 Mon Sep 17 00:00:00 2001 From: ka-weihe Date: Tue, 6 Apr 2021 15:57:08 +0200 Subject: [PATCH] map: wrap up delete fix (#9601) --- vlib/builtin/map.v | 9 +-------- vlib/builtin/map_test.v | 8 ++++---- vlib/v/checker/checker.v | 13 ++++++++++++- vlib/v/checker/tests/map_delete.out | 20 ++++++++++++++++++++ vlib/v/checker/tests/map_delete.vv | 11 +++++++++++ vlib/v/gen/c/fn.v | 4 ++-- 6 files changed, 50 insertions(+), 15 deletions(-) create mode 100644 vlib/v/checker/tests/map_delete.out create mode 100644 vlib/v/checker/tests/map_delete.vv diff --git a/vlib/builtin/map.v b/vlib/builtin/map.v index c2cb08a2c8..0f0c406a0f 100644 --- a/vlib/builtin/map.v +++ b/vlib/builtin/map.v @@ -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 diff --git a/vlib/builtin/map_test.v b/vlib/builtin/map_test.v index 469596d29c..a6bb768f52 100644 --- a/vlib/builtin/map_test.v +++ b/vlib/builtin/map_test.v @@ -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() { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index a35ac9f730..acc514a08b 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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() diff --git a/vlib/v/checker/tests/map_delete.out b/vlib/v/checker/tests/map_delete.out new file mode 100644 index 0000000000..ecfbca0e21 --- /dev/null +++ b/vlib/v/checker/tests/map_delete.out @@ -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 | } diff --git a/vlib/v/checker/tests/map_delete.vv b/vlib/v/checker/tests/map_delete.vv new file mode 100644 index 0000000000..0be6d170aa --- /dev/null +++ b/vlib/v/checker/tests/map_delete.vv @@ -0,0 +1,11 @@ +fn main() { + mut m := { + '1': 1 + } + m.delete(1) + m.delete(1, 2) + m2 := { + '1': 1 + } + m2.delete('1') +} \ No newline at end of file diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 745cba3d67..283382cc80 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -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)