mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
v.checker,v.gen: allow for &IAbc(voidptr(0))
where IAbc contains fields
This commit is contained in:
parent
e7cc93a120
commit
862c4cf371
@ -2721,9 +2721,12 @@ fn (mut c Checker) type_implements(typ ast.Type, interface_type ast.Type, pos to
|
||||
}
|
||||
continue
|
||||
}
|
||||
// voidptr is an escape hatch, it should be allowed to be passed
|
||||
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
|
||||
}
|
||||
return true
|
||||
|
@ -6549,6 +6549,7 @@ fn (mut g Gen) interface_table() string {
|
||||
} else {
|
||||
// the field is embedded in another struct
|
||||
cast_struct.write_string('\t\t.$cname = ($field_styp*)((char*)x')
|
||||
if st != ast.voidptr_type {
|
||||
for embed_type in st_sym.struct_info().embeds {
|
||||
embed_sym := g.table.get_type_symbol(embed_type)
|
||||
if _ := embed_sym.find_field(field.name) {
|
||||
@ -6556,6 +6557,7 @@ fn (mut g Gen) interface_table() string {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
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
Block a user