checker: check array type mismatch of array append (#10405)

pull/10422/head
yuyi 2021-06-11 02:34:36 +08:00 committed by GitHub
parent 9d852e22b2
commit 7878bad95e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 1 deletions

View File

@ -331,6 +331,13 @@ pub fn (mut c Checker) check_types(got ast.Type, expected ast.Type) bool {
return true return true
} }
fn (mut c Checker) check_array_value_types(got ast.Type, expected ast.Type) bool {
if expected.is_number() && got.is_number() && expected != c.table.mktyp(got) {
return false
}
return c.check_types(got, expected)
}
pub fn (mut c Checker) check_expected(got ast.Type, expected ast.Type) ? { pub fn (mut c Checker) check_expected(got ast.Type, expected ast.Type) ? {
if c.check_types(got, expected) { if c.check_types(got, expected) {
return return

View File

@ -1242,7 +1242,7 @@ pub fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
return ast.void_type return ast.void_type
} }
if right_final.kind == .array if right_final.kind == .array
&& c.check_types(left_value_type, c.table.value_type(right_type)) { && c.check_array_value_types(left_value_type, c.table.value_type(right_type)) {
// []T << []T // []T << []T
return ast.void_type return ast.void_type
} }

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/array_append_array_type_mismatch_err.vv:3:8: error: cannot append `[]int` to `[]byte`
1 | fn main() {
2 | mut bc := []byte{}
3 | bc << [0xCA, 0xFE, 0xBA, 0xBE]
| ~~~~~~~~~~~~~~~~~~~~~~~~
4 | println(bc)
5 | }

View File

@ -0,0 +1,5 @@
fn main() {
mut bc := []byte{}
bc << [0xCA, 0xFE, 0xBA, 0xBE]
println(bc)
}