1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix matching interface types (#8889)

This commit is contained in:
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('if (')
} }
g.write(cond_var) 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 { if sym.kind == .sum_type {
dot_or_ptr := if node.cond_type.is_ptr() { '->' } else { '.' } g.write('${dot_or_ptr}typ == ')
g.write(dot_or_ptr) g.expr(branch.exprs[sumtype_index])
g.write('typ == ')
} else if sym.kind == .interface_ { } else if sym.kind == .interface_ {
// g.write('._interface_idx == _${sym.name}_${branch_sym} ') typ := branch.exprs[sumtype_index] as ast.Type
g.write('._interface_idx == ') 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 { if is_expr && tmp_var.len == 0 {
g.write(') ? ') g.write(') ? ')
} else { } 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 }
}
}