Skip to content

Commit 7fff3fd

Browse files
Wilfredfacebook-github-bot
authored andcommitted
Ban aliases in catch blocks
Summary: An alias is completely ignored by HHVM in catch blocks, leading to users thinking that their code will catch exceptions that it ignores. This might be something we can support in the future when decls-in-compilation enables aliases in more positions, but today it's safer to update hh to reflect the runtime. Differential Revision: D36915826 fbshipit-source-id: e60d5ed19314878f57fed8479faf11f31f5f520d
1 parent 9219e79 commit 7fff3fd

File tree

6 files changed

+37
-1
lines changed

6 files changed

+37
-1
lines changed

hphp/hack/src/typing/nast_check/unbound_name_check.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ let handler ctx =
336336
let () =
337337
check_type_name
338338
env
339-
~allow_typedef:true
339+
~allow_typedef:false
340340
~allow_generics:false
341341
~kind:Name_context.ClassContext
342342
id
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?hh
2+
3+
type ExceptionAlias = Exception;
4+
5+
<<__EntryPoint>>
6+
function my_main(): void {
7+
try {
8+
throw new Exception();
9+
} catch (ExceptionAlias $a) {
10+
echo "never executed";
11+
}
12+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
File "catch_alias.php", line 9, characters 12-25:
2+
Expected a `class` but got a type alias. (Naming[2052])
3+
File "catch_alias.php", line 3, characters 6-19:
4+
Alias definition is here.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?hh
2+
3+
newtype ExceptionAlias = Exception;
4+
5+
<<__EntryPoint>>
6+
function my_main(): void {
7+
try {
8+
throw new Exception();
9+
} catch (ExceptionAlias $a) {
10+
echo "never executed";
11+
}
12+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
File "catch_newtype.php", line 9, characters 12-25:
2+
Expected a `class` but got a type alias. (Naming[2052])
3+
File "catch_newtype.php", line 3, characters 9-22:
4+
Alias definition is here.

hphp/hack/test/typecheck/unbound_name/catch.php.exp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
File "catch.php", line 15, characters 10-14:
2+
Expected a `class` but got a type alias. (Naming[2052])
3+
File "catch.php", line 5, characters 6-10:
4+
Alias definition is here.
15
File "catch.php", line 21, characters 10-26:
26
Unbound name: `NotFoundException` (an object type) (Naming[2049])
37
File "catch.php", line 27, characters 10-10:

0 commit comments

Comments
 (0)