Add an upcast expression
commitaac8ef17ea1784b08f5f8ffd46c5a79606479e1d
authorScott Owens <sowens@fb.com>
Tue, 14 Sep 2021 10:25:47 +0000 (14 03:25 -0700)
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
Tue, 14 Sep 2021 10:27:34 +0000 (14 03:27 -0700)
tree20c39f597695222382a8d6ada2cf6c396898bf88
parent43b486d6f4c7586b17f831e5e99f22ab83fe8d8f
Add an upcast expression

Summary:
Add a new expression `expr upcast hint` that checks that the type of expr is a subtype of hint and the whole expression has the type of the hint. It has no runtime effect. This differs from `as` in the following ways
* No runtime check
* The hint must be a supertype of the expression
* Any type should be allowed in the hint, including classes with erased generics and other non-enforceable types
* The hint type is used as the type of the expression, rather than refining with an intersection of both types

This feature will support sound dynamic. Previously, as dynamic was specially recognised and treated differently than other as. This will remove the need for special treatment and also allow things like converting the type of a `vec<int>` into `vec<dynamic>`, which as cannot support.

The feature is currently marked as unstable, and requires the file-level attribute `<<file:__EnableUnstableFeatures('upcast_expression')>>` to use.

Reviewed By: vsiles

Differential Revision: D30871803

fbshipit-source-id: b748c1fb8bff94bdbcc2839de15486dd145799e6
65 files changed:
hphp/hack/src/annotated_ast/aast.ml
hphp/hack/src/decl/direct_decl_smart_constructors.rs
hphp/hack/src/decl/direct_decl_smart_constructors_generated.rs
hphp/hack/src/facts/facts_smart_constructors_generated.rs
hphp/hack/src/hackfmt/hack_format.ml
hphp/hack/src/hhbc/hhbc_by_ref/emit_expression.rs
hphp/hack/src/ifc/ifc_utils.ml
hphp/hack/src/naming/naming.ml
hphp/hack/src/naming/nast.ml
hphp/hack/src/oxidized/aast_visitor/node_impl_gen.rs
hphp/hack/src/oxidized/aast_visitor/node_mut_impl_gen.rs
hphp/hack/src/oxidized/gen/aast.rs
hphp/hack/src/oxidized/impl_gen/aast_impl_gen.rs
hphp/hack/src/oxidized_by_ref/gen/aast.rs
hphp/hack/src/oxidized_by_ref/nast_visitor/node_impl_gen.rs
hphp/hack/src/parser/core/expression_parser.rs
hphp/hack/src/parser/decl_mode_smart_constructors_generated.rs
hphp/hack/src/parser/flatten_smart_constructors.rs
hphp/hack/src/parser/full_fidelity_operator_generated.ml
hphp/hack/src/parser/full_fidelity_syntax.ml
hphp/hack/src/parser/full_fidelity_syntax_kind.ml
hphp/hack/src/parser/full_fidelity_syntax_type.ml
hphp/hack/src/parser/full_fidelity_token_kind.ml
hphp/hack/src/parser/full_fidelity_validated_syntax.ml
hphp/hack/src/parser/js/full_fidelity_schema.json
hphp/hack/src/parser/lowerer/lowerer.rs
hphp/hack/src/parser/operator.rs
hphp/hack/src/parser/operator_generated.rs
hphp/hack/src/parser/positioned_smart_constructors.rs
hphp/hack/src/parser/readonly_check.rs
hphp/hack/src/parser/rust_parser_errors.rs
hphp/hack/src/parser/schema/full_fidelity_schema.ml
hphp/hack/src/parser/schema/full_fidelity_schema_version_number.rs
hphp/hack/src/parser/schema/operator_schema_definition.ml
hphp/hack/src/parser/schema/schema_definition.ml
hphp/hack/src/parser/schema/token_schema_definition.ml
hphp/hack/src/parser/smart_constructors_generated.rs
hphp/hack/src/parser/smart_constructors_wrappers.rs
hphp/hack/src/parser/syntax_by_ref/syntax_children_iterator_generated.rs
hphp/hack/src/parser/syntax_by_ref/syntax_impl_generated.rs
hphp/hack/src/parser/syntax_by_ref/syntax_serialize_generated.rs
hphp/hack/src/parser/syntax_by_ref/syntax_type_impl_generated.rs
hphp/hack/src/parser/syntax_by_ref/syntax_variant_generated.rs
hphp/hack/src/parser/syntax_generated.rs
hphp/hack/src/parser/syntax_kind.rs
hphp/hack/src/parser/syntax_sig.ml
hphp/hack/src/parser/syntax_smart_constructors_generated.rs
hphp/hack/src/parser/syntax_type.rs
hphp/hack/src/parser/token_kind.rs
hphp/hack/src/parser/verify_smart_constructors_generated.rs
hphp/hack/src/server/serverExtractStandalone.ml
hphp/hack/src/typing/nastInitCheck.ml
hphp/hack/src/typing/tast_check/readonly_check.ml
hphp/hack/src/typing/typing.ml
hphp/hack/src/typing/typing_type_wellformedness.ml
hphp/hack/test/sound_dynamic/typing/upcast.bad.php [new file with mode: 0644]
hphp/hack/test/sound_dynamic/typing/upcast.bad.php.exp [new file with mode: 0644]
hphp/hack/test/sound_dynamic/typing/upcast.good.php [new file with mode: 0644]
hphp/hack/test/sound_dynamic/typing/upcast.good.php.exp [new file with mode: 0644]
hphp/hack/test/typecheck/upcast.php [new file with mode: 0644]
hphp/hack/test/typecheck/upcast.php.exp [new file with mode: 0644]
hphp/hack/test/typecheck/upcast_errors.php [new file with mode: 0644]
hphp/hack/test/typecheck/upcast_errors.php.exp [new file with mode: 0644]
hphp/hack/test/typecheck/upcast_experimental.php [new file with mode: 0644]
hphp/hack/test/typecheck/upcast_experimental.php.exp [new file with mode: 0644]