Skip to content

Fix GH-25061: $x = $x - 1 on null is optimized to PRE_DEC - #25074

Closed
lazerg wants to merge 2 commits into
php:PHP-8.4from
lazerg:fix/gh-25061-dfa-sub-pre-dec
Closed

lazerg wants to merge 2 commits into
php:PHP-8.4from
lazerg:fix/gh-25061-dfa-sub-pre-dec

Conversation

@lazerg

@lazerg lazerg commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

The DFA pass turns $x = $x - 1 into PRE_DEC when the operand can only be null, undef or a number. That is fine for + 1, but not for - 1: null - 1 is -1, while --$x on null leaves it null. The ASSIGN_OP SUB rule right below already excludes MAY_BE_UNDEF|MAY_BE_NULL, so the two SUB contraction rules now do the same.

Fixes GH-25061

@arshidkv12

Copy link
Copy Markdown
Contributor

Thank you for your PR. Could you also check the following case?

function d5(?int $x) {
    $x--;
    return $x;
}

var_dump(d5(null));

Currently, this produces the following warning:

Warning: Decrement on type null has no effect, this will change in the next major version of PHP

Should this case also be handled?

@lazerg

lazerg commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

I left that case alone because it is the intended behavior. $x-- on null gives the same warning and NULL without opcache too, since decrementing null has been a no-op with that warning since 8.3. This PR only fixes the optimizer turning $x = $x - 1 into a decrement, which changed the result.

@arshidkv12

Copy link
Copy Markdown
Contributor

Thanks for the clarification. This PR only addresses the incorrect optimization that changes the result.

I think a separate PR would be needed to address the behavior of $x = null; $x--; in line with the Saner Increment/Decrement Operators RFC.

@ndossche ndossche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine, but add a comment to the edited lines that the MAY_BE_UNDEF|MAY_BE_NULL exists because of the currently inconsistent behaviour and that in PHP9 we should revisit this once the behaviour is fixed

@lazerg

lazerg commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

Added the comment on both edited lines.

@ndossche ndossche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@ndossche ndossche closed this in 0c406ea Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants