From cf3f1a96e1e3a5d9d2e4b29c39997390333582cf Mon Sep 17 00:00:00 2001 From: James Wu Date: Thu, 14 Jul 2022 11:31:54 -0700 Subject: [PATCH] Implement new module type alias visibility in the typechecker Summary: This diff adds a new OpaqueModule visibility for typedefs. It does not make use of the visibility in the diff. In the next diff, we will lower and declare code that uses module newtypes properly. Reviewed By: hgoldstein Differential Revision: D37467892 fbshipit-source-id: 16fd323b4b87c97ab20adcfbe1b5ce0f8745cb51 --- hphp/hack/src/annotated_ast/aast_defs.ml | 1 + .../oxidized/aast_visitor/node_impl_gen.rs | 3 ++- .../aast_visitor/node_mut_impl_gen.rs | 3 ++- hphp/hack/src/oxidized/gen/aast_defs.rs | 3 ++- .../oxidized/impl_gen/aast_defs_impl_gen.rs | 11 +++++++++- .../decl_visitor/node_impl_gen.rs | 3 ++- .../src/server/serverExtractStandalone.ml | 4 +++- hphp/hack/src/typing/typing_env.ml | 21 +++++++++++-------- .../src/typing/typing_type_wellformedness.ml | 4 +++- .../write_symbol_info/symbol_add_fact.ml | 4 +++- 10 files changed, 40 insertions(+), 17 deletions(-) diff --git a/hphp/hack/src/annotated_ast/aast_defs.ml b/hphp/hack/src/annotated_ast/aast_defs.ml index 84247b1a6cc8d0..2a68b2c84fb061 100644 --- a/hphp/hack/src/annotated_ast/aast_defs.ml +++ b/hphp/hack/src/annotated_ast/aast_defs.ml @@ -188,6 +188,7 @@ and vc_kind = and typedef_visibility = | Transparent | Opaque + | OpaqueModule and enum_ = { e_base: hint; diff --git a/hphp/hack/src/oxidized/aast_visitor/node_impl_gen.rs b/hphp/hack/src/oxidized/aast_visitor/node_impl_gen.rs index 2856407b492092..f4acc1ba8e95b7 100644 --- a/hphp/hack/src/oxidized/aast_visitor/node_impl_gen.rs +++ b/hphp/hack/src/oxidized/aast_visitor/node_impl_gen.rs @@ -3,7 +3,7 @@ // This source code is licensed under the MIT license found in the // LICENSE file in the "hack" directory of this source tree. // -// @generated SignedSource<<4663f33dc340eac94f007d08add7615d>> +// @generated SignedSource<<47475db39c74d793be28e5a2c9df1ac1>> // // To regenerate this file, run: // hphp/hack/src/oxidized_regen.sh @@ -2044,6 +2044,7 @@ impl Node

for TypedefVisibility { match self { TypedefVisibility::Transparent => Ok(()), TypedefVisibility::Opaque => Ok(()), + TypedefVisibility::OpaqueModule => Ok(()), } } } diff --git a/hphp/hack/src/oxidized/aast_visitor/node_mut_impl_gen.rs b/hphp/hack/src/oxidized/aast_visitor/node_mut_impl_gen.rs index 0ee8614131bd4e..b5a704f374f4b7 100644 --- a/hphp/hack/src/oxidized/aast_visitor/node_mut_impl_gen.rs +++ b/hphp/hack/src/oxidized/aast_visitor/node_mut_impl_gen.rs @@ -3,7 +3,7 @@ // This source code is licensed under the MIT license found in the // LICENSE file in the "hack" directory of this source tree. // -// @generated SignedSource<> +// @generated SignedSource<<30e561f4665ce11cc087fb2b94571bf9>> // // To regenerate this file, run: // hphp/hack/src/oxidized_regen.sh @@ -2044,6 +2044,7 @@ impl NodeMut

for TypedefVisibility { match self { TypedefVisibility::Transparent => Ok(()), TypedefVisibility::Opaque => Ok(()), + TypedefVisibility::OpaqueModule => Ok(()), } } } diff --git a/hphp/hack/src/oxidized/gen/aast_defs.rs b/hphp/hack/src/oxidized/gen/aast_defs.rs index ba913c474bc3d6..c3d97ffc713685 100644 --- a/hphp/hack/src/oxidized/gen/aast_defs.rs +++ b/hphp/hack/src/oxidized/gen/aast_defs.rs @@ -3,7 +3,7 @@ // This source code is licensed under the MIT license found in the // LICENSE file in the "hack" directory of this source tree. // -// @generated SignedSource<> +// @generated SignedSource<<5dc75b2deb593ef70fb33420b9fe9e6e>> // // To regenerate this file, run: // hphp/hack/src/oxidized_regen.sh @@ -510,6 +510,7 @@ arena_deserializer::impl_deserialize_in_arena!(VcKind); pub enum TypedefVisibility { Transparent, Opaque, + OpaqueModule, } impl TrivialDrop for TypedefVisibility {} arena_deserializer::impl_deserialize_in_arena!(TypedefVisibility); diff --git a/hphp/hack/src/oxidized/impl_gen/aast_defs_impl_gen.rs b/hphp/hack/src/oxidized/impl_gen/aast_defs_impl_gen.rs index a1ecf17f2c07fc..47bc4e3aa09c9b 100644 --- a/hphp/hack/src/oxidized/impl_gen/aast_defs_impl_gen.rs +++ b/hphp/hack/src/oxidized/impl_gen/aast_defs_impl_gen.rs @@ -3,7 +3,7 @@ // This source code is licensed under the MIT license found in the // LICENSE file in the "hack" directory of this source tree. // -// @generated SignedSource<<59a153c855ab440abb144288231d63e7>> +// @generated SignedSource<> // // To regenerate this file, run: // hphp/hack/src/oxidized_regen.sh @@ -946,6 +946,9 @@ impl TypedefVisibility { pub fn mk_opaque() -> Self { TypedefVisibility::Opaque } + pub fn mk_opaque_module() -> Self { + TypedefVisibility::OpaqueModule + } pub fn is_transparent(&self) -> bool { match self { TypedefVisibility::Transparent => true, @@ -958,6 +961,12 @@ impl TypedefVisibility { _ => false, } } + pub fn is_opaque_module(&self) -> bool { + match self { + TypedefVisibility::OpaqueModule => true, + _ => false, + } + } } impl ReifyKind { pub fn mk_erased() -> Self { diff --git a/hphp/hack/src/oxidized_by_ref/decl_visitor/node_impl_gen.rs b/hphp/hack/src/oxidized_by_ref/decl_visitor/node_impl_gen.rs index 67024cae8f36a2..ccd8cdadf5bc1f 100644 --- a/hphp/hack/src/oxidized_by_ref/decl_visitor/node_impl_gen.rs +++ b/hphp/hack/src/oxidized_by_ref/decl_visitor/node_impl_gen.rs @@ -3,7 +3,7 @@ // This source code is licensed under the MIT license found in the // LICENSE file in the "hack" directory of this source tree. // -// @generated SignedSource<> +// @generated SignedSource<> // // To regenerate this file, run: // hphp/hack/src/oxidized_regen.sh @@ -1163,6 +1163,7 @@ impl<'a> Node<'a> for TypedefVisibility { match self { TypedefVisibility::Transparent => {} TypedefVisibility::Opaque => {} + TypedefVisibility::OpaqueModule => {} } } } diff --git a/hphp/hack/src/server/serverExtractStandalone.ml b/hphp/hack/src/server/serverExtractStandalone.ml index 7b3bf36b74b8e8..88c50959f20014 100644 --- a/hphp/hack/src/server/serverExtractStandalone.ml +++ b/hphp/hack/src/server/serverExtractStandalone.ml @@ -1340,7 +1340,9 @@ end = struct let pp_typedef_visiblity ppf = function | Aast_defs.Transparent -> Fmt.string ppf "type" - | Aast_defs.Opaque -> Fmt.string ppf "newtype" + | Aast_defs.Opaque + | Aast_defs.OpaqueModule -> + Fmt.string ppf "newtype" let pp_fixme ppf code = Fmt.pf ppf "/* HH_FIXME[%d] */@." code diff --git a/hphp/hack/src/typing/typing_env.ml b/hphp/hack/src/typing/typing_env.ml index c787807a152dfd..1de309c152b1ed 100644 --- a/hphp/hack/src/typing/typing_env.ml +++ b/hphp/hack/src/typing/typing_env.ml @@ -604,6 +604,14 @@ let env_with_constructor_droot_member env = let decl_env = { env.decl_env with droot_member = Some member } in { env with decl_env } +let set_module env m = { env with genv = { env.genv with this_module = m } } + +let get_module env = Option.map env.genv.this_module ~f:snd + +let set_internal env b = { env with genv = { env.genv with this_internal = b } } + +let get_internal env = env.genv.this_internal + let get_typedef env x = let res = Decl_provider.get_typedef @@ -623,13 +631,16 @@ let is_typedef env x = | _ -> false let is_typedef_visible env ?(expand_visible_newtype = true) td = - let { Typing_defs.td_vis; td_pos; _ } = td in + let { Typing_defs.td_vis; td_pos; td_module; _ } = td in match td_vis with | Aast.Opaque -> expand_visible_newtype && Relative_path.equal (Pos.filename (Pos_or_decl.unsafe_to_raw_pos td_pos)) (get_file env) + | Aast.OpaqueModule -> + expand_visible_newtype + && Option.equal String.equal (get_module env) (Option.map td_module ~f:snd) | Aast.Transparent -> true let get_class (env : env) (name : Decl_provider.type_key) : Cls.t option = @@ -961,14 +972,6 @@ let set_fn_kind env fn_type = let genv = { genv with fun_kind = fn_type } in { env with genv } -let set_module env m = { env with genv = { env.genv with this_module = m } } - -let get_module env = Option.map env.genv.this_module ~f:snd - -let set_internal env b = { env with genv = { env.genv with this_internal = b } } - -let get_internal env = env.genv.this_internal - let set_support_dynamic_type env b = { env with genv = { env.genv with this_support_dynamic_type = b } } diff --git a/hphp/hack/src/typing/typing_type_wellformedness.ml b/hphp/hack/src/typing/typing_type_wellformedness.ml index 3ba371dcb34e67..5e98365ac0e472 100644 --- a/hphp/hack/src/typing/typing_type_wellformedness.ml +++ b/hphp/hack/src/typing/typing_type_wellformedness.ml @@ -430,7 +430,9 @@ let typedef tenv t = * references. *) t.t_tparams - | Opaque -> []); + | Opaque + | OpaqueModule -> + []); tenv; } in diff --git a/hphp/hack/src/typing/write_symbol_info/symbol_add_fact.ml b/hphp/hack/src/typing/write_symbol_info/symbol_add_fact.ml index 31a6a23bbc4b09..3d70ce66b76159 100644 --- a/hphp/hack/src/typing/write_symbol_info/symbol_add_fact.ml +++ b/hphp/hack/src/typing/write_symbol_info/symbol_add_fact.ml @@ -421,7 +421,9 @@ let typedef_defn ctx source_text elem decl_id progress = let is_transparent = match elem.t_vis with | Transparent -> true - | Opaque -> false + | Opaque + | OpaqueModule -> + false in let tparams = List.map