Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax error: Cannot create array of lambdas in member variables #6364

Closed
SiebelsTim opened this issue Oct 13, 2015 · 4 comments
Closed

Syntax error: Cannot create array of lambdas in member variables #6364

SiebelsTim opened this issue Oct 13, 2015 · 4 comments

Comments

@SiebelsTim
Copy link
Contributor

https://3v4l.org/Or9LN

<?hh
$x = [$x ==> $x + 1];
var_dump($x[0](1)); // outputs 2

https://3v4l.org/PLRkL

<?hh

class Test {
  private $x = [$x ==> $x + 1]; // Syntax error
}

The typechecker is fine with it, the runtime not. This doesn't work with collections either.

@SiebelsTim SiebelsTim changed the title Syntax error: Cannot create array of lambda in class context Syntax error: Cannot create array of lambdas in member variables Oct 13, 2015
@jwatzman
Copy link
Contributor

Weird -- thanks for the report.

@smeenai
Copy link
Contributor

smeenai commented Jan 11, 2016

hh_client now gives an error for the second piece of code:

Initialization of class property must be a static literal expression. (Typing[4152])

Would it still be considered a bug in the parser, in this case?

@fredemmott
Copy link
Contributor

#4277

@lexidor
Copy link
Collaborator

lexidor commented May 24, 2020

I am going over old issues on this repository, to see which ones apply to the current versions of hhvm.

The new behavior (must be a static literal expression) is valid. Hhvm does not (nor PHP) allow things that can not be known statically to appear as constant initializers. A ==> style lambda is very similar to a function() use () {} at runtime. This requires the creation of a Closure object. The [] is not required to trigger this behavior.

Footnote: The legacy Hack Collections were an exception to this rule.

class Test {
    private $q = (
        function(int $a) {
            return $a;
        }
    );
}

This code is invalid for the same reason.
PHP: PHP Fatal error: Constant expression contains invalid operations
HHVM: Fatal error: Uncaught Error: Invalid expression in constant initializer

@lexidor lexidor closed this as completed May 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants