From 7feb1742d3981aeb239571baba4114da72ca9cba Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 13 Nov 2020 13:37:11 +0200 Subject: [PATCH] cgen: skip deleted map entries in the auto generated .str() method --- vlib/builtin/map_test.v | 14 ++++++++++++++ vlib/v/gen/auto_str_methods.v | 1 + 2 files changed, 15 insertions(+) diff --git a/vlib/builtin/map_test.v b/vlib/builtin/map_test.v index bd822381d1..aa4c0bf377 100644 --- a/vlib/builtin/map_test.v +++ b/vlib/builtin/map_test.v @@ -339,3 +339,17 @@ fn test_mut_map_with_relation_op_in_fn() { assert 'seven' in m assert 'eight' in m } + +fn test_map_str_after_delete() { + mut m := { + 'first': 1, + 'second': 2, + 'third': 3, + } + osm := '$m' + m.delete('second') + nsm := '$m' + println('m: $m') + assert osm == "{'first': 1, 'second': 2, 'third': 3}" + assert nsm == "{'first': 1, 'third': 3}" +} diff --git a/vlib/v/gen/auto_str_methods.v b/vlib/v/gen/auto_str_methods.v index 347939e422..3689a9b2f5 100644 --- a/vlib/v/gen/auto_str_methods.v +++ b/vlib/v/gen/auto_str_methods.v @@ -256,6 +256,7 @@ fn (mut g Gen) gen_str_for_map(info table.Map, styp string, str_fn_name string) g.auto_str_funcs.writeln('\tstrings__Builder sb = strings__new_builder(m.key_values.len*10);') g.auto_str_funcs.writeln('\tstrings__Builder_write(&sb, tos_lit("{"));') g.auto_str_funcs.writeln('\tfor (unsigned int i = 0; i < m.key_values.len; ++i) {') + g.auto_str_funcs.writeln('\t\tif (m.key_values.keys[i].str == 0) { continue; }') g.auto_str_funcs.writeln('\t\tstring key = (*(string*)DenseArray_get(m.key_values, i));') g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("\'%.*s\\000\'", 2, key));') g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, tos_lit(": "));')