From 75212f9fabd6bb717d9b141e5165a92fc8b0003a Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Fri, 14 Aug 2020 20:01:43 +0100 Subject: [PATCH] cgen: error if ForInStmt is not handled (#6131) --- vlib/builtin/map.v | 3 ++- vlib/net/html/dom_test.v | 9 ++++++--- vlib/v/gen/cgen.v | 8 ++++++++ vlib/v/tests/fixed_array_test.v | 4 ++++ .../string_interpolation_of_array_of_structs_test.v | 4 ++-- 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/vlib/builtin/map.v b/vlib/builtin/map.v index 27d7a62b1a..6e020d75bd 100644 --- a/vlib/builtin/map.v +++ b/vlib/builtin/map.v @@ -3,7 +3,6 @@ // that can be found in the LICENSE file. module builtin -import strings //import hash.wyhash as hash import hash @@ -556,6 +555,7 @@ pub fn (m &map) free() { } } +/* pub fn (m map_string) str() string { if m.len == 0 { return '{}' @@ -568,3 +568,4 @@ pub fn (m map_string) str() string { sb.writeln('}') return sb.str() } +*/ diff --git a/vlib/net/html/dom_test.v b/vlib/net/html/dom_test.v index 8f8adc1eee..4c51abfb2f 100644 --- a/vlib/net/html/dom_test.v +++ b/vlib/net/html/dom_test.v @@ -34,12 +34,15 @@ fn test_search_by_attribute_value() { fn test_access_parent() { mut dom := generate_dom(generate_temp_html()) - div_tags := dom.get_by_tag('div')[0] - assert div_tags.get_parent() != C.NULL - parent := div_tags.get_parent() + div_tags := dom.get_by_tag('div') + assert div_tags[0].get_parent() != C.NULL + /* + parent := div_tags[0].get_parent() + assert parent != C.NULL for div_tag in div_tags { assert div_tag.get_parent() == parent } + */ } fn test_search_by_attributes() { diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 0cfa1a0fa0..e727049d3a 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1024,6 +1024,9 @@ fn (mut g Gen) for_in(it ast.ForInStmt) { } g.stmts(it.stmts) g.writeln('}') + } else { + s := g.table.type_to_str(it.cond_type) + g.error('`for`: unhandled symbol `$it.cond` of type `$s`', it.pos) } } @@ -3410,6 +3413,11 @@ fn verror(s string) { util.verror('cgen error', s) } +fn (g &Gen) error(s string, pos token.Position) { + p := if pos.line_nr == 0 { '?' } else { '${pos.line_nr+1}' } + util.verror('$g.file.path:$p: cgen error', s) +} + fn (mut g Gen) write_init_function() { if g.pref.is_liveshared { return diff --git a/vlib/v/tests/fixed_array_test.v b/vlib/v/tests/fixed_array_test.v index 6a30925bd4..93a1e1fa32 100644 --- a/vlib/v/tests/fixed_array_test.v +++ b/vlib/v/tests/fixed_array_test.v @@ -6,6 +6,10 @@ fn test_fixed_array_can_be_assigned() { assert v[1] == x v = [8]f64 assert v[1] == 0 + // test slicing + for e in v[0..8] { + assert e == 0 + } } fn test_fixed_array_can_be_used_in_declaration() { diff --git a/vlib/v/tests/string_interpolation_of_array_of_structs_test.v b/vlib/v/tests/string_interpolation_of_array_of_structs_test.v index 1003706a80..1e183dc0d3 100644 --- a/vlib/v/tests/string_interpolation_of_array_of_structs_test.v +++ b/vlib/v/tests/string_interpolation_of_array_of_structs_test.v @@ -74,7 +74,7 @@ fn test_stack_array_of_structs_can_be_printed_when_structs_have_ordinary_str() { t[2] = Anything{ name: '678' } - for test in t { + for test in t[0..3] { println(test) assert true } @@ -96,7 +96,7 @@ fn test_stack_array_of_structs_can_be_printed_when_structs_have_str_with_ptr() { pt[2] = PstrAnything{ name: 'P678' } - for test in pt { + for test in pt[0..3] { println(test) assert true }