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

test: add unexpected allowed expression position showcase #17640

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

xepozz
Copy link

@xepozz xepozz commented Jan 30, 2025

Hi!

I'm playing with PHP's internals and see how throw can be used within any other expr variations.

Realtime example: https://3v4l.org/WkOUr

So, quite interesting. Is it expected behavior?

try {
    (function () {
        return "string" + throw null ?? throw new Exception('return throw null ?? throw new Exception();');
    })();
} catch (Exception $e) {
    var_dump($e->getMessage());
}

try {
    (function () {
        return "string1" . 5 . "ok" / throw throw throw throw new Exception("cool");
    })();
} catch (Exception $e) {
    var_dump($e->getMessage());
}

The result is successful:

string(43) "return throw null ?? throw new Exception();"
string(4) "cool"

@iluuu1994
Copy link
Member

iluuu1994 commented Jan 30, 2025

@xepozz Yes, this is expected behavior. You only get an Error exception when the throw is actually executed. https://3v4l.org/fkSnW

@xepozz
Copy link
Author

xepozz commented Jan 30, 2025

@iluuu1994 Yeap, I understand how it works.

I don't like that throw can be used as this: https://3v4l.org/RqqZK

Looks like we have one more way to do same thing:

  • throw
  • return + throw
  • return + throw + throw + throw ...

As well as the rest operations with the throw operation: https://3v4l.org/j7tO4

@iluuu1994
Copy link
Member

@xepozz throw is an expression since https://wiki.php.net/rfc/throw_expression, and it also accepts arbitrary expressions as its exception argument, so naturally you can combine it in non-sensical ways.

@xepozz
Copy link
Author

xepozz commented Jan 30, 2025

Actually, it does not make such constructions less weird.

Anyway, would you accept the test case? Is there something left from my side?

@iluuu1994
Copy link
Member

Anyway, would you accept the test case? Is there something left from my side?

Sure, expanding the tests is useful, as long as they test something that hasn't been tested before. It's not completely clear to me from the PR what that is.

return "string" + throw null ?? throw new Exception('return throw null ?? throw new Exception();');

What is this test showing? That the throw may occur after +, or the throw (null ?? throw) precedence?

return "string1" . 5 . "ok" / throw throw throw throw new Exception("cool");

Similarly, what is this showing? That throw throw may be used? Then why do we need 4 of them? 🙂

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

Successfully merging this pull request may close these issues.

2 participants