Skip to content

Commit fb5bfcb

Browse files
committed
Add a ZEND_UNCOMPARABLE value
To explicitly indicate that objects are uncomparable. For now this has no functional difference from the usual 1 return value, but makes intent clearer.
1 parent bef4b2e commit fb5bfcb

File tree

9 files changed

+17
-14
lines changed

9 files changed

+17
-14
lines changed

Zend/zend_object_handlers.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,7 +1617,7 @@ ZEND_API int zend_std_compare_objects(zval *o1, zval *o2) /* {{{ */
16171617
return ret;
16181618
}
16191619
}
1620-
return 1;
1620+
return ZEND_UNCOMPARABLE;
16211621
}
16221622

16231623
zobj1 = Z_OBJ_P(o1);
@@ -1627,7 +1627,7 @@ ZEND_API int zend_std_compare_objects(zval *o1, zval *o2) /* {{{ */
16271627
return 0; /* the same object */
16281628
}
16291629
if (zobj1->ce != zobj2->ce) {
1630-
return 1; /* different classes */
1630+
return ZEND_UNCOMPARABLE; /* different classes */
16311631
}
16321632
if (!zobj1->properties && !zobj2->properties) {
16331633
zend_property_info *info;

Zend/zend_operators.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,12 @@ static zend_always_inline int i_zend_is_true(zval *op)
401401
return result;
402402
}
403403

404+
/* Indicate that two values cannot be compared. This value should be returned for both orderings
405+
* of the operands. This implies that all of ==, <, <= and >, >= will return false, because we
406+
* canonicalize >/>= to </<= with swapped operands. */
407+
// TODO: Use a different value to allow an actual distinction here.
408+
#define ZEND_UNCOMPARABLE 1
409+
404410
ZEND_API int ZEND_FASTCALL zend_compare(zval *op1, zval *op2);
405411

406412
ZEND_API int ZEND_FASTCALL compare_function(zval *result, zval *op1, zval *op2);

ext/date/php_date.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,7 +1903,7 @@ static int date_object_compare_date(zval *d1, zval *d2) /* {{{ */
19031903

19041904
if (!o1->time || !o2->time) {
19051905
php_error_docref(NULL, E_WARNING, "Trying to compare an incomplete DateTime or DateTimeImmutable object");
1906-
return 1;
1906+
return ZEND_UNCOMPARABLE;
19071907
}
19081908
if (!o1->time->sse_uptodate) {
19091909
timelib_update_ts(o1->time, o1->time->tz_info);
@@ -2040,7 +2040,7 @@ static int date_object_compare_timezone(zval *tz1, zval *tz2) /* {{{ */
20402040

20412041
if (o1->type != o2->type) {
20422042
php_error_docref(NULL, E_WARNING, "Trying to compare different kinds of DateTimeZone objects");
2043-
return 1;
2043+
return ZEND_UNCOMPARABLE;
20442044
}
20452045

20462046
switch (o1->type) {
@@ -3901,7 +3901,7 @@ static int date_interval_compare_objects(zval *o1, zval *o2) {
39013901
* smaller, equal or greater depending on the point in time at which the interval starts. As
39023902
* such, we treat DateInterval objects are non-comparable and emit a warning. */
39033903
zend_error(E_WARNING, "Cannot compare DateInterval objects");
3904-
return 1;
3904+
return ZEND_UNCOMPARABLE;
39053905
}
39063906

39073907
/* {{{ date_interval_read_property */

ext/intl/breakiterator/breakiterator_class.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@ static int BreakIterator_compare_objects(zval *object1,
8484
*bio2;
8585

8686
ZEND_COMPARE_OBJECTS_FALLBACK(object1, object2);
87-
if (Z_TYPE_P(object1) != Z_TYPE_P(object2)) {
88-
return 1; /* object and non-object */
89-
}
9087

9188
bio1 = Z_INTL_BREAKITERATOR_P(object1);
9289
bio2 = Z_INTL_BREAKITERATOR_P(object2);

ext/intl/timezone/timezone_class.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ static int TimeZone_compare_objects(zval *object1, zval *object2)
285285
}
286286
}
287287

288-
return 1;
288+
return ZEND_UNCOMPARABLE;
289289
}
290290
/* }}} */
291291

ext/pdo/pdo_dbh.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,7 @@ static zend_function *dbh_method_get(zend_object **object, zend_string *method_n
12981298

12991299
static int dbh_compare(zval *object1, zval *object2)
13001300
{
1301-
return -1;
1301+
return ZEND_UNCOMPARABLE;
13021302
}
13031303

13041304
static HashTable *dbh_get_gc(zend_object *object, zval **gc_data, int *gc_count)

ext/pdo/pdo_stmt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,7 +2165,7 @@ static zend_function *dbstmt_method_get(zend_object **object_pp, zend_string *me
21652165

21662166
static int dbstmt_compare(zval *object1, zval *object2)
21672167
{
2168-
return -1;
2168+
return ZEND_UNCOMPARABLE;
21692169
}
21702170

21712171
zend_object_handlers pdo_dbstmt_object_handlers;
@@ -2585,7 +2585,7 @@ static zend_string *row_get_classname(const zend_object *object)
25852585

25862586
static int row_compare(zval *object1, zval *object2)
25872587
{
2588-
return -1;
2588+
return ZEND_UNCOMPARABLE;
25892589
}
25902590

25912591
void pdo_row_free_storage(zend_object *std)

ext/simplexml/simplexml.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1297,10 +1297,10 @@ static int sxe_objects_compare(zval *object1, zval *object2) /* {{{ */
12971297
} else if (sxe1->document->ptr == sxe2->document->ptr) {
12981298
return 0;
12991299
}
1300+
return 1;
13001301
} else {
13011302
return !(sxe1->node == sxe2->node);
13021303
}
1303-
return 1;
13041304
}
13051305
/* }}} */
13061306

ext/spl/spl_observer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ static int spl_object_storage_compare_objects(zval *o1, zval *o2) /* {{{ */
345345
zo2 = (zend_object *)Z_OBJ_P(o2);
346346

347347
if (zo1->ce != spl_ce_SplObjectStorage || zo2->ce != spl_ce_SplObjectStorage) {
348-
return 1;
348+
return ZEND_UNCOMPARABLE;
349349
}
350350

351351
return zend_hash_compare(&(Z_SPLOBJSTORAGE_P(o1))->storage, &(Z_SPLOBJSTORAGE_P(o2))->storage, (compare_func_t)spl_object_storage_compare_info, 0);

0 commit comments

Comments
 (0)