From ff9837386a1fd1d22e8047f25a2fc946f65db75e Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 7 Apr 2021 03:40:43 +0300 Subject: [PATCH] Revert "map: wrap up delete fix (#9601)" This reverts commit cbcc0d34b5688dc8bb5d41a634754dada21173d1. --- 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, 15 insertions(+), 50 deletions(-) delete mode 100644 vlib/v/checker/tests/map_delete.out delete mode 100644 vlib/v/checker/tests/map_delete.vv diff --git a/vlib/builtin/map.v b/vlib/builtin/map.v index 0f0c406a0f..c2cb08a2c8 100644 --- a/vlib/builtin/map.v +++ b/vlib/builtin/map.v @@ -637,9 +637,16 @@ 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(key voidptr) { +pub fn (mut m map) delete_1(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 a6bb768f52..469596d29c 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(3) - m2.delete(four) - m2.delete(5) + m2.delete_1(3) + m2.delete_1(four) + m2.delete_1(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) + m3.delete_1(1) } fn test_voidptr_keys() { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index acc514a08b..a35ac9f730 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'] { + } else if left_type_sym.kind == .map && method_name in ['clone', 'keys', 'move', 'delete_1'] { 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,17 +1746,6 @@ 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 deleted file mode 100644 index ecfbca0e21..0000000000 --- a/vlib/v/checker/tests/map_delete.out +++ /dev/null @@ -1,20 +0,0 @@ -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 deleted file mode 100644 index 0be6d170aa..0000000000 --- a/vlib/v/checker/tests/map_delete.vv +++ /dev/null @@ -1,11 +0,0 @@ -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 283382cc80..745cba3d67 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' { + if left_sym.kind == .map && node.name == 'delete_1' { left_info := left_sym.info as ast.Map elem_type_str := g.typ(left_info.key_type) - g.write('map_delete(&') + g.write('map_delete_1(&') g.expr(node.left) g.write(', &($elem_type_str[]){') g.expr(node.args[0].expr)