Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Erased types #146

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion Zend/Optimizer/zend_inference.c
Original file line number Diff line number Diff line change
Expand Up @@ -2370,7 +2370,7 @@ static uint32_t zend_convert_type(const zend_script *script, zend_type type, zen
*pce = NULL;
}

if (!ZEND_TYPE_IS_SET(type)) {
if (!ZEND_TYPE_IS_CHECKED(type)) {
return MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF|MAY_BE_RC1|MAY_BE_RCN;
}

Expand Down
19 changes: 19 additions & 0 deletions Zend/tests/erased_types/include_erased_in_non_erased.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(types='erased');

function test(string $p1, string $p2 = 'p2') {
var_dump($p1, $p2);
}

class P {
public function test(string $p): string {
return $p;
}
}

trait T {
public function test2(string $p): string {
return $p;
}
}
25 changes: 25 additions & 0 deletions Zend/tests/erased_types/include_erased_in_non_erased.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
Erased types: Include erased in non-erased
--FILE--
<?php

require __DIR__ . '/include_erased_in_non_erased.inc';

test(42);
test('42', 43);

class C extends P {
use T;
}
$c = new C();
var_dump($c->test(42));
var_dump($c->test2(42));

?>
--EXPECT--
int(42)
string(2) "p2"
string(2) "42"
int(43)
int(42)
int(42)
17 changes: 17 additions & 0 deletions Zend/tests/erased_types/include_non_erased_in_erased.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

function test(string $p1, string $p2 = 'p2') {
var_dump($p1, $p2);
}

class P {
public function test(string $p): string {
return $p;
}
}

trait T {
public function test2(string $p): string {
return $p;
}
}
27 changes: 27 additions & 0 deletions Zend/tests/erased_types/include_non_erased_in_erased.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Erased types: Include non-erased in erased
--FILE--
<?php

declare(types='erased');

require __DIR__ . '/include_non_erased_in_erased.inc';

test(42);
test('42', 43);

class C extends P {
use T;
}
$c = new C();
var_dump($c->test(42));
var_dump($c->test2(42));

?>
--EXPECT--
string(2) "42"
string(2) "p2"
string(2) "42"
string(2) "43"
string(2) "42"
string(2) "42"
17 changes: 17 additions & 0 deletions Zend/tests/erased_types/parameter_types.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Erased types: Parameter types
--FILE--
<?php

declare(types='erased');

function test(string $p1, string $p2 = 'p2') {
var_dump($p1, $p2);
}

test(42, 43);

?>
--EXPECT--
int(42)
int(43)
21 changes: 21 additions & 0 deletions Zend/tests/erased_types/property_types.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Erased types: Property types
--FILE--
<?php

declare(types='erased');

class C {
public string $prop;
}

$c = new C();
$c->prop = 42;
var_dump($c);

?>
--EXPECTF--
object(C)#%d (1) {
["prop"]=>
int(42)
}
16 changes: 16 additions & 0 deletions Zend/tests/erased_types/return_types.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Erased types: Return types
--FILE--
<?php

declare(types='erased');

function test(): string {
return 42;
}

var_dump(test());

?>
--EXPECT--
int(42)
17 changes: 17 additions & 0 deletions Zend/tests/erased_types/static_parameter_types.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Erased types: Static property types
--FILE--
<?php

declare(types='erased');

class C {
public static string $prop;
}

C::$prop = 42;
var_dump(C::$prop);

?>
--EXPECT--
int(42)
23 changes: 23 additions & 0 deletions Zend/tests/erased_types/typed_references.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
Erased types: Typed references
--FILE--
<?php

declare(types='erased');

class C {
public string $prop;
}

$c = new C();
$c->prop = 'foo';
$ref = &$c->prop;
$ref = 42;
var_dump($c);

?>
--EXPECTF--
object(C)#%d (1) {
["prop"]=>
&int(42)
}
34 changes: 34 additions & 0 deletions Zend/tests/erased_types/uninitialized.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Erased types: Uninitialized properties
--FILE--
<?php

declare(types='erased');

class C {
public int $prop;
}

$c = new C();
var_dump($c);

try {
$c->prop;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

try {
$c->prop++;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECTF--
object(C)#%d (0) {
["prop"]=>
uninitialized(int)
}
Typed property C::$prop must not be accessed before initialization
Typed property C::$prop must not be accessed before initialization
47 changes: 44 additions & 3 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */
FC(in_namespace) = 0;
FC(has_bracketed_namespaces) = 0;
FC(declarables).ticks = 0;
FC(types_mode) = ZEND_TYPES_MODE_CHECKED;
zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0);
}
/* }}} */
Expand Down Expand Up @@ -2697,6 +2698,10 @@ static void zend_emit_return_type_check(
return;
}

if (FC(types_mode) == ZEND_TYPES_MODE_ERASED) {
return;
}

if (expr && expr->op_type == IS_CONST && ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE(expr->u.constant))) {
/* we don't need run-time check */
return;
Expand Down Expand Up @@ -6900,7 +6905,34 @@ static void zend_compile_declare(zend_ast *ast) /* {{{ */
if (Z_LVAL(value_zv) == 1) {
CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
}
} else if (zend_string_equals_literal_ci(name, "types")) {
if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ 0)) {
zend_error_noreturn(E_COMPILE_ERROR,
"types declaration pragma must be the very first statement in the script");
}

zval value_zv;
if (ast->child[1] != NULL) {
zend_error_noreturn(E_COMPILE_ERROR,
"types declaration must not use block mode");
}

zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);

if (Z_TYPE(value_zv) != IS_STRING) {
types_decl_value_error:
zend_error_noreturn(E_COMPILE_ERROR, "types declaration must be a string with the value \"checked\" or \"erased\"");
}

if (zend_string_equals_literal_ci(Z_STR(value_zv), "checked")) {
FC(types_mode) = ZEND_TYPES_MODE_CHECKED;
} else if (zend_string_equals_literal_ci(Z_STR(value_zv), "erased")) {
FC(types_mode) = ZEND_TYPES_MODE_ERASED;
} else {
goto types_decl_value_error;
}

zval_ptr_dtor(&value_zv);
} else {
zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
}
Expand Down Expand Up @@ -7323,6 +7355,14 @@ static zend_type zend_compile_typename_ex(
}

ast->attr = orig_ast_attr;

// FIXME: Also add the flag to list element types.
if (FC(types_mode) == ZEND_TYPES_MODE_CHECKED && (ZEND_TYPE_FULL_MASK(type) & ~_ZEND_TYPE_CHECKED_BIT)) {
ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_CHECKED_BIT;
} else {
ZEND_TYPE_FULL_MASK(type) &= ~_ZEND_TYPE_CHECKED_BIT;
}

return type;
}
/* }}} */
Expand Down Expand Up @@ -7718,7 +7758,7 @@ static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32
SET_NODE(opline->result, &var_node);
opline->op1.num = i + 1;

if (type_ast) {
if (type_ast && ZEND_TYPE_IS_CHECKED(arg_info->type)) {
/* Allocate cache slot to speed-up run-time class resolution */
opline->extended_value =
zend_alloc_cache_slots(zend_type_get_num_classes(arg_info->type));
Expand All @@ -7728,8 +7768,9 @@ static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32
| (is_promoted ? _ZEND_IS_PROMOTED_BIT : 0);
ZEND_TYPE_FULL_MASK(arg_info->type) |= arg_info_flags;
if (opcode == ZEND_RECV) {
opline->op2.num = type_ast ?
ZEND_TYPE_FULL_MASK(arg_info->type) : MAY_BE_ANY;
opline->op2.num = type_ast && ZEND_TYPE_IS_CHECKED(arg_info->type)
? ZEND_TYPE_PURE_MASK(arg_info->type)
: MAY_BE_ANY;
}

if (is_promoted) {
Expand Down
6 changes: 6 additions & 0 deletions Zend/zend_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ typedef struct _zend_declarables {
zend_long ticks;
} zend_declarables;

typedef enum {
ZEND_TYPES_MODE_CHECKED = 0,
ZEND_TYPES_MODE_ERASED,
} zend_types_mode;

/* Compilation context that is different for each file, but shared between op arrays. */
typedef struct _zend_file_context {
zend_declarables declarables;
Expand All @@ -119,6 +124,7 @@ typedef struct _zend_file_context {
HashTable *imports_const;

HashTable seen_symbols;
zend_types_mode types_mode;
} zend_file_context;

typedef union _zend_parser_stack_elem {
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ static zend_always_inline bool zend_verify_recv_arg_type(zend_function *zf, uint
ZEND_ASSERT(arg_num <= zf->common.num_args);
cur_arg_info = &zf->common.arg_info[arg_num-1];

if (ZEND_TYPE_IS_SET(cur_arg_info->type)
if (ZEND_TYPE_IS_CHECKED(cur_arg_info->type)
&& UNEXPECTED(!zend_check_type(&cur_arg_info->type, arg, cache_slot, zf->common.scope, 0, 0))) {
zend_verify_arg_error(zf, cur_arg_info, arg_num, arg);
return 0;
Expand Down
14 changes: 13 additions & 1 deletion Zend/zend_object_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ static zend_always_inline uintptr_t zend_get_property_offset(zend_class_entry *c
}

offset = property_info->offset;
if (EXPECTED(!ZEND_TYPE_IS_SET(property_info->type))) {
if (!ZEND_TYPE_IS_CHECKED(property_info->type)) {
property_info = NULL;
} else {
*info_ptr = property_info;
Expand Down Expand Up @@ -935,6 +935,12 @@ ZEND_API zval *zend_std_read_property(zend_object *zobj, zend_string *name, int
}
}
if (type != BP_VAR_IS) {
if (!prop_info && retval != &EG(uninitialized_zval)) {
prop_info = zend_get_property_info_for_slot(zobj, retval);
if (prop_info && !ZEND_TYPE_IS_SET(prop_info->type)) {
prop_info = NULL;
}
}
if (prop_info) {
zend_typed_property_uninitialized_access(prop_info, name);
} else {
Expand Down Expand Up @@ -1329,6 +1335,12 @@ ZEND_API zval *zend_std_get_property_ptr_ptr(zend_object *zobj, zend_string *nam
return zend_std_get_property_ptr_ptr(zobj, name, type, cache_slot);
}
if (UNEXPECTED(type == BP_VAR_RW || type == BP_VAR_R)) {
if (!prop_info) {
prop_info = zend_get_property_info_for_slot(zobj, retval);
if (prop_info && !ZEND_TYPE_IS_SET(prop_info->type)) {
prop_info = NULL;
}
}
if (prop_info) {
zend_typed_property_uninitialized_access(prop_info, name);
retval = &EG(error_zval);
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void zend_object_dtor_property(zend_object *object, zval *p)
if (UNEXPECTED(Z_ISREF_P(p)) &&
(ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(p)))) {
zend_property_info *prop_info = zend_get_property_info_for_slot_self(object, p);
if (ZEND_TYPE_IS_SET(prop_info->type)) {
if (ZEND_TYPE_IS_CHECKED(prop_info->type)) {
ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(p), prop_info);
}
}
Expand Down
Loading