mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: check array init's element type
This commit is contained in:
@@ -657,6 +657,7 @@ pub:
|
|||||||
pub struct ArrayInit {
|
pub struct ArrayInit {
|
||||||
pub:
|
pub:
|
||||||
pos token.Position
|
pos token.Position
|
||||||
|
elem_type_pos token.Position
|
||||||
exprs []Expr
|
exprs []Expr
|
||||||
is_fixed bool
|
is_fixed bool
|
||||||
has_val bool
|
has_val bool
|
||||||
|
@@ -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
|
return array_init.typ
|
||||||
}
|
}
|
||||||
// a = []
|
// a = []
|
||||||
|
6
vlib/v/checker/tests/unknown_array_element_type.out
Normal file
6
vlib/v/checker/tests/unknown_array_element_type.out
Normal 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 | }
|
4
vlib/v/checker/tests/unknown_array_element_type.vv
Normal file
4
vlib/v/checker/tests/unknown_array_element_type.vv
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
fn main() {
|
||||||
|
a := []abc{}
|
||||||
|
println(a)
|
||||||
|
}
|
@@ -14,6 +14,7 @@ fn (mut p Parser) array_init() ast.ArrayInit {
|
|||||||
// p.warn('array_init() exp=$p.expected_type')
|
// p.warn('array_init() exp=$p.expected_type')
|
||||||
mut array_type := table.void_type
|
mut array_type := table.void_type
|
||||||
mut elem_type := table.void_type
|
mut elem_type := table.void_type
|
||||||
|
mut elem_type_pos := first_pos
|
||||||
mut exprs := []ast.Expr{}
|
mut exprs := []ast.Expr{}
|
||||||
mut is_fixed := false
|
mut is_fixed := false
|
||||||
mut has_val := false
|
mut has_val := false
|
||||||
@@ -24,6 +25,7 @@ fn (mut p Parser) array_init() ast.ArrayInit {
|
|||||||
p.next()
|
p.next()
|
||||||
// []string
|
// []string
|
||||||
if p.tok.kind in [.name, .amp] && p.tok.line_nr == line_nr {
|
if p.tok.kind in [.name, .amp] && p.tok.line_nr == line_nr {
|
||||||
|
elem_type_pos = p.tok.position()
|
||||||
elem_type = p.parse_type()
|
elem_type = p.parse_type()
|
||||||
// this is set here becasue its a known type, others could be the
|
// this is set here becasue its a known type, others could be the
|
||||||
// result of expr so we do those in checker
|
// result of expr so we do those in checker
|
||||||
@@ -118,6 +120,7 @@ fn (mut p Parser) array_init() ast.ArrayInit {
|
|||||||
typ: array_type
|
typ: array_type
|
||||||
exprs: exprs
|
exprs: exprs
|
||||||
pos: pos
|
pos: pos
|
||||||
|
elem_type_pos: elem_type_pos
|
||||||
has_len: has_len
|
has_len: has_len
|
||||||
len_expr: len_expr
|
len_expr: len_expr
|
||||||
has_cap: has_cap
|
has_cap: has_cap
|
||||||
|
Reference in New Issue
Block a user