Skip to content

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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Zend/tests/extended_identifiers.phpt
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"
10 changes: 10 additions & 0 deletions Zend/tests/grammar/callable_class_forbidden.phpt
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
11 changes: 7 additions & 4 deletions Zend/tests/grammar/regression_004.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
Test possible function naming regression on procedural scope
Test possible function naming regression on procedural scope now allowed
--FILE--
<?php

Expand All @@ -9,7 +9,10 @@ class Obj
function return(){} // valid
}

function echo(){} // not valid
function echo(){print("test\n");} // valid
\echo();
Copy link
Contributor

@mvorisek mvorisek Sep 5, 2021

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 function

Copy link
Member

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 writing kw(); causes a compile error, that's okay. However, if you have function kw() {} and writing kw(); compiles but will do something other than calling the function kw(), then I think we should prevent the declaration.

Copy link
Contributor Author

@TysonAndre TysonAndre Sep 6, 2021

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 | NEW new ('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)

namespace\echo();
?>
--EXPECTF--
Parse error: syntax error, unexpected token "echo", expecting "(" in %s on line %d
--EXPECT--
test
test
9 changes: 6 additions & 3 deletions Zend/tests/grammar/regression_005.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
Test possible constant naming regression on procedural scope
Test possible constant naming regression on procedural scope now allowed
--FILE--
<?php

Expand All @@ -9,6 +9,9 @@ class Obj
}

const return = 'nope';
var_dump(\return);
var_dump(Obj::return);
?>
--EXPECTF--
Parse error: syntax error, unexpected token "return", expecting identifier in %s on line %d
--EXPECT--
string(4) "nope"
string(3) "yep"
2 changes: 1 addition & 1 deletion Zend/tests/lsb_008.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ class static {
}
?>
--EXPECTF--
Parse error: %s error,%sexpecting %s in %s on line %d
Fatal error: Cannot use 'static' as class name as it is reserved in %s on line 2
2 changes: 1 addition & 1 deletion Zend/tests/lsb_009.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ interface static {
}
?>
--EXPECTF--
Parse error: %s error,%sexpecting %s in %s on line %d
Fatal error: Cannot use 'static' as class name as it is reserved in %s on line 2
1 change: 1 addition & 0 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ static const struct reserved_class_name reserved_class_names[] = {
{ZEND_STRL("iterable")},
{ZEND_STRL("object")},
{ZEND_STRL("mixed")},
{ZEND_STRL("callable")},
{NULL, 0}
};

Expand Down
14 changes: 7 additions & 7 deletions Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ unset_variable:
;

function_declaration_statement:
function returns_ref T_STRING backup_doc_comment '(' parameter_list ')' return_type
function returns_ref identifier backup_doc_comment '(' parameter_list ')' return_type
backup_fn_flags '{' inner_statement_list '}' backup_fn_flags
{ $$ = zend_ast_create_decl(ZEND_AST_FUNC_DECL, $2 | $13, $1, $4,
zend_ast_get_str($3), $6, NULL, $11, $8, NULL); CG(extra_fn_flags) = $9; }
Expand All @@ -579,10 +579,10 @@ is_variadic:

class_declaration_statement:
class_modifiers T_CLASS { $<num>$ = CG(zend_lineno); }
T_STRING extends_from implements_list backup_doc_comment '{' class_statement_list '}'
identifier extends_from implements_list backup_doc_comment '{' class_statement_list '}'
{ $$ = zend_ast_create_decl(ZEND_AST_CLASS, $1, $<num>3, $7, zend_ast_get_str($4), $5, $6, $9, NULL, NULL); }
| T_CLASS { $<num>$ = CG(zend_lineno); }
T_STRING extends_from implements_list backup_doc_comment '{' class_statement_list '}'
identifier extends_from implements_list backup_doc_comment '{' class_statement_list '}'
{ $$ = zend_ast_create_decl(ZEND_AST_CLASS, 0, $<num>2, $6, zend_ast_get_str($3), $4, $5, $8, NULL, NULL); }
;

Expand All @@ -599,19 +599,19 @@ class_modifier:

trait_declaration_statement:
T_TRAIT { $<num>$ = CG(zend_lineno); }
T_STRING backup_doc_comment '{' class_statement_list '}'
identifier backup_doc_comment '{' class_statement_list '}'
{ $$ = zend_ast_create_decl(ZEND_AST_CLASS, ZEND_ACC_TRAIT, $<num>2, $4, zend_ast_get_str($3), NULL, NULL, $6, NULL, NULL); }
;

interface_declaration_statement:
T_INTERFACE { $<num>$ = CG(zend_lineno); }
T_STRING interface_extends_list backup_doc_comment '{' class_statement_list '}'
identifier interface_extends_list backup_doc_comment '{' class_statement_list '}'
{ $$ = zend_ast_create_decl(ZEND_AST_CLASS, ZEND_ACC_INTERFACE, $<num>2, $5, zend_ast_get_str($3), NULL, $4, $7, NULL, NULL); }
;

enum_declaration_statement:
T_ENUM { $<num>$ = CG(zend_lineno); }
T_STRING enum_backing_type implements_list backup_doc_comment '{' class_statement_list '}'
identifier enum_backing_type implements_list backup_doc_comment '{' class_statement_list '}'
{ $$ = zend_ast_create_decl(ZEND_AST_CLASS, ZEND_ACC_ENUM|ZEND_ACC_FINAL, $<num>2, $6, zend_ast_get_str($3), NULL, $5, $8, NULL, $4); }
;

Expand Down Expand Up @@ -1033,7 +1033,7 @@ class_const_decl:
;

const_decl:
T_STRING '=' expr backup_doc_comment { $$ = zend_ast_create(ZEND_AST_CONST_ELEM, $1, $3, ($4 ? zend_ast_create_zval_from_str($4) : NULL)); }
identifier '=' expr backup_doc_comment { $$ = zend_ast_create(ZEND_AST_CONST_ELEM, $1, $3, ($4 ? zend_ast_create_zval_from_str($4) : NULL)); }
;

echo_expr_list:
Expand Down