Skip to content

Allow '??' on constant expressions #1868

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

Closed
wants to merge 3 commits into from

Conversation

marcioAlmada
Copy link
Contributor

@marcioAlmada marcioAlmada commented Apr 17, 2016

It looks like we forgot to allow the null coalesce operator on constant expressions https://3v4l.org/LoPsg:

<?php

const X = null ?? 2;

function($arg = null ?? 'foo') { return $arg; };

function() { static $var = null ?? 'foo'; return $var; };

new class { private $property = null ?? 'foo'; };

Fatal error: Constant expression contains invalid operations in /in/LoPsg on line 3

Reference https://wiki.php.net/rfc/const_scalar_exprs

zend_fetch_dimension_by_zval(&tmp, &op1, &op2);
if (ast->attr == ZEND_DIM_UNSET) {
/* @TODO fetch index without throwing the notice at Zend/zend_execute.c:1596 */
/* zend_fetch_dimension_address_UNSET(&tmp, &op1, &op2, IS_TMP_VAR); */
Copy link
Contributor Author

@marcioAlmada marcioAlmada Apr 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ is zend_fetch_dimension_address_UNSET the proper way to achieve this here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't that actually be an IS fetch?

@laruence laruence added the RFC label Apr 19, 2016
@marcioAlmada
Copy link
Contributor Author

@laruence should we really require RFC? This fits more in the 'bug' camp to me.

@bwoebi
Copy link
Member

bwoebi commented Apr 19, 2016

Nope, that looks like an oversight; there's definitely no ground for a RFC here.

@laruence laruence added Bug and removed RFC labels Apr 20, 2016
@laruence
Copy link
Member

okey, mark as bug

@marcioAlmada marcioAlmada force-pushed the bug/coalesce-const-expr branch from 99bc5cc to eaecc3e Compare April 20, 2016 22:00
@smalyshev
Copy link
Contributor

What is this useful for? I have hard time seeing for what one would use such thing?

@marcioAlmada
Copy link
Contributor Author

marcioAlmada commented Apr 20, 2016

@smalyshev if const X = A['foo'] ?: 2; already works, why shouldn't const X = A['foo'] ?? 2; work too?

@bwoebi
Copy link
Member

bwoebi commented Apr 20, 2016

@smalyshev constants can be arrays. May be useful in that case.

Also, this is not a question of why but rather of completeness.

@smalyshev
Copy link
Contributor

There should always be a question of why. But constant arrays looks like a good case, those can be defined elsewhere, that makes sense.

@marcioAlmada
Copy link
Contributor Author

@bwoebi should be okay now :)

@php-pulls
Copy link

Comment on behalf of bwoebi at php.net:

Merged via 672a995

@php-pulls php-pulls closed this Apr 20, 2016
@bwoebi
Copy link
Member

bwoebi commented Apr 20, 2016

Wrong commit, meant 9f3eab4...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants