Skip to content

[RFC] Implement match blocks #11933

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

Conversation

iluuu1994
Copy link
Member

@iluuu1994 iluuu1994 commented Aug 10, 2023

https://wiki.php.net/rfc/match_blocks

  • Make throw statements non-terminators in match blocks. Alternatively, [WIP] Auto-extend live-ranges #11880.
  • We should probably allow blocks with no result when the match result isn't used.
  • Syntax bikeshedding. The omitted ; is subtle. We could do <= $value or some other symbol. The downside is that people may expect this to work anywhere in the block, which is currently not the case. The result must appear at the end of the block. Edit: I've settled on <- and => for now. I picked <- over <= because ligatures often render <= as ≤.

This is a much simpler alternative to master...iluuu1994:php-src:match-blocks-var-tracking, which allowed return, break, continue, and goto out of match expression blocks. This PR disallows escaping match blocks whose result value is used. This avoids tracking VARs that may have to be freed if an expression is escaped midway.

@iluuu1994 iluuu1994 changed the title [RFC] Implement match blocks (WIP) [RFC] Implement match blocks Aug 24, 2023
});
?>
--EXPECTF--
Fatal error: 'goto' into loop or switch statement is disallowed in %s on line %d
Copy link
Member

Choose a reason for hiding this comment

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

The error message should mention “match”.

--FILE--
<?php
$x = null;
$y = $x ?? {
Copy link

Choose a reason for hiding this comment

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

Is

$x = {
  echo "test\n";
  null
};

also possible?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, not in this PR. Please read the RFC, the rationale is explained there. https://wiki.php.net/rfc/match_blocks#why_not_language-level_blocks

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

Successfully merging this pull request may close these issues.

3 participants