checker: improve array literal element type mismatch error (#6825)

pull/6842/head
Nick Treleaven 2020-11-15 22:41:24 +00:00 committed by GitHub
parent cd2a2cef25
commit 4882d338f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 14 deletions

View File

@ -2300,10 +2300,8 @@ pub fn (mut c Checker) array_init(mut array_init ast.ArrayInit) table.Type {
c.expected_type = elem_type
continue
}
if !c.check_types(typ, elem_type) {
elem_type_sym := c.table.get_type_symbol(elem_type)
c.error('expected array element with type `$elem_type_sym.source_name`',
array_init.pos)
c.check_expected(typ, elem_type) or {
c.error('invalid array element: $err', expr.position())
}
}
if expecting_interface_array {

View File

@ -0,0 +1,12 @@
vlib/v/checker/tests/array_element_type.vv:2:8: error: unknown type `abc`
1 | fn main() {
2 | _ = []abc{}
| ~~~
3 | _ = [2, '']
4 | }
vlib/v/checker/tests/array_element_type.vv:3:10: error: invalid array element: expected `int`, not `string`
1 | fn main() {
2 | _ = []abc{}
3 | _ = [2, '']
| ~~
4 | }

View File

@ -0,0 +1,4 @@
fn main() {
_ = []abc{}
_ = [2, '']
}

View File

@ -1,6 +0,0 @@
vlib/v/checker/tests/unknown_array_element_type_a.vv:2:9: error: unknown type `abc`
1 | fn main() {
2 | a := []abc{}
| ~~~
3 | println(a)
4 | }

View File

@ -1,4 +0,0 @@
fn main() {
a := []abc{}
println(a)
}