-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Proposal: Allow reserved keywords in more element declaration names(class, function, etc) #7465
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
Open
TysonAndre
wants to merge
2
commits into
php:master
Choose a base branch
from
TysonAndre:more-element-declarations
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--TEST-- | ||
Test allowing semi-reserved identifiers in more contexts | ||
--FILE-- | ||
<?php | ||
|
||
// When php's syntax rules change to allow keywords in more contexts, | ||
// those keywords will start being forbidden in those specific contexts. | ||
|
||
// Previously not allowed | ||
trait Or { | ||
} | ||
|
||
// Previously not allowed since 8.0 | ||
class Match { | ||
use \Or; | ||
const OR = 123; | ||
} | ||
|
||
final class Readonly { | ||
} | ||
|
||
// Previously not allowed since 7.4 | ||
function fn() { | ||
echo "in fn\n"; | ||
} | ||
\fn(); | ||
var_dump(\Match::OR); | ||
var_dump(get_class(new \Match())); | ||
var_dump(get_class(new \Readonly())); | ||
?> | ||
--EXPECT-- | ||
in fn | ||
int(123) | ||
string(5) "Match" | ||
string(8) "Readonly" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--TEST-- | ||
Callable forbidden as class name since it is also a type hint | ||
--FILE-- | ||
<?php | ||
// Previously this was a thrown ParseError - now it's a fatal compilation error. | ||
class Callable { | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use 'Callable' as class name as it is reserved in %s on line 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems quite confusing to me: https://3v4l.org/ksZdk, global declaration of
echo
function should not be allowed even if it is not a real functionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think we should not allow declaring functions for keywords that can be used in a function-like way. Basically, if you have
function kw() {}
and writingkw();
causes a compile error, that's okay. However, if you havefunction kw() {}
and writingkw();
compiles but will do something other than calling the functionkw()
, then I think we should prevent the declaration.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems easier to remember than using a cut-off date. For global functions, that would be:
INCLUDE|INCLUDE_ONCE|EVAL|REQUIRE|REQUIRE_ONCE| OR | XOR | AND| INSTANCEOF |
NEWnew ('expression')
is allowed |CLONE|EXIT|IF|ELSEIF| ELSE | ENDIF |ECHO| DO |WHILE| ENDWHILE|
FOR| ENDFOR |FOREACH| ENDFOREACH |DECLARE| ENDDECLARE | AS | TRY | CATCH (requires {}) | FINALLY|
THROW|USE| INSTEADOF | GLOBAL | VAR |UNSET|ISSET|EMPTY|CONTINUE(continue (2);) | GOTO|
FUNCTION| CONST |RETURN|PRINT|YIELD|LIST|SWITCH| ENDSWITCH | CASE | DEFAULT |BREAK(break (2);)|
ARRAY| CALLABLE | EXTENDS | IMPLEMENTS | NAMESPACE | TRAIT | INTERFACE | CLASS|
__CLASS__
|__TRAIT__
|__FUNCTION__
|__METHOD__
|__LINE__
|__FILE__
|__DIR__
|__NAMESPACE__
|
FN(\fn() => expr,
confusing in array key) | MATCH (requires {}) | ENUM (already allowed through tokenizer workaround)// semi_reserved:
| STATIC | ABSTRACT | FINAL | PRIVATE | PROTECTED | PUBLIC | READONLY (also see other PR)