diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index ff7bf3c5f2..720011fc6d 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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) { diff --git a/vlib/v/checker/tests/array_insert_type_mismatch.out b/vlib/v/checker/tests/array_insert_type_mismatch.out new file mode 100644 index 0000000000..edd33051af --- /dev/null +++ b/vlib/v/checker/tests/array_insert_type_mismatch.out @@ -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 | } diff --git a/vlib/v/checker/tests/array_insert_type_mismatch.vv b/vlib/v/checker/tests/array_insert_type_mismatch.vv new file mode 100644 index 0000000000..518b44aa32 --- /dev/null +++ b/vlib/v/checker/tests/array_insert_type_mismatch.vv @@ -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) +} diff --git a/vlib/v/checker/tests/array_insert_type_mismatch_a.out b/vlib/v/checker/tests/array_insert_type_mismatch_a.out deleted file mode 100644 index 33652e1d1f..0000000000 --- a/vlib/v/checker/tests/array_insert_type_mismatch_a.out +++ /dev/null @@ -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 | } diff --git a/vlib/v/checker/tests/array_insert_type_mismatch_a.vv b/vlib/v/checker/tests/array_insert_type_mismatch_a.vv deleted file mode 100644 index 77744c0e07..0000000000 --- a/vlib/v/checker/tests/array_insert_type_mismatch_a.vv +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - mut a := [1, 2] - a.insert(1, 2.3) - println(a) -} diff --git a/vlib/v/checker/tests/array_insert_type_mismatch_b.out b/vlib/v/checker/tests/array_insert_type_mismatch_b.out deleted file mode 100644 index 5240b5b901..0000000000 --- a/vlib/v/checker/tests/array_insert_type_mismatch_b.out +++ /dev/null @@ -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 | } diff --git a/vlib/v/checker/tests/array_insert_type_mismatch_b.vv b/vlib/v/checker/tests/array_insert_type_mismatch_b.vv deleted file mode 100644 index b4c98b0361..0000000000 --- a/vlib/v/checker/tests/array_insert_type_mismatch_b.vv +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - mut a := [1, 2] - a.insert(1, 'abc') - println(a) -} diff --git a/vlib/v/checker/tests/array_insert_type_mismatch_c.out b/vlib/v/checker/tests/array_insert_type_mismatch_c.out deleted file mode 100644 index e01f8a40c6..0000000000 --- a/vlib/v/checker/tests/array_insert_type_mismatch_c.out +++ /dev/null @@ -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 | } diff --git a/vlib/v/checker/tests/array_insert_type_mismatch_c.vv b/vlib/v/checker/tests/array_insert_type_mismatch_c.vv deleted file mode 100644 index a97cc2335e..0000000000 --- a/vlib/v/checker/tests/array_insert_type_mismatch_c.vv +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - mut a := [1.1, 2.2] - a.insert(1, [2, 3]) - println(a) -} diff --git a/vlib/v/checker/tests/array_insert_type_mismatch_d.out b/vlib/v/checker/tests/array_insert_type_mismatch_d.out deleted file mode 100644 index 7946adaa28..0000000000 --- a/vlib/v/checker/tests/array_insert_type_mismatch_d.out +++ /dev/null @@ -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 | } diff --git a/vlib/v/checker/tests/array_insert_type_mismatch_d.vv b/vlib/v/checker/tests/array_insert_type_mismatch_d.vv deleted file mode 100644 index d5de569ffd..0000000000 --- a/vlib/v/checker/tests/array_insert_type_mismatch_d.vv +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - mut a := [1, 2] - a.insert(1, ['aa', 'bb', 'cc']) - println(a) -} diff --git a/vlib/v/checker/tests/array_prepend_type_mismatch.out b/vlib/v/checker/tests/array_prepend_type_mismatch.out new file mode 100644 index 0000000000..0c42699373 --- /dev/null +++ b/vlib/v/checker/tests/array_prepend_type_mismatch.out @@ -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 | } diff --git a/vlib/v/checker/tests/array_prepend_type_mismatch.vv b/vlib/v/checker/tests/array_prepend_type_mismatch.vv new file mode 100644 index 0000000000..925156e07c --- /dev/null +++ b/vlib/v/checker/tests/array_prepend_type_mismatch.vv @@ -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) +} diff --git a/vlib/v/checker/tests/array_prepend_type_mismatch_a.out b/vlib/v/checker/tests/array_prepend_type_mismatch_a.out deleted file mode 100644 index 19ee60e5fa..0000000000 --- a/vlib/v/checker/tests/array_prepend_type_mismatch_a.out +++ /dev/null @@ -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 | } diff --git a/vlib/v/checker/tests/array_prepend_type_mismatch_a.vv b/vlib/v/checker/tests/array_prepend_type_mismatch_a.vv deleted file mode 100644 index ca76b9672d..0000000000 --- a/vlib/v/checker/tests/array_prepend_type_mismatch_a.vv +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - mut a := [1, 2] - a.prepend(2.3) - println(a) -} diff --git a/vlib/v/checker/tests/array_prepend_type_mismatch_b.out b/vlib/v/checker/tests/array_prepend_type_mismatch_b.out deleted file mode 100644 index bb33d621f9..0000000000 --- a/vlib/v/checker/tests/array_prepend_type_mismatch_b.out +++ /dev/null @@ -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 | } diff --git a/vlib/v/checker/tests/array_prepend_type_mismatch_b.vv b/vlib/v/checker/tests/array_prepend_type_mismatch_b.vv deleted file mode 100644 index 08b8a166f0..0000000000 --- a/vlib/v/checker/tests/array_prepend_type_mismatch_b.vv +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - mut a := [1, 2] - a.prepend('abc') - println(a) -} diff --git a/vlib/v/checker/tests/array_prepend_type_mismatch_c.out b/vlib/v/checker/tests/array_prepend_type_mismatch_c.out deleted file mode 100644 index f9a7eda66b..0000000000 --- a/vlib/v/checker/tests/array_prepend_type_mismatch_c.out +++ /dev/null @@ -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 | } diff --git a/vlib/v/checker/tests/array_prepend_type_mismatch_c.vv b/vlib/v/checker/tests/array_prepend_type_mismatch_c.vv deleted file mode 100644 index 5d8356bdab..0000000000 --- a/vlib/v/checker/tests/array_prepend_type_mismatch_c.vv +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - mut a := [1.1, 2.2] - a.prepend([2, 3]) - println(a) -} diff --git a/vlib/v/checker/tests/array_prepend_type_mismatch_d.out b/vlib/v/checker/tests/array_prepend_type_mismatch_d.out deleted file mode 100644 index fad8cb14f6..0000000000 --- a/vlib/v/checker/tests/array_prepend_type_mismatch_d.out +++ /dev/null @@ -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 | } diff --git a/vlib/v/checker/tests/array_prepend_type_mismatch_d.vv b/vlib/v/checker/tests/array_prepend_type_mismatch_d.vv deleted file mode 100644 index abaddfd633..0000000000 --- a/vlib/v/checker/tests/array_prepend_type_mismatch_d.vv +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - mut a := [1, 2] - a.prepend(['aa', 'bb', 'cc']) - println(a) -}