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 {
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

View File

@ -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 | }

View File

@ -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')
}
}