Represent inferred type as an unused type parameter
commitda545d5896d314e92ea22c0add52bbdba5d513e7
authorWilfred Hughes <wilfred@fb.com>
Thu, 20 May 2021 03:29:19 +0000 (19 20:29 -0700)
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
Thu, 20 May 2021 03:30:44 +0000 (19 20:30 -0700)
tree8ea74d708568b44e7ac3aa6e8238e4bbc2673b7d
parent25113185b7228f0ff77e88d27b905789c1ad0af2
Represent inferred type as an unused type parameter

Summary:
The runtime value representing an expression tree has no type information associated with it. The type information is an erased type parameter that is not used in the expression (some languages call this a "phantom type").

For example, an `ExampleLocalVar` might occur in an `ExprTree<Example, ExampleAst, ExampleInt>` or an `ExprTree<Example, ExampleAst, ExampleString>`. The `ExampleLocalVar` will just have the name of the variable at runtime (e.g. `'$x'`).

If a user constructs an `ExprTree` directly, we can require them to annotate the type of their expression.

```
class Example {
  public static function makeTree<<<__Explicit>> TInfer>>(...) { ... }
}

// Construct an AST that represents an integer in the Example DSL.
$et = Example::makeTree<ExampleInt>(...);
```

When we desugar to `makeTree` internally, generate a type parameter and add it to the `makeTree` call.

```
// User writes
$et = Example`1`;

// et_virtualized_expr after desugaring:
Example::intType();
// et_runtime_expr after desugaring (ignoring metadata arguments):
Example::makeTree((Example $v) ==> $v->visitInt(1));

// et_runtime_expr when typechecking (ignoring metadata arguments):
Example::makeTree<_>((Example $v) ==> $v->visitInt(1))
  where _ as type_of_et_virtualized_expr
```

Reviewed By: vassilmladenov

Differential Revision: D27610682

fbshipit-source-id: 3391c69908313374949e142e8b2446ba9ff0860c
46 files changed:
hphp/hack/src/parser/lowerer/desugar_expression_tree.rs
hphp/hack/src/typing/tast_check/rvalue_check.ml
hphp/hack/src/typing/typing.ml
hphp/hack/src/typing/typing_env.ml
hphp/hack/src/typing/typing_env.mli
hphp/hack/test/hhi/expr_tree.hhi
hphp/hack/test/nast/expression_tree.php.exp
hphp/hack/test/nast/expression_tree_assign.php.exp
hphp/hack/test/nast/expression_tree_for.php.exp
hphp/hack/test/nast/expression_tree_loop.php.exp
hphp/hack/test/nast/expression_tree_splice.php.exp
hphp/hack/test/nast/hh_show.php.exp
hphp/hack/test/typecheck/expression_trees/boolean_operators.php.exp
hphp/hack/test/typecheck/expression_trees/consecutive_splicing.php.like_types.exp
hphp/hack/test/typecheck/expression_trees/default_parameter.php.exp
hphp/hack/test/typecheck/expression_trees/default_parameter.php.like_types.exp [copied from hphp/hack/test/typecheck/expression_trees/default_parameter.php.exp with 94% similarity]
hphp/hack/test/typecheck/expression_trees/helper_function_noreturn_bad.php [new file with mode: 0644]
hphp/hack/test/typecheck/expression_trees/helper_function_noreturn_bad.php.exp [new file with mode: 0644]
hphp/hack/test/typecheck/expression_trees/helper_function_noreturn_bad.php.like_types.exp [new file with mode: 0644]
hphp/hack/test/typecheck/expression_trees/helper_function_noreturn_good.php [new file with mode: 0644]
hphp/hack/test/typecheck/expression_trees/helper_function_noreturn_good.php.exp [copied from hphp/hack/test/typecheck/expression_trees/namespace.php.exp with 100% similarity]
hphp/hack/test/typecheck/expression_trees/helper_function_void.php [new file with mode: 0644]
hphp/hack/test/typecheck/expression_trees/helper_function_void.php.exp [copied from hphp/hack/test/typecheck/expression_trees/namespace.php.exp with 100% similarity]
hphp/hack/test/typecheck/expression_trees/helper_function_void2.php [new file with mode: 0644]
hphp/hack/test/typecheck/expression_trees/helper_function_void2.php.exp [new file with mode: 0644]
hphp/hack/test/typecheck/expression_trees/helper_function_void2.php.like_types.exp [new file with mode: 0644]
hphp/hack/test/typecheck/expression_trees/hover_type.php.exp
hphp/hack/test/typecheck/expression_trees/hover_type.php.like_types.exp
hphp/hack/test/typecheck/expression_trees/namespace.php.exp
hphp/hack/test/typecheck/expression_trees/splice.php.like_types.exp
hphp/hack/test/typecheck/expression_trees/splice_err1.php.exp
hphp/hack/test/typecheck/expression_trees/splice_err2.php.exp
hphp/hack/test/typecheck/expression_trees/splice_err2.php.like_types.exp
hphp/hack/test/typecheck/expression_trees/splice_err3.php.exp
hphp/hack/test/typecheck/expression_trees/splice_err3.php.like_types.exp
hphp/hack/test/typecheck/expression_trees/splice_err4.php.exp
hphp/hack/test/typecheck/expression_trees/splice_err4.php.like_types.exp
hphp/hack/test/typecheck/expression_trees/splice_err5.php [new file with mode: 0644]
hphp/hack/test/typecheck/expression_trees/splice_err5.php.exp [new file with mode: 0644]
hphp/hack/test/typecheck/expression_trees/splice_err5.php.like_types.exp [copied from hphp/hack/test/typecheck/expression_trees/splice.php.like_types.exp with 57% similarity]
hphp/hack/test/typecheck/expression_trees/splice_err_closure.php [new file with mode: 0644]
hphp/hack/test/typecheck/expression_trees/splice_err_closure.php.exp [copied from hphp/hack/test/typecheck/expression_trees/namespace.php.exp with 100% similarity]
hphp/hack/test/typecheck/expression_trees/splice_infer.php.like_types.exp
hphp/hack/test/typecheck/expression_trees/void_return_errors.php.exp
hphp/hack/test/typecheck/expression_trees/void_return_errors2.php.exp
hphp/test/slow/expression_trees/expression_tree.inc