checker: fix type mismatch of `in` array

pull/4516/head
yuyi 2020-04-20 04:26:58 +08:00 committed by GitHub
parent b62bb22fa6
commit dbb81b89fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 0 deletions

View File

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

View File

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

View File

@ -0,0 +1,5 @@
fn main() {
if 1 in ['1', '2'] {
println('ok')
}
}