mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: properly cast to sumtypes in array prepend and insert (#11289)
This commit is contained in:
parent
4824b409b1
commit
3249f8f0e7
@ -411,7 +411,7 @@ fn (mut g Gen) gen_array_insert(node ast.CallExpr) {
|
||||
if left_info.elem_type == ast.string_type {
|
||||
g.write('string_clone(')
|
||||
}
|
||||
g.expr(node.args[1].expr)
|
||||
g.expr_with_cast(node.args[1].expr, node.args[1].typ, left_info.elem_type)
|
||||
if left_info.elem_type == ast.string_type {
|
||||
g.write(')')
|
||||
}
|
||||
@ -441,7 +441,7 @@ fn (mut g Gen) gen_array_prepend(node ast.CallExpr) {
|
||||
g.write('.len)')
|
||||
} else {
|
||||
g.write(', &($elem_type_str[]){')
|
||||
g.expr(node.args[0].expr)
|
||||
g.expr_with_cast(node.args[0].expr, node.args[0].typ, left_info.elem_type)
|
||||
g.write('})')
|
||||
}
|
||||
}
|
||||
|
20
vlib/v/tests/array_of_sumtypes_test.v
Normal file
20
vlib/v/tests/array_of_sumtypes_test.v
Normal file
@ -0,0 +1,20 @@
|
||||
struct Node {
|
||||
mut:
|
||||
tag string
|
||||
content []Content
|
||||
}
|
||||
|
||||
struct Text {
|
||||
}
|
||||
|
||||
type Content = Node | Text
|
||||
|
||||
fn test_push_prepend_insert() {
|
||||
mut body := Node{}
|
||||
body.content << Node{
|
||||
tag: 'a'
|
||||
}
|
||||
body.content.prepend(Node{ tag: 'b' })
|
||||
body.content.insert(1, Node{ tag: 'c' })
|
||||
assert body.content.len == 3
|
||||
}
|
Loading…
Reference in New Issue
Block a user