v.checker: allow for a `f(unsafe{voidptr(0)})` call of `fn f(x &Interface){}`
parent
2f9e03b360
commit
e7cc93a120
|
@ -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 {
|
||||
|
|
|
@ -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)'
|
||||
}
|
Loading…
Reference in New Issue