language
stringlengths
0
24
filename
stringlengths
9
214
code
stringlengths
99
9.93M
PHP
hhvm/hphp/hack/test/typecheck/abstract_redeclare_abstract_class_constant.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. abstract class A { abstract const arraykey P; } abstract class B extends A { abstract const int P; }
PHP
hhvm/hphp/hack/test/typecheck/abstract_redeclare_abstract_class_constant_bad.php
////file1.php <?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. abstract class A { abstract const int P; } ////file2.php <?hh abstract class B extends A { abstract const arraykey P; }
PHP
hhvm/hphp/hack/test/typecheck/abstract_redeclare_constructor.php
<?hh abstract class MyParent { public function __construct(): void {} } abstract class MyChild extends MyParent { abstract public function __construct(): void; }
PHP
hhvm/hphp/hack/test/typecheck/abstract_tconst_allowed_self.php
<?hh // Copyright 2004-present Facebook. All Rights Reserved. interface ISiteVariable { abstract const type TVal; } trait SiteVariable implements ISiteVariable { private static ?self::TVal $data = null; }
PHP
hhvm/hphp/hack/test/typecheck/abstract_tconst_allowed_this.php
<?hh // strict class D {} abstract class C { abstract const type T as D; const type Tc = int; public function __construct(private this::T $x) {} public function bar(): this::T { return $this->x; } } function expect_D(D $_): void {} function test(C $c): void { expect_D($c->bar()); } function foo(C::Tc $x): C::Tc { return $x; }
PHP
hhvm/hphp/hack/test/typecheck/abstract_tconst_different_file.php
//// file1.php <?hh // Copyright 2004-present Facebook. All Rights Reserved. abstract class A { abstract const type T; } class B { const type T = vec<A::T>; public function get(): this::T { return vec[]; } } //// file2.php <?hh // Copyright 2004-present Facebook. All Rights Reserved. class C extends B { public function test(): void { // make sure the error about abstract A::T is not raised here. $x = $this->get(); } }
PHP
hhvm/hphp/hack/test/typecheck/abstract_tconst_not_allowed.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. abstract class C { abstract const type T; } function f<T>(): void {} function test(): void { f<C::T>(); }
PHP
hhvm/hphp/hack/test/typecheck/abstract_tconst_not_allowed_arg.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. abstract class C { abstract const type T; } function f(C::T $_): void {}
PHP
hhvm/hphp/hack/test/typecheck/abstract_tconst_not_allowed_duplicate_trait.php
<?hh // Copyright 2004-present Facebook. All Rights Reserved. abstract class A { abstract const type T; } trait MyTrait { public function foo(A::T $_): void {} } class B { use MyTrait; }
PHP
hhvm/hphp/hack/test/typecheck/abstract_tconst_not_allowed_return.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. abstract class C { abstract const type T; abstract public function foo(): this::T; } function f(C $c): C::T { return $c->foo(); }
PHP
hhvm/hphp/hack/test/typecheck/add_union_any.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. /* HH_FIXME[4030] */ function makeAny() { return 3; } function expectInt(int $x):void { } function testit(bool $f, int $z):void { if ($f) $z = makeAny(); $w = $z + 1; expectInt($w); }
PHP
hhvm/hphp/hack/test/typecheck/add_vector.php
<?hh // strict /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ class MyPhonyVector<T> { private Vector<T> $x; public function __construct() { $this->x = Vector {}; } public function add(T $x): void { $this->x[] = $x; } public function get(int $x): T { return $this->x[0]; } } class X {} class A extends X {} class B extends X {} function test(MyPhonyVector<X> $v): void { $x = new MyPhonyVector(); $x->add(new B()); $x->add(new A()); test($x); }
PHP
hhvm/hphp/hack/test/typecheck/akempty_promotion_15.php
<?hh //strict function test(bool $b): varray<string> { $a = darray[]; if ($b) { $a[4] = 'aaa'; } return $a; }
PHP
hhvm/hphp/hack/test/typecheck/akempty_promotion_8.php
<?hh //strict /** * Test confilicting AKempty promotions - in one branch it's promoted to * AKvarray, in another to AKdarray. Should be unresolved of those two afterwards. */ function test(bool $b): void { $a = varray[]; if ($b) { $a[] = 'aaa'; } else { $a[0] = 'aaa'; } // error - one of the branches result in a map-like array f($a); } function f(varray<string> $_): void {}
PHP
hhvm/hphp/hack/test/typecheck/akempty_tuple_bad.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. function omg(): (?string, varray<int>) { $x = varray[]; return $x; } function breakit(): void { $x = omg(); list($y, $z) = $x; echo $y; } //breakit();
PHP
hhvm/hphp/hack/test/typecheck/alok_check1.php
<?hh /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ type Foo<TBar> = TBar<T1>;
PHP
hhvm/hphp/hack/test/typecheck/ambiguous_constructor.php
<?hh // strict trait T1 { public function __construct() {} } trait T2 { public function __construct() {} } class C { use T1, T2; }
PHP
hhvm/hphp/hack/test/typecheck/ambiguous_declaration_child.php
<?hh trait T1 { public function foo(): void {} } trait T2 { public function foo(): void {} } class C { use T1, T2; public function foo(): void {} }
PHP
hhvm/hphp/hack/test/typecheck/ambiguous_declaration_concrete.php
<?hh trait T1 { public function foo(): void {} } trait T2 { public function foo(): void {} } class C { use T1, T2; }
PHP
hhvm/hphp/hack/test/typecheck/ambiguous_declaration_identical.php
<?hh interface I1 { public function foo(): int; } interface I2 { public function foo(): int; } interface I3 extends I1, I2 {}
PHP
hhvm/hphp/hack/test/typecheck/ambiguous_declaration_interface.php
<?hh interface I1 { public function foo(): int; } interface I2 { public function foo(): num; } interface I3 extends I1, I2 {}
PHP
hhvm/hphp/hack/test/typecheck/ambiguous_declaration_interface_trait.php
<?hh interface I1 { public function foo(): num; } trait T { abstract public function foo(): int; } class C implements I1 { use T; }
PHP
hhvm/hphp/hack/test/typecheck/ambiguous_declaration_interface_trait_good.php
<?hh class A { public function foo(): void {} } interface I { require extends A; } trait T { public function foo(): void {} } class B extends A implements I { use T; }
PHP
hhvm/hphp/hack/test/typecheck/ambiguous_declaration_one_synthetic.php
<?hh class A { public function foo(): void {} } trait T1 { require extends A; } trait T2 { public function foo(): void {} } class B extends A { use T1, T2; }
PHP
hhvm/hphp/hack/test/typecheck/ambiguous_declaration_one_synthetic_1.php
<?hh class A { public function foo(): void {} } trait T1 { require extends A; } trait T2 { public function foo(): void {} } trait T3 { public function foo(): void {} } class B extends A { use T1, T2, T3; }
PHP
hhvm/hphp/hack/test/typecheck/ambiguous_declaration_one_synthetic_2.php
<?hh class A { public function foo(): void {} } trait T1 { public function foo(): void {} } trait T2 { require extends A; } trait T3 { public function foo(): void {} } class B extends A { use T1, T2, T3; }
PHP
hhvm/hphp/hack/test/typecheck/ambiguous_declaration_one_synthetic_3.php
<?hh class A { public function foo(): void {} } trait T1 { public function foo(): void {} } trait T2 { public function foo(): void {} } trait T3 { require extends A; } class B extends A { use T1, T2, T3; }
PHP
hhvm/hphp/hack/test/typecheck/ambiguous_declaration_parent.php
<?hh class P { public function foo(): num { return 0; } } trait T { public function foo(): int { return 0; } } class C extends P { use T; // no error, trait definition is unique }
PHP
hhvm/hphp/hack/test/typecheck/ambiguous_declaration_synthetic.php
<?hh class C { public function foo(): void {} } trait T { require extends C; } trait T2 { require extends C; } class D extends C { use T, T2; }
PHP
hhvm/hphp/hack/test/typecheck/ambiguous_distant_constructor.php
<?hh // strict trait TX { public function __construct() {} } trait T1 { use TX; } trait T2 { public function __construct() {} } class C { use T1, T2; }
PHP
hhvm/hphp/hack/test/typecheck/ambiguous_inheritance.php
<?hh // strict interface I1 { public function foo(): mixed; } interface I2 { public function foo(): int; } abstract class C implements I1, I2 {}
PHP
hhvm/hphp/hack/test/typecheck/ambiguous_member_good.php
<?hh // strict interface I1<T> { public function dupe1(int $x): void; public function dupe2(): vec<string>; public function dupe3(): T; } interface I2<T> { public function dupe1(int $x): void; public function dupe2(): vec<string>; public function dupe3(): T; } function test<T as I1<int> as I2<int>>(T $x): void { $x->dupe1(0); $x->dupe2(); $x->dupe3(); }
PHP
hhvm/hphp/hack/test/typecheck/anon1.php
<?hh // strict /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function test(): void { $x = 0; $f = function($y) use ($x) { return $x + $y; }; $f(0); }
PHP
hhvm/hphp/hack/test/typecheck/anon10.php
<?hh // strict /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function test(): int { $f = function($x = 0) { return $x; }; return $f(); }
PHP
hhvm/hphp/hack/test/typecheck/anon11.php
<?hh // strict /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function test(): string { $f = function($x = 0) { return $x; }; return $f(); }
PHP
hhvm/hphp/hack/test/typecheck/anon12.php
<?hh // strict /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function test(): string { $f = function($x = 0, $y) { return $x; }; return $f(); }
PHP
hhvm/hphp/hack/test/typecheck/anon13.php
<?hh // strict /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function show<T>(T $x): void {} function test(): Vector<(function(int): int)> { $v = Vector { function($x) { return $x; }, }; $v[] = function($x) use ($v) { return $v[0]($x); }; return $v; }
PHP
hhvm/hphp/hack/test/typecheck/anon14.php
<?hh // strict /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function show<T>(T $x): void {} function test(): void { $v = Vector { function($x) { return $x; }, }; $v[0] = function($x) use ($v) { return $v[0]($x); }; $v[0](1); }
PHP
hhvm/hphp/hack/test/typecheck/anon15.php
<?hh /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function main(): void { $foo = function(darray<string, mixed> $rel): darray<string, mixed> { return darray[ 'id' => $rel['id'], 'relation' => $rel['relation_type'], ]; }; }
PHP
hhvm/hphp/hack/test/typecheck/anon16.php
<?hh // strict function expect_mixed_func((function(): mixed) $func): void {} function test(): void { $func = (): int ==> 1; expect_mixed_func($func); }
PHP
hhvm/hphp/hack/test/typecheck/anon17.php
<?hh // strict function expects_vec_func_str(Vector<(function(): string)> $vec): void {} function test(): void { $funcs = Vector { () ==> 1.0, () ==> 0, () ==> '' }; expects_vec_func_str($funcs); }
PHP
hhvm/hphp/hack/test/typecheck/anon2.php
<?hh // strict /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function test(): void { $x = 0; $f = function($y) use ($x) { return $x + $y; }; $f(''); }
PHP
hhvm/hphp/hack/test/typecheck/anon3.php
<?hh // strict /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function test(): void { $f = function($y) { return $y; }; $f(''); $f(0); }
PHP
hhvm/hphp/hack/test/typecheck/anon4.php
<?hh // strict /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function test(): void { $v = Vector<nothing> {}; $f = $v[0]; $f = function($y) use ($f) { return $f($y); }; $f(''); }
PHP
hhvm/hphp/hack/test/typecheck/anon6.php
<?hh // strict /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function test(): (function(int): int) { $f = function($y) { return $y; }; return $f; }
PHP
hhvm/hphp/hack/test/typecheck/anon7.php
<?hh // strict /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function test(): (function(int): int) { $f = function($y) { return $y.''; }; return $f; }
PHP
hhvm/hphp/hack/test/typecheck/anon8.php
<?hh // strict /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function test(): (function(): string) { $f = function($y) { return $y.''; }; return $f; }
PHP
hhvm/hphp/hack/test/typecheck/anon9.php
<?hh // strict /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function test(): (function(string): string) { $f = function() { return ''; }; return $f; }
PHP
hhvm/hphp/hack/test/typecheck/anonymous_shape1.php
<?hh //strict /** * Nested anonymous shape */ type s = shape('x' => shape('y' => int)); function test(): s { return shape('x' => shape('y' => 'aaa')); }
PHP
hhvm/hphp/hack/test/typecheck/anonymous_shape2.php
<?hh //strict /** * Anonymous shape return type */ function f(): shape('x' => int) { return shape('x' => 4); } function test( ): shape( 'y' => int, ... ) { return f(); }
PHP
hhvm/hphp/hack/test/typecheck/anonymous_shape3.php
<?hh //strict /** * Anonymous shape parameter type hint */ function test(shape('x' => int) $s): void { $s['y']; }
PHP
hhvm/hphp/hack/test/typecheck/anon_scope_bug.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. function test(int $x): void { function () { $x; // should be undefined }; }
PHP
hhvm/hphp/hack/test/typecheck/anon_yield.php
<?hh /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function test(): (function(): Awaitable<int>) { $f = async function() { return 0; }; return $f; }
PHP
hhvm/hphp/hack/test/typecheck/anyarray.php
<?hh //strict function takes_array(AnyArray<int, int> $_): void {} function takes_container(Container<int> $_): void {} function takes_keyed_container(KeyedContainer<int, int> $_): void {} function gives_array(): AnyArray<int, int> { return vec[]; } function handles_generic_array<Tk as arraykey, Tv>( AnyArray<Tk, Tv> $a, ): AnyArray<Tk, Tv> { return $a; } function takes_reified_array<reify TParam>(): void {} function expect_int(int $_): void {} function does_things(): void { takes_array(varray[]); takes_array(darray[]); takes_array(vec[]); takes_array(dict[]); takes_array(keyset[]); takes_array(gives_array()); takes_container(gives_array()); handles_generic_array(handles_generic_array(gives_array())); $a = gives_array(); $d = dict[]; foreach ($a as $k => $v) { $d[$k] = $v; } takes_array($d); if ($a) {} takes_reified_array<AnyArray<string, mixed>>(); if (HH\is_vec_or_varray($a)) { foreach($a as $k => $_) { expect_int($k); } } } interface Parent_ { public function foo(AnyArray<int, int> $f): void; public function bar(): AnyArray<arraykey, mixed>; } class Child implements Parent_ { public function foo(AnyArray<arraykey, int> $f): void {} public function bar(): AnyArray<arraykey, mixed> { return vec[]; } } class Child2 extends Child { public function bar(): AnyArray<int, string> { return dict[]; } } class Child3 extends Child2 { public function bar(): dict<int, string> { return dict[]; } } function takes_any_array(AnyArray<arraykey, mixed> $a): void {} function m(mixed $m): void { if (HH\is_any_array($m)) { hh_show($m); takes_any_array($m); } else if (HH\is_vec_or_varray($m)) { takes_any_array($m); } else if (HH\is_vec_or_varray($m)) { takes_any_array($m); } else if ($m is dict<_,_>) { takes_any_array($m); } else if ($m is vec<_>) { takes_any_array($m); } else if ($m is keyset<_>) { takes_any_array($m); } else if ($m is AnyArray<_,_>) { hh_show($m); takes_any_array($m); } } <<__Memoize>> function memoized(AnyArray<int, AnyArray<string, string>> $a): void {} function purefn(AnyArray<int, string> $a): void { $a[42]; foreach ($a as $k => $v) {} }
PHP
hhvm/hphp/hack/test/typecheck/anyarray2.php
<?hh //strict function fun(dict<string, vec<KeyedContainer<string, string>>> $in): void { $stack = Vector {dict['k' => 'v']}; while (!$stack->isEmpty()) { $row = $stack->pop(); $r = idx($in, $row['k'], vec[]); $stack->addAll($r); } }
PHP
hhvm/hphp/hack/test/typecheck/anyarray_bad.php
<?hh //strict function takes_array(AnyArray<int, int> $_): void {} function gives_array(): AnyArray<int, int> { return vec[]; } function handles_generic_array<Tk as arraykey, Tv>( AnyArray<Tk, Tv> $a, ): AnyArray<Tk, Tv> { return $a; } function does_things(): void { takes_array(Map {}); // error takes_array(Set {}); // error takes_array(ImmVector {}); // error handles_generic_array<int, string>(gives_array()); // error $a = gives_array(); $a[42]; $a[42] = 43; // error } interface Parent_ { public function foo(AnyArray<arraykey, int> $f): void; } class Child implements Parent_ { public function foo(AnyArray<int, int> $f): void {} // error } function fromArrays(mixed ...$argv): Set<arraykey> { $ret = Set {}; foreach ($argv as $arr) { if (!HH\is_any_array($arr)) { return Set {}; } hh_show($arr); $ret->addAll($arr); } return $ret; // error }
PHP
hhvm/hphp/hack/test/typecheck/append_blow.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. function test2(int $id): void { $res = Map {}; $res[$id] = vec[1]; $res[$id][0] += 0; $res[$id][1] += 0; $res[$id][2] += 0; $res[$id][3] += 0; $res[$id][4] += 0; $res[$id][5] += 0; hh_force_solve(); hh_show($res); }
PHP
hhvm/hphp/hack/test/typecheck/arg_null.php
<?hh /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function foo(int $x = null): void { }
PHP
hhvm/hphp/hack/test/typecheck/arithmetic_any.php
////file1.php <?hh function returnsAny() { return 5; } ////file2.php <?hh// strict // ADDITION function doAddIntAny(int $x): void { $y = $x + returnsAny(); hh_show($y); } function doAddAnyInt(int $x): void { $y = returnsAny() + $x; hh_show($y); } function doAddFloatAny(float $x): void { $y = $x + returnsAny(); hh_show($y); } function doAddAnyFloat(float $x): void { $y = returnsAny() + $x; hh_show($y); } function doAddAnyAny(): void { $y = returnsAny() + returnsAny(); hh_show($y); } function doAddNumAny(num $x): void { $y = $x + returnsAny(); hh_show($y); } function doAddAnyNum(num $x): void { $y = returnsAny() + $x; hh_show($y); } // DIVISION function doDivAnyFloat(float $x): void { $y = returnsAny() / 2.0; hh_show($y); } function doDivFloatAny(float $x): void { $y = 2.0 / returnsAny(); hh_show($y); } function doDivAnyAny(): void { $y = returnsAny() / returnsAny(); hh_show($y); } function doDivAnyInt(int $x): void { $y = returnsAny() / $x; hh_show($y); } function doDivIntAny(int $x): void { $y = $x / returnsAny(); hh_show($y); } function sorted<T>( Traversable<T> $collection, ?(function(T, T): int) $comparator = null, ): Vector<T> { return Vector {}; } function TestSort():void { $v = sorted(varray[], function ($x, $y) { hh_show($x); hh_show($y); return $x - $y; }); } function TestUnion(?int $x, bool $b):void { if ($b) { $x = 3; } /* HH_FIXME[4110] */ $y = $x - returnsAny(); hh_show($y); } function doMulIntFloat(int $x, float $y): void { $z = $x * $y; hh_show($z); }
PHP
hhvm/hphp/hack/test/typecheck/arithmetic_default_types.php
<?hh // (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. function testInc(bool $b): void { $v = Vector<_>{}; // Relies on ++ defaulting to int if ($b) { $v[0]++; } else { $v[0] = 1; } } function testNeg(bool $b): void { $v = Vector<_>{}; // Relies on - defaulting to int if ($b) { $v[0] = -$v[0]; } else { $v[0] = 1; } } function testPlus(bool $b): void { $v = Vector<_>{}; // Relies on += defaulting to int if ($b) { $v[0] += 2; } else { $v[0] = 1; } }
PHP
hhvm/hphp/hack/test/typecheck/arithmetic_enum.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. enum E : int as int { A = 3; } function testit<T as num>(T $x):void { $x = 3 + E::A; $y = E::A + E::A; $z = $x + $x; hh_show($x); hh_show($y); hh_show($z); }
PHP
hhvm/hphp/hack/test/typecheck/arithmetic_lower.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. function expectFloat(float $x): void {} function expectInt(int $x): void {} function expectNum(num $x): void {} function testfloat(num $n):void { $m = Vector { 3.4 }; $x = $m[0] + $n; expectFloat($x); $m = Vector { 3.4 }; $x = $m[0] - $n; expectFloat($x); $m = Vector { 3.4 }; $x = $m[0] * $n; expectFloat($x); $m = Vector { 3.4 }; $x = $m[0] / $n; expectFloat($x); $m = Vector { 3.4 }; $x = $m[0] + $m[0]; expectFloat($x); $m = Vector { 3.4 }; $x = $m[0] - $m[0]; expectFloat($x); $m = Vector { 3.4 }; $x = $m[0] * $m[0]; expectFloat($x); $m = Vector { 3.4 }; $x = $m[0] / $m[0]; expectFloat($x); $m = Vector { 3.4 }; $x = -$m[0]; expectFloat($x); } /* function testfloat2(bool $b, num $n):void { $m = $b ? Vector { } : varray[3.4]; $x = $m[0] + $n; expectFloat($x); $m = $b ? Vector { } : Vector { 3.4 }; $x = $m[0] - $n; expectFloat($x); $m = $b ? Vector { } : Vector { 3.4 }; $x = $m[0] * $n; expectFloat($x); $m = $b ? Vector { } : Vector { 3.4 }; $x = $m[0] / $n; expectFloat($x); $m = $b ? Vector { } : Vector { 3.4 }; $x = $m[0] + $m[0]; expectFloat($x); $m = $b ? Vector { } : Vector { 3.4 }; $x = $m[0] - $m[0]; expectFloat($x); $m = $b ? Vector { } : Vector { 3.4 }; $x = $m[0] * $m[0]; expectFloat($x); $m = $b ? Vector { } : Vector { 3.4 }; $x = $m[0] / $m[0]; expectFloat($x); $m = $b ? Vector { } : Vector { 3.4 }; $x = -$m[0]; expectFloat($x); } */ function testints():void { $m = Vector { 3 }; $x = $m[0] + $m[0]; expectInt($x); $m = Vector { 3 }; $x = $m[0] - $m[0]; expectInt($x); $m = Vector { 3 }; $x = $m[0] * $m[0]; expectInt($x); $m = Vector { 3 }; $x = -$m[0]; expectInt($x); } function testint(): void { $m = Vector {}; expectNum($m[0] + 1); $m[] = 0.1; // ok, but can't infer $m[] = 1; // ok $m = Vector {}; expectFloat($m[0] + 1); $m[] = 0.1; // ok, but can't infer $m[] = 1; // nok $m = Vector {}; expectInt($m[0] + 1); $m[] = 1; // ok, but can't infer $m[] = 0.1; // nok }
PHP
hhvm/hphp/hack/test/typecheck/array.php
<?hh //strict function f(varray<int> $x): void { // this should be an error, since it can never be true if ($x === null) { } }
PHP
hhvm/hphp/hack/test/typecheck/arraykey_type_hint.php
<?hh // strict /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function f(arraykey $k1, arraykey $k2): arraykey { return $k1; } function generic<T as arraykey>(T $k1, T $k2): T { return $k2; } function cast(arraykey $k1, arraykey $k2): (string, int) { return tuple((string)$k1, (int)$k2); } function f_opt(?arraykey $k1, arraykey $k2): arraykey { if (null === $k1) { return 0; } return f($k1, $k2); } abstract class C {} function get_classname(): classname<C> { return C::class; } function test(): void { f(1, 1); f('a', 'a'); f(1, 'a'); f('a', 1); f(get_classname(), C::class); generic(1, 1); generic('a', 'a'); generic(get_classname(), get_classname()); f_opt(1, 1); f_opt('a', 'a'); f_opt('a', 1); f_opt(1, 'a'); f_opt(null, 1); f_opt(null, 'a'); } function test_switch(arraykey $x): bool { $c = 1; if ($c == $x) { return true; } if (0 === $x) { return true; } if ('' === $x) { return true; } switch ($x) { case $c: return true; case 'a': $res = false; break; case 3: $res = true; break; default: $res = true; } return $res; }
PHP
hhvm/hphp/hack/test/typecheck/arraykey_type_hint_bad.php
<?hh // strict /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ class C {} function f(arraykey $k1, arraykey $k2): arraykey { return $k2; } function test(): void { $c = new C(); f($c, 2); }
PHP
hhvm/hphp/hack/test/typecheck/array_append_dict_element_property_bad.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. class C { public vec<int> $vs = vec[]; } function test(dict<string, C> $d): void { $d['foo']->vs[] = 'bar'; }
PHP
hhvm/hphp/hack/test/typecheck/array_append_dict_element_property_good.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. class C { public vec<string> $vs = vec[]; } function test(dict<string, C> $d): void { $d['foo']->vs[] = 'bar'; }
PHP
hhvm/hphp/hack/test/typecheck/array_append_map_element.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. function test(Map<string, vec<int>> $m): void { $m['foo'][] = 'bar'; }
PHP
hhvm/hphp/hack/test/typecheck/array_append_pair_component_bad.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. function test(Pair<int, vec<string>> $p): void { $p[1][] = 'foo'; }
PHP
hhvm/hphp/hack/test/typecheck/array_append_pair_component_good.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. function test(Pair<int, Vector<string>> $p): void { $p[1][] = 'foo'; }
PHP
hhvm/hphp/hack/test/typecheck/array_append_set_bad.php
<?hh function f(Set<int> $s) : void { $s[] = "1"; } // Allowing the above is a typehole function expect_int(int $i) : void { } <<__EntryPoint>>function main() : void { $s = Set<int>{}; f($s); foreach ($s as $i) { expect_int($i); } } function f2() : void { $s = Set{}; $s[] = 1.1; }
PHP
hhvm/hphp/hack/test/typecheck/array_append_shape_field.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. function test(shape('x' => vec<int>) $s): shape('x' => vec<num>) { $s['x'][] = 3.14; return $s; }
PHP
hhvm/hphp/hack/test/typecheck/array_append_unresolved_bad.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. function test(bool $b, Vector<num> $x, Vector<arraykey> $y, string $z): void { if ($b) { $v = $x; } else { $v = $y; } // $v : Vector<num> | Vector<arraykey> and only values that are subtypes of // both num and arraykey can be appended to $v. $v[] = $z; }
PHP
hhvm/hphp/hack/test/typecheck/array_append_unresolved_good.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. function test1(bool $b, vec<int> $x, vec<string> $y, float $z): void { if ($b) { $v = $x; } else { $v = $y; } $v[] = $z; hh_show($v); } function test2(bool $b, Vector<num> $x, Vector<arraykey> $y, int $z): void { if ($b) { $v = $x; } else { $v = $y; } // $v : Vector<num> | Vector<arraykey> and only values that are subtypes of // both num and arraykey can be appended to $v. $v[] = $z; }
PHP
hhvm/hphp/hack/test/typecheck/array_append_vec.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. function test(vec<int> $vs): vec<arraykey> { $vs[] = 'foo'; return $vs; }
PHP
hhvm/hphp/hack/test/typecheck/array_append_vector.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. function test(Vector<int> $vs): void { $vs[] = 'foo'; }
PHP
hhvm/hphp/hack/test/typecheck/array_append_vec_property_bad.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. class C { public vec<int> $vs = vec[]; } function test(C $c): void { $c->vs[] = 'foo'; }
PHP
hhvm/hphp/hack/test/typecheck/array_append_vec_property_good.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. class C { public vec<string> $vs = vec[]; } function test(C $c): void { $c->vs[] = 'foo'; }
PHP
hhvm/hphp/hack/test/typecheck/array_assign_undefined_var.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. function foo(): void { $undefined_var['x'] = 0; }
PHP
hhvm/hphp/hack/test/typecheck/array_direct_index.php
<?hh /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ class Toto { public static function f1(): float { $x = darray[5 => 5.0]; return $x[5]; } public static function f2(): float { return (darray[5 => 5.0])[5]; } public static function f3(): float { $x = Map { 5 => 5.0 }; return $x[5]; } public static function f4(): float { return (Map { 5 => 5.0 })[5]; } }
PHP
hhvm/hphp/hack/test/typecheck/array_foreach.php
<?hh /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function foo3(varray<string> $arr): void { foreach ($arr as $v) { f2($v); } $arr[] = 'meh'; $arr[10] = 'meh'; f2($arr[10]); foo3($arr); foo5($arr); } function foo4(varray<string> $arr): void { foreach ($arr as $k => $v) { f1($k); f2($v); } } function foo5(darray<int, string> $arr): void { foreach ($arr as $v) { f2($v); } $arr[10] = 'meh'; f2($arr[10]); foo5($arr); } function foo6(darray<int, string> $arr): void { foreach ($arr as $k => $v) { f1($k); f2($v); } } function f1(int $k): void {} function f2(string $v): void {}
PHP
hhvm/hphp/hack/test/typecheck/array_get_generic_loop.php
<?hh // strict function f<T1 as varray<int> as T2, T2 as T1> (T1 $x): void { $x[5]=5; } function g<T1 as varray<int> as string>(T1 $x) : void { $x[4] = 3; }
PHP
hhvm/hphp/hack/test/typecheck/array_get_generic_lvalue_array_append.php
<?hh //strict /** * Appending to a vector-like array makes it unsafe to return it as original * generic type. */ function f<Tv as varray<mixed>>(Tv $a): Tv { $a[] = 4; return $a; } function test(varray<string> $a): varray<string> { return f($a); }
PHP
hhvm/hphp/hack/test/typecheck/array_get_generic_lvalue_array_get.php
<?hh //strict /** * Assignment to array makes it unsafe to return it as original generic type */ function f<Tv as darray<mixed, mixed>>(Tv $a): Tv { $a['a'] = 4; return $a; } function test(darray<string, string> $a): darray<string, string> { return f($a); }
PHP
hhvm/hphp/hack/test/typecheck/array_get_generic_non_lvalue.php
<?hh //strict /** * Generic type remains generic if it was not modified. */ function f<Tv as KeyedContainer<string, mixed>>(Tv $a): Tv { $a['a']; return $a; } function test(darray<string, string> $a): darray<string, string> { return f($a); }
PHP
hhvm/hphp/hack/test/typecheck/array_get_newtype.php
//// file1.php <?hh newtype MyArray as darray<mixed, mixed> = darray<string, string>; //// file2.php <?hh function f(MyArray $a): MyArray { $a['x'] = 4; return $a; }
PHP
hhvm/hphp/hack/test/typecheck/array_get_tconst.php
<?hh abstract class C { abstract const type MyArray as darray<mixed, mixed>; protected function f(this::MyArray $a): this::MyArray { $a['a'] = 4; return $a; } } final class D extends C { const type MyArray = darray<string, string>; public function test(this::MyArray $a): this::MyArray { return $this->f($a); } }
PHP
hhvm/hphp/hack/test/typecheck/array_mixed_strict.php
<?hh // strict interface I {} class C implements I {} class D implements I {} class Q { public varray<I> $arr; public function __construct() { $this->arr = varray[new C(), new D()]; } }
PHP
hhvm/hphp/hack/test/typecheck/array_non_static.php
<?hh // strict function get(): int { return 1; } class Foo { public varray<int> $vec = varray[get(), 2]; }
PHP
hhvm/hphp/hack/test/typecheck/array_ref_var.php
<?hh /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function foo(int $var): void { $a = varray[&$var]; }
PHP
hhvm/hphp/hack/test/typecheck/array_set_might_throw.php
<?hh // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. class Foo { } <<__EntryPoint>> function test(): void { $x = new Foo(); try { $y = vec[]; $y[4] = null; // hack assumes this never throws $x = 1; // so hack thinks this always occurs } catch (Exception $e) { } $x += 5; // boom }
PHP
hhvm/hphp/hack/test/typecheck/array_sub.php
<?hh /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function get_keys<T1, T2>(darray<T1, T2> $x): varray<T1> { $result = varray[]; foreach ($x as $k => $v) { $result[] = $k; } return $result; } function test(varray<int> $a): void { get_keys($a); }
PHP
hhvm/hphp/hack/test/typecheck/array_sub2.php
<?hh /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function get_keys<T2>(darray<bool, T2> $x): varray<bool> { $result = varray[]; foreach ($x as $k => $v) { $result[] = $k; } return $result; } function test(varray<int> $a): void { get_keys($a); }
PHP
hhvm/hphp/hack/test/typecheck/array_tuple.php
<?hh // strict /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function expect_arraykey(arraykey $ak):void { } function test(): void { $array = varray[varray['', 0], varray['', 0]]; foreach ($array as $update) { $index = 1; $update[$index] = 0; // force conversion from tuple-like to vec-like list($name, $value) = $update; expect_arraykey($name); expect_arraykey($value); } }
PHP
hhvm/hphp/hack/test/typecheck/array_tuple2.php
<?hh /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function expect_arraykey(arraykey $ak):void { } function test(): void { $array = varray[varray['', 0], varray['', 0]]; foreach ($array as $update) { $index = 1; $update[$index] = 0; // force conversion from tuple-like to vec-like list($name, $value) = $update; expect_arraykey($name); expect_arraykey($value); } }
PHP
hhvm/hphp/hack/test/typecheck/array_values1.php
<?hh // strict function test(KeyedContainer<string, int> $xs): varray<int> { return array_values($xs); }
PHP
hhvm/hphp/hack/test/typecheck/array_values2.php
<?hh function test1(varray<string> $xs): varray<string> { return array_values($xs); } function test2(darray<int, string> $xs): varray<string> { return array_values($xs); } function test3(darray<string, string> $xs): varray<string> { return array_values($xs); } function test4(ConstSet<int> $xs): varray<int> { return array_values($xs); } function test5(ConstMap<string, int> $xs): varray<int> { return array_values($xs); } function test6(ConstVector<int> $xs): varray<int> { return array_values($xs); }
PHP
hhvm/hphp/hack/test/typecheck/assignment1.php
<?hh /** * Copyright (c) 2014, Facebook, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the "hack" directory of this source tree. * * */ function main(): void { $arr = darray[]; $arr[$arr->getLength()] = 1; }
PHP
hhvm/hphp/hack/test/typecheck/assign_array_get_pair_component_good.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. function test(Pair<int, Map<string, int>> $p): void { $p[1]['foo'] = 42; }
PHP
hhvm/hphp/hack/test/typecheck/assign_array_property.php
<?hh // strict // Copyright 2004-present Facebook. All Rights Reserved. class Ref<T> { public function __construct(public T $value) {} } function test(dict<string, int> $d): void { $r = new Ref($d); $r->value['pi'] = 3.14; }