fix(optimize): conservative invalidation for by-ref calls in match subject (#384)#444
Merged
nahime0 merged 1 commit intoJul 6, 2026
Conversation
…bject Fixes illegalstudio#384. Constant propagation treated a call's unknown local-write set (None) as 'no writes', so a variable mutated by a by-reference call used as a match subject kept its pre-call constant for a later read in the same expression: echo match(bump($i)) {..} . '|' . $i printed the stale value. propagate_expr now clears the environment when the write set is unknown AND the expression has side effects (a by-reference-mutating call). Pure calls (gettype, strlen, ...) cannot mutate a local, so their operands stay foldable — this keeps existing folds intact (e.g. gettype($a * $b) still constant-folds), avoiding a regression in the int-overflow-promotion tests. Rebased onto current main. Regression test in constant_propagation/straight_line.rs.
mirchaemanuel
force-pushed
the
fix/384-match-subject-byref-writeback
branch
from
July 6, 2026 08:46
7c85448 to
5b9f1b6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #384. A by-reference call used as a
matchsubject lost its writeback for constant-propagation purposes:echo match(bump($i)) { 1 => "one", default => "other" } . "|" . $i;printedone|0instead of PHP'sone|1.Root cause
This is an AST constant-propagation bug, not a lowering/codegen one. With
--emit-ir --no-ir-optthe trailing$iis already emitted asconst_i64 0, so the stale value is folded before EIR.In
src/optimize/propagate/expr.rs,propagate_exprcleared the constant environment only whenexpr_local_writes(&expr)returnedSome(non-empty). A call expression returnsNone(unknown write set), andNonewas treated as "no writes" — so the pre-call constant$i = 0was propagated into the read sequenced after thematch. The same call in an arithmetic expression orifcondition works only because statement-level side-effect clearing (env_after_expr_side_effects) invalidates across statement boundaries; within a single expression there was no invalidation.This also contradicts the module's own documented contract: "unknown calls force conservative invalidation."
Fix
propagate_exprnow clears the environment when the write set is unknown (None) as well as when it is a known non-empty set. A read sequenced after any by-reference-mutating call in the same expression therefore observes the post-call value, matching PHP.Tests
test_constant_propagation_match_subject_byref_writebackintests/codegen/optimizer/constant_propagation/straight_line.rs(failsone|0before, passesone|1after).propagateunit tests, 242 optimizer codegen tests, 1093 regressions/types/callables/control codegen tests — all green.cargo buildwarning-free;git diff --checkclean.