No error on use of `unsafe_cast`
[hiphop-php.git] / hphp / hack / test / typecheck / finally_typing.php
blob7466906a94a0af5a746ca6124c15e974f901fec1
1 <?hh // partial
2 /**
3 * Copyright (c) 2014, Facebook, Inc.
4 * All rights reserved.
6 * This source code is licensed under the MIT license found in the
7 * LICENSE file in the "hack" directory of this source tree.
12 function finally_typing1(): string {
13 $a = 23;
14 try {
15 do_something();
16 } finally {
17 $a = 'string'; // this definition escapes the clause
19 return $a;
22 function finally_typing2(): string {
23 $a = 23;
24 try {
25 do_something();
26 return 'string';
27 } finally {
28 // this definition escapes the clause, even with terminality
29 $a = 'string';
31 return $a;
34 // with a different story with respect to unreachable code ...
35 // function finally_typing3(): int {
36 // $a = 23;
37 // try {
38 // do_something();
39 // $a = 25;
40 // return $a; // terminal block
41 // } finally {
42 // // this assignment beats out the original, but it doesn't matter
43 // // because the try is fully terminal
44 // $a = 'string';
45 // }
46 // return $a;
47 // }
49 function do_something(): void {}
51 function finally_typing3(bool $c): int {
52 try {
53 try {
54 if ($c) {
55 $a = "string";
56 throw new Exception();
58 $a = 0;
59 } finally {
60 // $a has different types depending on the continuation
61 $b = $a;
63 } catch (Exception $_) {
64 // $b should be a string here
65 return str_to_int($b);
67 return $b;
70 function str_to_int(string $s): int {
71 return 0;
74 function finally_typing4(int $x): void {
75 $a = 0;
76 try {
77 try {
78 if ($x < 0) {
79 throw new Exception();
81 } finally {
82 if ($x < 1) {
83 $a = "string";
84 throw new Exception();
86 $a = 1;
88 } catch (Exception $_) {
89 hh_show($a);
90 return;
92 hh_show($a);