Skip to content

Commit f2cd60a

Browse files
Chaoqiang Dengfacebook-github-bot
authored andcommitted
Output the function type for memoized functions
Summary: Previously, when we raise a global read caused by memoized functions/methods, we only print out the return type of memoized function. As required by BA (i.e. there is the need to learn the parameter types of memoized functions, especially if a vc of type IViewerContext is used as the parameter), this diff prints out the whole function type of memoized function, which will be stored in "data_type" column of our DB table. To do: create a separate table to store the parameter types. Reviewed By: lavenzg Differential Revision: D40880563 fbshipit-source-id: 73d1c566383fc36674a03bdcb8c9bd8ed7be6b4c
1 parent 8b18019 commit f2cd60a

6 files changed

Lines changed: 32 additions & 31 deletions

File tree

hphp/hack/src/typing/tast_check/global_access_check.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,9 +816,9 @@ let visitor =
816816
(SSet.singleton expr_str)
817817
(GlobalAccessPatternSet.singleton NoPattern)
818818
GlobalAccessCheck.DefiniteGlobalRead
819-
| Call (func_expr, _, tpl, _) ->
819+
| Call (((func_ty, _, _) as func_expr), _, tpl, _) ->
820820
(* First check if the called function is memoized. *)
821-
let func_ty_str = Tast_env.print_ty env ty in
821+
let func_ty_str = Tast_env.print_ty env func_ty in
822822
(match check_func_is_memoized func_expr env with
823823
| Some memoized_func_name ->
824824
raise_global_access_error

hphp/hack/test/typecheck/global_access_check/global_write.php.exp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ File "global_write.php", line 83, characters 7-21:
4141
File "global_write.php", line 87, characters 5-16:
4242
[Test::test_method_call]{Foo::$bar}(int)<WriteLiteral> A global variable is possibly written via reference. (Other[11002])
4343
File "global_write.php", line 89, characters 10-28:
44-
[Test::test_method_call]{Foo::memoized_lsb}(Bar) A global variable is definitely read. (Other[11004])
44+
[Test::test_method_call]{Foo::memoized_lsb}((function(): Bar)) A global variable is definitely read. (Other[11004])
4545
File "global_write.php", line 90, characters 5-16:
4646
[Test::test_method_call]{Foo::memoized_lsb}(int)<WriteLiteral> A global variable is possibly written via reference. (Other[11002])
4747
File "global_write.php", line 92, characters 10-19:
48-
[Test::test_method_call]{\memoized}(Bar) A global variable is definitely read. (Other[11004])
48+
[Test::test_method_call]{\memoized}((function(): Bar)) A global variable is definitely read. (Other[11004])
4949
File "global_write.php", line 93, characters 5-16:
5050
[Test::test_method_call]{\memoized}(int)<WriteLiteral> A global variable is possibly written via reference. (Other[11002])
5151
File "global_write.php", line 95, characters 10-29:
@@ -55,7 +55,7 @@ File "global_write.php", line 98, characters 10-29:
5555
File "global_write.php", line 99, characters 5-19:
5656
[Test::test_method_call]{Foo::$static_vec_bar}(int)<WriteLiteral> A global variable is possibly written via reference. (Other[11002])
5757
File "global_write.php", line 103, characters 10-27:
58-
[Test::test_method_call]{\memoized_vec_int}(vec<int>) A global variable is definitely read. (Other[11004])
58+
[Test::test_method_call]{\memoized_vec_int}((function(): vec<int>)) A global variable is definitely read. (Other[11004])
5959
File "global_write.php", line 107, characters 22-30:
6060
[Test::test_method_call]{Foo::$bar}(Bar) A global variable is definitely read. (Other[11004])
6161
File "global_write.php", line 108, characters 5-16:

hphp/hack/test/typecheck/global_access_check/test_memoized.php.exp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ File "test_memoized.php", line 20, characters 12-21:
77
File "test_memoized.php", line 46, characters 5-25:
88
[Test::test_method_call]{Foo::$bar}(int)<WriteLiteral> A global variable is definitely written. (Other[11001])
99
File "test_memoized.php", line 48, characters 10-35:
10-
[Test::test_method_call]{Foo::foo_method_memoized}(int) A global variable is definitely read. (Other[11004])
10+
[Test::test_method_call]{Foo::foo_method_memoized}((function(): int)) A global variable is definitely read. (Other[11004])
1111
File "test_memoized.php", line 51, characters 10-39:
12-
[Test::test_method_call]{Foo::foo_method_memoized_lsb}(Bar) A global variable is definitely read. (Other[11004])
12+
[Test::test_method_call]{Foo::foo_method_memoized_lsb}((function(): Bar)) A global variable is definitely read. (Other[11004])
1313
File "test_memoized.php", line 52, characters 5-16:
1414
[Test::test_method_call]{Foo::foo_method_memoized_lsb}(int)<WriteLiteral> A global variable is possibly written via reference. (Other[11002])
1515
File "test_memoized.php", line 54, characters 10-23:
16-
[Test::test_method_call]{\fun_memoized}(Bar) A global variable is definitely read. (Other[11004])
16+
[Test::test_method_call]{\fun_memoized}((function(): Bar)) A global variable is definitely read. (Other[11004])
1717
File "test_memoized.php", line 55, characters 5-16:
1818
[Test::test_method_call]{\fun_memoized}(int)<WriteLiteral> A global variable is possibly written via reference. (Other[11002])
1919
File "test_memoized.php", line 57, characters 10-31:
20-
[Test::test_method_call]{\fun_memoized_vec_int}(vec<int>) A global variable is definitely read. (Other[11004])
20+
[Test::test_method_call]{\fun_memoized_vec_int}((function(): vec<int>)) A global variable is definitely read. (Other[11004])
2121
File "test_memoized.php", line 61, characters 10-43:
22-
[Test::test_method_call]{Foo->foo_instance_method_memoized}(int) A global variable is definitely read. (Other[11004])
22+
[Test::test_method_call]{Foo->foo_instance_method_memoized}((function(): int)) A global variable is definitely read. (Other[11004])
2323
File "test_memoized.php", line 64, characters 10-47:
24-
[Test::test_method_call]{Foo->foo_instance_method_memoized_lsb}(Bar) A global variable is definitely read. (Other[11004])
24+
[Test::test_method_call]{Foo->foo_instance_method_memoized_lsb}((function(): Bar)) A global variable is definitely read. (Other[11004])
2525
File "test_memoized.php", line 65, characters 5-16:
2626
[Test::test_method_call]{Foo->foo_instance_method_memoized_lsb}(int)<WriteLiteral> A global variable is possibly written via reference. (Other[11002])
2727
File "test_memoized.php", line 68, characters 10-48:
28-
[Test::test_method_call]{Foo->foo_instance_method_memoized_lsb}(?Bar) A global variable is definitely read. (Other[11004])
28+
[Test::test_method_call]{Foo->foo_instance_method_memoized_lsb}((function(): ?Bar)) A global variable is definitely read. (Other[11004])
2929
File "test_memoized.php", line 70, characters 7-18:
3030
[Test::test_method_call]{Foo->foo_instance_method_memoized_lsb}(int)<WriteLiteral> A global variable is possibly written via reference. (Other[11002])

hphp/hack/test/typecheck/global_access_check/test_reads.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Foo {
1212
function call_mixed(mixed $x): void {}
1313

1414
<<__Memoize>>
15-
function fun_memoized(): Bar {
15+
function fun_memoized(int $i, string $s): Bar {
1616
return new Bar();
1717
}
1818

@@ -37,12 +37,13 @@ public function test_method_call(): int {
3737
call_mixed((Foo::$bar)->prop); // global read
3838
call_mixed(self::$static_prop); // global read
3939

40-
$a = fun_memoized(); // global read
41-
$b = fun_memoized()->prop + 1; // global read
42-
$c = fun_memoized()->prop > 0 ? 1 : -1; // global read
40+
$x = new Bar();
41+
$a = fun_memoized(1, "a"); // global read
42+
$b = fun_memoized(1, "a")->prop + 1; // global read
43+
$c = fun_memoized(1, "a")->prop > 0 ? 1 : -1; // global read
4344

44-
if (!(fun_memoized()->prop > 0)) { // global read
45-
fun_memoized()->prop = 1;
45+
if (!(fun_memoized(1, "a")->prop > 0)) { // global read
46+
fun_memoized(1, "a")->prop = 1;
4647
}
4748

4849
return self::$static_prop; // global read

hphp/hack/test/typecheck/global_access_check/test_reads.php.exp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ File "test_reads.php", line 37, characters 17-25:
2222
[Test::test_method_call]{Foo::$bar}(Bar) A global variable is definitely read. (Other[11004])
2323
File "test_reads.php", line 38, characters 16-33:
2424
[Test::test_method_call]{Test::$static_prop}(int) A global variable is definitely read. (Other[11004])
25-
File "test_reads.php", line 40, characters 10-23:
26-
[Test::test_method_call]{\fun_memoized}(Bar) A global variable is definitely read. (Other[11004])
27-
File "test_reads.php", line 41, characters 10-23:
28-
[Test::test_method_call]{\fun_memoized}(Bar) A global variable is definitely read. (Other[11004])
29-
File "test_reads.php", line 42, characters 10-23:
30-
[Test::test_method_call]{\fun_memoized}(Bar) A global variable is definitely read. (Other[11004])
31-
File "test_reads.php", line 44, characters 11-24:
32-
[Test::test_method_call]{\fun_memoized}(Bar) A global variable is definitely read. (Other[11004])
33-
File "test_reads.php", line 45, characters 7-30:
25+
File "test_reads.php", line 41, characters 10-29:
26+
[Test::test_method_call]{\fun_memoized}((function(int $i, string $s): Bar)) A global variable is definitely read. (Other[11004])
27+
File "test_reads.php", line 42, characters 10-29:
28+
[Test::test_method_call]{\fun_memoized}((function(int $i, string $s): Bar)) A global variable is definitely read. (Other[11004])
29+
File "test_reads.php", line 43, characters 10-29:
30+
[Test::test_method_call]{\fun_memoized}((function(int $i, string $s): Bar)) A global variable is definitely read. (Other[11004])
31+
File "test_reads.php", line 45, characters 11-30:
32+
[Test::test_method_call]{\fun_memoized}((function(int $i, string $s): Bar)) A global variable is definitely read. (Other[11004])
33+
File "test_reads.php", line 46, characters 7-36:
3434
[Test::test_method_call]{\fun_memoized}(int)<WriteLiteral> A global variable is possibly written via reference. (Other[11002])
35-
File "test_reads.php", line 48, characters 12-29:
35+
File "test_reads.php", line 49, characters 12-29:
3636
[Test::test_method_call]{Test::$static_prop}(int) A global variable is definitely read. (Other[11004])
37-
File "test_reads.php", line 55, characters 23-39:
37+
File "test_reads.php", line 56, characters 23-39:
3838
[Test::test_loop]{Foo::$static_prop}(int) A global variable is definitely read. (Other[11004])
39-
File "test_reads.php", line 56, characters 12-20:
39+
File "test_reads.php", line 57, characters 12-20:
4040
[Test::test_loop]{Foo::$bar}(Bar) A global variable is definitely read. (Other[11004])

hphp/hack/test/typecheck/global_access_check/test_singleton_write_via_ref.php.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ File "test_singleton_write_via_ref.php", line 40, characters 7-25:
1313
File "test_singleton_write_via_ref.php", line 42, characters 7-25:
1414
[Test::test_method_call]{Foo::$bar}(Baz)<Singleton> A global variable is possibly written via reference. (Other[11002])
1515
File "test_singleton_write_via_ref.php", line 45, characters 10-23:
16-
[Test::test_method_call]{\fun_memoized}(Bar) A global variable is definitely read. (Other[11004])
16+
[Test::test_method_call]{\fun_memoized}((function(): Bar)) A global variable is definitely read. (Other[11004])
1717
File "test_singleton_write_via_ref.php", line 47, characters 7-25:
1818
[Test::test_method_call]{\fun_memoized}(Baz)<Singleton> A global variable is possibly written via reference. (Other[11002])
1919
File "test_singleton_write_via_ref.php", line 49, characters 7-25:

0 commit comments

Comments
 (0)