Skip to content

Commit 4d7639a

Browse files
committed
Code review fixes
1 parent 0158e65 commit 4d7639a

File tree

5 files changed

+42
-19
lines changed

5 files changed

+42
-19
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Typed class constants with static in union
3+
--FILE--
4+
<?php
5+
class A {
6+
public const static|B CONST1 = B;
7+
public const static|stdClass|B CONST2 = B;
8+
}
9+
class B {}
10+
define("B", new B());
11+
var_dump(new A());
12+
?>
13+
--EXPECT--
14+
object(A)#2 (0) {
15+
}

Zend/zend_execute.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ static bool zend_check_intersection_for_property_or_class_constant_class_type(
948948

949949
static bool zend_check_and_resolve_property_or_class_constant_class_type(
950950
const zend_class_entry *scope, zend_type member_type, const zend_class_entry *value_ce) {
951-
if (ZEND_TYPE_HAS_LIST(member_type)) {
951+
if (ZEND_TYPE_HAS_LIST(member_type)) {
952952
zend_type *list_type;
953953
if (ZEND_TYPE_IS_INTERSECTION(member_type)) {
954954
return zend_check_intersection_for_property_or_class_constant_class_type(
@@ -968,14 +968,21 @@ if (ZEND_TYPE_HAS_LIST(member_type)) {
968968
return true;
969969
}
970970
} ZEND_TYPE_LIST_FOREACH_END();
971+
972+
if ((ZEND_TYPE_PURE_MASK(member_type) & MAY_BE_STATIC)) {
973+
return value_ce == scope;
974+
}
975+
971976
return false;
972977
}
973-
} else if ((ZEND_TYPE_PURE_MASK(member_type) & MAY_BE_STATIC)) {
974-
return value_ce == scope;
975-
} else {
978+
} else if ((ZEND_TYPE_PURE_MASK(member_type) & MAY_BE_STATIC) && value_ce == scope) {
979+
return true;
980+
} else if (ZEND_TYPE_HAS_NAME(member_type)) {
976981
const zend_class_entry *ce = zend_ce_from_type(scope, &member_type);
977982
return ce && instanceof_function(value_ce, ce);
978983
}
984+
985+
return false;
979986
}
980987

981988
static zend_always_inline bool i_zend_check_property_type(const zend_property_info *info, zval *property, bool strict)

Zend/zend_inheritance.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,13 +1682,15 @@ static zend_always_inline bool check_trait_property_or_constant_value_compatibil
16821682
if (UNEXPECTED(Z_TYPE_P(op1) == IS_CONSTANT_AST)) {
16831683
ZVAL_COPY_OR_DUP(&op1_tmp, op1);
16841684
if (UNEXPECTED(zval_update_constant_ex(&op1_tmp, ce) != SUCCESS)) {
1685+
zval_ptr_dtor(&op1_tmp);
16851686
return false;
16861687
}
16871688
op1 = &op1_tmp;
16881689
}
16891690
if (UNEXPECTED(Z_TYPE_P(op2) == IS_CONSTANT_AST)) {
16901691
ZVAL_COPY_OR_DUP(&op2_tmp, op2);
16911692
if (UNEXPECTED(zval_update_constant_ex(&op2_tmp, ce) != SUCCESS)) {
1693+
zval_ptr_dtor(&op2_tmp);
16921694
return false;
16931695
}
16941696
op2 = &op2_tmp;
@@ -2275,7 +2277,7 @@ static zend_class_entry* find_first_constant_definition(zend_class_entry *ce, ze
22752277
}
22762278
/* }}} */
22772279

2278-
static bool emit_incompatible_trait_constant_error(
2280+
static void emit_incompatible_trait_constant_error(
22792281
zend_class_entry *ce, zend_class_constant *existing_constant, zend_class_constant *trait_constant, zend_string *name,
22802282
zend_class_entry **traits, size_t current_trait
22812283
) {
@@ -2286,8 +2288,6 @@ static bool emit_incompatible_trait_constant_error(
22862288
ZSTR_VAL(name),
22872289
ZSTR_VAL(ce->name)
22882290
);
2289-
2290-
return false;
22912291
}
22922292

22932293
static bool do_trait_constant_check(
@@ -2304,22 +2304,26 @@ static bool do_trait_constant_check(
23042304
zend_class_constant *existing_constant = Z_PTR_P(zv);
23052305

23062306
if ((ZEND_CLASS_CONST_FLAGS(trait_constant) & flags_mask) != (ZEND_CLASS_CONST_FLAGS(existing_constant) & flags_mask)) {
2307-
return emit_incompatible_trait_constant_error(ce, existing_constant, trait_constant, name, traits, current_trait);
2307+
emit_incompatible_trait_constant_error(ce, existing_constant, trait_constant, name, traits, current_trait);
2308+
return false;
23082309
}
23092310

23102311
if (ZEND_TYPE_IS_SET(trait_constant->type) != ZEND_TYPE_IS_SET(existing_constant->type)) {
2311-
return emit_incompatible_trait_constant_error(ce, existing_constant, trait_constant, name, traits, current_trait);
2312+
emit_incompatible_trait_constant_error(ce, existing_constant, trait_constant, name, traits, current_trait);
2313+
return false;
23122314
} else if (ZEND_TYPE_IS_SET(trait_constant->type)) {
23132315
inheritance_status status1 = zend_perform_covariant_type_check(ce, existing_constant->type, traits[current_trait], trait_constant->type);
23142316
inheritance_status status2 = zend_perform_covariant_type_check(traits[current_trait], trait_constant->type, ce, existing_constant->type);
23152317
if (status1 == INHERITANCE_ERROR || status2 == INHERITANCE_ERROR) {
2316-
return emit_incompatible_trait_constant_error(ce, existing_constant, trait_constant, name, traits, current_trait);
2318+
emit_incompatible_trait_constant_error(ce, existing_constant, trait_constant, name, traits, current_trait);
2319+
return false;
23172320
}
23182321
}
23192322

23202323
if (!check_trait_property_or_constant_value_compatibility(ce, &trait_constant->value, &existing_constant->value)) {
23212324
/* There is an existing constant of the same name, and it conflicts with the new one, so let's throw a fatal error */
2322-
return emit_incompatible_trait_constant_error(ce, existing_constant, trait_constant, name, traits, current_trait);
2325+
emit_incompatible_trait_constant_error(ce, existing_constant, trait_constant, name, traits, current_trait);
2326+
return false;
23232327
}
23242328

23252329
/* There is an existing constant which is compatible with the new one, so no need to add it */

ext/opcache/ZendAccelerator.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,9 @@ static void accel_copy_permanent_strings(zend_new_interned_string_func_t new_int
731731
p->key = new_interned_string(p->key);
732732
}
733733
c = (zend_constant*)Z_PTR(p->val);
734+
if (c->name) {
735+
c->name = new_interned_string(c->name);
736+
}
734737
if (Z_TYPE(c->value) == IS_STRING) {
735738
ZVAL_STR(&c->value, new_interned_string(Z_STR(c->value)));
736739
}

ext/reflection/tests/ReflectionClassConstant_basic1.phpt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function reflectClassConstant($base, $constant) {
3131
echo "hasType():\n";
3232
var_dump($constInfo->hasType());
3333
echo "getType():\n";
34-
var_dump($constInfo->getType());
34+
echo $constInfo->getType() ?? "NULL";
3535
echo "\n**********************************\n";
3636
}
3737

@@ -83,9 +83,7 @@ string(21) "/** My Doc comment */"
8383
hasType():
8484
bool(true)
8585
getType():
86-
object(ReflectionNamedType)#3 (0) {
87-
}
88-
86+
bool
8987
**********************************
9088
**********************************
9189
Reflecting on class constant TestClass::PROT
@@ -118,7 +116,6 @@ hasType():
118116
bool(false)
119117
getType():
120118
NULL
121-
122119
**********************************
123120
**********************************
124121
Reflecting on class constant TestClass::PRIV
@@ -151,7 +148,6 @@ hasType():
151148
bool(false)
152149
getType():
153150
NULL
154-
155151
**********************************
156152
**********************************
157153
Reflecting on class constant TestClass::FINAL
@@ -184,7 +180,6 @@ hasType():
184180
bool(false)
185181
getType():
186182
NULL
187-
188183
**********************************
189184
**********************************
190185
Reflecting on class constant TestClass::PRIV
@@ -217,7 +212,6 @@ hasType():
217212
bool(false)
218213
getType():
219214
NULL
220-
221215
**********************************
222216

223217
Fatal error: Uncaught ReflectionException: Constant TestClass::BAD_CONST does not exist in %s:%d

0 commit comments

Comments
 (0)