1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker, cgen: fix array of sumtype appending alias (#15034)

This commit is contained in:
yuyi 2022-07-12 14:26:11 +08:00 committed by GitHub
parent 8a4313c1b8
commit 62d800a775
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 3 deletions

View File

@ -406,7 +406,7 @@ 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_sym.kind != .array {
if right_sym.kind != .array {
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)

View File

@ -695,8 +695,9 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) {
tmp_var := g.new_tmp_var()
array_info := left.unaliased_sym.info as ast.Array
noscan := g.check_noscan(array_info.elem_type)
//&& array_info.elem_type != g.unwrap_generic(node.right_type)
if right.unaliased_sym.kind == .array && array_info.elem_type != right.typ {
if right.unaliased_sym.kind == .array && array_info.elem_type != right.typ
&& !(right.sym.kind == .alias
&& g.table.sumtype_has_variant(array_info.elem_type, node.right_type, false)) {
// push an array => PUSH_MANY, but not if pushing an array to 2d array (`[][]int << []int`)
g.write('_PUSH_MANY${noscan}(')
mut expected_push_many_atype := left.typ

View File

@ -0,0 +1,27 @@
type Attribute = NextHop | Origin
type Origin = u8
type NextHop = []u8
fn test_array_of_sumtype_append_alias() {
mut attrs := []Attribute{cap: 100}
bytes := []u8{len: 4}
attrs << NextHop(bytes)
attrs << Origin(1)
for attr in attrs {
if attr is Origin {
println(attr)
continue
}
if attr is NextHop {
println(attr)
continue
}
panic('oh no !')
}
assert attrs.len == 2
assert attrs[0] == Attribute(NextHop(bytes))
assert attrs[1] == Attribute(Origin(1))
}