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

Allow null and false as standalone types #7546

Merged
merged 4 commits into from
Apr 8, 2022
Merged

Conversation

Girgias
Copy link
Member

@Girgias Girgias commented Oct 2, 2021

RFC: https://wiki.php.net/rfc/null-standalone-type

Also a drive-by consistency fix for error messages.

@Girgias Girgias added the RFC label Oct 2, 2021
Copy link
Contributor

@TysonAndre TysonAndre left a comment

Choose a reason for hiding this comment

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

The implementation looks good but I had some nits on test cases.

Being able to indicate something is always null would save the need to add redundant @return null phpdoc (and assert that all subclasses (e.g. from mocking libraries) also return null).

It would also allow type checkers such as Phan/Psalm/PHPStan to more aggressively warn about unnecessarily using the return value, impossible type checks, etc.

Zend/tests/type_declarations/typed_properties_110.phpt Outdated Show resolved Hide resolved
--FILE--
<?php

function test1(): null|false {}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Would be useful and help readability to call these and test the expected behavior, and have example implementations where return null; is properly included in the implementation.

Readers of the RFC might be looking at the tests to see how it's used

Warning: Uncaught TypeError: Return value of test1() must be of the type false or null, none returned....

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll add such a test case

@autaut03
Copy link

autaut03 commented Oct 2, 2021

Regarding the motivation:

"However, the opposite isn't true, if the child method returns always null it must still use the original function signature and can only provide this information through documentation. " - and why exactly is this important? What are the real world proper examples that care if child method returns strictly null or not?

"The inability to type the unit type in PHP is a deficiency which should be resolved. " - again, why should it be resolved? What's the reasoning? You can't type "true" either, there aren't any literal types except for "false". And imho, that's a good thing.

@Danack
Copy link
Contributor

Danack commented Oct 2, 2021

Hi @autaut03 PRs discussions are for detailed technical aspects of the code in the PR. Wider discussions of the RFC being good or bad should be on the internals mailing list.

@Girgias Girgias changed the title Allow 'null' as a standalone type Allow null and false as standalone types Feb 20, 2022
@Girgias Girgias force-pushed the null-standalone branch 2 times, most recently from 7936168 to 1041d2a Compare March 19, 2022 14:19
Zend/tests/type_declarations/standalone_null.phpt Outdated Show resolved Hide resolved
Zend/zend_compile.c Outdated Show resolved Hide resolved
Zend/zend_compile.c Outdated Show resolved Hide resolved
@Girgias Girgias merged commit 6039c07 into php:master Apr 8, 2022
@Girgias Girgias deleted the null-standalone branch April 8, 2022 16:23
nicolas-grekas added a commit to symfony/symfony that referenced this pull request May 10, 2022
…n type on PHP 8.2 (alexandre-daubois)

This PR was merged into the 4.4 branch.

Discussion
----------

[Serializer][PropertyInfo] Fix support for "false" built-in type on PHP 8.2

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes (new language feature support)
| New feature?  | no
| Deprecations? | no
| Tickets       | _NA_
| License       | MIT
| Doc PR        | _NA_

`false` and `null` built-in types have been merged in PHP 8.2 a few hours ago: php/php-src#7546 🎉
RFC: https://wiki.php.net/rfc/null-false-standalone-types

PropertyInfo and Serializer both need an update to support them.

Commits
-------

4187d3f [Serializer][PropertyInfo] Fix support for "false" built-in type on PHP 8.2
symfony-splitter pushed a commit to symfony/serializer that referenced this pull request May 10, 2022
…n type on PHP 8.2 (alexandre-daubois)

This PR was merged into the 4.4 branch.

Discussion
----------

[Serializer][PropertyInfo] Fix support for "false" built-in type on PHP 8.2

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes (new language feature support)
| New feature?  | no
| Deprecations? | no
| Tickets       | _NA_
| License       | MIT
| Doc PR        | _NA_

`false` and `null` built-in types have been merged in PHP 8.2 a few hours ago: php/php-src#7546 🎉
RFC: https://wiki.php.net/rfc/null-false-standalone-types

PropertyInfo and Serializer both need an update to support them.

Commits
-------

4187d3fddd [Serializer][PropertyInfo] Fix support for "false" built-in type on PHP 8.2
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.

7 participants