From c9b395f9cf41d7a3283b99739ed42938d0b00bf9 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 5 Jun 2020 15:44:25 +0800 Subject: [PATCH] checker: fix for_in_index type error --- vlib/v/checker/checker.v | 5 ++--- vlib/v/checker/tests/for_in_index_optional.out | 7 +++++++ vlib/v/checker/tests/for_in_index_optional.vv | 6 ++++++ .../tests/{for-in-index-type.out => for_in_index_type.out} | 2 +- .../tests/{for-in-index-type.vv => for_in_index_type.vv} | 0 5 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 vlib/v/checker/tests/for_in_index_optional.out create mode 100644 vlib/v/checker/tests/for_in_index_optional.vv rename vlib/v/checker/tests/{for-in-index-type.out => for_in_index_type.out} (67%) rename vlib/v/checker/tests/{for-in-index-type.vv => for_in_index_type.vv} (100%) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 8a895d8057..d04426c71a 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1663,9 +1663,8 @@ fn (mut c Checker) stmt(node ast.Stmt) { scope.update_var_type(it.key_var, key_type) } value_type := c.table.value_type(typ) - if value_type == table.void_type { - typ_sym := c.table.get_type_symbol(typ) - c.error('for in: cannot index `$typ_sym.name`', it.cond.position()) + if value_type == table.void_type || typ.has_flag(.optional) { + c.error('for in: cannot index `${c.table.type_to_str(typ)}`', it.cond.position()) } it.cond_type = typ it.kind = sym.kind diff --git a/vlib/v/checker/tests/for_in_index_optional.out b/vlib/v/checker/tests/for_in_index_optional.out new file mode 100644 index 0000000000..5ee1eb8682 --- /dev/null +++ b/vlib/v/checker/tests/for_in_index_optional.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/for_in_index_optional.v:3:18: error: for in: cannot index `?[]string` + 1 | import os + 2 | fn main() { + 3 | for file in os.ls('.') { + | ~~~~~~~ + 4 | println(file) + 5 | } diff --git a/vlib/v/checker/tests/for_in_index_optional.vv b/vlib/v/checker/tests/for_in_index_optional.vv new file mode 100644 index 0000000000..c384c790f2 --- /dev/null +++ b/vlib/v/checker/tests/for_in_index_optional.vv @@ -0,0 +1,6 @@ +import os +fn main() { + for file in os.ls('.') { + println(file) + } +} diff --git a/vlib/v/checker/tests/for-in-index-type.out b/vlib/v/checker/tests/for_in_index_type.out similarity index 67% rename from vlib/v/checker/tests/for-in-index-type.out rename to vlib/v/checker/tests/for_in_index_type.out index 40c8bacaf8..e04978ca57 100644 --- a/vlib/v/checker/tests/for-in-index-type.out +++ b/vlib/v/checker/tests/for_in_index_type.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/for-in-index-type.v:2:11: error: for in: cannot index `any_int` +vlib/v/checker/tests/for_in_index_type.v:2:11: error: for in: cannot index `any_int` 1 | fn main() { 2 | for a in 52 { | ~~ diff --git a/vlib/v/checker/tests/for-in-index-type.vv b/vlib/v/checker/tests/for_in_index_type.vv similarity index 100% rename from vlib/v/checker/tests/for-in-index-type.vv rename to vlib/v/checker/tests/for_in_index_type.vv