Skip to content

Fix constant folding of comparison chains - #699

Merged
lagergren merged 4 commits into
masterfrom
fix/chained-comparison-constants
Oct 14, 2026
Merged

lagergren merged 4 commits into
masterfrom
fix/chained-comparison-constants

Conversation

@lagergren

@lagergren lagergren commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Why?

  • Constant comparison chains such as 1 < 2 < 3 never fold: constPrev is initialized to null and never assigned, so every attempted comparison throws a NullPointerException that the fallback silently catches.
  • Merely assigning constPrev would leave two hazards in the old loop: retaining a true constant prefix when a later operand is nonconstant, and stopping validation after a false prefix.

How?

  • Validate every operand before attempting to fold the chain.
  • Fold only when every operand is constant, initializing the previous value from the first operand and advancing it after each comparison.
  • Preserve the existing runtime fallback for unsupported constant operations and retain runtime operand evaluation, including after a false constant prefix.

Implications

This is an independent compiler bug fix based directly on master. It does not depend on the LSP implementation or its embedding API changes. Runtime chain generation is unchanged.

Changes

  • CmpChainExpression.java: separate operand checking from constant evaluation, correctly carry the preceding constant through the chain, and explain the folding and equality rules beside their implementations.
  • xdk/src/test/resources/compiler-errors/comparisonChains.x: one concise Ecstasy regression case run by the existing CompilerErrorsTest. The chain 1 < 2 < 3 must fold to True and collide with the preceding True switch case, producing COMPILER-77 (duplicate value).

Manual verification

The retained 1 < 2 < 3 case was verified against unchanged master in the original regression run: it reports COMPILER-78 (constant required), rather than the expected COMPILER-77 (duplicate value). With the fix, the simplified fixture reports the expected duplicate-case diagnostic.

After simplifying the fixture, :xdk:test --tests org.xvm.xdk.CompilerErrorsTest --rerun-tasks --no-build-cache and spotlessCheck passed. JUnit XML confirms three tests, zero failures, zero errors and zero skips. The XDK libraries were rebuilt with the corrected compiler.

@ggleyzer ggleyzer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I cannot reproduce any of the claimed issues. I tried:

    Boolean f1 = 1 < 2 < 3;
    assert f1;

    f1 = 1 < 2 < 0;
    assert !f1;

and everything compiles and runs. What is the problem you're fixing?

lagergren added a commit that referenced this pull request Oct 13, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Oct 13, 2026
Replace raw collections and duplicated validation, branch joining, operand emission, and declaration rendering with typed shared helpers. Remove dead paths and clarify null and type invariants.

Fix constant comparison folding, equality-chain branch emission, and tuple foreach targets. Cover these paths with Ecstasy compiler and runtime regressions. The constant-folding fix is also isolated in PR #699.
@lagergren
lagergren requested a review from ggleyzer October 14, 2026 06:12
@lagergren
lagergren merged commit 25dcd8d into master Oct 14, 2026
5 checks passed
@lagergren
lagergren deleted the fix/chained-comparison-constants branch October 14, 2026 13:58
lagergren added a commit that referenced this pull request Oct 14, 2026
Replace raw collections and duplicated validation, branch joining, operand emission, and declaration rendering with typed shared helpers. Remove dead paths and clarify null and type invariants.

Fix constant comparison folding, equality-chain branch emission, and tuple foreach targets. Cover these paths with Ecstasy compiler and runtime regressions. The constant-folding fix is also isolated in PR #699.
lagergren added a commit that referenced this pull request Oct 14, 2026
Replace raw collections and duplicated validation, branch joining, operand emission, and declaration rendering with typed shared helpers. Remove dead paths and clarify null and type invariants.

Fix constant comparison folding, equality-chain branch emission, and tuple foreach targets. Cover these paths with Ecstasy compiler and runtime regressions. The constant-folding fix is also isolated in PR #699.
lagergren added a commit that referenced this pull request Oct 15, 2026
Replace raw collections and duplicated validation, branch joining, operand emission, and declaration rendering with typed shared helpers. Remove dead paths and clarify null and type invariants.

Fix constant comparison folding, equality-chain branch emission, and tuple foreach targets. Cover these paths with Ecstasy compiler and runtime regressions. The constant-folding fix is also isolated in PR #699.
lagergren added a commit that referenced this pull request Oct 19, 2026
Replace raw collections and duplicated validation, branch joining, operand emission, and declaration rendering with typed shared helpers. Remove dead paths and clarify null and type invariants.

Fix constant comparison folding, equality-chain branch emission, and tuple foreach targets. Cover these paths with Ecstasy compiler and runtime regressions. The constant-folding fix is also isolated in PR #699.
lagergren added a commit that referenced this pull request Oct 20, 2026
Replace raw collections and duplicated validation, branch joining, operand emission, and declaration rendering with typed shared helpers. Remove dead paths and clarify null and type invariants.

Fix constant comparison folding, equality-chain branch emission, and tuple foreach targets. Cover these paths with Ecstasy compiler and runtime regressions. The constant-folding fix is also isolated in PR #699.
lagergren added a commit that referenced this pull request Oct 21, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Oct 21, 2026
Replace raw collections and duplicated validation, branch joining, operand emission, and declaration rendering with typed shared helpers. Remove dead paths and clarify null and type invariants.

Fix constant comparison folding, equality-chain branch emission, and tuple foreach targets. Cover these paths with Ecstasy compiler and runtime regressions. The constant-folding fix is also isolated in PR #699.
lagergren added a commit that referenced this pull request Oct 26, 2026
Replace raw collections and duplicated validation, branch joining, operand emission, and declaration rendering with typed shared helpers. Remove dead paths and clarify null and type invariants.

Fix constant comparison folding, equality-chain branch emission, and tuple foreach targets. Cover these paths with Ecstasy compiler and runtime regressions. The constant-folding fix is also isolated in PR #699.
lagergren added a commit that referenced this pull request Oct 26, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Oct 26, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Oct 26, 2026
Replace raw collections and duplicated validation, branch joining, operand emission, and declaration rendering with typed shared helpers. Remove dead paths and clarify null and type invariants.

Fix constant comparison folding, equality-chain branch emission, and tuple foreach targets. Cover these paths with Ecstasy compiler and runtime regressions. The constant-folding fix is also isolated in PR #699.
lagergren added a commit that referenced this pull request Oct 27, 2026
Replace raw collections and duplicated validation, branch joining, operand emission, and declaration rendering with typed shared helpers. Remove dead paths and clarify null and type invariants.

Fix constant comparison folding, equality-chain branch emission, and tuple foreach targets. Cover these paths with Ecstasy compiler and runtime regressions. The constant-folding fix is also isolated in PR #699.
lagergren added a commit that referenced this pull request Oct 27, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Oct 28, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Oct 28, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Oct 29, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Oct 29, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Oct 29, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Nov 2, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Nov 4, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Nov 5, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Nov 9, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Nov 11, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Nov 22, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Nov 25, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Nov 25, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Nov 25, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Nov 26, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Nov 26, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Nov 27, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
lagergren added a commit that referenced this pull request Nov 28, 2026
Mirror the LSP branch cleanup while retaining this branch’s compiler diagnostics and tests. Replace duplicated validation and emission paths with typed helpers, remove dead code, and make null and type invariants explicit.

Include Ecstasy regressions for comparison chains, tuple foreach targets, loop control flow, branch narrowing, and switch ranges. Constant-chain folding is also isolated in PR #699.
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.

2 participants