Permalink
Browse files

method calls, inout, references

Summary:
This implements calling methods on a dynamic type.

inout parameters become dynamic upon getting called by a dynamic, as do reference types.

The same behavior occurs for the Any type, since those can be modified as well in a function that takes TAny.

Reviewed By: dlreeves

Differential Revision: D6764204

fbshipit-source-id: a10d1a2170d842840923a560797a61639d14446b
  • Loading branch information...
jamesjwu authored and hhvm-bot committed Feb 28, 2018
1 parent fcfef1c commit a7dbcac43391abd63611afaa293970aa6025b574
@@ -1,6 +1,6 @@
(* @generated from nast.src.ml by hphp/hack/tools/ppx/facebook:generate_ppx *)
(* Copyright (c) 2004-present, Facebook, Inc. All rights reserved. *)
(* SourceShasum<<62d52bec361a362a46dcd7be0dd835d1595471fa>> *)
(* SourceShasum<<59267a2e38de5dd8c4eff8ab604b81871d432aa1>> *)
(* DO NOT EDIT MANUALLY. *)
[@@@ocaml.text
@@ -1804,7 +1804,6 @@ and expr_
tel,
[])) (Env.fresh_type())
| Call (call_type, e, hl, el, uel) ->
let env, te, ty = check_call ~is_using_clause ~expected env p call_type e hl el uel ~in_suspend:false in
Typing_mutability.enforce_mutable_call env te;
env, te, ty
@@ -4874,19 +4873,19 @@ and param_modes { fp_pos; fp_kind; _ } (pos, e) =
Errors.inout_annotation_missing pos fp_pos
and inout_write_back env { fp_type; _ } (_, e) =
match e with
| Callconv (Ast.Pinout, e1) ->
(* Translate the write-back semantics of inout parameters.
*
* This matters because we want to:
* (1) make sure we can write to the original argument
* (modifiable lvalue check)
* (2) allow for growing of locals / Tunresolveds (type side effect)
* but otherwise unify the argument type with the parameter hint
*)
let env, _te, _ty = assign_ (fst e1) Reason.URparam_inout env e1 fp_type in
env
| _ -> env
match e with
| Callconv (Ast.Pinout, e1) ->
(* Translate the write-back semantics of inout parameters.
*
* This matters because we want to:
* (1) make sure we can write to the original argument
* (modifiable lvalue check)
* (2) allow for growing of locals / Tunresolveds (type side effect)
* but otherwise unify the argument type with the parameter hint
*)
let env, _te, _ty = assign_ (fst e1) Reason.URparam_inout env e1 fp_type in
env
| _ -> env
and call ~expected ?receiver_type pos env fty el uel =
let env, tel, tuel, ty = call_ ~expected ~receiver_type pos env fty el uel in
@@ -4911,6 +4910,15 @@ and call_ ~expected ~receiver_type pos env fty el uel =
let env, te, arg_ty =
expr ~expected:(pos, Reason.URparam, (Reason.Rnone, Tany)) ~is_func_arg:true env elt
in
let env =
match elt with
| _, Callconv (Ast.Pinout, e1) ->
let env, _te, _ty = assign_ (fst e1) Reason.URparam_inout env e1 efty in
env
| _, Unop (Ast.Uref, e1) ->
let env, _te, _ty = assign_ (fst e1) Reason.URparam env e1 efty in
env
| _ -> env in
let env, _arg_ty = check_valid_rvalue pos env arg_ty in
env, te
end in
@@ -0,0 +1,14 @@
<?hh // strict
function testObjGet(dynamic $x): void {
$z = 5;
hh_show($z);
$y = $x(inout $z);
hh_show($y);
hh_show($z);
$w = "5";
hh_show($w);
$y = $x->method(&$w);
hh_show($y);
hh_show($w);
}
@@ -0,0 +1,13 @@
File "inout.php", line 5, characters 3-13:
int
File "inout.php", line 7, characters 3-13:
dynamic
File "inout.php", line 8, characters 3-13:
dynamic
File "inout.php", line 10, characters 3-13:
string
File "inout.php", line 12, characters 3-13:
dynamic
File "inout.php", line 13, characters 3-13:
dynamic
No errors

0 comments on commit a7dbcac

Please sign in to comment.