From dbb81b89fdd0d009b669f1ed90a76bb6b3e22a22 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 20 Apr 2020 04:26:58 +0800 Subject: [PATCH] checker: fix type mismatch of `in` array --- vlib/v/checker/checker.v | 6 ++++++ vlib/v/checker/tests/inout/in_array_mismatch_type.out | 6 ++++++ vlib/v/checker/tests/inout/in_array_mismatch_type.vv | 5 +++++ 3 files changed, 17 insertions(+) create mode 100644 vlib/v/checker/tests/inout/in_array_mismatch_type.out create mode 100644 vlib/v/checker/tests/inout/in_array_mismatch_type.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 42a91eb870..6570ed7471 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -294,6 +294,12 @@ pub fn (c mut Checker) infix_expr(infix_expr mut ast.InfixExpr) table.Type { if !(right.kind in [.array, .map, .string]) { c.error('`in` can only be used with an array/map/string.', infix_expr.pos) } + if right.kind == .array { + right_sym := c.table.get_type_symbol(right.array_info().elem_type) + if left.kind != .alias && left.kind != right_sym.kind { + c.error('the data type on the left of `in` does not match the array item type.', infix_expr.pos) + } + } return table.bool_type } if !c.table.check(right_type, left_type) { diff --git a/vlib/v/checker/tests/inout/in_array_mismatch_type.out b/vlib/v/checker/tests/inout/in_array_mismatch_type.out new file mode 100644 index 0000000000..c9ba19b18c --- /dev/null +++ b/vlib/v/checker/tests/inout/in_array_mismatch_type.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/inout/in_array_mismatch_type.v:2:7: error: the data type on the left of `in` does not match the array item type. + 1| fn main() { + 2| if 1 in ['1', '2'] { + ~~ + 3| println('ok') + 4| } \ No newline at end of file diff --git a/vlib/v/checker/tests/inout/in_array_mismatch_type.vv b/vlib/v/checker/tests/inout/in_array_mismatch_type.vv new file mode 100644 index 0000000000..0d9e17e7ef --- /dev/null +++ b/vlib/v/checker/tests/inout/in_array_mismatch_type.vv @@ -0,0 +1,5 @@ +fn main() { + if 1 in ['1', '2'] { + println('ok') + } +} \ No newline at end of file