From 4636777b1ce7739614882a88dacf77bb1b3d01fe Mon Sep 17 00:00:00 2001 From: Catherine Gasnier Date: Thu, 13 May 2021 09:25:53 -0700 Subject: [PATCH] opaque types should provide proper constraints on their type params Summary: See added test case for an example. This test case use to not have any error. Reviewed By: andrewjkennedy Differential Revision: D28380086 fbshipit-source-id: 7a2a519ec6d233b4cdbfbd41be0fb47513d77b12 --- hphp/hack/src/typing/typing_check_decls.ml | 16 ++++++++++------ .../hack/test/typecheck/constraint_newtype_param.php | 9 +++++++++ .../test/typecheck/constraint_newtype_param.php.exp | 8 ++++++++ .../test/typecheck/constraints/tapply_is_newtype.php | 2 +- .../typecheck/constraints/tapply_is_newtype.php.exp | 20 ++++++++++---------- hphp/hack/test/typecheck/constraints/typedef.php | 2 +- hphp/hack/test/typecheck/newtype8.php | 2 +- 7 files changed, 40 insertions(+), 19 deletions(-) create mode 100644 hphp/hack/test/typecheck/constraint_newtype_param.php create mode 100644 hphp/hack/test/typecheck/constraint_newtype_param.php.exp rewrite hphp/hack/test/typecheck/constraints/tapply_is_newtype.php.exp (74%) diff --git a/hphp/hack/src/typing/typing_check_decls.ml b/hphp/hack/src/typing/typing_check_decls.ml index c152bb24329..108bb1a2aa6 100644 --- a/hphp/hack/src/typing/typing_check_decls.ml +++ b/hphp/hack/src/typing/typing_check_decls.ml @@ -349,7 +349,7 @@ let typedef tenv t = t_constraint; t_kind; t_mode = _; - t_vis = _; + t_vis; t_namespace = _; t_user_attributes = _; t_span = _; @@ -375,11 +375,15 @@ let typedef tenv t = check_hint_wellkindedness tenv_with_typedef_tparams t_kind; let env = { - (* Since typedefs cannot have constraints we shouldn't check - * if its type params satisfy the constraints of any tapply it - * references. - *) - typedef_tparams = t.t_tparams; + typedef_tparams = + ( if equal_typedef_visibility t_vis Transparent then + (* Since type aliases cannot have constraints we shouldn't check + * if its type params satisfy the constraints of any tapply it + * references. + *) + t.t_tparams + else + [] ); tenv; } in diff --git a/hphp/hack/test/typecheck/constraint_newtype_param.php b/hphp/hack/test/typecheck/constraint_newtype_param.php new file mode 100644 index 00000000000..19bef470b3d --- /dev/null +++ b/hphp/hack/test/typecheck/constraint_newtype_param.php @@ -0,0 +1,9 @@ +//// file1.php + {} +newtype B = A; + +//// file2.php + $_): void {} diff --git a/hphp/hack/test/typecheck/constraint_newtype_param.php.exp b/hphp/hack/test/typecheck/constraint_newtype_param.php.exp new file mode 100644 index 00000000000..2d6d51b5f51 --- /dev/null +++ b/hphp/hack/test/typecheck/constraint_newtype_param.php.exp @@ -0,0 +1,8 @@ +File "constraint_newtype_param.php--file1.php", line 3, characters 23-26: +Some type constraint(s) here are violated (Typing[4323]) + File "constraint_newtype_param.php--file1.php", line 2, characters 9-9: + `T` is a constrained type parameter + File "constraint_newtype_param.php--file1.php", line 2, characters 14-17: + Expected `bool` + File "constraint_newtype_param.php--file1.php", line 3, characters 25-25: + But got `T as int` diff --git a/hphp/hack/test/typecheck/constraints/tapply_is_newtype.php b/hphp/hack/test/typecheck/constraints/tapply_is_newtype.php index b8196167047..232de4721c1 100644 --- a/hphp/hack/test/typecheck/constraints/tapply_is_newtype.php +++ b/hphp/hack/test/typecheck/constraints/tapply_is_newtype.php @@ -3,7 +3,7 @@ interface Constraint {} -newtype NewConstraint as Constraint = Constraint; +newtype NewConstraint as Constraint = Constraint; //// file2.php {} // definitions since they cannot put constraints on their type parameters type AliasConstraint = Constraint; -newtype NewConstraint as Constraint = AliasConstraint; +newtype NewConstraint as Constraint = AliasConstraint; diff --git a/hphp/hack/test/typecheck/newtype8.php b/hphp/hack/test/typecheck/newtype8.php index fe06fabb5c0..f70e74319d6 100644 --- a/hphp/hack/test/typecheck/newtype8.php +++ b/hphp/hack/test/typecheck/newtype8.php @@ -3,7 +3,7 @@ newtype FooInt as int = int; newtype FooString as string = string; newtype FooArray as varray = varray; -newtype FooMap as Map = Map; +newtype FooMap as Map = Map; //// test.php