Skip to content

Commit a176aff

Browse files
GH-15976: clarify error messages for enum/trait/interface/alias names
Instead of always saying that a name is reserved or deprecated and cannot/should not be used as a class name, take the usage into account and say the name cannot be used as an enum name, trait name, etc. In the process, for class names add a missing "a".
1 parent b2bae73 commit a176aff

File tree

8 files changed

+24
-16
lines changed

8 files changed

+24
-16
lines changed

Zend/tests/gh15976/alias-names.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ class_alias( 'stdClass', 'bool' );
88

99
?>
1010
--EXPECTF--
11-
Deprecated: Using "_" as a class name is deprecated since 8.4 in %salias-names.php on line 3
11+
Deprecated: Using "_" as a type alias is deprecated since 8.4 in %salias-names.php on line 3
1212

13-
Fatal error: Cannot use 'bool' as class name as it is reserved in %salias-names.php on line 4
13+
Fatal error: Cannot use 'bool' as a type alias as it is reserved in %salias-names.php on line 4

Zend/tests/gh15976/class-names.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ class bool {}
1010
--EXPECTF--
1111
Deprecated: Using "_" as a class name is deprecated since 8.4 in %sclass-names.php on line 3
1212

13-
Fatal error: Cannot use 'bool' as class name as it is reserved in %sclass-names.php on line 4
13+
Fatal error: Cannot use 'bool' as a class name as it is reserved in %sclass-names.php on line 4

Zend/tests/gh15976/enum-names.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ enum bool {}
88

99
?>
1010
--EXPECTF--
11-
Deprecated: Using "_" as a class name is deprecated since 8.4 in %senum-names.php on line 3
11+
Deprecated: Using "_" as an enum name is deprecated since 8.4 in %senum-names.php on line 3
1212

13-
Fatal error: Cannot use 'bool' as class name as it is reserved in %senum-names.php on line 4
13+
Fatal error: Cannot use 'bool' as an enum name as it is reserved in %senum-names.php on line 4

Zend/tests/gh15976/interface-names.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ interface bool {}
88

99
?>
1010
--EXPECTF--
11-
Deprecated: Using "_" as a class name is deprecated since 8.4 in %sinterface-names.php on line 3
11+
Deprecated: Using "_" as an interface name is deprecated since 8.4 in %sinterface-names.php on line 3
1212

13-
Fatal error: Cannot use 'bool' as class name as it is reserved in %sinterface-names.php on line 4
13+
Fatal error: Cannot use 'bool' as an interface name as it is reserved in %sinterface-names.php on line 4

Zend/tests/gh15976/trait-names.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ trait bool {}
88

99
?>
1010
--EXPECTF--
11-
Deprecated: Using "_" as a class name is deprecated since 8.4 in %strait-names.php on line 3
11+
Deprecated: Using "_" as a trait name is deprecated since 8.4 in %strait-names.php on line 3
1212

13-
Fatal error: Cannot use 'bool' as class name as it is reserved in %strait-names.php on line 4
13+
Fatal error: Cannot use 'bool' as a trait name as it is reserved in %strait-names.php on line 4

Zend/zend_API.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3571,7 +3571,7 @@ ZEND_API zend_result zend_register_class_alias_ex(const char *name, size_t name_
35713571
zend_str_tolower_copy(ZSTR_VAL(lcname), name, name_len);
35723572
}
35733573

3574-
zend_assert_valid_class_name(lcname);
3574+
zend_assert_valid_class_name(lcname, "a type alias");
35753575

35763576
lcname = zend_new_interned_string(lcname);
35773577

Zend/zend_compile.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,14 @@ static bool zend_is_reserved_class_name(const zend_string *name) /* {{{ */
237237
}
238238
/* }}} */
239239

240-
void zend_assert_valid_class_name(const zend_string *name) /* {{{ */
240+
void zend_assert_valid_class_name(const zend_string *name, const char *type) /* {{{ */
241241
{
242242
if (zend_is_reserved_class_name(name)) {
243243
zend_error_noreturn(E_COMPILE_ERROR,
244-
"Cannot use '%s' as class name as it is reserved", ZSTR_VAL(name));
244+
"Cannot use '%s' as %s as it is reserved", ZSTR_VAL(name), type);
245245
}
246246
if (zend_string_equals_literal(name, "_")) {
247-
zend_error(E_DEPRECATED, "Using \"_\" as a class name is deprecated since 8.4");
247+
zend_error(E_DEPRECATED, "Using \"_\" as %s is deprecated since 8.4", type);
248248
}
249249
}
250250
/* }}} */
@@ -6985,7 +6985,7 @@ static zend_type zend_compile_single_typename(zend_ast *ast)
69856985
uint32_t fetch_type = zend_get_class_fetch_type_ast(ast);
69866986
if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
69876987
class_name = zend_resolve_class_name_ast(ast);
6988-
zend_assert_valid_class_name(class_name);
6988+
zend_assert_valid_class_name(class_name, "a class name");
69896989
} else {
69906990
zend_ensure_valid_class_fetch_type(fetch_type);
69916991
zend_string_addref(class_name);
@@ -8996,7 +8996,15 @@ static void zend_compile_class_decl(znode *result, zend_ast *ast, bool toplevel)
89968996
zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested");
89978997
}
89988998

8999-
zend_assert_valid_class_name(unqualified_name);
8999+
const char *type = "a class name";
9000+
if (decl->flags & ZEND_ACC_ENUM) {
9001+
type = "an enum name";
9002+
} else if (decl->flags & ZEND_ACC_INTERFACE) {
9003+
type = "an interface name";
9004+
} else if (decl->flags & ZEND_ACC_TRAIT) {
9005+
type = "a trait name";
9006+
}
9007+
zend_assert_valid_class_name(unqualified_name, type);
90009008
name = zend_prefix_with_ns(unqualified_name);
90019009
name = zend_new_interned_string(name);
90029010
lcname = zend_string_tolower(name);

Zend/zend_compile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ ZEND_API void zend_set_function_arg_flags(zend_function *func);
10011001

10021002
int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem);
10031003

1004-
void zend_assert_valid_class_name(const zend_string *const_name);
1004+
void zend_assert_valid_class_name(const zend_string *const_name, const char *type);
10051005

10061006
zend_string *zend_type_to_string_resolved(zend_type type, zend_class_entry *scope);
10071007
ZEND_API zend_string *zend_type_to_string(zend_type type);

0 commit comments

Comments
 (0)