-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[inferhack] add simple test cases for type aliases
Reviewed By: skcho Differential Revision: D58293785 fbshipit-source-id: 205e0c9383383b373ce0953030526fb92463d377
- Loading branch information
1 parent
f557551
commit cae946d
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright (c) Facebook, Inc. and its affiliates. | ||
// | ||
// This source code is licensed under the MIT license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
<<file: __EnableUnstableFeatures('case_types')>> | ||
|
||
namespace Aliases; | ||
|
||
type A = int; | ||
newtype B = A; | ||
|
||
class D<T> {} | ||
type AliasD<T> = D<T>; | ||
|
||
// the following is just here to test that the alias parsing doesn't fall over too badly | ||
// should it encounter a case type - we don't actually do anything sensible with them yet | ||
case type CT3 = string | bool; | ||
|
||
class HasConstant { | ||
const type T = int; | ||
} | ||
|
||
class C { | ||
public function foo(A $x): int { | ||
return $x; | ||
} | ||
|
||
public async function fail(): Awaitable<int> { | ||
return 42; | ||
} | ||
|
||
public async function testGenericOK(): Awaitable<void> { | ||
$x = new D<int>(); | ||
if ($x is D<_>) { | ||
return; | ||
} | ||
$_ = $this->fail(); | ||
} | ||
|
||
/* I think this ought to work, but hh rejects it for some reason! | ||
Bug reported to Hack team | ||
Fixed in D58323800 | ||
public async function testGenericAliasOK(): Awaitable<void> { | ||
$x = new D<int>(); | ||
if ($x is AliasD<_>) { | ||
return; | ||
} | ||
$_ = $this->fail(); | ||
} | ||
$_ = $this->fail(); | ||
} | ||
*/ | ||
|
||
// This one doesn't work yet because we're not doing explicit parameter assertion/verification | ||
// on non-generic parameters yet (Hackc does them implicitly now) | ||
public async function testParameterAliasOK_FP(A $x): Awaitable<void> { | ||
if ($x is int) { | ||
return; | ||
} | ||
$_ = $this->fail(); | ||
} | ||
|
||
// so this one, which seems more complex, works OK 'cos Hackc does emit explicit | ||
// parameter verification here | ||
public async function testParameterGenericAliasOK( | ||
AliasD<int> $x, | ||
): Awaitable<void> { | ||
if ($x is D<_>) { | ||
return; | ||
} | ||
$_ = $this->fail(); | ||
} | ||
|
||
public async function testPrimitiveAliasOK(): Awaitable<void> { | ||
if (3 is A) { | ||
return; | ||
} | ||
$_ = $this->fail(); | ||
} | ||
|
||
public async function testPrimitiveAlias2OK(): Awaitable<void> { | ||
if (3 is B) { | ||
return; | ||
} | ||
$_ = $this->fail(); | ||
} | ||
|
||
// don't do type constants yet :-( | ||
public async function testTypeConstantOK_FP(): Awaitable<void> { | ||
if (3 is HasConstant::T) { | ||
return; | ||
} | ||
$_ = $this->fail(); | ||
} | ||
} |
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