Skip to content

Commit 65a7822

Browse files
committed
Improve generated names for anonymous classes
In order of preference, the generated name will be: new class extends ParentClass {}; // -> ParentClass@anonymous new class implements FirstInterface, SecondInterface {}; // -> FirstInterface@anonymous new class {}; // -> class@anonymous This is intended to display a more useful class name in error messages and stack traces, and thus make debugging easier.
1 parent 53e527a commit 65a7822

15 files changed

+38
-27
lines changed

Zend/tests/object_types/return_type_in_class.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ $three = new class extends Two {
1818
};
1919
$three->a();
2020
--EXPECTF--
21-
Fatal error: Uncaught TypeError: Return value of class@anonymous::a() must be an object, int returned in %s:13
21+
Fatal error: Uncaught TypeError: Return value of Two@anonymous::a() must be an object, int returned in %s:%d
2222
Stack trace:
23-
#0 %s(16): class@anonymous->a()
23+
#0 %s(%d): Two@anonymous->a()
2424
#1 {main}
2525
thrown in %s on line 13

Zend/tests/object_types/return_type_inheritance_in_class.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ $three = new class extends Two {
1818
};
1919
$three->a();
2020
--EXPECTF--
21-
Fatal error: Uncaught TypeError: Return value of class@anonymous::a() must be an object, int returned in %s:13
21+
Fatal error: Uncaught TypeError: Return value of Two@anonymous::a() must be an object, int returned in %s:%d
2222
Stack trace:
23-
#0 %s(16): class@anonymous->a()
23+
#0 %s(%d): Two@anonymous->a()
2424
#1 {main}
2525
thrown in %s on line 13

Zend/tests/object_types/return_type_inheritance_in_interface.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ $three = new class implements Two {
1818
};
1919
$three->a();
2020
--EXPECTF--
21-
Fatal error: Uncaught TypeError: Return value of class@anonymous::a() must be an object, int returned in %s:13
21+
Fatal error: Uncaught TypeError: Return value of Two@anonymous::a() must be an object, int returned in %s:%d
2222
Stack trace:
23-
#0 %s(16): class@anonymous->a()
23+
#0 %s(%d): Two@anonymous->a()
2424
#1 {main}
2525
thrown in %s on line 13

Zend/tests/temporary_cleaning_013.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,10 @@ caught Exception 12
288288
caught Exception 13
289289
caught Exception 14
290290

291-
Notice: Indirect modification of overloaded element of class@anonymous has no effect in %s on line %d
291+
Notice: Indirect modification of overloaded element of ArrayAccess@anonymous has no effect in %s on line %d
292292
caught Exception 15
293293

294-
Notice: Indirect modification of overloaded element of class@anonymous has no effect in %s on line %d
294+
Notice: Indirect modification of overloaded element of ArrayAccess@anonymous has no effect in %s on line %d
295295
caught Exception 16
296296
caught Exception 17
297297
caught Exception 18

Zend/tests/type_declarations/typed_properties_065.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ offsetSet(1e50)
6161
int(1)
6262
int(0)
6363
int(-1)
64-
Cannot decrement a reference held by property class@anonymous::$foo of type int past its minimal value
64+
Cannot decrement a reference held by property ArrayAccess@anonymous::$foo of type int past its minimal value
6565
integer
66-
Cannot decrement a reference held by property class@anonymous::$foo of type int past its minimal value
66+
Cannot decrement a reference held by property ArrayAccess@anonymous::$foo of type int past its minimal value
6767
integer
68-
Cannot increment a reference held by property class@anonymous::$foo of type int past its maximal value
68+
Cannot increment a reference held by property ArrayAccess@anonymous::$foo of type int past its maximal value
6969
integer
70-
Cannot increment a reference held by property class@anonymous::$foo of type int past its maximal value
70+
Cannot increment a reference held by property ArrayAccess@anonymous::$foo of type int past its maximal value
7171
integer

Zend/zend_compile.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6528,14 +6528,25 @@ void zend_compile_implements(zend_ast *ast) /* {{{ */
65286528
}
65296529
/* }}} */
65306530

6531-
static zend_string *zend_generate_anon_class_name(uint32_t start_lineno) /* {{{ */
6531+
static zend_string *zend_generate_anon_class_name(zend_ast_decl *decl)
65326532
{
65336533
zend_string *filename = CG(active_op_array)->filename;
6534-
zend_string *result = zend_strpprintf(0, "class@anonymous%c%s:%" PRIu32 "$%" PRIx32,
6535-
'\0', ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
6534+
uint32_t start_lineno = decl->start_lineno;
6535+
6536+
/* Use parent or first interface as prefix. */
6537+
zend_string *prefix = ZSTR_KNOWN(ZEND_STR_CLASS);
6538+
if (decl->child[0]) {
6539+
prefix = zend_resolve_const_class_name_reference(decl->child[0], "class name");
6540+
} else if (decl->child[1]) {
6541+
zend_ast_list *list = zend_ast_get_list(decl->child[1]);
6542+
prefix = zend_resolve_const_class_name_reference(list->child[0], "interface name");
6543+
}
6544+
6545+
zend_string *result = zend_strpprintf(0, "%s@anonymous%c%s:%" PRIu32 "$%" PRIx32,
6546+
ZSTR_VAL(prefix), '\0', ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
6547+
zend_string_release(prefix);
65366548
return zend_new_interned_string(result);
65376549
}
6538-
/* }}} */
65396550

65406551
zend_op *zend_compile_class_decl(zend_ast *ast, zend_bool toplevel) /* {{{ */
65416552
{
@@ -6572,7 +6583,7 @@ zend_op *zend_compile_class_decl(zend_ast *ast, zend_bool toplevel) /* {{{ */
65726583

65736584
zend_register_seen_symbol(lcname, ZEND_SYMBOL_CLASS);
65746585
} else {
6575-
name = zend_generate_anon_class_name(decl->start_lineno);
6586+
name = zend_generate_anon_class_name(decl);
65766587
lcname = zend_string_tolower(name);
65776588
}
65786589
lcname = zend_new_interned_string(lcname);

ext/opcache/tests/bug78937_1.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var_dump(foo());
1919
--EXPECTF--
2020
Warning: Can't preload unlinked class Foo: Unknown parent Bar in %spreload_bug78937.inc on line 6
2121

22-
Warning: Can't preload unlinked class class@anonymous: Unknown parent Bar in %spreload_bug78937.inc on line 3
22+
Warning: Can't preload unlinked class Bar@anonymous: Unknown parent Bar in %spreload_bug78937.inc on line 3
2323

2424
Fatal error: Anonymous class wasn't preloaded in %spreload_bug78937.inc on line 3
2525

ext/opcache/tests/bug78937_2.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ var_dump(foo());
2020
--EXPECTF--
2121
Warning: Can't preload unlinked class Foo: Unknown parent Bar in %spreload_bug78937.inc on line 6
2222

23-
Warning: Can't preload unlinked class class@anonymous: Unknown parent Bar in %spreload_bug78937.inc on line 3
24-
object(class@anonymous)#%d (0) {
23+
Warning: Can't preload unlinked class Bar@anonymous: Unknown parent Bar in %spreload_bug78937.inc on line 3
24+
object(Bar@anonymous)#%d (0) {
2525
}

ext/opcache/tests/bug78937_3.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var_dump(foo());
1818
--EXPECTF--
1919
Warning: Can't preload unlinked class Foo: Unknown parent Bar in %spreload_bug78937.inc on line 6
2020

21-
Warning: Can't preload unlinked class class@anonymous: Unknown parent Bar in %spreload_bug78937.inc on line 3
21+
Warning: Can't preload unlinked class Bar@anonymous: Unknown parent Bar in %spreload_bug78937.inc on line 3
2222

2323
Fatal error: Uncaught Error: Class 'Bar' not found in %spreload_bug78937.inc:3
2424
Stack trace:

ext/opcache/tests/bug78937_4.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ var_dump(new Foo);
2020
--EXPECTF--
2121
Warning: Can't preload unlinked class Foo: Unknown parent Bar in %spreload_bug78937.inc on line 6
2222

23-
Warning: Can't preload unlinked class class@anonymous: Unknown parent Bar in %spreload_bug78937.inc on line 3
23+
Warning: Can't preload unlinked class Bar@anonymous: Unknown parent Bar in %spreload_bug78937.inc on line 3
2424

2525
Fatal error: Class foo wasn't preloaded in %spreload_bug78937.inc on line 6

0 commit comments

Comments
 (0)