mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: check array_insert
array_prepend
type mismatch
This commit is contained in:
parent
5ff7d07138
commit
5a6d440f68
@ -767,6 +767,24 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type {
|
||||
call_expr.return_type = info.elem_type
|
||||
call_expr.receiver_type = left_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)
|
||||
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())
|
||||
}
|
||||
}
|
||||
}
|
||||
if method := c.table.type_find_method(left_type_sym, method_name) {
|
||||
if !method.is_pub && !c.is_builtin_mod && !c.pref.is_test && left_type_sym.mod != c.mod &&
|
||||
|
7
vlib/v/checker/tests/array_insert_type_mismatch_a.out
Normal file
7
vlib/v/checker/tests/array_insert_type_mismatch_a.out
Normal file
@ -0,0 +1,7 @@
|
||||
vlib/v/checker/tests/array_insert_type_mismatch_a.v: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 | }
|
5
vlib/v/checker/tests/array_insert_type_mismatch_a.vv
Normal file
5
vlib/v/checker/tests/array_insert_type_mismatch_a.vv
Normal file
@ -0,0 +1,5 @@
|
||||
fn main() {
|
||||
mut a := [1, 2]
|
||||
a.insert(1, 2.3)
|
||||
println(a)
|
||||
}
|
7
vlib/v/checker/tests/array_insert_type_mismatch_b.out
Normal file
7
vlib/v/checker/tests/array_insert_type_mismatch_b.out
Normal file
@ -0,0 +1,7 @@
|
||||
vlib/v/checker/tests/array_insert_type_mismatch_b.v: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 | }
|
5
vlib/v/checker/tests/array_insert_type_mismatch_b.vv
Normal file
5
vlib/v/checker/tests/array_insert_type_mismatch_b.vv
Normal file
@ -0,0 +1,5 @@
|
||||
fn main() {
|
||||
mut a := [1, 2]
|
||||
a.insert(1, 'abc')
|
||||
println(a)
|
||||
}
|
7
vlib/v/checker/tests/array_insert_type_mismatch_c.out
Normal file
7
vlib/v/checker/tests/array_insert_type_mismatch_c.out
Normal file
@ -0,0 +1,7 @@
|
||||
vlib/v/checker/tests/array_insert_type_mismatch_c.v: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 | }
|
5
vlib/v/checker/tests/array_insert_type_mismatch_c.vv
Normal file
5
vlib/v/checker/tests/array_insert_type_mismatch_c.vv
Normal file
@ -0,0 +1,5 @@
|
||||
fn main() {
|
||||
mut a := [1.1, 2.2]
|
||||
a.insert(1, [2, 3])
|
||||
println(a)
|
||||
}
|
7
vlib/v/checker/tests/array_insert_type_mismatch_d.out
Normal file
7
vlib/v/checker/tests/array_insert_type_mismatch_d.out
Normal file
@ -0,0 +1,7 @@
|
||||
vlib/v/checker/tests/array_insert_type_mismatch_d.v: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 | }
|
5
vlib/v/checker/tests/array_insert_type_mismatch_d.vv
Normal file
5
vlib/v/checker/tests/array_insert_type_mismatch_d.vv
Normal file
@ -0,0 +1,5 @@
|
||||
fn main() {
|
||||
mut a := [1, 2]
|
||||
a.insert(1, ['aa', 'bb', 'cc'])
|
||||
println(a)
|
||||
}
|
7
vlib/v/checker/tests/array_prepend_type_mismatch_a.out
Normal file
7
vlib/v/checker/tests/array_prepend_type_mismatch_a.out
Normal file
@ -0,0 +1,7 @@
|
||||
vlib/v/checker/tests/array_prepend_type_mismatch_a.v: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 | }
|
5
vlib/v/checker/tests/array_prepend_type_mismatch_a.vv
Normal file
5
vlib/v/checker/tests/array_prepend_type_mismatch_a.vv
Normal file
@ -0,0 +1,5 @@
|
||||
fn main() {
|
||||
mut a := [1, 2]
|
||||
a.prepend(2.3)
|
||||
println(a)
|
||||
}
|
7
vlib/v/checker/tests/array_prepend_type_mismatch_b.out
Normal file
7
vlib/v/checker/tests/array_prepend_type_mismatch_b.out
Normal file
@ -0,0 +1,7 @@
|
||||
vlib/v/checker/tests/array_prepend_type_mismatch_b.v:3:12: error: type mismatch, should use `int`
|
||||
1 | fn main() {
|
||||
2 | mut a := [1, 2]
|
||||
3 | a.prepend('abc')
|
||||
| ~~~~~
|
||||
4 | println(a)
|
||||
5 | }
|
5
vlib/v/checker/tests/array_prepend_type_mismatch_b.vv
Normal file
5
vlib/v/checker/tests/array_prepend_type_mismatch_b.vv
Normal file
@ -0,0 +1,5 @@
|
||||
fn main() {
|
||||
mut a := [1, 2]
|
||||
a.prepend('abc')
|
||||
println(a)
|
||||
}
|
7
vlib/v/checker/tests/array_prepend_type_mismatch_c.out
Normal file
7
vlib/v/checker/tests/array_prepend_type_mismatch_c.out
Normal file
@ -0,0 +1,7 @@
|
||||
vlib/v/checker/tests/array_prepend_type_mismatch_c.v: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 | }
|
5
vlib/v/checker/tests/array_prepend_type_mismatch_c.vv
Normal file
5
vlib/v/checker/tests/array_prepend_type_mismatch_c.vv
Normal file
@ -0,0 +1,5 @@
|
||||
fn main() {
|
||||
mut a := [1.1, 2.2]
|
||||
a.prepend([2, 3])
|
||||
println(a)
|
||||
}
|
7
vlib/v/checker/tests/array_prepend_type_mismatch_d.out
Normal file
7
vlib/v/checker/tests/array_prepend_type_mismatch_d.out
Normal file
@ -0,0 +1,7 @@
|
||||
vlib/v/checker/tests/array_prepend_type_mismatch_d.v: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 | }
|
5
vlib/v/checker/tests/array_prepend_type_mismatch_d.vv
Normal file
5
vlib/v/checker/tests/array_prepend_type_mismatch_d.vv
Normal file
@ -0,0 +1,5 @@
|
||||
fn main() {
|
||||
mut a := [1, 2]
|
||||
a.prepend(['aa', 'bb', 'cc'])
|
||||
println(a)
|
||||
}
|
Loading…
Reference in New Issue
Block a user