cgen: properly cast to sumtypes in array prepend and insert (#11289)

pull/11291/head
Lukas Neubert 2021-08-24 05:25:09 +02:00 committed by GitHub
parent 4824b409b1
commit 3249f8f0e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View File

@ -411,7 +411,7 @@ fn (mut g Gen) gen_array_insert(node ast.CallExpr) {
if left_info.elem_type == ast.string_type { if left_info.elem_type == ast.string_type {
g.write('string_clone(') 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 { if left_info.elem_type == ast.string_type {
g.write(')') g.write(')')
} }
@ -441,7 +441,7 @@ fn (mut g Gen) gen_array_prepend(node ast.CallExpr) {
g.write('.len)') g.write('.len)')
} else { } else {
g.write(', &($elem_type_str[]){') 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('})') g.write('})')
} }
} }

View 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
}