language
stringlengths 0
24
| filename
stringlengths 9
214
| code
stringlengths 99
9.93M
|
---|---|---|
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/higher_order_all_dyn.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
<<__SupportDynamicType>>
function iter
<Tv1 as supportdyn<mixed>>(
vec<Tv1> $traversable,
supportdyn<(function (Tv1): void)> $value_func,
): void {
}
<<__SupportDynamicType>>
function print_line(string $_):void { }
function testit(vec<~string> $vs1, ~vec<string> $vs2):void {
$f = print_line<>;
// Works, because we infer Tv=string
iter($vs2, $f);
// Works, because we're explicit
iter<string>($vs1, $f);
// Does not work, because we commit to Tv=~string
//iter($vs1, $f);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/hof.good.php | <?hh
interface I {}
function hof((function (mixed): bool) $f): void {}
function hof_like(~(function (mixed): bool) $f): void {}
function hof_null(?(function (mixed): bool) $f): void {}
function hof_like_null(~?(function (mixed): bool) $f): void {}
function test(): void {
hof(($i) ==> $i is I);
hof_like(($i) ==> $i is I);
hof_null(($i) ==> $i is I);
hof_like_null(($i) ==> $i is I);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/implicit_return.bad.php | <?hh
function f(): ~int {}
async function af(): Awaitable<~int> {}
function implicit_return() : dynamic {}
async function async_implicit_return(): Awaitable<dynamic> {} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/infer.good.php | <?hh
<<file:__EnableUnstableFeatures('upcast_expression')>>
function test(Traversable<int> $e) : void {
varray($e) upcast dynamic;
}
function test2(Traversable<int> $e) : void {
$v = varray($e);
$v upcast dynamic;
}
function foo<T>(vec<T> $v): T {
return $v[0];
}
function test3(vec<int> $v): dynamic {
return foo($v) upcast dynamic;
}
function test4(vec<int> $v): dynamic {
$e = foo($v);
return $e upcast dynamic;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/infinite_loop_awaitable_generic_upper_bound.bad.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
abstract class Tupper {}
<<__SupportDynamicType>>
abstract class G<T as supportdyn<mixed> as Tupper> {}
abstract class Inf<T as supportdyn<mixed> as Tupper> {
private ~T $t;
private async function f(): Awaitable<~G<T>> {}
private async function infinite(): Awaitable<void> {
$f = await $this->f();
$f as G<_>;
$this->t->noexist();
}
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/infinite_loop_typeconst_upper_bound.bad.php | <?hh
// (c) Meta Platforms, Inc. and affiliates.
<<__SupportDynamicType>>
interface I1<TEnt as supportdyn<mixed>> {}
<<__SupportDynamicType>>
final class C1<TEnt as supportdyn<mixed>> implements I1<TEnt> {}
<<__SupportDynamicType>>
final class C2<TEnt as supportdyn<mixed>> implements I1<TEnt> {}
trait T {
abstract const type TEnt as NoExist;
final protected function empty(): this::TEnt::TViewerContext {}
private function broken(~I1<this::TEnt> $i): void {
if ($i is C1<_>) {
} else if ($i is C2<_>) {
$this->empty();
}
}
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/inout.bad.php | <?hh
function foo(inout ~int $i):void { }
function bar(~Vector<int> $v):void {
foo(inout $v[0]);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/inout.good.php | <?hh
function foo(inout dynamic $d):void { }
function foo2(inout ~int $i):void { }
function bar(dynamic $d, ~vec<int> $v):void {
foo(inout $d[0]);
foo2(inout $v[0]);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/inout_2.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
<<__SupportDynamicType>>
function getVec():~vec<int> {
return vec[3];
}
<<__SupportDynamicType>>
function test(inout int $x):void {
$x = getVec()[0];
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/inout_assign_like.good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
function get():~int { return 3; }
function put(inout ~int $s):void {
$s = get();
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/interfaces_1.bad.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
// A class that implements an interface that extends dynamic,
// must itself implements dynamic
<<__SupportDynamicType>>
interface I {}
class D implements I {} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/interfaces_1.good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
// a class that implements dynamic can implement any interface
// (including interfaces that do not extend dynamic)
interface I1 {}
<<__SupportDynamicType>>
interface I2 {}
<<__SupportDynamicType>>
class D implements I1, I2 {} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/interfaces_2.bad.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
// If an interface extends another interface, then it can support dynamic
// if and only if the extended interface supports dynamic
<<__SupportDynamicType>>
interface I1 {}
interface I2 extends I1 {} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/interfaces_2.good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
// If an interface extends another interface, then it can support dynamic
// if and only if the extended interface support dynamic
<<__SupportDynamicType>>
interface I1 {}
<<__SupportDynamicType>>
interface I2 extends I1 {} // this is OK
interface I3 {}
<<__SupportDynamicType>>
interface I4 extends I3 {} // this is also OK |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/interfaces_3.good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
class Evil<T> {}
<<__SupportDynamicType>>
interface I {
public function foo(Evil<int> $x) : Evil<string>;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/interfaces_4.good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
interface I {
public function foo(vec<int> $x):int;
}
<<__SupportDynamicType>>
interface J extends I {
public function bar():string;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/interfaces_5.bad.php | <?hh
<<__SupportDynamicType>>
interface I {}
<<__SupportDynamicType>>
trait T implements I {}
class C {
use T;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/intersection_dynamic.bad.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
enum E : string as string { A = "A"; }
function getLikeE(): ~E {
return E::A;
}
function foo(): (~E & string) {
// Should fail
return getLikeE();
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/intersection_dynamic.good.php | <?hh
enum E : string as string { A = "A"; }
function getLikeE(): ~E {
return E::A;
}
<<__SupportDynamicType>>
function foo(): (~E & string) {
// Should succeed, because inside the function
// we treat the return type as fully-enforced
// and therefore allow dynamic to flow to it.
return getLikeE();
}
<<__SupportDynamicType>>
interface I {
public function get():string;
}
<<__SupportDynamicType>>
class C implements I {
public function __construct(private ~E $item) {}
// This is allowed too
public function get():(~E & string) { return $this->item; }
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/invoke_intersect.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
<<__SupportDynamicType>>
class C implements J {
public function foo():void { }
}
<<__SupportDynamicType>>
interface J { }
function make1():~C {
throw new Exception("A");
}
function make2():(~C & J) {
throw new Exception("A");
}
<<__SupportDynamicType>>
function testit(vec<int> $_):void {
$x1 = make1();
$x2 = make2();
// Succeeds
$x1->foo();
// Fails
$x2->foo();
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/is_any_array_mutate.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
<<__SupportDynamicType>>
function testmutate(vec<int> $v, dynamic $m):void {
if (!HH\is_any_array($m)) {
$m = dict[];
}
$m[0] = 5;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/keyset_append.good.php | <?hh
function test(dynamic $d, keyset<int> $ks) : void {
$ks[] = $d;
hh_expect_equivalent<keyset<(~int & arraykey)>>($ks);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/keyset_create.good.php | <?hh
function test_create_dyn_idx(dynamic $d) : keyset<arraykey> {
keyset<arraykey>[$d, 1];
$x = keyset[$d, 1];
hh_expect_equivalent<keyset<(~int & arraykey)>>($x);
return keyset[$d, 1];
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/keyset_explicit.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
function getStrings():~vec<string> {
return vec[];
}
function getArraykeys():~vec<arraykey> {
return vec[];
}
function test1(string $s, dynamic $dyn, arraykey $ak):void {
$ls = getStrings()[0];
$lak = getArraykeys()[0];
$a = keyset<string>[$s];
hh_expect_equivalent<keyset<string>>($a);
// Like types should be accepted as elements
$c = keyset<string>[$ls,$s];
hh_expect_equivalent<keyset<(~string & arraykey)>>($c);
$d = keyset<string>[$dyn];
hh_expect_equivalent<keyset<(~string & arraykey)>>($c);
// But a supertype should be respected as a type argument
$d = keyset<arraykey>[$ls,$s];
hh_expect_equivalent<keyset<arraykey>>($d);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/keyset_index.good.php | <?hh
function test_dyn_index(
dynamic $d,
keyset<int> $ks1,
keyset<arraykey> $ks2,
) : void {
$a = $ks1[$d];
hh_expect_equivalent<int>($a);
$a = $ks2[$d];
hh_expect_equivalent<arraykey>($a);
}
class C<Tk as arraykey> {
public function __construct(
private keyset<Tk> $ks1,
private keyset<arraykey> $ks2,
private keyset<int> $ks3,
) {}
public function f(dynamic $d): void {
$a = $this->ks1[$d];
hh_expect_equivalent<Tk>($a);
$a = $this->ks2[$d];
hh_expect_equivalent<arraykey>($a);
$a = $this->ks3[$d];
hh_expect_equivalent<int>($a);
}
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/lambdaapp.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
<<__SupportDynamicType>>
function test(
vec<string> $value,
supportdyn<(function(vec<string>):~int)> $f,
): void {
$g = (vec<string> $x) ==> 5;
$g($value);
$f($value);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/lambda_as_generic.good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
function expectLike<T as supportdyn<mixed>>(~T $_):void { }
<<__SupportDynamicType>>
function bar(int $x):int {
return $x+1;
}
<<__SupportDynamicType>>
function testit():void {
expectLike($x ==> bar($x));
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/lambda_infer.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
<<__SupportDynamicType>>
function genericfun<T as supportdyn<mixed> >(
?supportdyn<(function(vec<T>): void)> $f,
): ~Vector<T> {
throw new Exception("A");
}
<<__SupportDynamicType>>
function testit(): void {
$f = genericfun(($ts): void ==> { $x = $ts[3]; });
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/lambda_inference.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
type AwaitablePredicate<T> = supportdyn<(function(T): Awaitable<~bool>)>;
<<__SupportDynamicType>>
function ho(IAsyncPredicate<C,int> $predicate): void { }
<<__SupportDynamicType>>
interface IAsyncPredicate<-T as supportdyn<mixed>, +Tc> { }
<<__SupportDynamicType>>
function asyncLambda<Tv as supportdyn<mixed>, Tc as supportdyn<mixed>>(
AwaitablePredicate<Tv> $lambda,
): ~IAsyncPredicate<Tv,Tc> {
throw new Exception("A");
}
<<__SupportDynamicType>>
function expectString(string $_):void { }
<<__SupportDynamicType>>
function test(keyset<int> $_): void {
ho(asyncLambda(async ($sig) ==> $sig->p));
ho(asyncLambda(async ($s) ==> { expectString($s->foo()); return false; }));
}
<<__SupportDynamicType>>
class C {
public bool $p = false;
public function foo():string { return "A"; }
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/lambda_variadic.good.php | <?hh
<<__SupportDynamicType>>
interface I { }
<<__SupportDynamicType>>
class C {
public function __construct(
private ~supportdyn<(function(
I...
): ~Awaitable<void>)> $fn):void {}
}
<<__SupportDynamicType>>
function foo(vec<int> $_):void {
$f = async (I... $a) ==> { };
// This works
new C($f);
// This doesn't
new C(async (I... $a) ==> { }) ;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/like_call.good.php | <?hh
<<__SupportDynamicType>>
class C { public function p() : bool { return true; } }
<<__SupportDynamicType>>
class D<-T> {
public function __construct() {}
}
<<__SupportDynamicType>>
function my_plambda<Tv as supportdyn<mixed>>(
~(function(Tv):bool) $lambda,
): ~D<Tv> {
return new D();
}
function m(~(function(~D<C>):void) $f): void {
$f(my_plambda($x ==> $x->p()));
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/like_expected.good.php | <?hh
<<__SupportDynamicType>>
function foo(int $_):void { }
function get():~int {
return 3;
}
function bar():void {
$x = get();
foo($x);
foo(get());
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/like_float_generic_div.good.php | <?hh
function MyDivAlt<Tx as float, Ty as float>(~Tx $x, ~Ty $y): ~float {
$z = $x / $y;
return $z;
}
function MyAddAlt<Tx as float, Ty as float>(~Tx $x, ~Ty $y): ~float {
$z = $x + $y;
return $z;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/like_higher_order.bad.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
<<__SupportDynamicType>>
class MyVec<<<__RequireDynamic>> T> {
public function exists((function(T):bool) $f):bool { return false; }
}
<<__SupportDynamicType>>
class C {
public function pred():bool { return false; }
}
// If $x : C is inferred, then we can't call through the dynamic side, since
// C -> bool is not a subtype of dynamic. If dynamic is inferred, then the
// call could in principle be made to work, but it is not clear that we want
// to infer different types for $x when calling through a like typed method
// exists.
function test(~MyVec<C> $v):void {
$v->exists($x ==> $x->pred());
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/like_lambda_variance.good.php | <?hh
function my_reduce<Tv as supportdyn<mixed>, Ta as supportdyn<mixed>>(
~Traversable<Tv> $traversable,
~(function(Ta, Tv): Ta) $accumulator,
~Ta $initial,
) : void {}
function f(~dict<string, ?bool> $d) : void {
my_reduce($d, ($x, $y) ==> $x ?? $y, null);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/like_meth_call_sd.good.php | <?hh
class C {}
class D extends C {}
<<__SupportDynamicType>>
class V<T> {
public function filter(~supportdyn<(function(T): bool)> $fn): ~V<T> { return $this; }
}
function f(~V<C> $v) : void {
$v->filter($x ==> $x is C || $x is D);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/like_push_infer.good.php | <?hh
<<__SupportDynamicType>>
function my_vec<Tv as supportdyn<mixed>>(Traversable<Tv> $arr)[]: ~vec<Tv> { return vec[]; }
<<__SupportDynamicType>>
class B {}
<<__SupportDynamicType>>
enum class C : ~B {
B a = new B();
}
<<__NoAutoDynamic>>
function test(~dict<string, HH\MemberOf<C, ~B>> $c) : ~vec<HH\MemberOf<C, B>> {
$v = my_vec($c);
return $v;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/like_push_nested.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
<<__SupportDynamicType>>
class MyMap<T1 as supportdyn<mixed>, T2 as supportdyn<mixed>> {}
<<__SupportDynamicType>>
class MyVector<T as supportdyn<mixed>> {}
<<__SupportDynamicType>>
class C { }
function expect1(~MyMap<string,MyVector<C>> $_):void { }
function expect2(~MyMap<~string, MyVector<C>> $_): void {}
function expect3(~MyMap<string,~MyVector<C>> $_): void {}
function expect4(~MyMap<string, MyVector<~C>> $_): void {}
function expect5(~MyMap<string, ~MyVector<~C>> $_): void {}
<<__SupportDynamicType>>
function test1(MyMap<~string,MyVector<C>> $m1, MyMap<string,~MyVector<C>> $m2, MyMap<string,MyVector<~C>> $m3, MyMap<string,~MyVector<~C>> $m4):void {
expect1($m1);
expect1($m2);
expect1($m3);
expect1($m4);
expect2($m1);
expect2($m2);
expect2($m3);
expect2($m4);
expect3($m1);
expect3($m2);
expect3($m3);
expect3($m4);
expect4($m1);
expect4($m2);
expect4($m3);
expect4($m4);
expect5($m1);
expect5($m2);
expect5($m3);
expect5($m4);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/like_type_enforced.good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
<<__SupportDynamicType>>
class C {
public function __construct(private int $x) { }
// Strong signature
public function makeAddCopy(~int $y): C {
// Hack shows an error here, but not if we precede it with
// $y as int;
// This should not be necessary
$c = new C($this->x + $y);
return $c;
}
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/loop_bug.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
<<__SupportDynamicType>>
interface ITop<T as supportdyn<mixed>> {}
<<__SupportDynamicType>>
interface INo<T as supportdyn<mixed>> extends ITop<T> {}
<<__SupportDynamicType>>
interface IYes<T as supportdyn<mixed>> extends ITop<T> {
public function f(T $e): void;
}
final class Local {
public static function fail<T as supportdyn<mixed>>(
~ITop<T> $processor,
T $e,
): void {
if ($processor is INo<_>) {
// nothing
} else if ($processor is IYes<_>) {
$processor->f($e);
}
}
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/map_create.good.php | <?hh
function test_create_dyn_idx(dynamic $d) : Map<(~int & arraykey), int> {
Map<arraykey, int>{$d => 1, 1 => 1};
$x = Map{$d => 1, 1 => 1};
hh_expect_equivalent<Map<(~int & arraykey), int>>($x);
return Map{$d => 1, 1 => 1};
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/map_index.bad.php | <?hh
function test_dyn_idx(
dynamic $d,
Map<int, int> $m1,
Map<arraykey, int> $m2,
) : void {
$m1[$d] = 1;
$m1[$d] = "s";
$m2[$d] = "s";
$m1[$d] = $d;
$m2[$d] = $d;
}
class C<Tk as arraykey> {
public function __construct(
private Map<Tk, int> $m1,
private Map<arraykey, int> $m2,
private Map<int, int> $m3,
) {}
public function f(dynamic $d): void {
$this->m1[$d] = 1;
$this->m3[$d] = 1;
$this->m1[$d] = "s";
$this->m2[$d] = "s";
$this->m3[$d] = "s";
$this->m1[$d] = $d;
$this->m2[$d] = $d;
$this->m3[$d] = $d;
}
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/map_index.good.php | <?hh
function test_dyn_index(
dynamic $d,
Map<int, int> $m1,
Map<arraykey, int> $m2,
) : void {
$a = $m1[$d];
hh_expect_equivalent<~int>($a);
$a = $m2[$d];
hh_expect_equivalent<~int>($a);
$m2[$d] = 1;
}
class C<Tk as arraykey> {
public function __construct(
private Map<Tk, int> $m1,
private Map<arraykey, int> $m2,
private Map<int, int> $m3,
) {}
public function f(dynamic $d): void {
$a = $this->m1[$d];
hh_expect_equivalent<~int>($a);
$a = $this->m2[$d];
hh_expect_equivalent<~int>($a);
$a = $this->m3[$d];
hh_expect_equivalent<~int>($a);
$this->m2[$d] = 1;
}
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/marker_types.good.php | <?hh
function f(
HH\FIXME\TANY_MARKER<int> $tany,
HH\FIXME\POISON_MARKER<string> $poison,
vec<HH\FIXME\TANY_MARKER<float>> $vec_float,
vec<HH\FIXME\TANY_MARKER<HH\FIXME\POISON_MARKER<float>>> $vec_like_float,
): void {
hh_show($tany);
hh_show($poison);
hh_show($vec_float);
hh_show($vec_like_float);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/meth_call.bad.php | <?hh
class C {}
function f(dynamic $d) : void {
$c = new C();
$d->m(new C());
$d->m($c);
$d->m(vec[$c]);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/meth_call.good.php | <?hh
<<__SupportDynamicType>>
class C {}
function f (dynamic $d) : dynamic {
$c = new C();
return $d->m(1, "x", 1 + 2, new C(), $c, vec[$c]);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/meth_caller.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
<<__SupportDynamicType>>
class C {
public function foo(int $_):bool { return false; }
}
function expect1(supportdyn<(function(C,int):bool)> $_):void { }
function testit(~C $c):void {
$f = meth_caller(C::class, 'foo');
expect1($f);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/minus.good.php | <?hh
<<__SupportDynamicType>>
function minus_int(int $x) : int {
return -$x;
}
<<__SupportDynamicType>>
function minus_float(float $x) : float {
return -$x;
}
<<__SupportDynamicType>>
function minus_num(num $x) : num {
return -$x;
}
<<__SupportDynamicType>>
function minus_nothing(nothing $x) : nothing {
return -$x;
}
function test(dynamic $d, int $i, float $f, num $n, vec<~int> $vi, vec<~float> $vf, vec<~num> $vn) : void {
$va = Vector{};
hh_expect_equivalent<int>(-$i);
hh_expect_equivalent<float>(-$f);
hh_expect_equivalent<num>(-$n);
hh_expect_equivalent<~int>(-$vi[0]);
hh_expect_equivalent<~float>(-$vf[0]);
hh_expect_equivalent<~num>(-$vn[0]);
hh_expect_equivalent<dynamic>(-$d);
// Expect an unresolved type to default to int for unary minus
hh_expect<~int>(-$va[0]);
// All the expressions above should act like function calls to the correctly
// overloaded function. It's ambiguous what to do in the -dynamic case,
// since dynamic = ~nothing, and there isn't an overloded case for nothing.
// One could argue that the return should be nothing, and hence the result
// should just be dynamic.
hh_expect_equivalent<int>(minus_int($i));
hh_expect_equivalent<float>(minus_float($f));
hh_expect_equivalent<num>(minus_num($n));
hh_expect_equivalent<~int>(minus_int($vi[0]));
hh_expect_equivalent<~float>(minus_float($vf[0]));
hh_expect_equivalent<~num>(minus_num($vn[0]));
hh_expect_equivalent<dynamic>(minus_nothing($d));
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/multi_arg_dynamic.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
function get():~?int {
return 3;
}
<<__SupportDynamicType>>
function expectDynInt(dynamic $d, ?int $x):void { }
function expectDyn(dynamic $d):void { }
function testit():void {
expectDyn(get());
expectDynInt(get(), get());
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/nested_foreach_dynamic.good.php | <?hh
function f(dynamic $d) : void {
$v1 = vec[];
if ($d) { $v1 = $d; }
$v1[] = vec[];
$v1[0][] = $d;
foreach ($v1 as $v2) {
foreach ($v2 as $v3) {
}
}
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/newtype_cov_like_strip.good.php | <?hh
<<__SupportDynamicType>>
function getx<T as nonnull as supportdyn<mixed>>(T $value): ~classname<T> {
throw new TypeAssertionException("");
}
<<__SupportDynamicType>>
function map<Tv1 as supportdyn<mixed>, Tv2 as arraykey as supportdyn<mixed>>(
Traversable<Tv1> $traversable,
(function(Tv1): ~Tv2) $value_func,
) : keyset<Tv2> { return keyset[]; }
<<__SupportDynamicType>>
function my_vec<Tv as supportdyn<mixed>>(Traversable<Tv> $arr)[]: ~vec<Tv> { return vec[]; }
<<__SupportDynamicType>>
class B {}
<<__SupportDynamicType>>
enum class C : ~B {
B a = new B();
}
<<__NoAutoDynamic>>
function test() : ~keyset<classname<HH\MemberOf<C, B>>> {
$c = C::getValues();
$v = my_vec($c);
$x = map($v,($item) ==> {
$g = getx($item);
return $g;
});
return $x;
}
<<__NoAutoDynamic>>
function test2() : ~keyset<classname<HH\MemberOf<C, B>>> {
$v = vec(C::getValues());
return map($v,($item) ==> getx($item));
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/nonenforceable_property.bad.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
class C {
private ?vec<int> $v;
public function foo<T as supportdyn<mixed>>(~T $x) : ~T { return $x; }
public function bar(?vec<int> $v): void {
$this->v = $this->foo($v);
}
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/nonenforceable_static_property_01.bad.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
<<__SupportDynamicType>>
abstract class C {
abstract public function foo() : Awaitable<~vec<int>>;
private static ?vec<int> $x;
public async function bar(): Awaitable<~void> {
self::$x = await $this->foo();
}
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/noreturn.good.php | <?hh
<<__SupportDynamicType>>
function my_invariant_violation(string $fmt_args)[]: noreturn { throw new Exception(); }
class C3 {
public function g(): ~vec<string> { return vec[]; }
public function f(bool $b): int {
if ($b) {
return 4;
} else {
my_invariant_violation($this->g()[0]);
}
}
}
function bar(vec<int> $vi):noreturn {
foo($vi); // Now we have no continuation
}
function foo(vec<int> $vi):noreturn {
throw new Exception("A");
}
<<__SupportDynamicType>>
function getVecInt(): ~vec<int> { return vec[]; }
<<__SupportDynamicType>>
function fbar(vec<int> $vi):noreturn {
// Here we do have a potential continuation: dynamic return
ffoo(getVecInt());
}
<<__SupportDynamicType>>
function ffoo(vec<int> $vi):noreturn {
throw new Exception("A");
}
<<__SupportDynamicType>>
class C {
public static function mbar(vec<int> $vi):noreturn {
self::mfoo(getVecInt());
}
<<__SupportDynamicType>>
public static function mfoo(vec<int> $vi):noreturn {
throw new Exception("A");
}
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/nullable_dynamic.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
<<__SupportDynamicType>>
class C {
const type TC = shape('a' => int);
}
<<__SupportDynamicType>>
function getShape():~C::TC {
return shape('a' => 3);
}
<<__SupportDynamicType>>
function getVec():~vec<int> {
return vec[];
}
<<__SupportDynamicType>>
function shapeget(vec<int> $vi):void {
$s = null;
try {
$s = getShape();
} finally {
if ($s is nonnull) { }
}
// $s ends up with type ?dynamic in dynamic checking mode
$i = $s['a'];
}
<<__SupportDynamicType>>
function shapeput(vec<int> $vi):void {
$s = null;
try {
$s = getShape();
} finally {
if ($s is nonnull) { }
}
// $s ends up with type ?dynamic in dynamic checking mode
$s['a'] = 1;
}
<<__SupportDynamicType>>
function vecappend(vec<int> $vi):void {
$s = null;
try {
$s = getVec();
} finally {
if ($s is nonnull) { }
}
// $s ends up with type ?dynamic in dynamic checking mode
$s[] = 1;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/nullable_inference.good.php | <?hh
<<__SupportDynamicType>>
function my_map_nullable<Tx as supportdyn<mixed> , Ty as supportdyn<mixed> >(?Tx $x, supportdyn<(function(Tx): ~Ty)> $fn
): void {
}
<<__SupportDynamicType>>
interface Getter<T as supportdyn<mixed> > {
public function getPlaceholder(): ~?T;
}
<<__SupportDynamicType>>
function onPlaceholder<T as supportdyn<mixed> >(Getter<T> $field, supportdyn<(function(T): ~bool)> $callback): void {
my_map_nullable($field->getPlaceholder(), $callback);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/nullsafe_call.good.php | <?hh
<<__SupportDynamicType>>
class C {
public function f() : int { return 0; }
}
function test(?C $c) : void {
$c?->f();
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/nullsafe_like_access.good.php | <?hh
// Copyright 2004-present Facebook. All Rights Reserved.
<<__SupportDynamicType>>
class A {
public function __construct(public int $x) {}
}
<<__SupportDynamicType>>
class MyVector<T as supportdyn<mixed>> {
public function isEmpty() : bool { return false; }
public function get() : ~T { throw new Exception("A"); }
public function append(T $x) : void {}
public function pess_append(~T $x) : void {}
}
function test(?A $a): void {
$x = new MyVector();
$y1 = $x->get();
$y2 = $y1?->x;
expect<?int>($y2);
$x->pess_append($a);
}
function test2(?A $a): void {
$x = new MyVector();
expect($x->get()?->x);
$x->pess_append($a);
}
function test3(?A $a): void {
$x = new MyVector();
expect<?int>($x->get()?->x);
$x->append($a);
}
function test4(?A $a): void {
$x = new MyVector();
expect($x->get()?->x);
$x->append($a);
}
<<__SupportDynamicType>>
function expect<T as supportdyn<mixed>>(T $_): void {} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/override_sound_dynamic.bad.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
class C {
<<__SupportDynamicType>>
public function foo(int $_):void { }
}
class D extends C {
public function foo(int $_):void { }
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/override_sound_dynamic.good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
<<file:__EnableUnstableFeatures('upcast_expression')>>
class C {
public function foo(int $_):void { }
public function bar(): dynamic { return ("A" upcast dynamic); }
}
class D extends C {
public function foo(dynamic $_):void { }
public function bar(): vec<int> { return vec[]; }
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/override_sound_dynamic_2.good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
class C {
<<__SupportDynamicType>>
public function foo(int $_):void { }
}
<<__SupportDynamicType>>
class D extends C {
public function foo(int $_):void { }
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/override_sound_dynamic_4.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
<<file:__EnableUnstableFeatures('upcast_expression')>>
<<__SupportDynamicType>>
interface I {
public function bar():~Awaitable<dynamic>;
}
<<__SupportDynamicType>>
class C {
public async function foo():Awaitable<dynamic> {
return "A" upcast dynamic;
}
}
<<__SupportDynamicType>>
class D extends C implements I {
public async function foo():Awaitable<int> {
return 5;
}
public async function bar():Awaitable<int> {
return 5;
}
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/pair.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
<<file:__EnableUnstableFeatures('upcast_expression')>>
function getLikeInt():~vec<int> { $x = vec["A" upcast dynamic]; return $x; }
function make():void {
$p = Pair{getLikeInt()[0],"A"};
hh_expect_equivalent<Pair<~int,string>>($p);
$x = $p[0];
hh_expect_equivalent<~int>($x);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/pair_explicit.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
function getStrings():~vec<string> {
return vec[];
}
function getArraykeys():~vec<arraykey> {
return vec[];
}
function test1(string $s, dynamic $d, arraykey $ak):void {
$ls = getStrings()[0];
$lak = getArraykeys()[0];
// Expect Pair<string,string> and Pair<string,arraykey>
$a = Pair<string,string>{$s, $s};
hh_expect_equivalent<Pair<string,string>>($a);
$b = Pair<string,arraykey>{$s, $s};
hh_expect_equivalent<Pair<string,arraykey>>($b);
// Like types should be accepted as elements
$c = Pair<string,string>{$ls,$s};
hh_expect_equivalent<Pair<~string,string>>($c);
// But a supertype should be respected as a type argument
$d = Pair<string, arraykey>{$ls,$ls};
hh_expect_equivalent<Pair<~string,~arraykey>>($d);
// Obvs we should be allowed to ask for a like type
$e = Pair<~string,~arraykey>{$s,$s};
hh_expect_equivalent<Pair<~string,~arraykey>>($e);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/param_with_default_value.good.php | <?hh
<<__SupportDynamicType>>
class D {
public function foo(vec<int> $v, bool $x = false) : void {
}
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/parent_1.good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
class Foo {}
<<__SupportDynamicType>>
class Bar extends Foo {} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/parent_2.bad.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
<<__SupportDynamicType>>
class Foo {}
class Bar extends Foo {} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/parent_2.good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
class Foo {
public function foo(int $x) : int {
return $x;
}
}
<<__SupportDynamicType>>
class Bar extends Foo {} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/parent_3.bad.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
class Box<<<__RequireDynamic>> T> {}
class Foo {
public function foo(Box<int> $x) : Box<int> {
return $x;
}
}
<<__SupportDynamicType>>
class Bar extends Foo {} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/parent_3.good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
class Box<T> {}
class Foo {
<<__SupportDynamicType>>
public function foo(Box<int> $x) : Box<int> {
return $x;
}
}
<<__SupportDynamicType>>
class Bar extends Foo {} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/plus.good.php | <?hh
<<__SupportDynamicType>>
function plus_int_int(int $x, int $y) : int {
return $x + $y;
}
<<__SupportDynamicType>>
function plus_float_float(float $x, float $y) : float {
return $x + $y;
}
<<__SupportDynamicType>>
function plus_num_num(num $x, num $y) : num {
return $x + $y;
}
<<__SupportDynamicType>>
function plus_nothing_nothing(nothing $x, nothing $y) : nothing {
return $x + $y;
}
<<__SupportDynamicType>>
function plus_num_nothing(num $x, nothing $y) : nothing {
return $x + $y;
}
<<__SupportDynamicType>>
function plus_num_int(num $x, int $y) : num {
return $x + $y;
}
<<__SupportDynamicType>>
function plus_float_int(float $x, int $y) : float {
return $x + $y;
}
<<__SupportDynamicType>>
function plus_float_num(float $x, num $y) : float {
return $x + $y;
}
function test(dynamic $d, int $i, float $f, num $n, vec<~int> $vi, vec<~float> $vf, vec<~num> $vn) : void {
hh_expect_equivalent<int>($i + $i);
hh_expect_equivalent<float>($f + $f);
hh_expect_equivalent<num>($n + $n);
hh_expect_equivalent<num>($n + $i);
hh_expect_equivalent<float>($f + $i);
hh_expect_equivalent<float>($f + $n);
hh_expect_equivalent<~int>($vi[0] + $i);
hh_expect_equivalent<~float>($vf[0] + $f);
hh_expect_equivalent<~num>($vn[0] + $n);
hh_expect_equivalent<~num>($vn[0] + $i);
hh_expect_equivalent<~float>($vf[0] + $i);
hh_expect_equivalent<~float>($vf[0] + $n);
hh_expect_equivalent<~num>($vn[0] + $d);
hh_expect_equivalent<~int>($vi[0] + $d);
hh_expect_equivalent<~float>($vf[0] + $d);
hh_expect_equivalent<~num>($d + $vn[0]);
hh_expect_equivalent<~int>($d + $vi[0]);
hh_expect_equivalent<~float>($d + $vf[0]);
hh_expect_equivalent<dynamic>($d + $d);
// All the expressions above should act like function calls to the correctly
// overloaded function. It's ambiguous what to do in the dynamic case,
// since dynamic = ~nothing, and there isn't an overloded case for nothing.
// One could argue that the return should be nothing, and hence the result
// should just be dynamic.
hh_expect_equivalent<int>(plus_int_int($i, $i));
hh_expect_equivalent<float>(plus_float_float($f, $f));
hh_expect_equivalent<num>(plus_num_num($n, $n));
hh_expect_equivalent<num>(plus_num_int($n, $i));
hh_expect_equivalent<float>(plus_float_int($f, $i));
hh_expect_equivalent<float>(plus_float_num($f, $n));
hh_expect_equivalent<~int>(plus_int_int($vi[0], $i));
hh_expect_equivalent<~float>(plus_float_float($vf[0], $f));
hh_expect_equivalent<~num>(plus_num_num($vn[0], $n));
hh_expect_equivalent<~num>(plus_num_int($vn[0], $i));
hh_expect_equivalent<~float>(plus_float_int($vf[0], $i));
hh_expect_equivalent<~float>(plus_float_num($vf[0], $n));
hh_expect_equivalent<dynamic>(plus_num_nothing($vn[0], $d));
hh_expect_equivalent<dynamic>(plus_nothing_nothing($d, $d));
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/printf1.good.php | <?hh
interface MyPlainSprintf {
public function format_s(mixed $s): ~string;
}
function my_sprintf(HH\FormatString<MyPlainSprintf> $f, mixed ...$_): string {
return 'hi';
}
function f(): void {
my_sprintf('%s', 'hi');
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/printf2.good.php | <?hh
interface Dummy {
public function format_s(~string $s) : ~string;
}
function takesFS(~?\HH\FormatString<Dummy> $f = null) : void {}
function _() : void {
takesFS();
takesFS(null);
takesFS('%s', 's');
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/private_dynamic.bad.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
<<file:__EnableUnstableFeatures('upcast_expression')>>
class D { }
<<__SupportDynamicType>>
class C {
// Legal, as we don't check signatures for private members
private vec<int> $items;
private function setItems(vec<int> $vi):void {
// Unfortunately illegal, as setItems might be called through dynamic
$this->items = $vi;
}
public function __construct() { $this->items = vec[]; }
public function getItems(): vec<int> {
return $this->items;
}
public function isMember(int $x):bool {
return false;
}
private function bad1(dynamic $d):void {
// Illegal, as there exists a private member items of non-enforceable type
$d->items = vec["A"];
}
}
<<__SupportDynamicType>>
class Wrap {
// Legal, as we don't check signatures for private members
private D $item;
public function __construct() { $this->item = new D(); }
private function bad2(dynamic $d):dynamic {
// Illegal, as there exists a private member items of type not subtype of dynamic
return $d->item;
}
}
class NonDyn {
private D $item1;
private vec<int> $item2;
public function __construct() { $this->item1 = new D(); $this->item2 = vec[]; }
public function bad3(dynamic $d):void {
$d->item2 = vec["A"];
}
public function bad4(dynamic $d):dynamic {
return $d->item1;
}
public function get():int {
return $this->item2[0];
}
}
<<__SupportDynamicType>>
class Dyn extends NonDyn { }
// Demonstrates why we need the checks above
<<__EntryPoint>>
function main():void {
$d = new Dyn() upcast dynamic;
$d->bad3($d);
$x = $d->get();
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/private_good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
<<__SupportDynamicType>>
class C {
// Legal, as we don't check signatures for private members
private vec<int> $items;
private function setItems(vec<int> $vi):void { $this->items = $vi; }
public function __construct() { $this->items = vec[]; }
public function getItems(): vec<int> {
return $this->items;
}
public function isMember(int $x):bool {
return false;
}
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/prop.bad.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
class C { }
class G<T> { }
<<__SupportDynamicType>>
class D { }
<<__SupportDynamicType>>
class E {
// This should be rejected, as it is not a subtype of dynamic
public ?C $x;
// This should be rejected, as it is not enforceable
public ?vec<int> $z;
// This should not be rejected: although it's not enforceable or a subtype
// of dynamic, it's private. We check possible dynamic access to such
// properties within code, not here at the definition.
private ?G<C> $q;
// This is safe
public ?D $y;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/prop.good.php | <?hh
<<__SupportDynamicType>> class E<T as supportdyn<mixed>> {}
<<__SupportDynamicType>> class C {
public ~E<int> $x;
public function __construct() { $this->x = new E<int>(); }
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/push_bounded.good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
<<__SupportDynamicType>>
class C<+T> { }
function foo<T as C<~int>>(T $x):~C<int> {
return $x;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/push_dynamic_arg.bad.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
<<__SupportDynamicType>>
class Inv<T> { }
function test1(Inv<dynamic> $c):~Inv<int> {
return $c;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/push_dynamic_arg.good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
<<__SupportDynamicType>>
class Cov<+T> { }
function test1(Cov<dynamic> $c):~Cov<int> {
return $c;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/push_fun_type.good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
function expect1(~supportdyn<(function(~int):string)> $f):void { }
function test1(supportdyn<(function(~int):~string)> $f):void {
expect1($f);
}
function expect2(~(function():void) $f):void { }
function test2((function():~void) $f):void {
expect2($f);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/push_generic_super.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
function foo<T super vec<int>>(vec<~int> $v):~T {
return $v;
}
function bar<T super (int,string)>((~int,string) $p):~T {
return $p;
}
function boo<T super vec<int> as supportdyn<mixed>>(vec<~int> $v):~T {
return $v;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/push_like.bad.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
// No SDT
class NoSDTC {}
<<__SupportDynamicType>>
class B { }
<<__SupportDynamicType>>
class C extends B {
public function bar():void { }
}
<<__SupportDynamicType>>
class Box<T as B> {
public function __construct(public ~T $item) { }
}
function get():~int {
return 3;
}
function return_pair_direct():~(int,NoSDTC) {
return tuple(get(), new NoSDTC());
}
function return_pair():~(int,NoSDTC) {
$x = tuple(get(), new NoSDTC());
return $x;
}
function test_constraint_bad(Box<B> $b):~Box<C> {
// This is dynamic-aware subtype but not
// an ordinary subtype because we could lose errors.
// e.g. consider $b->item->bar() which produces an error on Box<B>
// but not on ~Box<C>
return $b;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/push_like_await.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
function expect<T as supportdyn<mixed>>(supportdyn<(function():~Awaitable<vec<T>>)> $f):void { }
function expect2<T as supportdyn<mixed> >((function():~Awaitable<vec<T>>) $_):void { }
function any(
supportdyn<(function(): Awaitable<~vec<int>>)> $fn1,
supportdyn<(function(): ~Awaitable<vec<int>>)> $fn2,
(function(): ~Awaitable<vec<int>>) $fn3,
): void {
// This works
expect($fn1); expect2($fn1);
// This also works
$f = <<__SupportDynamicType>> async () ==> { return await $fn2(); };
expect($f);
expect2($f);
// This does not work
expect(async () ==> { return await $fn2(); } );
expect2(async () ==> { return await $fn2(); } );
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/push_like_enumclass.good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
function expect_string(~string $_): void {}
<<__SupportDynamicType>>
interface ExBox {}
<<__SupportDynamicType>>
class Box<T as supportdyn<mixed> > implements ExBox {
public function __construct(public ~T $data)[] {}
}
<<__SupportDynamicType>>
class IBox extends Box<int> {
public function add(~int $x)[write_props]: void {
$this->data = $this->data + $x;
}
}
enum class E: ~ExBox {
~Box<string> A = new Box('bli');
}
<<__SupportDynamicType>>
function e<T as supportdyn<mixed> >(~HH\MemberOf<E, Box<T>> $param): ~T {
return $param->data;
}
<<__SupportDynamicType>>
function testit(): void {
expect_string(e(E::A));
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/push_like_newtype.good.php | ////file1.php
<?hh
<<__SupportDynamicType>>
class C { }
newtype N<T> as C = C;
function makeN<T>(T $_):N<T> { return new C(); }
newtype NC<+T as supportdyn<mixed>> as C = C;
function makeNC<T as supportdyn<mixed>>(T $_):NC<T> { return new C(); }
////file2.php
<?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
<<file:__EnableUnstableFeatures('upcast_expression')>>
function get():~int {
return 3;
}
function return_n_direct(N<~int> $n):~N<int> {
return $n;
}
function return_n():~N<int> {
return makeN(get());
}
function return_nc_direct(NC<~int> $n):~NC<int> {
return $n;
}
function return_nc():~NC<int> {
$x = makeNC(get());
return $x;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/push_like_option.good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
<<__SupportDynamicType>>
class C{}
function test(~?dict<string, C> $v) : void {}
function repro(dict<string, ~C> $v) : void {
test($v);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/push_like_sdt.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
<<__SupportDynamicType>>
interface I<+T> { }
<<__SupportDynamicType>>
class Wrap<+T> implements I<T> {
public function __construct(private T $item) { }
}
<<__SupportDynamicType>>
function getVecInt():~vec<int> { return vec[3]; }
<<__SupportDynamicType>>
function vectorAppend<Tv as supportdyn<mixed> >(MyVector<Tv> $v, Tv $item):void { }
function vectorAppend_likes<Tv as supportdyn<mixed> >(~MyVector<Tv> $v, ~Tv $item):void { }
function vectorAppend_likes_special(~MyVector<~Wrap<int>> $v, ~Wrap<~int> $item):void { }
function vectorAppend_likes2<Tv as supportdyn<mixed> >(~Tv $item, ~MyVector<Tv> $v):void { }
function test_transitive<T as I<int>>(~T $_):void { }
<<__SupportDynamicType>>
class MyVector<T as supportdyn<mixed>> {}
<<__SupportDynamicType>>
function makeVector<T as supportdyn<mixed>>():MyVector<T> {
return new MyVector<T>();
}
function simple(~MyVector<~Wrap<int>> $_):void { }
<<__SupportDynamicType>>
function test1():void {
$x = makeVector<Wrap<int>>();
$y = new Wrap(getVecInt()[0]);
vectorAppend($x, $y); // vectorAppend<Wrap<int>> works
}
<<__SupportDynamicType>>
function test2():void {
$x = makeVector<Wrap<int>>();
$y = new Wrap(getVecInt()[0]);
vectorAppend_likes($x, $y); // vectorAppend_likes<Wrap<int>> works
}
<<__SupportDynamicType>>
function test3():void {
$x = makeVector<Wrap<int>>();
$y = new Wrap(getVecInt()[0]);
vectorAppend_likes2($y, $x); // vectorAppend_likes2<Wrap<int>> works
}
function test4():void {
$x = new Wrap(getVecInt()[0]);
test_transitive($x);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/push_like_sdt_call.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
<<__SupportDynamicType>>
class C<T as supportdyn<mixed>> { }
<<__SupportDynamicType>>
class Aw<T as supportdyn<mixed>> {}
function bar(): Aw<~C<int>> {
throw new Exception("A");
}
<<__SupportDynamicType>>
function foo<T as supportdyn<mixed>>(Aw<C<T>> $gen_value): void { }
function testit(): void {
// Succeeds
foo<int>(bar());
// Fails
foo(bar());
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/push_nested.good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
function test1(vec<vec<~int>> $v):~vec<vec<int>> {
return $v;
}
function test2(vec<~vec<int>> $v):~vec<vec<int>> {
return $v;
}
function test3(vec<~vec<~int>> $v):~vec<vec<int>> {
return $v;
}
function test4(~vec<~vec<~int>> $v):~vec<vec<int>> {
return $v;
}
function test5(?vec<~int> $v):~?vec<int> {
return $v;
}
function test6(?vec<~int> $v):?~vec<int> {
return $v;
}
<<__SupportDynamicType>>
class B<+T> { }
<<__SupportDynamicType>>
class C<+T> extends B<T> { }
<<__SupportDynamicType>>
class D<+T> extends B<T> { }
function chain(~C<int> $c):~B<int> {
return $c;
}
function test8(C<~int> $c):~B<int> {
return $c;
}
function test9((C<~int> | D<~int>) $u):~B<int> {
return $u;
}
function test10((C<~int> | D<~string>) $u):~(C<int> | D<string>) {
return $u;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/push_newtype_bounded.good.php | ////file1.php
<?hh
<<__SupportDynamicType>>
class Cov<+T> { }
<<__SupportDynamicType>>
class Derived<+T> extends Cov<T> { }
newtype MyMemberOf<+TType> as TType = TType;
newtype MyLabel<TType> = mixed;
newtype N as Cov<~int> = Derived<~int>;
newtype P<+T> as Cov<T> = Cov<T>;
////file2.php
<?hh
function valueOf<TType as supportdyn<mixed> >(~MyLabel<TType> $label): ~MyMemberOf<TType> {
throw new Exception("A");
}
function getE(~MyLabel<int> $x): ~int {
return valueOf<_>($x);
}
function testN(N $n): ~Cov<int> {
return $n;
}
function testP(P<~int> $p) : ~Cov<int> {
return $p;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/push_tuple_constrained.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
//<<__SupportDynamicType>>
class C {
public static function updateTime(
string $time_key,
IBKSExpression<MinsNum> $new_time,
): void {
$t = tuple(BKSString::const($time_key), $new_time);
// Works
BKSMap::const($t);
// Does not work
BKSMap::const(tuple(BKSString::const($time_key), $new_time));
}
}
<<__SupportDynamicType>>
final class BKSMap<+TKey as supportdyn<mixed> as MinsMapkey, +TValue as supportdyn<mixed> as BKSClientValue>
extends BKSExpression<BKSClientMap<TKey, TValue>>
{
public static function const(
(IBKSExpression<TKey>, IBKSExpression<TValue>) $keys_and_values
): ~this {
throw new Exception("A");
}
}
<<__SupportDynamicType>>
interface MinsMapkey extends MinsNonnull {}
<<__SupportDynamicType>>
interface MinsNonnull extends MinsValue {}
<<__SupportDynamicType>>
interface MinsValue extends MinsNonnullType {
}
<<__SupportDynamicType>>
interface MinsNonnullType {
}
type MinsType = ?MinsNonnullType;
<<__SupportDynamicType>>
interface IBKSExpression<+T as supportdyn<mixed>>
{
}
type BKSClientValue = ?MinsValue;
type BKSClientMap<+TKey, +TValue> =
MinsBaseMap<TKey, TValue>;
<<__SupportDynamicType>>
interface MinsBaseMap<+Tk as supportdyn<mixed>, +Tv as supportdyn<mixed>>
extends MinsNonnull {}
<<__SupportDynamicType>>
abstract class BKSExpression<+T as supportdyn<mixed> as MinsType> implements IBKSExpression<T> { }
<<__SupportDynamicType>>
final class BKSString extends BKSConstant<string, MinsString> {
}
<<__SupportDynamicType>>
abstract class BKSConstant<TBKSServerType as supportdyn<mixed> , +TBKSClientValue as supportdyn<mixed> as BKSClientValue>
extends BKSExpression<TBKSClientValue>
{
public static function const(TBKSServerType $value): ~this {
throw new Exception("A");
}
}
<<__SupportDynamicType>>
interface MinsString extends MinsMapkey, IBKSExpression<MinsString> {
}
<<__SupportDynamicType>>
interface MinsNum extends MinsMapkey {
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/refine_consistent_tpenv.bad.php | <?hh
interface I {}
class C implements I {
}
function f<T>(T $x) : void where T as bool {}
class D<T as I> {
public function getT(): ~?T { return null; }
public function f() : void {
$t = $this->getT();
if ($t is C) {
// This won't fail if the tpenv is inconsistent
f(1);
}
}
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/refine_like_tyvar.good.php | <?hh
<<__SupportDynamicType>>
interface I1 {}
<<__SupportDynamicType>>
interface I2 extends I1 {
public function m() : void;
}
<<__SupportDynamicType>>
interface I3 extends I1 {
public function m() : void;
}
function getI(): vec<I1> {
return vec[];
}
function map_and_filter_nulls<Tv1 , Tv2 >(
Traversable<Tv1> $traversable,
(function(Tv1): ~?Tv2) $value_func,
): vec<Tv2> {
return vec[];
}
function f(): void {
$x = getI()
|> map_and_filter_nulls(
$$,
$y ==> {
if ($y is I2 || $y is I3) {
$z = $y;
}
else {
$z = null;
}
return $z;
}
);
$x[0]->m();
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/refine_nonnull_call.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
<<__SupportDynamicType>>
class C { }
<<__SupportDynamicType>>
class D { }
<<__SupportDynamicType>>
function getC():~?C { return new C(); }
<<__SupportDynamicType>>
function foo(C $_):D { return new D(); }
<<__SupportDynamicType>>
function bar():void {
$nc = getC();
$nc as nonnull;
// We should allow argument to have type (dynamic & nonnull) | C
$ld = foo($nc);
// But result should be ~D because the function
// essentially has the type (C->D) & (dynamic->dynamic)
hh_expect_equivalent<~D>($ld);
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/refine_sd_box.bad.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
class MyBox<+Tv as supportdyn<mixed>> {
public function __construct(private Tv $item) { }
public function get():Tv {
return $this->item;
}
}
function foo(supportdyn<nonnull> $x): void {
$x as MyBox<_>;
$y = $x->get();
$y->bogus();
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/refine_sd_shape.good.php | <?hh
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
type TS = supportdyn<shape(?'a' => int, ...)>;
function foo(TS $x):int {
if (Shapes::idx($x, 'a') !== null) {
$y = $x['a'];
return 3;
}
else return 0;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/reify.bad.php | <?hh
<<file:__EnableUnstableFeatures('upcast_expression')>>
class C<reify T> {}
function expect_Cdyn(C<dynamic> $c) : void { }
function expect_Cint(C<int> $c) : void { }
function test(C<dynamic> $cd, C<int> $ci) : void {
$ci upcast C<dynamic>;
$cd upcast C<int>;
new C<int>() upcast C<dynamic>;
new C<dynamic>() upcast C<int>;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/reify.good.php | <?hh
<<file:__EnableUnstableFeatures('upcast_expression')>>
<<__SupportDynamicType>>
class C<reify T> {}
function test(dynamic $d, C<int> $c) : void {
$c upcast dynamic;
} |
PHP | hhvm/hphp/hack/test/sound_dynamic/typing/return_disposable.good.php | <?hh
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
<<__SupportDynamicType>>
class C implements IDisposable {
public function __dispose(): void { }
}
<<__SupportDynamicType, __ReturnDisposable>>
function testit(vec<int> $_): C {
return new C();
}
<<__SupportDynamicType>>
class D {
<<__ReturnDisposable>>
public static function foo(vec<int> $_): C {
return new C();
}
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.