Fix bug with enforcing purity in constant initializers
[hiphop-php.git] / hphp / hack / test / enum_class / typing / typing.extends1.bad.php
blobb79465ec3542e5e227d8707ab3d7ac388325f5a2
1 <?hh
2 // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
4 // User code
5 function expect_int(int $_): void {}
6 function expect_string(string $_): void {}
7 function expect_num(num $_): void {}
9 interface ExBox {}
11 class Box<T> implements ExBox {
12 public function __construct(public T $data)[] {}
15 class IBox extends Box<int> {
16 public function add(int $x)[write_props]: void {
17 $this->data = $this->data + $x;
21 abstract final class Helper {
22 public static function ibox()[]: IBox {
23 return new IBox(42);
27 // Enum Class user code
28 enum class E: ExBox {
29 Box<string> A = new Box('bli');
30 IBox B = Helper::ibox();
31 Box<int> B2 = new Box(42);
34 enum class F: ExBox extends E {
35 Box<num> C = new Box(3.14);
38 abstract class Base {
39 abstract const type TEnum as E;
41 public function get<T>(HH\MemberOf<this::TEnum, Box<T>> $param): T {
42 return $param->data;
45 public static function dictGen(): dict<string, ExBox> {
46 $dict = dict[];
47 $ts = type_structure(static::class, 'TEnum');
48 $cls = $ts['classname'];
49 foreach ($cls::getValues() as $key => $elt) {
50 $dict[$key] = $elt;
52 return $dict;
56 class Ext extends Base {
57 const type TEnum = E; // [0]
60 function test_constraint(): void {
61 $ext = new Ext();
62 echo $ext->get(F::C); // F vs E, because of [0].
63 echo "\n";