Skip to content

Fix number type variable collision in arithmetic operators#131

Merged
mdgriffith merged 1 commit intomdgriffith:mainfrom
dillonkearns:fix/number-type-unification
Mar 31, 2026
Merged

Fix number type variable collision in arithmetic operators#131
mdgriffith merged 1 commit intomdgriffith:mainfrom
dillonkearns:fix/number-type-unification

Conversation

@dillonkearns
Copy link
Copy Markdown
Contributor

@dillonkearns dillonkearns commented Mar 31, 2026

When multiple arithmetic operators appeared within the same tuple or record, their internal number type variables could collide because toExpressionDetails reuses index values across siblings.

This eagerly resolves type variables in applyType after unification, so concrete types (Float, Int, etc.) are stored directly in the result instead of leaving GenericType references in the inference cache where they could be overwritten by siblings.

Before:

Elm.tuple
    (Elm.Op.plus (Elm.float 1.5) (Elm.float 2.5))
    (Elm.Op.plus (Elm.int 1) (Elm.int 2))

generated ( Int, Int )

Now it correctly generates the type annotation ( Float, Int ).

When multiple arithmetic operators appeared as siblings (e.g. in a
tuple or record), their internal "number" type variables could collide
because toExpressionDetails reuses index values across siblings.

The fix eagerly resolves fully-determined type variables in applyType
after unification, so concrete types (Float, Int, etc.) are stored
directly in the result instead of leaving GenericType references in
the inference cache where they could be overwritten by siblings.

This is a general fix in Compiler.applyType that protects all callers,
not just arithmetic operators.

Before: `Elm.tuple (Elm.Op.plus (Elm.float 1.5) (Elm.float 2.5)) (Elm.Op.plus (Elm.int 1) (Elm.int 2))`
generated `( Int, Int )` instead of `( Float, Int )`.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@mdgriffith
Copy link
Copy Markdown
Owner

amazing. Tests, clear explanation, 😻

@mdgriffith mdgriffith merged commit 91cf5b6 into mdgriffith:main Mar 31, 2026
1 check passed
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