Skip to content

Instantly share code, notes, and snippets.

@pbl64k
Created October 23, 2015 11:18
Show Gist options
  • Save pbl64k/6e936c13e9fb5b625f25 to your computer and use it in GitHub Desktop.
Save pbl64k/6e936c13e9fb5b625f25 to your computer and use it in GitHub Desktop.
Type erasure allows unsafe coercions using generics even in Hack's strict mode.
<?hh
require_once(__DIR__.'/minimum-test.hh');
Unsafe::test();
<?hh // strict
interface ITest
{
}
final class Dummy
{
final public function emit(): void
{
print('Test.'."\n");
}
}
final class Unsafe<Tx> implements ITest
{
final public function __construct(private Tx $x)
{
}
// the culprit
final public static function convert(ITest $x): Unsafe<Tx>
{
invariant($x instanceof Unsafe, '...or else');
return $x;
}
final public static function mkInt(): Unsafe<int>
{
return new self(1);
}
final public static function emit(Unsafe<Dummy> $x): void
{
$x->x->emit();
}
final public static function test(): void
{
Unsafe::emit(Unsafe::convert(Unsafe::mkInt()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment