checker: more checks for in and !in

pull/4932/head
Swastik Baranwal 2020-05-17 20:33:32 +05:30 committed by GitHub
parent 0def084932
commit d697b2848a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 8 deletions

View File

@ -399,24 +399,23 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) table.Type {
.array { .array {
right_sym := c.table.get_type_symbol(right.array_info().elem_type) right_sym := c.table.get_type_symbol(right.array_info().elem_type)
if left.kind != right_sym.kind { if left.kind != right_sym.kind {
c.error('the data type on the left of `in` does not match the array item type', c.error('the data type on the left of `$infix_expr.op.str()` does not match the array item type',
infix_expr.pos) infix_expr.pos)
} }
} }
.map { .map {
key_sym := c.table.get_type_symbol(right.map_info().key_type) key_sym := c.table.get_type_symbol(right.map_info().key_type)
if left.kind != key_sym.kind { if left.kind != key_sym.kind {
c.error('the data type on the left of `in` does not match the map key type', c.error('the data type on the left of `$infix_expr.op.str()` does not match the map key type', infix_expr.pos)
infix_expr.pos)
} }
} }
.string { .string {
if left.kind != .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 { 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 return table.bool_type

View File

@ -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') 32 | println('all right')
33 | } 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 | }

View File

@ -31,4 +31,14 @@ fn main() {
if '2' in a_i { if '2' in a_i {
println('all right') println('all right')
} }
if 1 !in a_s {
println('ok')
}
if '1' !in a_i {
println('good')
}
if 5 !in m {
println('yay')
}
} }