cgen: fix infix generics bug (#14048)

Julien de Carufel 2022-04-16 06:23:19 -04:00 committed by Jef Roosens
parent e65f602a4f
commit 72a7c19f83
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 34 additions and 3 deletions

View File

@ -515,9 +515,15 @@ fn (mut g Gen) infix_expr_is_op(node ast.InfixExpr) {
g.write('_typ $cmp_op ')
// `_Animal_Dog_index`
sub_type := match node.right {
ast.TypeNode { node.right.typ }
ast.None { g.table.type_idxs['None__'] }
else { ast.Type(0) }
ast.TypeNode {
g.unwrap_generic(node.right.typ)
}
ast.None {
g.table.type_idxs['None__']
}
else {
ast.Type(0)
}
}
sub_sym := g.table.sym(sub_type)
g.write('_${sym.cname}_${sub_sym.cname}_index')

View File

@ -0,0 +1,25 @@
interface Any {}
struct Concrete {
a int
}
struct Container {
concrete Any
}
fn (container &Container) get_first_struct<T>() ?&T {
concrete := container.concrete
if concrete is T {
println(concrete.a)
return concrete
}
return error("can't cast")
}
fn test_generic_empty_interface_to_struct() {
concrete := Concrete{12345}
container := Container{concrete}
cast_concrete := container.get_first_struct<Concrete>() or { &Concrete{} }
assert 12345 == cast_concrete.a
}