checker: fix multi_array type mismatch check (#7486)

pull/7489/head
yuyi 2020-12-23 14:05:44 +08:00 committed by GitHub
parent d964dedbb2
commit 1c64635d61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 147 additions and 113 deletions

View File

@ -1228,24 +1228,12 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type {
}
return call_expr.return_type
} else if left_type_sym.kind == .array && method_name in ['insert', 'prepend'] {
array_info := left_type_sym.info as table.Array
elem_sym := c.table.get_type_symbol(array_info.elem_type)
info := left_type_sym.info as table.Array
arg_expr := if method_name == 'insert' { call_expr.args[1].expr } else { call_expr.args[0].expr }
arg_sym := c.table.get_type_symbol(c.expr(arg_expr))
if arg_sym.kind == .array {
info := arg_sym.info as table.Array
sym := c.table.get_type_symbol(info.elem_type)
if sym.kind != elem_sym.kind &&
((elem_sym.kind == .int && sym.kind != .any_int) ||
(elem_sym.kind == .f64 && sym.kind != .any_float)) {
c.error('type mismatch, should use `$elem_sym.name[]`', arg_expr.position())
}
} else {
if arg_sym.kind != elem_sym.kind &&
((elem_sym.kind == .int && arg_sym.kind != .any_int) ||
(elem_sym.kind == .f64 && arg_sym.kind != .any_float)) {
c.error('type mismatch, should use `$elem_sym.name`', arg_expr.position())
}
arg_type := c.expr(arg_expr)
arg_sym := c.table.get_type_symbol(arg_type)
if !c.check_types(arg_type, info.elem_type) && !c.check_types(left_type, arg_type) {
c.error('cannot $method_name `$arg_sym.name` to `$left_type_sym.name`', arg_expr.position())
}
}
if method := c.table.type_find_method(left_type_sym, method_name) {

View File

@ -0,0 +1,56 @@
vlib/v/checker/tests/array_insert_type_mismatch.vv:3:14: error: cannot insert `any_float` to `[]int`
1 | fn main() {
2 | mut a := [1, 2]
3 | a.insert(1, 2.3)
| ~~~
4 | a.insert(1, 'abc')
5 | a.insert(1, [1.1, 2.2])
vlib/v/checker/tests/array_insert_type_mismatch.vv:4:14: error: cannot insert `string` to `[]int`
2 | mut a := [1, 2]
3 | a.insert(1, 2.3)
4 | a.insert(1, 'abc')
| ~~~~~
5 | a.insert(1, [1.1, 2.2])
6 | a.insert(1, ['aa', 'bb', 'cc'])
vlib/v/checker/tests/array_insert_type_mismatch.vv:5:14: error: cannot insert `[]f64` to `[]int`
3 | a.insert(1, 2.3)
4 | a.insert(1, 'abc')
5 | a.insert(1, [1.1, 2.2])
| ~~~~~~~~~~
6 | a.insert(1, ['aa', 'bb', 'cc'])
7 | a.insert(1, [[1]])
vlib/v/checker/tests/array_insert_type_mismatch.vv:6:14: error: cannot insert `[]string` to `[]int`
4 | a.insert(1, 'abc')
5 | a.insert(1, [1.1, 2.2])
6 | a.insert(1, ['aa', 'bb', 'cc'])
| ~~~~~~~~~~~~~~~~~~
7 | a.insert(1, [[1]])
8 | a.insert(1, [[['aa']]])
vlib/v/checker/tests/array_insert_type_mismatch.vv:7:14: error: cannot insert `[][]int` to `[]int`
5 | a.insert(1, [1.1, 2.2])
6 | a.insert(1, ['aa', 'bb', 'cc'])
7 | a.insert(1, [[1]])
| ~~~~~
8 | a.insert(1, [[['aa']]])
9 | println(a)
vlib/v/checker/tests/array_insert_type_mismatch.vv:8:14: error: cannot insert `[][][]string` to `[]int`
6 | a.insert(1, ['aa', 'bb', 'cc'])
7 | a.insert(1, [[1]])
8 | a.insert(1, [[['aa']]])
| ~~~~~~~~~~
9 | println(a)
10 |
vlib/v/checker/tests/array_insert_type_mismatch.vv:12:14: error: cannot insert `[][][]int` to `[][]int`
10 |
11 | mut b := [[1, 2, 3]]
12 | b.insert(0, [[[2]]])
| ~~~~~~~
13 | b.insert(0, [[[['aa']]]])
14 | println(b)
vlib/v/checker/tests/array_insert_type_mismatch.vv:13:14: error: cannot insert `[][][][]string` to `[][]int`
11 | mut b := [[1, 2, 3]]
12 | b.insert(0, [[[2]]])
13 | b.insert(0, [[[['aa']]]])
| ~~~~~~~~~~~~
14 | println(b)
15 | }

View File

@ -0,0 +1,15 @@
fn main() {
mut a := [1, 2]
a.insert(1, 2.3)
a.insert(1, 'abc')
a.insert(1, [1.1, 2.2])
a.insert(1, ['aa', 'bb', 'cc'])
a.insert(1, [[1]])
a.insert(1, [[['aa']]])
println(a)
mut b := [[1, 2, 3]]
b.insert(0, [[[2]]])
b.insert(0, [[[['aa']]]])
println(b)
}

View File

@ -1,7 +0,0 @@
vlib/v/checker/tests/array_insert_type_mismatch_a.vv:3:14: error: type mismatch, should use `int`
1 | fn main() {
2 | mut a := [1, 2]
3 | a.insert(1, 2.3)
| ~~~
4 | println(a)
5 | }

View File

@ -1,5 +0,0 @@
fn main() {
mut a := [1, 2]
a.insert(1, 2.3)
println(a)
}

View File

@ -1,7 +0,0 @@
vlib/v/checker/tests/array_insert_type_mismatch_b.vv:3:14: error: type mismatch, should use `int`
1 | fn main() {
2 | mut a := [1, 2]
3 | a.insert(1, 'abc')
| ~~~~~
4 | println(a)
5 | }

View File

@ -1,5 +0,0 @@
fn main() {
mut a := [1, 2]
a.insert(1, 'abc')
println(a)
}

View File

@ -1,7 +0,0 @@
vlib/v/checker/tests/array_insert_type_mismatch_c.vv:3:14: error: type mismatch, should use `f64[]`
1 | fn main() {
2 | mut a := [1.1, 2.2]
3 | a.insert(1, [2, 3])
| ~~~~~~
4 | println(a)
5 | }

View File

@ -1,5 +0,0 @@
fn main() {
mut a := [1.1, 2.2]
a.insert(1, [2, 3])
println(a)
}

View File

@ -1,7 +0,0 @@
vlib/v/checker/tests/array_insert_type_mismatch_d.vv:3:14: error: type mismatch, should use `int[]`
1 | fn main() {
2 | mut a := [1, 2]
3 | a.insert(1, ['aa', 'bb', 'cc'])
| ~~~~~~~~~~~~~~~~~~
4 | println(a)
5 | }

View File

@ -1,5 +0,0 @@
fn main() {
mut a := [1, 2]
a.insert(1, ['aa', 'bb', 'cc'])
println(a)
}

View File

@ -0,0 +1,56 @@
vlib/v/checker/tests/array_prepend_type_mismatch.vv:3:12: error: cannot prepend `any_float` to `[]int`
1 | fn main() {
2 | mut a := [1, 2]
3 | a.prepend(2.3)
| ~~~
4 | a.prepend('abc')
5 | a.prepend([2.2, 3.3])
vlib/v/checker/tests/array_prepend_type_mismatch.vv:4:12: error: cannot prepend `string` to `[]int`
2 | mut a := [1, 2]
3 | a.prepend(2.3)
4 | a.prepend('abc')
| ~~~~~
5 | a.prepend([2.2, 3.3])
6 | a.prepend(['aa', 'bb', 'cc'])
vlib/v/checker/tests/array_prepend_type_mismatch.vv:5:12: error: cannot prepend `[]f64` to `[]int`
3 | a.prepend(2.3)
4 | a.prepend('abc')
5 | a.prepend([2.2, 3.3])
| ~~~~~~~~~~
6 | a.prepend(['aa', 'bb', 'cc'])
7 | a.prepend([[1]])
vlib/v/checker/tests/array_prepend_type_mismatch.vv:6:12: error: cannot prepend `[]string` to `[]int`
4 | a.prepend('abc')
5 | a.prepend([2.2, 3.3])
6 | a.prepend(['aa', 'bb', 'cc'])
| ~~~~~~~~~~~~~~~~~~
7 | a.prepend([[1]])
8 | a.prepend([[['aa']]])
vlib/v/checker/tests/array_prepend_type_mismatch.vv:7:12: error: cannot prepend `[][]int` to `[]int`
5 | a.prepend([2.2, 3.3])
6 | a.prepend(['aa', 'bb', 'cc'])
7 | a.prepend([[1]])
| ~~~~~
8 | a.prepend([[['aa']]])
9 | println(a)
vlib/v/checker/tests/array_prepend_type_mismatch.vv:8:12: error: cannot prepend `[][][]string` to `[]int`
6 | a.prepend(['aa', 'bb', 'cc'])
7 | a.prepend([[1]])
8 | a.prepend([[['aa']]])
| ~~~~~~~~~~
9 | println(a)
10 |
vlib/v/checker/tests/array_prepend_type_mismatch.vv:12:12: error: cannot prepend `[][][]int` to `[][]int`
10 |
11 | mut b := [[1, 2, 3]]
12 | b.prepend([[[2]]])
| ~~~~~~~
13 | b.prepend([[[['aa']]]])
14 | println(b)
vlib/v/checker/tests/array_prepend_type_mismatch.vv:13:12: error: cannot prepend `[][][][]string` to `[][]int`
11 | mut b := [[1, 2, 3]]
12 | b.prepend([[[2]]])
13 | b.prepend([[[['aa']]]])
| ~~~~~~~~~~~~
14 | println(b)
15 | }

View File

@ -0,0 +1,15 @@
fn main() {
mut a := [1, 2]
a.prepend(2.3)
a.prepend('abc')
a.prepend([2.2, 3.3])
a.prepend(['aa', 'bb', 'cc'])
a.prepend([[1]])
a.prepend([[['aa']]])
println(a)
mut b := [[1, 2, 3]]
b.prepend([[[2]]])
b.prepend([[[['aa']]]])
println(b)
}

View File

@ -1,7 +0,0 @@
vlib/v/checker/tests/array_prepend_type_mismatch_a.vv:3:12: error: type mismatch, should use `int`
1 | fn main() {
2 | mut a := [1, 2]
3 | a.prepend(2.3)
| ~~~
4 | println(a)
5 | }

View File

@ -1,5 +0,0 @@
fn main() {
mut a := [1, 2]
a.prepend(2.3)
println(a)
}

View File

@ -1,7 +0,0 @@
vlib/v/checker/tests/array_prepend_type_mismatch_b.vv:3:12: error: type mismatch, should use `int`
1 | fn main() {
2 | mut a := [1, 2]
3 | a.prepend('abc')
| ~~~~~
4 | println(a)
5 | }

View File

@ -1,5 +0,0 @@
fn main() {
mut a := [1, 2]
a.prepend('abc')
println(a)
}

View File

@ -1,7 +0,0 @@
vlib/v/checker/tests/array_prepend_type_mismatch_c.vv:3:12: error: type mismatch, should use `f64[]`
1 | fn main() {
2 | mut a := [1.1, 2.2]
3 | a.prepend([2, 3])
| ~~~~~~
4 | println(a)
5 | }

View File

@ -1,5 +0,0 @@
fn main() {
mut a := [1.1, 2.2]
a.prepend([2, 3])
println(a)
}

View File

@ -1,7 +0,0 @@
vlib/v/checker/tests/array_prepend_type_mismatch_d.vv:3:12: error: type mismatch, should use `int[]`
1 | fn main() {
2 | mut a := [1, 2]
3 | a.prepend(['aa', 'bb', 'cc'])
| ~~~~~~~~~~~~~~~~~~
4 | println(a)
5 | }

View File

@ -1,5 +0,0 @@
fn main() {
mut a := [1, 2]
a.prepend(['aa', 'bb', 'cc'])
println(a)
}