Permalink
Browse files

Typechecker support for throwing and catching throwables and errors

Summary:
PHP7 mode is default now, so these are thrown.

fixes #8153

Reviewed By: vassilmladenov

Differential Revision: D7304627

fbshipit-source-id: 390e5a8bb35a5bdd2282b9d520fd105ace26a58a
  • Loading branch information...
fredemmott committed Mar 22, 2018
1 parent 5434132 commit 9235b1f388d754a9e5e72fff88590ec602bfe6e0
@@ -28,7 +28,7 @@ module Classes = struct
let cHH_BuiltinEnum = "\\HH\\BuiltinEnum"
let cException = "\\Exception"
let cThrowable= "\\Throwable"
let cStdClass = "\\stdClass"
let cDateTime = "\\DateTime"
@@ -2816,7 +2816,7 @@ and uninstantiable_error reason_pos cid c_tc_pos c_name c_usage_pos c_ty =
Errors.uninstantiable_class c_usage_pos c_tc_pos c_name reason_msgl
and exception_ty pos env ty =
let exn_ty = Reason.Rthrow pos, Tclass ((pos, SN.Classes.cException), []) in
let exn_ty = Reason.Rthrow pos, Tclass ((pos, SN.Classes.cThrowable), []) in
Type.sub_type pos (Reason.URthrow) env ty exn_ty
and shape_field_pos = function
@@ -1,6 +1,6 @@
File "catch_non_exception.php", line 7, characters 12-23:
Invalid exception (Typing[4110])
File "catch_non_exception.php", line 7, characters 12-23:
This is an object of type Exception because it is used as an exception
This is an object of type Throwable because it is used as an exception
File "catch_non_exception.php", line 7, characters 12-23:
It is incompatible with an object of type NonException
@@ -0,0 +1,51 @@
<?hh // strict
class Foo implements Throwable {
public function getMessage(): string {
return __FUNCTION__;
}
public function getCode(): int {
return __LINE__;
}
public function getLine(): int {
return __LINE__;
}
public function getFile(): string {
return __FILE__;
}
public function getTrace(): array<mixed> {
return [];
}
public function getTraceAsString(): string {
return 'nope';
}
public function getPrevious(): ?Throwable {
return null;
}
public function __toString(): string {
return __FUNCTION__;
}
}
function catches(): void {
try {
} catch (Exception $_) {
}
try {
} catch (Throwable $_) {
}
try {
} catch (Error $_) {
}
try {
} catch (Foo $_) {
}
}
function throws(): void {
throw new Exception();
throw new Error();
throw new TypeError();
throw new Foo();
}
@@ -0,0 +1 @@
No errors
@@ -1,6 +1,6 @@
File "throw_non_exception.php", line 6, characters 9-26:
Invalid exception (Typing[4110])
File "throw_non_exception.php", line 6, characters 9-26:
This is an object of type Exception because it is used as an exception
This is an object of type Throwable because it is used as an exception
File "throw_non_exception.php", line 6, characters 9-26:
It is incompatible with an object of type NonException

0 comments on commit 9235b1f

Please sign in to comment.