-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathfinally_typing.php
More file actions
93 lines (86 loc) · 1.72 KB
/
finally_typing.php
File metadata and controls
93 lines (86 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?hh // partial
/**
* 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 finally_typing1(): string {
$a = 23;
try {
do_something();
} finally {
$a = 'string'; // this definition escapes the clause
}
return $a;
}
function finally_typing2(): string {
$a = 23;
try {
do_something();
return 'string';
} finally {
// this definition escapes the clause, even with terminality
$a = 'string';
}
return $a;
}
// with a different story with respect to unreachable code ...
// function finally_typing3(): int {
// $a = 23;
// try {
// do_something();
// $a = 25;
// return $a; // terminal block
// } finally {
// // this assignment beats out the original, but it doesn't matter
// // because the try is fully terminal
// $a = 'string';
// }
// return $a;
// }
function do_something(): void {}
function finally_typing3(bool $c): int {
try {
try {
if ($c) {
$a = "string";
throw new Exception();
}
$a = 0;
} finally {
// $a has different types depending on the continuation
$b = $a;
}
} catch (Exception $_) {
// $b should be a string here
return str_to_int($b);
}
return $b;
}
function str_to_int(string $s): int {
return 0;
}
function finally_typing4(int $x): void {
$a = 0;
try {
try {
if ($x < 0) {
throw new Exception();
}
} finally {
if ($x < 1) {
$a = "string";
throw new Exception();
}
$a = 1;
}
} catch (Exception $_) {
hh_show($a);
return;
}
hh_show($a);
}