From 47c18c45703c42a9b8029d801d820f61811932e6 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 28 Apr 2022 13:44:30 +0800 Subject: [PATCH] checker: fix error for array of sumtype appending literal value (#14200) --- vlib/v/checker/checker.v | 4 ++-- ...rray_of_sumtype_append_literal_type_test.v | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/array_of_sumtype_append_literal_type_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index fc4b62fab1..7fdc39c72b 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -935,13 +935,13 @@ pub fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { return ast.void_type } else if left_value_sym.kind == .sum_type { if right_final.kind != .array { - if !c.table.is_sumtype_or_in_variant(left_value_type, right_type) { + if !c.table.is_sumtype_or_in_variant(left_value_type, ast.mktyp(right_type)) { c.error('cannot append `$right_sym.name` to `$left_sym.name`', right_pos) } } else { right_value_type := c.table.value_type(right_type) - if !c.table.is_sumtype_or_in_variant(left_value_type, right_value_type) { + if !c.table.is_sumtype_or_in_variant(left_value_type, ast.mktyp(right_value_type)) { c.error('cannot append `$right_sym.name` to `$left_sym.name`', right_pos) } diff --git a/vlib/v/tests/array_of_sumtype_append_literal_type_test.v b/vlib/v/tests/array_of_sumtype_append_literal_type_test.v new file mode 100644 index 0000000000..57f3010444 --- /dev/null +++ b/vlib/v/tests/array_of_sumtype_append_literal_type_test.v @@ -0,0 +1,20 @@ +type Typ_var = f64 | int + +fn test_array_of_sumtype_append_literal_type() { + mut arr := []Typ_var{} + + // literal int/float type error + arr << 123 + arr << 1.23 + + // cast/wrap in type + arr << int(123) + arr << f64(1.23) + arr << Typ_var(456) + arr << Typ_var(4.56) + + println(arr) + + assert arr[0] == Typ_var(123) + assert arr[1] == Typ_var(1.23) +}