Skip to content

Commit

Permalink
Make closure and soft type specifiers a parse error
Browse files Browse the repository at this point in the history
Summary:
These hints may never be used with `is`-expressions because:
- We have no information on a function's inputs/output at runtime.
- Soft typehints would be meaningless when testing types.

Reviewed By: periodic1236, oulgen

Differential Revision: D7076934

fbshipit-source-id: 42639d367cfa6a73b384098d6abf3796ad87b935
  • Loading branch information
kmeht authored and hhvm-bot committed Feb 25, 2018
1 parent ca49cc8 commit d9a87a1
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 1 deletion.
12 changes: 12 additions & 0 deletions hphp/hack/src/parser/full_fidelity_parser_errors.ml
Expand Up @@ -1746,6 +1746,18 @@ let expression_errors node parents is_hack is_hack_file hhvm_compat_mode errors
&& leading_width lambda_signature = 0
-> failwith "syntax error, unexpected T_LAMBDA_ARROW";
(* End of bug-port *)
| IsExpression
{ is_right_operand = hint
; _ } ->
begin match syntax hint with
| ClosureTypeSpecifier _ ->
make_error_from_node hint
(SyntaxError.invalid_is_expression_hint "Callable") :: errors
| SoftTypeSpecifier _ ->
make_error_from_node hint
(SyntaxError.invalid_is_expression_hint "Soft") :: errors
| _ -> errors
end
| _ -> errors (* Other kinds of expressions currently produce no expr errors. *)

let require_errors node parents hhvm_compat_mode trait_use_clauses errors =
Expand Down
2 changes: 1 addition & 1 deletion hphp/hack/src/parser/full_fidelity_syntax_error.ml
Expand Up @@ -392,6 +392,6 @@ let variadic_param_with_type_in_php name type_ =
^ type_ ^ "); variadic params with type constraints are not "
^ "supported in non-Hack files"
let final_property = "Properties cannot be declared final"

let property_has_multiple_visibilities name =
"Multiple access type modifiers are not allowed: properties of " ^ name
let invalid_is_expression_hint hint = hint ^ " typehints cannot be used with `is` expressions"
1 change: 1 addition & 0 deletions hphp/hack/src/parser/full_fidelity_syntax_error.mli
Expand Up @@ -237,3 +237,4 @@ val self_or_parent_colon_colon_class_outside_of_class : string -> string
val variadic_param_with_type_in_php : string -> string -> string
val final_property : string
val property_has_multiple_visibilities : string -> string
val invalid_is_expression_hint : string -> string
@@ -0,0 +1 @@
(13,16)-(13,32) Callable typehints cannot be used with `is` expressions
@@ -0,0 +1,14 @@
<?hh
/**
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the "hack" directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

function test(mixed $e): bool {
return $e is (function(): int);
}
@@ -0,0 +1 @@
(13,16)-(13,19) Soft typehints cannot be used with `is` expressions
@@ -0,0 +1,14 @@
<?hh
/**
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the "hack" directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

function test(mixed $e): bool {
return $e is @int;
}
2 changes: 2 additions & 0 deletions hphp/hack/test/full_fidelity/full_fidelity_unit_test.ml
Expand Up @@ -191,6 +191,8 @@ let error_tests =
make_test_case_from_files testname test_errors in
List.map
[
"is_expression/test_callable_hint";
"is_expression/test_soft_hint";
"test_default_param_errors";
"test_alias_errors";
"test_method_modifier_errors";
Expand Down

0 comments on commit d9a87a1

Please sign in to comment.