checker: disallow array sum types without default field (#6892)
parent
838ad16296
commit
650cdef8b4
|
@ -815,15 +815,15 @@ pub:
|
||||||
|
|
||||||
pub struct ArrayInit {
|
pub struct ArrayInit {
|
||||||
pub:
|
pub:
|
||||||
pos token.Position
|
pos token.Position // `[]` in []Type{} position
|
||||||
elem_type_pos token.Position
|
elem_type_pos token.Position // `Type` in []Type{} position
|
||||||
exprs []Expr // `[expr, expr]` or `[expr]Type{}` for fixed array
|
exprs []Expr // `[expr, expr]` or `[expr]Type{}` for fixed array
|
||||||
ecmnts [][]Comment // optional iembed comments after each expr
|
ecmnts [][]Comment // optional iembed comments after each expr
|
||||||
is_fixed bool
|
is_fixed bool
|
||||||
has_val bool // fixed size literal `[expr, expr]!!`
|
has_val bool // fixed size literal `[expr, expr]!!`
|
||||||
mod string
|
mod string
|
||||||
len_expr Expr
|
len_expr Expr // len: expr
|
||||||
cap_expr Expr
|
cap_expr Expr // cap: expr
|
||||||
default_expr Expr // init: expr
|
default_expr Expr // init: expr
|
||||||
has_len bool
|
has_len bool
|
||||||
has_cap bool
|
has_cap bool
|
||||||
|
@ -832,8 +832,8 @@ pub mut:
|
||||||
is_interface bool // array of interfaces e.g. `[]Animal` `[Dog{}, Cat{}]`
|
is_interface bool // array of interfaces e.g. `[]Animal` `[Dog{}, Cat{}]`
|
||||||
interface_types []table.Type // [Dog, Cat]
|
interface_types []table.Type // [Dog, Cat]
|
||||||
interface_type table.Type // Animal
|
interface_type table.Type // Animal
|
||||||
elem_type table.Type
|
elem_type table.Type // element type
|
||||||
typ table.Type
|
typ table.Type // array type
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ChanInit {
|
pub struct ChanInit {
|
||||||
|
|
|
@ -2246,6 +2246,11 @@ pub fn (mut c Checker) array_init(mut array_init ast.ArrayInit) table.Type {
|
||||||
if array_init.has_default {
|
if array_init.has_default {
|
||||||
c.expr(array_init.default_expr)
|
c.expr(array_init.default_expr)
|
||||||
}
|
}
|
||||||
|
if sym.kind in [.sum_type, .union_sum_type] {
|
||||||
|
if array_init.has_len && !array_init.has_default {
|
||||||
|
c.error('cannot initalize sum type array without default value', array_init.elem_type_pos)
|
||||||
|
}
|
||||||
|
}
|
||||||
if sym.kind == .placeholder {
|
if sym.kind == .placeholder {
|
||||||
c.error('unknown type `$sym.source_name`', array_init.elem_type_pos)
|
c.error('unknown type `$sym.source_name`', array_init.elem_type_pos)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
vlib/v/checker/tests/array_init_sum_type_without_init_value_and_len_err.vv:4:9: error: cannot initalize sum type array without default value
|
||||||
|
2 |
|
||||||
|
3 | fn main() {
|
||||||
|
4 | a := []Foo{len:10}
|
||||||
|
| ~~~
|
||||||
|
5 | println(a)
|
||||||
|
6 |
|
|
@ -0,0 +1,7 @@
|
||||||
|
type Foo = string | int
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
a := []Foo{len:10}
|
||||||
|
println(a)
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue