Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
0ac57d5
Make zend_type a 2-field struct
nikic Sep 20, 2019
8b62d66
Store pass_by_reference+is_variadic in type mask
nikic Sep 24, 2019
4a9735f
WIP Union types
nikic Sep 25, 2019
8f9c57f
Fetch unresolved classes from autoload table
nikic Oct 18, 2019
9c58da4
Fix coercion error
nikic Oct 18, 2019
48e3379
Remove special exceptions when failing to resolve prop type class
nikic Oct 18, 2019
82cfa0c
Fix the typed reference assignment logic
nikic Oct 18, 2019
c786f78
Make loading in type errors robust
nikic Oct 18, 2019
67c0d29
Add tests for redundant types
nikic Oct 18, 2019
af73a41
Tweak error messages
nikic Oct 18, 2019
4a8d0af
Handle iterable + Traversable
nikic Oct 18, 2019
471c27f
Move test files
nikic Oct 18, 2019
f681fb6
More tests
nikic Oct 18, 2019
20495e1
Check for standalone null/false
nikic Oct 18, 2019
291f52c
Use smart_str for type error
nikic Oct 18, 2019
b49438b
Proper type error for union types
nikic Oct 18, 2019
887656d
Fix weak typing logic
nikic Oct 18, 2019
24f863a
Cleanup MAY_BE_BOOL
nikic Oct 18, 2019
37b3997
Add weak typing tests
nikic Oct 18, 2019
ccf57e9
Support union types in AST printer
nikic Oct 18, 2019
c1152c9
Add strict type variant of test
nikic Oct 21, 2019
7fc7982
Implement correct incdec handling
nikic Oct 21, 2019
209f7f1
Test property references
nikic Oct 21, 2019
5213df3
Implement full property invariance check
nikic Oct 21, 2019
9e50dfe
Add ReflectionUnionType
nikic Oct 21, 2019
b0f545c
Add a class loading test
nikic Oct 21, 2019
824ed44
Remove unused variable
nikic Oct 21, 2019
f1d05a4
Try to fix ZTS build (untested)
nikic Oct 21, 2019
fad9419
Allocate property type lists on arena
nikic Oct 22, 2019
686e61f
Remove duplicate test
nikic Oct 23, 2019
e0e204d
Add test for void + class
nikic Oct 23, 2019
476adfa
Run coverage job
nikic Oct 23, 2019
e7aecad
Adjust test after property type loading changes
nikic Oct 23, 2019
716cd38
Add test for generator with multiple class return types
nikic Oct 23, 2019
7e7d240
Add test for iterable+Traversable redundancy with extra class
nikic Oct 23, 2019
6abe540
Add inheritance test and fix some related issues
nikic Oct 23, 2019
59802bb
Fixup reflection after rebase
nikic Nov 7, 2019
f85a717
Revert "Run coverage job"
nikic Nov 8, 2019
c06e6c9
Add json skipifs
nikic Nov 8, 2019
956d3a3
Extra variance test
nikic Nov 8, 2019
8bb01a8
Fix issues related to inheritance of internal union types
nikic Nov 8, 2019
3a9ff5d
Properly destroy internal property types
nikic Nov 8, 2019
edcd6d9
Fix missing cache slot increment
nikic Nov 8, 2019
b036ffa
Use goto for common code-paths
nikic Nov 8, 2019
ccbd868
Add test for iterable variance
nikic Nov 8, 2019
54f9bd4
Fix accidental whitespace change
nikic Nov 8, 2019
c443f3b
Apply cache slot fix to jit as well
nikic Nov 8, 2019
b1615d1
Fix file cache
nikic Nov 8, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement correct incdec handling
  • Loading branch information
nikic committed Nov 7, 2019
commit 7fc79823213de94ccc9ee963e19e07aabe8a37f9
132 changes: 132 additions & 0 deletions Zend/tests/type_declarations/union_types/incdec_prop.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
--TEST--
Increment/decrement a typed property with int|float type
--FILE--
<?php

class Test {
public int|float $prop;
public int|bool $prop2;
}

/* Incrementing a int|float property past int min/max is legal */

$test = new Test;
$test->prop = PHP_INT_MAX;
$x = $test->prop++;
var_dump(is_double($test->prop));

$test->prop = PHP_INT_MAX;
$x = ++$test->prop;
var_dump(is_double($test->prop));

$test->prop = PHP_INT_MIN;
$x = $test->prop--;
var_dump(is_double($test->prop));

$test->prop = PHP_INT_MIN;
$x = --$test->prop;
var_dump(is_double($test->prop));

$test = new Test;
$test->prop = PHP_INT_MAX;
$r =& $test->prop;
$x = $test->prop++;
var_dump(is_double($test->prop));

$test->prop = PHP_INT_MAX;
$x = ++$test->prop;
$r =& $test->prop;
var_dump(is_double($test->prop));

$test->prop = PHP_INT_MIN;
$x = $test->prop--;
$r =& $test->prop;
var_dump(is_double($test->prop));

$test->prop = PHP_INT_MIN;
$x = --$test->prop;
$r =& $test->prop;
var_dump(is_double($test->prop));

/* Incrementing a non-int|float property past int min/max is an error,
* even if the result of the overflow (a float) would technically be allowed
* under a type coercion. */

try {
$test->prop2 = PHP_INT_MAX;
$x = $test->prop2++;
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}

try {
$test->prop2 = PHP_INT_MAX;
$x = ++$test->prop2;
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}

try {
$test->prop2 = PHP_INT_MIN;
$x = $test->prop2--;
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}

try {
$test->prop2 = PHP_INT_MIN;
$x = --$test->prop2;
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}

try {
$test->prop2 = PHP_INT_MAX;
$r =& $test->prop2;
$x = $test->prop2++;
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}

try {
$test->prop2 = PHP_INT_MAX;
$r =& $test->prop2;
$x = ++$test->prop2;
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}

try {
$test->prop2 = PHP_INT_MIN;
$r =& $test->prop2;
$x = $test->prop2--;
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}

try {
$test->prop2 = PHP_INT_MIN;
$r =& $test->prop2;
$x = --$test->prop2;
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECT--
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
Cannot increment property Test::$prop2 of type int|bool past its maximal value
Cannot increment property Test::$prop2 of type int|bool past its maximal value
Cannot decrement property Test::$prop2 of type int|bool past its minimal value
Cannot decrement property Test::$prop2 of type int|bool past its minimal value
Cannot increment a reference held by property Test::$prop2 of type int|bool past its maximal value
Cannot increment a reference held by property Test::$prop2 of type int|bool past its maximal value
Cannot decrement a reference held by property Test::$prop2 of type int|bool past its minimal value
Cannot decrement a reference held by property Test::$prop2 of type int|bool past its minimal value
27 changes: 15 additions & 12 deletions Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -1650,14 +1650,10 @@ static zend_property_info *zend_get_prop_not_accepting_double(zend_reference *re
return NULL;
}

static ZEND_COLD zend_long zend_throw_incdec_ref_error(zend_reference *ref OPLINE_DC)
static ZEND_COLD zend_long zend_throw_incdec_ref_error(
zend_reference *ref, zend_property_info *error_prop OPLINE_DC)
{
zend_property_info *error_prop = zend_get_prop_not_accepting_double(ref);
ZEND_ASSERT(error_prop);
zend_string *type_str = zend_type_to_string(error_prop->type);
// TODO!!! No longer true with union types
/* Currently there should be no way for a typed reference to accept both int and double.
* Generalize this and the related property code once this becomes possible. */
if (ZEND_IS_INCREMENT(opline->opcode)) {
zend_type_error(
"Cannot increment a reference held by property %s::$%s of type %s past its maximal value",
Expand Down Expand Up @@ -1714,8 +1710,11 @@ static void zend_incdec_typed_ref(zend_reference *ref, zval *copy OPLINE_DC EXEC
}

if (UNEXPECTED(Z_TYPE_P(var_ptr) == IS_DOUBLE) && Z_TYPE_P(copy) == IS_LONG) {
zend_long val = zend_throw_incdec_ref_error(ref OPLINE_CC);
ZVAL_LONG(var_ptr, val);
zend_property_info *error_prop = zend_get_prop_not_accepting_double(ref);
if (UNEXPECTED(error_prop)) {
zend_long val = zend_throw_incdec_ref_error(ref, error_prop OPLINE_CC);
ZVAL_LONG(var_ptr, val);
}
} else if (UNEXPECTED(!zend_verify_ref_assignable_zval(ref, var_ptr, EX_USES_STRICT_TYPES()))) {
zval_ptr_dtor(var_ptr);
ZVAL_COPY_VALUE(var_ptr, copy);
Expand All @@ -1742,8 +1741,10 @@ static void zend_incdec_typed_prop(zend_property_info *prop_info, zval *var_ptr,
}

if (UNEXPECTED(Z_TYPE_P(var_ptr) == IS_DOUBLE) && Z_TYPE_P(copy) == IS_LONG) {
zend_long val = zend_throw_incdec_prop_error(prop_info OPLINE_CC);
ZVAL_LONG(var_ptr, val);
if (!(ZEND_TYPE_FULL_MASK(prop_info->type) & MAY_BE_DOUBLE)) {
zend_long val = zend_throw_incdec_prop_error(prop_info OPLINE_CC);
ZVAL_LONG(var_ptr, val);
}
} else if (UNEXPECTED(!zend_verify_property_type(prop_info, var_ptr, EX_USES_STRICT_TYPES()))) {
zval_ptr_dtor(var_ptr);
ZVAL_COPY_VALUE(var_ptr, copy);
Expand All @@ -1761,7 +1762,8 @@ static void zend_pre_incdec_property_zval(zval *prop, zend_property_info *prop_i
} else {
fast_long_decrement_function(prop);
}
if (UNEXPECTED(Z_TYPE_P(prop) != IS_LONG) && UNEXPECTED(prop_info)) {
if (UNEXPECTED(Z_TYPE_P(prop) != IS_LONG) && UNEXPECTED(prop_info)
&& !(ZEND_TYPE_FULL_MASK(prop_info->type) & MAY_BE_DOUBLE)) {
zend_long val = zend_throw_incdec_prop_error(prop_info OPLINE_CC);
ZVAL_LONG(prop, val);
}
Expand Down Expand Up @@ -1799,7 +1801,8 @@ static void zend_post_incdec_property_zval(zval *prop, zend_property_info *prop_
} else {
fast_long_decrement_function(prop);
}
if (UNEXPECTED(Z_TYPE_P(prop) != IS_LONG) && UNEXPECTED(prop_info)) {
if (UNEXPECTED(Z_TYPE_P(prop) != IS_LONG) && UNEXPECTED(prop_info)
&& !(ZEND_TYPE_FULL_MASK(prop_info->type) & MAY_BE_DOUBLE)) {
zend_long val = zend_throw_incdec_prop_error(prop_info OPLINE_CC);
ZVAL_LONG(prop, val);
}
Expand Down