mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix infix generics bug (#14048)
This commit is contained in:
parent
43931a8e77
commit
16ead4e63c
@ -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')
|
||||
|
25
vlib/v/tests/generic_empty_interface_to_struct_test.v
Normal file
25
vlib/v/tests/generic_empty_interface_to_struct_test.v
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user