checker: check array init's element type

pull/5096/head
yuyi 2020-05-28 19:32:12 +08:00 committed by GitHub
parent 6da1d3aff8
commit 977eb895e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 0 deletions

View File

@ -657,6 +657,7 @@ pub:
pub struct ArrayInit {
pub:
pos token.Position
elem_type_pos token.Position
exprs []Expr
is_fixed bool
has_val bool

View File

@ -1334,6 +1334,10 @@ pub fn (mut c Checker) array_init(mut array_init ast.ArrayInit) table.Type {
}
}
}
sym := c.table.get_type_symbol(array_init.elem_type)
if sym.kind == .placeholder {
c.error('unknown type `$sym.name`', array_init.elem_type_pos)
}
return array_init.typ
}
// a = []

View File

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

View File

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

View File

@ -14,6 +14,7 @@ fn (mut p Parser) array_init() ast.ArrayInit {
// p.warn('array_init() exp=$p.expected_type')
mut array_type := table.void_type
mut elem_type := table.void_type
mut elem_type_pos := first_pos
mut exprs := []ast.Expr{}
mut is_fixed := false
mut has_val := false
@ -24,6 +25,7 @@ fn (mut p Parser) array_init() ast.ArrayInit {
p.next()
// []string
if p.tok.kind in [.name, .amp] && p.tok.line_nr == line_nr {
elem_type_pos = p.tok.position()
elem_type = p.parse_type()
// this is set here becasue its a known type, others could be the
// result of expr so we do those in checker
@ -118,6 +120,7 @@ fn (mut p Parser) array_init() ast.ArrayInit {
typ: array_type
exprs: exprs
pos: pos
elem_type_pos: elem_type_pos
has_len: has_len
len_expr: len_expr
has_cap: has_cap