cgen: fix error for generic sumtype casting to typenode (#14188)

yuyi 2022-04-27 16:32:43 +08:00 committed by Jef Roosens
parent 604f4f5e44
commit 8b085a32a5
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 56 additions and 6 deletions

View File

@ -5207,9 +5207,10 @@ fn (mut g Gen) as_cast(node ast.AsCast) {
// Make sure the sum type can be cast to this type (the types
// are the same), otherwise panic.
// g.insert_before('
styp := g.typ(node.typ)
sym := g.table.sym(node.typ)
mut expr_type_sym := g.table.sym(node.expr_type)
unwrapped_node_typ := g.unwrap_generic(node.typ)
styp := g.typ(unwrapped_node_typ)
sym := g.table.sym(unwrapped_node_typ)
mut expr_type_sym := g.table.sym(g.unwrap_generic(node.expr_type))
if mut expr_type_sym.info is ast.SumType {
dot := if node.expr_type.is_ptr() { '->' } else { '.' }
g.write('/* as */ *($styp*)__as_cast(')
@ -5223,9 +5224,8 @@ fn (mut g Gen) as_cast(node ast.AsCast) {
g.write(')')
g.write(dot)
// g.write('typ, /*expected:*/$node.typ)')
sidx := g.type_sidx(node.typ)
expected_sym := g.table.sym(node.typ)
g.write('_typ, $sidx) /*expected idx: $sidx, name: $expected_sym.name */ ')
sidx := g.type_sidx(unwrapped_node_typ)
g.write('_typ, $sidx) /*expected idx: $sidx, name: $sym.name */ ')
// fill as cast name table
for variant in expr_type_sym.info.variants {

View File

@ -0,0 +1,50 @@
module main
struct Empty {}
struct Node<T> {
value T
mut:
next Tree<T>
}
type Tree<T> = Empty | Node<T>
fn create<T>() Tree<T> {
empty := Empty{}
mut curr := Node<T>{10, empty}
for _ in 0 .. 10 {
curr.next = Node<T>{20, empty}
}
return curr
}
fn create_node<T>(args []T) Tree<T> {
empty := Empty{}
if args.len == 0 {
return empty
}
mut curr := Node<T>{args[0], empty}
for i := 1; i < args.len; i += 1 {
curr.next = Node<T>{args[i], empty}
curr = curr.next as Node<T>
}
return curr
}
fn merge_nodes<T>(head Tree<T>) Tree<T> {
println('$head')
return Empty{}
}
fn test_generic_sumtype_cast() {
node := create_node<int>([0, 3, 1, 0, 4, 5, 2, 0])
merge_nodes(node)
create<int>()
assert true
}