Permalink
Browse files

Prepend __ to "reserved" variable names

Summary:
When generating the closure function, the closure parameters had always included a set number parameters: Global_..._GeneratedClosure $closure, mixed $coroutineData, and ?\Exception $exception_generated. This is a problem because if the function that the developer declares has any variables of those names, they would smash the existing variables, producing errors. For instance:

```
coroutine function variable_names(): int {
  $closure = 1;
  $coroutineResult = suspend SuspendCoroutine::suspend(
    $cont ==> {
      $cont->resume(2);
    }
  );
  return $closure + $coroutineResult;
}
```
produces the following error:
```
Warning: Cannot access property on non-object in test.php on line 9

Catchable fatal error: Argument 1 passed to SuspendCoroutine::suspend() must implement interface InternalCoroutineContinuation, int given in test.php on line 14
```
In order to prevent user defined variables from smashing the generated closure parameters, we will use variable names prepended with `__`. These variable names are typically reserved for HHVM, so the hope is that users will not prepend their own variable names with `__`. If they do, then we will run into the same problems as before.

In this diff, all previous instances of the variable `$closure` are renamed to `$__closure`, including those referenced in the `clone()` methods that we generate. This is not necessary and produces slightly uglier autogenerated code, but it does same us the trouble of having two separate `$closure` syntaxes.

Reviewed By: michaeltingley

Differential Revision: D6739644

fbshipit-source-id: 8acdb7ba2ff21e61e9715b98340b4e242d081dfc
  • Loading branch information...
Thomas Jiang authored and hhvm-bot committed Jan 18, 2018
1 parent 353e053 commit 7904ba4f3ab95cf380724435f28cebcc776a9b80
Showing with 4 additions and 4 deletions.
  1. +4 −4 hphp/hack/src/parser/coroutine/coroutine_syntax.ml
@@ -817,7 +817,7 @@ let make_closure_type_syntax context =
(make_closure_type_arguments context)
let closure_variable =
"$closure"
"$__closure"
(* $closure *)
let closure_variable_syntax =
@@ -858,13 +858,13 @@ let clone_member_name_syntax =
make_name_syntax clone_member_name
let coroutine_data_variable =
"$coroutineData"
"$__coroutineData"
let coroutine_data_variable_syntax =
make_variable_expression_syntax coroutine_data_variable
let coroutine_result_variable =
"$coroutineResult"
"$__coroutineResult"
let coroutine_result_variable_syntax =
make_variable_expression_syntax coroutine_result_variable
@@ -890,7 +890,7 @@ let coroutine_data_parameter_syntax =
coroutine_data_variable
let exception_variable =
"$exception_generated"
"$__exception_generated"
let exception_variable_syntax =
make_variable_expression_syntax exception_variable

0 comments on commit 7904ba4

Please sign in to comment.