mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix match
ing interface types (#8889)
This commit is contained in:
parent
15daeaeafa
commit
1658c4789f
@ -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 {
|
||||
|
12
vlib/v/tests/match_interface_test.v
Normal file
12
vlib/v/tests/match_interface_test.v
Normal 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 }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user