Skip to content

fix(parser): preserve numeric variable types (#85) - #86

Closed
jefyokta wants to merge 2 commits into
swoole:masterfrom
jefyokta:master
Closed

fix(parser): preserve numeric variable types (#85)#86
jefyokta wants to merge 2 commits into
swoole:masterfrom
jefyokta:master

Conversation

@jefyokta

@jefyokta jefyokta commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #85.

Numeric variables were being converted to php::Var when native_types was not enabled. This allowed variables initially inferred as numeric types to be reassigned to incompatible types.

For example:

$x = 42;
$x = [1, 2];

Previously, this was allowed because $x was converted to php::Var.

This change preserves the inferred numeric type by default, making its behavior consistent with other inferred types such as string, array, and object.

The reassignment above will now be rejected because $x is inferred as php::Int.

Expected behavior

$x = 42;
$x = [1, 2]; // Error

native_types should no longer be required to strictly preserve the inferred numeric type.

Implementation

The change is made in getNativeType():

protected function getNativeType(string $type): string
{
    if ($type === Type::INT && $this->bigintTypes) {
        return Type::BIGINT;
    }
    if ($type === Type::FLOAT && $this->decimalTypes) {
        return Type::DECIMAL;
    }
    //-  return $this->nativeTypes ? $type : Type::VAR;
     return $type;
}

This method is only called once, so changing this behavior does not introduce additional conversion paths or require broader changes to the type inference logic. the only one line that call it:

$finalVarType = $this->getNormalAssignType($type);
$finalVarType = $this->isNativeType($finalVarType) ? $this->getNativeType($finalVarType) : $finalVarType;
$this->addLocalVar($var, $finalVarType);

If the intended design is that strict numeric type inference should explicitly require native_types, this PR can be rejected accordingly.

@jefyokta

jefyokta commented Sep 5, 2026

Copy link
Copy Markdown
Contributor Author

i forgot to test to build tpc with the new tpc, anyway $target convertion problem already fixed, but got incompetible with ForeachIterator's phpx.
ForeachIterator::nextKeyValue doesnt exists in phpx v2.7.0? i found it the in master branch, but not in release

error: no member named 'nextKeyValue' in 'php::ForeachIterator'
   28 |         while (tmp_var_0.nextKeyValue(key, value)) {
      |                ~~~~~~~~~ ^
1 error generated.
Fatal error: compile failed:
#0 [internal function]: TypePhp\Diagnostics\CliDiagnostic

https://github.com/swoole/phpx/blob/f5663b75c313d817680d26fcae021d67ef32e4f6/include/phpx.h#L2680

@jefyokta jefyokta closed this Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

without use native_typesnumeric variable stored as php::Var

1 participant