Skip to content

Commit d34b877

Browse files
vassilmladenovfacebook-github-bot
authored andcommitted
Support class pointers in enum classes
Summary: This was an oversight by me -- this function is never called on regular enums and the comment was wrong. Implementation is for legacy enums `extends Enum` and enum classes, which is why `enum E: class<C>` never raised the error observed on enum classes (see V1). Some more cleanup is necessary to truly finish this, but for now this fixes enum class's intentional support for class pointers. Reviewed By: viratyosin Differential Revision: D79917193 fbshipit-source-id: d9e2b679dcaf19983eb324c894c187f21b0daff9
1 parent c3bb450 commit d34b877

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

hphp/hack/src/typing/typing_enum.ml

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ let enum_check_const ty_exp env cc t =
9797
* like any, or dynamic. The free status of type parameter is caught during
9898
* naming (Unbound name), so we only check the kind of type that is used.
9999
*)
100-
let enum_check_type env (pos : Pos_or_decl.t) ur ty_interface ty _on_error =
100+
let enum_class_check_type env (pos : Pos_or_decl.t) ur ty_interface ty _on_error
101+
=
101102
let sd env =
102103
(* Allow pessimised enum class types when sound dynamic is enabled *)
103104
TypecheckerOptions.enable_sound_dynamic (Typing_env.get_tcopt env)
@@ -160,9 +161,7 @@ let enum_check_type env (pos : Pos_or_decl.t) ur ty_interface ty _on_error =
160161
| Tlabel _
161162
| Tneg _ ->
162163
false
163-
| Tclass_ptr _ ->
164-
(* TODO(T199606542) deprecate static ban on ::class for enums *)
165-
false
164+
| Tclass_ptr _ -> true
166165
in
167166
let (env, ty_err_opt) =
168167
match ty_interface with
@@ -225,17 +224,7 @@ let check_cyclic_constraint env (p, enum_name) constraint_ty =
225224

226225
(** Check an enum declaration of the form
227226
228-
enum E : <ty_exp> as <ty_constraint>
229-
230-
or
231-
232-
class E extends Enum<ty_exp>
233-
234-
where the absence of `<ty_constraint>` is assumed to default to arraykey.
235-
236-
Check that `<ty_exp>` is int or string, and that
237-
238-
ty_exp <: ty_constraint <: arraykey
227+
enum class E : <ty_exp>
239228
240229
Also that each type constant is of type `ty_exp`. *)
241230
let enum_class_check
@@ -292,7 +281,7 @@ let enum_class_check
292281
Option.iter ~f:(Typing_error_utils.add_typing_error ~env) ty_err2;
293282
(* Check that ty_exp <: arraykey *)
294283
let env =
295-
enum_check_type
284+
enum_class_check_type
296285
env
297286
pos
298287
Reason.URenum_underlying
@@ -311,7 +300,7 @@ let enum_class_check
311300
in
312301
Option.iter ~f:(Typing_error_utils.add_typing_error ~env) ty_err1;
313302
let env =
314-
enum_check_type
303+
enum_class_check_type
315304
env
316305
pos
317306
Reason.URenum_cstr
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?hh
2+
3+
abstract class A {}
4+
class C extends A {}
5+
class D extends A {}
6+
7+
enum BadEnum: class<A> {
8+
C = C::class;
9+
D = D::class;
10+
}
11+
12+
enum class GoodEnumClass: class<A> {
13+
class<A> C = C::class;
14+
class<A> D = D::class;
15+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ERROR: File "enums.php", line 8, characters 7-14:
2+
Using `C::class` in this position will trigger an implicit runtime conversion to string, please use `nameof C` (Typing[4482])
3+
ERROR: File "enums.php", line 9, characters 7-14:
4+
Using `D::class` in this position will trigger an implicit runtime conversion to string, please use `nameof D` (Typing[4482])

0 commit comments

Comments
 (0)