From 0d2bb714bc98bdb9cf0c6d33ec344f7a55c85bf1 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Wed, 10 Mar 2021 11:07:36 +0200 Subject: [PATCH] checker: disallow `x := [13]SumType{}` too --- vlib/v/checker/checker.v | 18 +++++++++++++----- ...sum_type_without_init_value_and_len_err.out | 13 ++++++++++--- ..._sum_type_without_init_value_and_len_err.vv | 6 ++++-- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 3ed066cc5c..4d0dee42a4 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2943,6 +2943,13 @@ fn (mut c Checker) check_array_init_para_type(para string, expr ast.Expr, pos to } } +pub fn (mut c Checker) ensure_sumtype_array_has_default_value(array_init ast.ArrayInit) { + sym := c.table.get_type_symbol(array_init.elem_type) + if sym.kind == .sum_type && !array_init.has_default { + c.error('cannot initialize sum type array without default value', array_init.pos) + } +} + pub fn (mut c Checker) array_init(mut array_init ast.ArrayInit) table.Type { // println('checker: array init $array_init.pos.line_nr $c.file.path') mut elem_type := table.void_type @@ -2956,21 +2963,22 @@ pub fn (mut c Checker) array_init(mut array_init ast.ArrayInit) table.Type { c.check_array_init_para_type('len', array_init.len_expr, array_init.pos) } } - sym := c.table.get_type_symbol(array_init.elem_type) if array_init.has_default { default_typ := c.expr(array_init.default_expr) c.check_expected(default_typ, array_init.elem_type) or { c.error(err.msg, array_init.default_expr.position()) } } - if sym.kind == .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 array_init.has_len { + c.ensure_sumtype_array_has_default_value(array_init) } c.ensure_type_exists(array_init.elem_type, array_init.elem_type_pos) or {} return array_init.typ } + if array_init.is_fixed { + c.ensure_sumtype_array_has_default_value(array_init) + c.ensure_type_exists(array_init.elem_type, array_init.elem_type_pos) or {} + } // a = [] if array_init.exprs.len == 0 { // a := fn_returing_opt_array() or { [] } diff --git a/vlib/v/checker/tests/array_init_sum_type_without_init_value_and_len_err.out b/vlib/v/checker/tests/array_init_sum_type_without_init_value_and_len_err.out index 5048e7efab..129d9db2d6 100644 --- a/vlib/v/checker/tests/array_init_sum_type_without_init_value_and_len_err.out +++ b/vlib/v/checker/tests/array_init_sum_type_without_init_value_and_len_err.out @@ -1,7 +1,14 @@ -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 +vlib/v/checker/tests/array_init_sum_type_without_init_value_and_len_err.vv:4:7: error: cannot initialize sum type array without default value 2 | 3 | fn main() { - 4 | a := []Foo{len:10} - | ~~~ + 4 | a := []Foo{len: 10} + | ~~~~~~ 5 | println(a) 6 | +vlib/v/checker/tests/array_init_sum_type_without_init_value_and_len_err.vv:7:13: error: cannot initialize sum type array without default value + 5 | println(a) + 6 | + 7 | fixed_a := [10]Foo{} + | ~~~~~~~~~ + 8 | println(fixed_a) + 9 | } diff --git a/vlib/v/checker/tests/array_init_sum_type_without_init_value_and_len_err.vv b/vlib/v/checker/tests/array_init_sum_type_without_init_value_and_len_err.vv index 935f5ba442..2494d52731 100644 --- a/vlib/v/checker/tests/array_init_sum_type_without_init_value_and_len_err.vv +++ b/vlib/v/checker/tests/array_init_sum_type_without_init_value_and_len_err.vv @@ -1,7 +1,9 @@ -type Foo = string | int +type Foo = int | string fn main() { - a := []Foo{len:10} + a := []Foo{len: 10} println(a) + fixed_a := [10]Foo{} + println(fixed_a) }