v.checker,v.gen: allow for `&IAbc(voidptr(0))` where IAbc contains fields
parent
e7cc93a120
commit
862c4cf371
|
@ -2721,8 +2721,11 @@ fn (mut c Checker) type_implements(typ ast.Type, interface_type ast.Type, pos to
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
c.error("`$styp` doesn't implement field `$ifield.name` of interface `$inter_sym.name`",
|
// voidptr is an escape hatch, it should be allowed to be passed
|
||||||
pos)
|
if utyp != ast.voidptr_type {
|
||||||
|
c.error("`$styp` doesn't implement field `$ifield.name` of interface `$inter_sym.name`",
|
||||||
|
pos)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
inter_sym.info.types << utyp
|
inter_sym.info.types << utyp
|
||||||
}
|
}
|
||||||
|
|
|
@ -6549,11 +6549,13 @@ fn (mut g Gen) interface_table() string {
|
||||||
} else {
|
} else {
|
||||||
// the field is embedded in another struct
|
// the field is embedded in another struct
|
||||||
cast_struct.write_string('\t\t.$cname = ($field_styp*)((char*)x')
|
cast_struct.write_string('\t\t.$cname = ($field_styp*)((char*)x')
|
||||||
for embed_type in st_sym.struct_info().embeds {
|
if st != ast.voidptr_type {
|
||||||
embed_sym := g.table.get_type_symbol(embed_type)
|
for embed_type in st_sym.struct_info().embeds {
|
||||||
if _ := embed_sym.find_field(field.name) {
|
embed_sym := g.table.get_type_symbol(embed_type)
|
||||||
cast_struct.write_string(' + __offsetof_ptr(x, $cctype, $embed_sym.embed_name()) + __offsetof_ptr(x, $embed_sym.cname, $cname)')
|
if _ := embed_sym.find_field(field.name) {
|
||||||
break
|
cast_struct.write_string(' + __offsetof_ptr(x, $cctype, $embed_sym.embed_name()) + __offsetof_ptr(x, $embed_sym.cname, $cname)')
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cast_struct.writeln('),')
|
cast_struct.writeln('),')
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
interface IAbc {
|
||||||
|
name string
|
||||||
|
xyz()
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Abc {
|
||||||
|
name string
|
||||||
|
x int = 123
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (a Abc) xyz() {}
|
||||||
|
|
||||||
|
fn f(i &IAbc) string {
|
||||||
|
return '$i'
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_voidptr_casted_as_an_interface_reference() {
|
||||||
|
mut pi := &IAbc(voidptr(0))
|
||||||
|
dump(pi)
|
||||||
|
assert f(pi) == '&IAbc(0)'
|
||||||
|
//
|
||||||
|
i := IAbc(Abc{})
|
||||||
|
pi = &i
|
||||||
|
dump(pi)
|
||||||
|
assert f(pi).contains('x: 123')
|
||||||
|
}
|
Loading…
Reference in New Issue