v.checker: allow for a `f(unsafe{voidptr(0)})` call of `fn f(x &Interface){}`

pull/10479/head
Delyan Angelov 2021-06-16 09:20:01 +03:00
parent 2f9e03b360
commit e7cc93a120
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 24 additions and 2 deletions

View File

@ -2698,9 +2698,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 method `$imethod.name` of interface `$inter_sym.name`",
pos)
}
}
// Verify fields
if mut inter_sym.info is ast.Interface {
for ifield in inter_sym.info.fields {

View File

@ -0,0 +1,19 @@
interface IAbc {
xyz()
}
struct Abc {}
fn (a Abc) xyz() {}
fn f(i &IAbc) string {
return '$i'
}
fn test_passing_voidptr_as_an_interface_reference() {
i := IAbc(Abc{})
assert f(&i) == '&IAbc(Abc{})'
// a voidptr() cast is an escape hatch, that should be allowed
// but perhaps it should be forced by the compiler to be in unsafe{}
assert f(unsafe { voidptr(0) }) == '&IAbc(0)'
}