From d697b2848ad70766ab9772b1af013ed5dad94efe Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Sun, 17 May 2020 20:33:32 +0530 Subject: [PATCH] checker: more checks for in and !in --- vlib/v/checker/checker.v | 11 +++++----- vlib/v/checker/tests/in_mismatch_type.out | 25 +++++++++++++++++++++-- vlib/v/checker/tests/in_mismatch_type.vv | 10 +++++++++ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 9e99926fd5..e2e0db8b0d 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -399,24 +399,23 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) table.Type { .array { right_sym := c.table.get_type_symbol(right.array_info().elem_type) if 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) + c.error('the data type on the left of `$infix_expr.op.str()` does not match the array item type', + infix_expr.pos) } } .map { key_sym := c.table.get_type_symbol(right.map_info().key_type) if left.kind != key_sym.kind { - c.error('the data type on the left of `in` does not match the map key type', - infix_expr.pos) + c.error('the data type on the left of `$infix_expr.op.str()` does not match the map key type', infix_expr.pos) } } .string { if left.kind != .string { - c.error('the data type on the left of `in` must be a string', infix_expr.pos) + c.error('the data type on the left of `$infix_expr.op.str()` must be a string', infix_expr.pos) } } else { - c.error('`in` can only be used with an array/map/string', infix_expr.pos) + c.error('`$infix_expr.op.str()` can only be used with an array/map/string', infix_expr.pos) } } return table.bool_type diff --git a/vlib/v/checker/tests/in_mismatch_type.out b/vlib/v/checker/tests/in_mismatch_type.out index c1b5dac613..5cdb2492d2 100644 --- a/vlib/v/checker/tests/in_mismatch_type.out +++ b/vlib/v/checker/tests/in_mismatch_type.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/in_mismatch_type.v:10:7: error: the data type on the left of `in` does not match the array item type +vlib/v/checker/tests/in_mismatch_type.v:10:7: error: the data type on the left of `in` does not match the array item type 8 | } 9 | s := 'abcd' 10 | if 1 in a_s { @@ -19,7 +19,7 @@ vlib/v/checker/tests/in_mismatch_type.v:16:7: error: the data type on the left o | ~~ 17 | println('dope') 18 | } -vlib/v/checker/tests/in_mismatch_type.v:19:9: error: the data type on the left of `in` must be a string +vlib/v/checker/tests/in_mismatch_type.v:19:9: error: the data type on the left of `in` must be a string 17 | println('dope') 18 | } 19 | if `a` in s { @@ -54,3 +54,24 @@ vlib/v/checker/tests/in_mismatch_type.v:31:9: error: the data type on the left o | ~~ 32 | println('all right') 33 | } +vlib/v/checker/tests/in_mismatch_type.v:34:7: error: the data type on the left of `!in` does not match the array item type + 32 | println('all right') + 33 | } + 34 | if 1 !in a_s { + | ~~~ + 35 | println('ok') + 36 | } +vlib/v/checker/tests/in_mismatch_type.v:37:9: error: the data type on the left of `!in` does not match the array item type + 35 | println('ok') + 36 | } + 37 | if '1' !in a_i { + | ~~~ + 38 | println('good') + 39 | } +vlib/v/checker/tests/in_mismatch_type.v:41:7: error: the data type on the left of `!in` does not match the map key type + 39 | } + 40 | + 41 | if 5 !in m { + | ~~~ + 42 | println('yay') + 43 | } diff --git a/vlib/v/checker/tests/in_mismatch_type.vv b/vlib/v/checker/tests/in_mismatch_type.vv index dd882d771d..2fc2bde89e 100644 --- a/vlib/v/checker/tests/in_mismatch_type.vv +++ b/vlib/v/checker/tests/in_mismatch_type.vv @@ -31,4 +31,14 @@ fn main() { if '2' in a_i { println('all right') } + if 1 !in a_s { + println('ok') + } + if '1' !in a_i { + println('good') + } + + if 5 !in m { + println('yay') + } }