1
0
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:
yuyi 2020-06-19 16:49:43 +08:00 committed by GitHub
parent 5ff7d07138
commit 5a6d440f68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 114 additions and 0 deletions

View File

@ -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.return_type = info.elem_type
call_expr.receiver_type = left_type call_expr.receiver_type = left_type
return call_expr.return_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 := 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 && if !method.is_pub && !c.is_builtin_mod && !c.pref.is_test && left_type_sym.mod != c.mod &&

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

View File

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

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

View File

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

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

View File

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

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

View File

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

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

View File

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

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

View File

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

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

View File

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

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

View File

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