cgen: fix `match`ing interface types (#8889)

pull/8904/head
spaceface 2021-02-22 13:55:43 +01:00 committed by GitHub
parent 15daeaeafa
commit 1658c4789f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 7 deletions

View File

@ -3610,16 +3610,15 @@ fn (mut g Gen) match_expr_sumtype(node ast.MatchExpr, is_expr bool, cond_var str
g.write('if (')
}
g.write(cond_var)
// branch_sym := g.table.get_type_symbol(branch.typ)
dot_or_ptr := if node.cond_type.is_ptr() { '->' } else { '.' }
if sym.kind == .sum_type {
dot_or_ptr := if node.cond_type.is_ptr() { '->' } else { '.' }
g.write(dot_or_ptr)
g.write('typ == ')
g.write('${dot_or_ptr}typ == ')
g.expr(branch.exprs[sumtype_index])
} else if sym.kind == .interface_ {
// g.write('._interface_idx == _${sym.name}_${branch_sym} ')
g.write('._interface_idx == ')
typ := branch.exprs[sumtype_index] as ast.Type
branch_sym := g.table.get_type_symbol(typ.typ)
g.write('${dot_or_ptr}_interface_idx == _${sym.cname}_${branch_sym.cname}_index')
}
g.expr(branch.exprs[sumtype_index])
if is_expr && tmp_var.len == 0 {
g.write(') ? ')
} else {

View File

@ -0,0 +1,12 @@
interface Animal { name string }
struct Dog { name string }
struct Cat { name string }
fn test_interface_match() {
a := Animal(Dog{name: 'Jet'})
match a {
Dog { assert true }
Cat { assert false }
else { assert false }
}
}