Skip to content

Annotate internal functions with mixed type #5618

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

Closed
wants to merge 4 commits into from
Closed
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
7 changes: 3 additions & 4 deletions Zend/zend_builtin_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ function zend_version(): string {}

function func_num_args(): int {}

/** @return mixed */
function func_get_arg(int $arg_num) {}
function func_get_arg(int $arg_num): mixed {}

function func_get_args(): array {}

Expand Down Expand Up @@ -69,12 +68,12 @@ function trigger_error(string $message, int $error_type = E_USER_NOTICE): bool {
/** @alias trigger_error */
function user_error(string $message, int $error_type = E_USER_NOTICE): bool {}

/** @return mixed */
/** @return string|array|object|null */
function set_error_handler($error_handler, int $error_types = E_ALL) {}

function restore_error_handler(): bool {}

/** @return mixed */
/** @return string|array|object|null */
function set_exception_handler($exception_handler) {}

function restore_exception_handler(): bool {}
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_builtin_functions_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_func_num_args, 0, 0, IS_LONG, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_func_get_arg, 0, 0, 1)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_func_get_arg, 0, 1, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO(0, arg_num, IS_LONG, 0)
ZEND_END_ARG_INFO()

Expand Down
3 changes: 1 addition & 2 deletions Zend/zend_closures.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public static function bind(Closure $closure, ?object $newthis, $newscope = UNKN
/** @alias Closure::bind */
public function bindTo(?object $newthis, $newscope = UNKNOWN): ?Closure {}

/** @return mixed */
public function call(object $newthis, ...$parameters) {}
public function call(object $newthis, mixed ...$parameters): mixed {}

/** @param callable $callable Not a proper type annotation due to bug #78770 */
public static function fromCallable($callable): Closure {}
Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_closures_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Closure_bindTo, 0, 1, Closu
ZEND_ARG_INFO(0, newscope)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Closure_call, 0, 0, 1)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Closure_call, 0, 1, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO(0, newthis, IS_OBJECT, 0)
ZEND_ARG_VARIADIC_INFO(0, parameters)
ZEND_ARG_VARIADIC_TYPE_INFO(0, parameters, IS_MIXED, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Closure_fromCallable, 0, 1, Closure, 0)
Expand Down
15 changes: 5 additions & 10 deletions Zend/zend_generators.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@ public function rewind(): void {}

public function valid(): bool {}

/** @return mixed */
public function current() {}
public function current(): mixed {}

/** @return mixed */
public function key() {}
public function key(): mixed {}

public function next(): void {}

/** @return mixed */
public function send($value) {}
public function send(mixed $value): mixed {}

/** @return mixed */
public function throw(Throwable $exception) {}
public function throw(Throwable $exception): mixed {}

/** @return mixed */
public function getReturn() {}
public function getReturn(): mixed {}
}
8 changes: 4 additions & 4 deletions Zend/zend_generators_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Generator_valid, 0, 0, _IS_BOOL, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Generator_current, 0, 0, 0)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Generator_current, 0, 0, IS_MIXED, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_Generator_key arginfo_class_Generator_current

#define arginfo_class_Generator_next arginfo_class_Generator_rewind

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Generator_send, 0, 0, 1)
ZEND_ARG_INFO(0, value)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Generator_send, 0, 1, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO(0, value, IS_MIXED, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Generator_throw, 0, 0, 1)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Generator_throw, 0, 1, IS_MIXED, 0)
ZEND_ARG_OBJ_INFO(0, exception, Throwable, 0)
ZEND_END_ARG_INFO()

Expand Down
15 changes: 13 additions & 2 deletions build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public function __construct(string $name, bool $isBuiltin) {

public static function fromNode(Node $node) {
if ($node instanceof Node\Name) {
if ($node->toString() === "mixed") {
return new SimpleType($node->toString(), true);
}

assert($node->isFullyQualified());
return new SimpleType($node->toString(), false);
}
Expand Down Expand Up @@ -97,6 +101,8 @@ public function toTypeCode() {
return "IS_VOID";
case "callable":
return "IS_CALLABLE";
case "mixed":
return "IS_MIXED";
default:
throw new Exception("Not implemented: $this->name");
}
Expand All @@ -123,6 +129,8 @@ public function toTypeMask() {
return "MAY_BE_OBJECT";
case "callable":
return "MAY_BE_CALLABLE";
case "mixed":
return "MAY_BE_ANY";
default:
throw new Exception("Not implemented: $this->name");
}
Expand Down Expand Up @@ -637,8 +645,11 @@ function parseFunctionLike(
$param->default->name->toLowerString() === "null" &&
$type && !$type->isNullable()
) {
throw new Exception(
"Parameter $varName of function $name has null default, but is not nullable");
$simpleType = $type->tryToSimpleType();
if ($simpleType === null || $simpleType->name !== "mixed") {
throw new Exception(
"Parameter $varName of function $name has null default, but is not nullable");
}
}

$foundVariadic = $param->variadic;
Expand Down
35 changes: 11 additions & 24 deletions ext/filter/filter.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,17 @@

function filter_has_var(int $type, string $variable_name): bool {}

/**
* @param mixed $options
* @return mixed
*/
function filter_input(int $type, string $variable_name, int $filter = FILTER_DEFAULT, $options = null) {}

/**
* @param mixed $variable
* @param mixed $options
* @return mixed
*/
function filter_var($variable, int $filter = FILTER_DEFAULT, $options = null) {}

/**
* @param mixed $options
* @return mixed
*/
function filter_input_array(int $type, $options = FILTER_DEFAULT, bool $add_empty = true) {}

/**
* @param mixed $options
* @return mixed
*/
function filter_var_array(array $data, $options = FILTER_DEFAULT, bool $add_empty = true) {}
/** @param array|int $options */
function filter_input(int $type, string $variable_name, int $filter = FILTER_DEFAULT, $options = null): mixed {}

/** @param array|int $options */
function filter_var(mixed $variable, int $filter = FILTER_DEFAULT, $options = null): mixed {}

/** @param array|int $options */
function filter_input_array(int $type, $options = FILTER_DEFAULT, bool $add_empty = true): array|false|null {}

/** @param array|int $options */
function filter_var_array(array $data, $options = FILTER_DEFAULT, bool $add_empty = true): array|false|null {}

function filter_list(): array {}

Expand Down
10 changes: 5 additions & 5 deletions ext/filter/filter_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_filter_has_var, 0, 2, _IS_BOOL,
ZEND_ARG_TYPE_INFO(0, variable_name, IS_STRING, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_filter_input, 0, 0, 2)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_filter_input, 0, 2, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO(0, type, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, variable_name, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, filter, IS_LONG, 0, "FILTER_DEFAULT")
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, options, "null")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_filter_var, 0, 0, 1)
ZEND_ARG_INFO(0, variable)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_filter_var, 0, 1, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO(0, variable, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, filter, IS_LONG, 0, "FILTER_DEFAULT")
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, options, "null")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_filter_input_array, 0, 0, 1)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_filter_input_array, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE|MAY_BE_NULL)
ZEND_ARG_TYPE_INFO(0, type, IS_LONG, 0)
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, options, "FILTER_DEFAULT")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, add_empty, _IS_BOOL, 0, "true")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_filter_var_array, 0, 0, 1)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_filter_var_array, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE|MAY_BE_NULL)
ZEND_ARG_TYPE_INFO(0, data, IS_ARRAY, 0)
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, options, "FILTER_DEFAULT")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, add_empty, _IS_BOOL, 0, "true")
Expand Down
22 changes: 13 additions & 9 deletions ext/gmp/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,16 @@ static zend_object *gmp_clone_obj(zend_object *obj) /* {{{ */
}
/* }}} */

static void shift_operator_helper(gmp_binary_ui_op_t op, zval *return_value, zval *op1, zval *op2) {
static void shift_operator_helper(gmp_binary_ui_op_t op, zval *return_value, zval *op1, zval *op2, zend_uchar opcode) {
zend_long shift = zval_get_long(op2);

if (shift < 0) {
php_error_docref(NULL, E_WARNING, "Shift cannot be negative");
RETVAL_FALSE;
zend_throw_error(
zend_ce_value_error, "%s must be greater than or equal to 0",
opcode == ZEND_POW ? "Exponent" : "Shift"
);
ZVAL_UNDEF(return_value);
return;
} else {
mpz_ptr gmpnum_op, gmpnum_result;
gmp_temp_t temp;
Expand Down Expand Up @@ -372,17 +376,17 @@ static int gmp_do_operation_ex(zend_uchar opcode, zval *result, zval *op1, zval
case ZEND_MUL:
DO_BINARY_UI_OP(mpz_mul);
case ZEND_POW:
shift_operator_helper(mpz_pow_ui, result, op1, op2);
shift_operator_helper(mpz_pow_ui, result, op1, op2, opcode);
return SUCCESS;
case ZEND_DIV:
DO_BINARY_UI_OP_EX(mpz_tdiv_q, gmp_mpz_tdiv_q_ui, 1);
case ZEND_MOD:
DO_BINARY_UI_OP_EX(mpz_mod, gmp_mpz_mod_ui, 1);
case ZEND_SL:
shift_operator_helper(mpz_mul_2exp, result, op1, op2);
shift_operator_helper(mpz_mul_2exp, result, op1, op2, opcode);
return SUCCESS;
case ZEND_SR:
shift_operator_helper(mpz_fdiv_q_2exp, result, op1, op2);
shift_operator_helper(mpz_fdiv_q_2exp, result, op1, op2, opcode);
return SUCCESS;
case ZEND_BW_OR:
DO_BINARY_OP(mpz_ior);
Expand Down Expand Up @@ -520,7 +524,7 @@ static ZEND_GINIT_FUNCTION(gmp)
ZEND_MINIT_FUNCTION(gmp)
{
zend_class_entry tmp_ce;
INIT_CLASS_ENTRY(tmp_ce, "GMP", NULL);
INIT_CLASS_ENTRY(tmp_ce, "GMP", class_GMP_methods);
gmp_ce = zend_register_internal_class(&tmp_ce);
gmp_ce->create_object = gmp_create_object;
gmp_ce->serialize = gmp_serialize;
Expand Down Expand Up @@ -1261,8 +1265,8 @@ ZEND_FUNCTION(gmp_pow)
}

if (exp < 0) {
php_error_docref(NULL, E_WARNING, "Negative exponent not supported");
RETURN_FALSE;
zend_argument_value_error(2, "must be greater than or equal to 0");
RETURN_THROWS();
}

if (Z_TYPE_P(base_arg) == IS_LONG && Z_LVAL_P(base_arg) >= 0) {
Expand Down
4 changes: 4 additions & 0 deletions ext/gmp/gmp.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

/** @generate-function-entries */

class GMP
{
}

/** @param int|bool|string $number */
function gmp_init($number, int $base = 0): GMP|false {}

Expand Down
5 changes: 5 additions & 0 deletions ext/gmp/gmp_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,8 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(gmp_binomial, arginfo_gmp_binomial)
ZEND_FE_END
};


static const zend_function_entry class_GMP_methods[] = {
ZEND_FE_END
};
20 changes: 12 additions & 8 deletions ext/gmp/tests/gmp_pow.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ var_dump(gmp_strval(gmp_pow(-2,10)));
var_dump(gmp_strval(gmp_pow(-2,11)));
var_dump(gmp_strval(gmp_pow("2",10)));
var_dump(gmp_strval(gmp_pow("2",0)));
var_dump(gmp_strval(gmp_pow("2",-1)));
try {
gmp_pow("2", -1);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
var_dump(gmp_strval(gmp_pow("-2",10)));
var_dump(gmp_strval(gmp_pow(20,10)));
var_dump(gmp_strval(gmp_pow(50,10)));
var_dump(gmp_strval(gmp_pow(50,-5)));
try {
gmp_pow(50,-5);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

$n = gmp_init("20");
var_dump(gmp_strval(gmp_pow($n,10)));
Expand All @@ -36,15 +44,11 @@ string(4) "1024"
string(5) "-2048"
string(4) "1024"
string(1) "1"

Warning: gmp_pow(): Negative exponent not supported in %s on line %d
string(1) "0"
gmp_pow(): Argument #2 ($exp) must be greater than or equal to 0
string(4) "1024"
string(14) "10240000000000"
string(17) "97656250000000000"

Warning: gmp_pow(): Negative exponent not supported in %s on line %d
string(1) "0"
gmp_pow(): Argument #2 ($exp) must be greater than or equal to 0
string(14) "10240000000000"
string(14) "10240000000000"
gmp_pow(): Argument #2 ($exp) must be of type int, array given
Expand Down
35 changes: 35 additions & 0 deletions ext/gmp/tests/gmp_pow2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
Test pow() with gmp object
--SKIPIF--
<?php if (!extension_loaded("gmp")) print "skip"; ?>
--FILE--
<?php

$n = gmp_init(2);
var_dump(pow($n, 10));
var_dump($n ** 10);

try {
pow($n, -10);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

try {
$n ** -10;
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

?>
--EXPECTF--
object(GMP)#%d (1) {
["num"]=>
string(4) "1024"
}
object(GMP)#%d (1) {
["num"]=>
string(4) "1024"
}
Exponent must be greater than or equal to 0
Exponent must be greater than or equal to 0
Loading