Skip to content

Commit f20a08a

Browse files
committed
Removed unnecessary reinsertion of constants into the constant table when a constant with the same name passes the compatibility check.
1 parent 1c9be13 commit f20a08a

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

Zend/zend_inheritance.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,28 +2223,30 @@ static zend_class_entry* find_first_constant_definition(zend_class_entry *ce, ze
22232223

22242224
static bool do_trait_constant_check(zend_class_entry *ce, zend_class_constant *trait_constant, zend_string *name, zend_class_entry **traits, size_t current_trait) /* {{{ */
22252225
{
2226-
bool is_compatible = false;
22272226
uint32_t flags_mask = ZEND_ACC_PPP_MASK | ZEND_ACC_FINAL;
22282227

22292228
zval *zv = zend_hash_find_known_hash(&ce->constants_table, name);
22302229
if (zv == NULL) {
2230+
// No existing constant of the same name, so this one can be added
22312231
return true;
22322232
}
22332233

22342234
zend_class_constant *existing_constant = Z_PTR_P(zv);
22352235

22362236
if ((ZEND_CLASS_CONST_FLAGS(trait_constant) & flags_mask) == (ZEND_CLASS_CONST_FLAGS(existing_constant) & flags_mask)) {
2237-
is_compatible = check_trait_property_or_constant_value_compatibility(ce, &trait_constant->value, &existing_constant->value);
2238-
}
2239-
if (!is_compatible) {
2240-
zend_error_noreturn(E_COMPILE_ERROR,
2241-
"%s and %s define the same constant (%s) in the composition of %s. However, the definition differs and is considered incompatible. Class was composed",
2242-
ZSTR_VAL(find_first_constant_definition(ce, traits, current_trait, name, existing_constant->ce)->name),
2243-
ZSTR_VAL(trait_constant->ce->name),
2244-
ZSTR_VAL(name),
2245-
ZSTR_VAL(ce->name));
2237+
if (check_trait_property_or_constant_value_compatibility(ce, &trait_constant->value, &existing_constant->value)) {
2238+
// There is an existing constant which is compatible with the new one, so no need to add it
2239+
return false;
2240+
}
22462241
}
2247-
return is_compatible;
2242+
2243+
// There is an existing constant of the same name, and it conflicts with the new one, so let's throw a fatal error
2244+
zend_error_noreturn(E_COMPILE_ERROR,
2245+
"%s and %s define the same constant (%s) in the composition of %s. However, the definition differs and is considered incompatible. Class was composed",
2246+
ZSTR_VAL(find_first_constant_definition(ce, traits, current_trait, name, existing_constant->ce)->name),
2247+
ZSTR_VAL(trait_constant->ce->name),
2248+
ZSTR_VAL(name),
2249+
ZSTR_VAL(ce->name));
22482250
}
22492251
/* }}} */
22502252

0 commit comments

Comments
 (0)