mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: fix error for array of sumtype appending literal value (#14200)
This commit is contained in:
parent
09f8b6a380
commit
7dd5d9ee61
@ -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)
|
||||
}
|
||||
|
20
vlib/v/tests/array_of_sumtype_append_literal_type_test.v
Normal file
20
vlib/v/tests/array_of_sumtype_append_literal_type_test.v
Normal file
@ -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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user