Skip to content

Commit b9f7123

Browse files
authored
Check at compile time that a built-in class is not being aliased (#9402)
If one tries to use such an alias as a type declaration the following error would be raised: Fatal error: Cannot use 'int' as class name as it is reserved
1 parent 396b2aa commit b9f7123

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
--TEST--
2+
Aliasing built-in types
3+
--FILE--
4+
<?php
5+
6+
/* Taken from zend_compile.c reserved_class_names[] struct */
7+
/* array and list are also added as they are language constructs */
8+
$types = [
9+
"bool",
10+
"false",
11+
"float",
12+
"int",
13+
"null",
14+
"parent",
15+
"self",
16+
"static",
17+
"string",
18+
"true",
19+
"void",
20+
"never",
21+
"iterable",
22+
"object",
23+
"mixed",
24+
"array",
25+
"list",
26+
];
27+
28+
foreach ($types as $type) {
29+
try {
30+
eval("use $type as A;");
31+
} catch (\Throwable $e) {
32+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
33+
}
34+
try {
35+
eval("use $type as A; function foo$type(A \$v): A { return \$v; }");
36+
} catch (\Throwable $e) {
37+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
38+
}
39+
}
40+
41+
?>
42+
DONE
43+
--EXPECT--
44+
CompileError: Cannot alias 'bool' as it is a built-in type
45+
CompileError: Cannot alias 'bool' as it is a built-in type
46+
CompileError: Cannot alias 'false' as it is a built-in type
47+
CompileError: Cannot alias 'false' as it is a built-in type
48+
CompileError: Cannot alias 'float' as it is a built-in type
49+
CompileError: Cannot alias 'float' as it is a built-in type
50+
CompileError: Cannot alias 'int' as it is a built-in type
51+
CompileError: Cannot alias 'int' as it is a built-in type
52+
CompileError: Cannot alias 'null' as it is a built-in type
53+
CompileError: Cannot alias 'null' as it is a built-in type
54+
CompileError: Cannot alias 'parent' as it is a built-in type
55+
CompileError: Cannot alias 'parent' as it is a built-in type
56+
CompileError: Cannot alias 'self' as it is a built-in type
57+
CompileError: Cannot alias 'self' as it is a built-in type
58+
ParseError: syntax error, unexpected token "static"
59+
ParseError: syntax error, unexpected token "static"
60+
CompileError: Cannot alias 'string' as it is a built-in type
61+
CompileError: Cannot alias 'string' as it is a built-in type
62+
CompileError: Cannot alias 'true' as it is a built-in type
63+
CompileError: Cannot alias 'true' as it is a built-in type
64+
CompileError: Cannot alias 'void' as it is a built-in type
65+
CompileError: Cannot alias 'void' as it is a built-in type
66+
CompileError: Cannot alias 'never' as it is a built-in type
67+
CompileError: Cannot alias 'never' as it is a built-in type
68+
CompileError: Cannot alias 'iterable' as it is a built-in type
69+
CompileError: Cannot alias 'iterable' as it is a built-in type
70+
CompileError: Cannot alias 'object' as it is a built-in type
71+
CompileError: Cannot alias 'object' as it is a built-in type
72+
CompileError: Cannot alias 'mixed' as it is a built-in type
73+
CompileError: Cannot alias 'mixed' as it is a built-in type
74+
ParseError: syntax error, unexpected token "array"
75+
ParseError: syntax error, unexpected token "array"
76+
ParseError: syntax error, unexpected token "list"
77+
ParseError: syntax error, unexpected token "list"
78+
DONE

Zend/zend_compile.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8063,6 +8063,13 @@ static void zend_compile_use(zend_ast *ast) /* {{{ */
80638063
zend_string *old_name = zend_ast_get_str(old_name_ast);
80648064
zend_string *new_name, *lookup_name;
80658065

8066+
/* Check that we are not attempting to alias a built-in type */
8067+
if (type == ZEND_SYMBOL_CLASS && zend_is_reserved_class_name(old_name)) {
8068+
zend_throw_exception_ex(zend_ce_compile_error, 0,
8069+
"Cannot alias '%s' as it is a built-in type", ZSTR_VAL(old_name));
8070+
return;
8071+
}
8072+
80668073
if (new_name_ast) {
80678074
new_name = zend_string_copy(zend_ast_get_str(new_name_ast));
80688075
} else {

0 commit comments

Comments
 (0)