Skip to content

Commit 0989b70

Browse files
committed
Print more precise warning for unresolved constants
1 parent 6768235 commit 0989b70

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

ext/opcache/ZendAccelerator.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3258,6 +3258,34 @@ static void preload_sort_classes(void *base, size_t count, size_t siz, compare_f
32583258
}
32593259
}
32603260

3261+
static void get_unresolved_initializer(zend_class_entry *ce, const char **kind, const char **name) {
3262+
zend_string *key;
3263+
zend_class_constant *c;
3264+
zend_property_info *prop;
3265+
3266+
*kind = "unknown";
3267+
*name = "";
3268+
3269+
ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->constants_table, key, c) {
3270+
if (Z_TYPE(c->value) == IS_CONSTANT_AST) {
3271+
*kind = "constant ";
3272+
*name = ZSTR_VAL(key);
3273+
}
3274+
} ZEND_HASH_FOREACH_END();
3275+
ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->properties_info, key, prop) {
3276+
zval *val;
3277+
if (prop->flags & ZEND_ACC_STATIC) {
3278+
val = &ce->default_static_members_table[OBJ_PROP_TO_NUM(prop->offset)];
3279+
} else {
3280+
val = &ce->default_properties_table[OBJ_PROP_TO_NUM(prop->offset)];
3281+
}
3282+
if (Z_TYPE_P(val) == IS_CONSTANT_AST) {
3283+
*kind = (prop->flags & ZEND_ACC_STATIC) ? "static property $" : "property $";
3284+
*name = ZSTR_VAL(key);
3285+
}
3286+
} ZEND_HASH_FOREACH_END();
3287+
}
3288+
32613289
static void preload_link(void)
32623290
{
32633291
zval *zv;
@@ -3479,7 +3507,9 @@ static void preload_link(void)
34793507
}
34803508
zend_string_release(key);
34813509
} else if (!(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED)) {
3482-
zend_error(E_WARNING, "Can't preload class %s with unresolved constants at %s:%d", ZSTR_VAL(ce->name), ZSTR_VAL(ce->info.user.filename), ce->info.user.line_start);
3510+
const char *kind, *name;
3511+
get_unresolved_initializer(ce, &kind, &name);
3512+
zend_error(E_WARNING, "Can't preload class %s with unresolved initializer for %s%s at %s:%d", ZSTR_VAL(ce->name), kind, name, ZSTR_VAL(ce->info.user.filename), ce->info.user.line_start);
34833513
} else if (!(ce->ce_flags & ZEND_ACC_PROPERTY_TYPES_RESOLVED)) {
34843514
zend_error(E_WARNING, "Can't preload class %s with unresolved property types at %s:%d", ZSTR_VAL(ce->name), ZSTR_VAL(ce->info.user.filename), ce->info.user.line_start);
34853515
} else {

ext/opcache/tests/preload_004.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ opcache.preload={PWD}/preload_undef_const.inc
1212
var_dump(class_exists('Foo'));
1313
?>
1414
--EXPECTF--
15-
Warning: Can't preload class Foo with unresolved constants at %s:%d in Unknown on line 0
15+
Warning: Can't preload class Foo with unresolved initializer for constant A at %s:%d in Unknown on line 0
1616
bool(false)

ext/opcache/tests/preload_009.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ var_dump(trait_exists('T'));
1313
var_dump(class_exists('Foo'));
1414
?>
1515
--EXPECTF--
16-
Warning: Can't preload class Foo with unresolved constants at %s:%d in Unknown on line 0
16+
Warning: Can't preload class Foo with unresolved initializer for constant C at %s:%d in Unknown on line 0
1717
bool(true)
1818
bool(false)

0 commit comments

Comments
 (0)