Skip to content

Commit

Permalink
[inferhack] add simple test cases for type aliases
Browse files Browse the repository at this point in the history
Reviewed By: skcho

Differential Revision: D58293785

fbshipit-source-id: 205e0c9383383b373ce0953030526fb92463d377
  • Loading branch information
Nick Benton authored and facebook-github-bot committed Jun 19, 2024
1 parent f557551 commit cae946d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
95 changes: 95 additions & 0 deletions infer/tests/codetoanalyze/hack/pulse/aliases.hack
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();
}
}
2 changes: 2 additions & 0 deletions infer/tests/codetoanalyze/hack/pulse/issues.exp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
aliases.hack, Aliases::C.testParameterAliasOK_FP, 5, PULSE_UNAWAITED_AWAITABLE, no_bucket, ERROR, [allocation part of the trace starts here,allocated here,awaitable becomes unreachable here]
aliases.hack, Aliases::C.testTypeConstantOK_FP, 5, PULSE_UNAWAITED_AWAITABLE, no_bucket, ERROR, [allocation part of the trace starts here,allocated here,awaitable becomes unreachable here]
arg_index_matcher.hack, Main.staticSink, 2, TAINT_ERROR, no_bucket, ERROR, [in call to `ArgIndexMatcher$static.source`,source of the taint here: value returned from `$root.Level1::taintSource` with kind `Simple`,return from call to `ArgIndexMatcher$static.source`,flows to this sink: value passed as argument `#1` to `ArgIndexMatcher$static.sink1` with kind `Simple`], source: $root.Level1::taintSource, sink: ArgIndexMatcher$static.sink1, tainted expression: $tainted
arg_index_matcher.hack, Main.instanceSink, 3, TAINT_ERROR, no_bucket, ERROR, [in call to `ArgIndexMatcher$static.source`,source of the taint here: value returned from `$root.Level1::taintSource` with kind `Simple`,return from call to `ArgIndexMatcher$static.source`,flows to this sink: value passed as argument `#1` to `ArgIndexMatcher.sink2` with kind `Simple`], source: $root.Level1::taintSource, sink: ArgIndexMatcher.sink2, tainted expression: $tainted
async.hack, $root.testnamingBad, 2, PULSE_UNAWAITED_AWAITABLE, no_bucket, ERROR, [allocation part of the trace starts here,allocated here,awaitable becomes unreachable here]
Expand Down

0 comments on commit cae946d

Please sign in to comment.