Created
October 23, 2015 11:18
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?hh | |
require_once(__DIR__.'/minimum-test.hh'); | |
Unsafe::test(); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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