Skip to content

Fix operands of arithmetic operators being type checked twice - #9501

Merged
cknitt merged 3 commits into
masterfrom
fix-unified-ops-typed-twice
Dec 2, 2026
Merged

cknitt merged 3 commits into
masterfrom
fix-unified-ops-typed-twice

Conversation

@cknitt

@cknitt cknitt commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Fixes #9500.

The operands of an arithmetic operator such as + were type checked twice, and each nested operator doubled that again:

let x = 1 + (1 + (1 + (@ppwarning("operand") 2))) // warning 22, eight times

Typecore.translate_unified_ops types each operand without an expected type, to choose the operator's type from them. It then typed an operand again against the chosen type. An operand that is itself an operator application did the same inside, so the work doubled at each level. Besides repeating diagnostics, this made a right-nested chain slow: 18 levels of 1 + (…) took 1.5 s to compile, now 0.04 s.

The fix. An operand gets the chosen type by unifying its type with a fresh instance of it. Only when that fails is it typed again against the chosen type, as before. That covers wrong operand types, which keep their old error message, and operands whose type equals the chosen type only through a GADT equation, which still type check. The instance matters: unifying with the shared predefined int itself would link that node to the operand's type, and every later use of int would see that type.

Tests.

  • ppwarning_operand_once: a @ppwarning on a nested int operand, a float operand, a left operand that takes its type from the right one, and both operands of an int fallback, each reported once.
  • unified_ops_operand_test: variable operands that take their type from the left operand, from the right operand, or fall back to int, an operand whose type is int only inside a GADT branch, and nested operators. Each function only compiles if its operands get the right type, and the tests check the results.
  • Error messages for operands of the wrong type are unchanged in the existing error tests.

Not changed. When the first attempt fails and the operand is typed again, its diagnostics are still reported twice, as before, for example for an operand of the wrong type.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du

Why:
    Typecore.translate_unified_ops types each operand of an operator
    such as `+` without an expected type, to choose the operator's type
    from it. It then typed an operand again against the chosen type. An
    operand that is itself an operator application did the same inside,
    so the work doubled with each level of nesting, and diagnostics on
    an operand, such as a @ppwarning, were reported again each time.

An operand is now given the chosen type by unifying its type with a
fresh instance of it. Only when that fails is it typed again against the
chosen type, for the same error message as before.

Diagnostic modified (@ppwarning on a nested operand):
Example:
    let x = 1 + (1 + (1 + (@ppwarning("operand") 2)))
Before (diagnostic):
    Warning number 22 (reported 8 times)
    operand
After (diagnostic):
    Warning number 22 (reported once)
    operand

Fixes #9500

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt

cknitt commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-01T20:37:54.534864Z 65b6611 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix the operands of arithmetic operators being type checked twice, and twice as often at each level of nesting, which slowed down nested expressions and repeated their diagnostics.

Semantic Diff

3 commits · Full page

Fix operands of arithmetic operators being type checked twice

2149039 · cknitt + Claude · Dec 01, 2026, 20:26 UTC

Why

Typecore.translate_unified_ops types each operand of an operator such as + without an expected type, to choose the operator's type from it. It then typed an operand again against the chosen type. An operand that is itself an operator application did the same inside, so the work doubled with each level of nesting, and diagnostics on an operand, such as a @ppwarning, were reported again each time.

An operand is now given the chosen type by unifying its type with a fresh instance of it. Only when that fails is it typed again against the chosen type, for the same error message as before.

Diagnostic modified (@ppwarning on a nested operand)

let x = 1 + (1 + (1 + (@ppwarning("operand") 2)))

diagnostic

-Warning number 22 (reported 8 times)
+Warning number 22 (reported once)
 operand

Fixes #9500

Test the typing rules of arithmetic operators on variable operands

65b6611 · cknitt + Claude · Dec 01, 2026, 20:35 UTC

The fix keeps an operand's first typed tree when its type fits, so each way an operator takes its type needs a test that compiles and runs: from the left operand, from the right operand, the fallback to int, and nested operators. Each function only compiles if its operands get the right type.

✨ Test operands that are typed again and left operands typed twice before

2b8872c · cknitt + Claude · Dec 01, 2026, 20:58 UTC · new

A review found two gaps in the tests. Typing an operand again is also how an operand whose type is int only inside a GADT branch type checks, and no test covered that. And the left operand in ppwarning_operand_once was an int, which was never typed twice, so it did not test the fix. It is replaced by left operands that take their type from the right operand or fall back to int. The changelog no longer says the cost grew by one typing per level: it doubled.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 21490390d0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread compiler/typing/typecore.ml Resolved
@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

🎉 Compiler benchmarks

Allocated words

In total: 655,165,618 → 650,988,316 (-0.64%).

Generated projects: 627,287,779 → 626,962,875 (-0.05%).

🎉 Improvement: 4 benchmarks allocate more than 1% less.

Benchmark Base Head Change
stress/LetRec.res 29,217,899 26,327,016 -9.89%
stress/RefsInLoop.res 16,120,558 15,663,476 -2.84%
stress/NestedClosures.res 9,295,346 9,032,082 -2.83%
packages/@rescript/runtime/Primitive_array.res 186,298 184,123 -1.17%
stress/InlineChain.res 3,409,348 3,384,422 -0.73%
packages/@rescript/belt/src/Belt_SortArrayString.res 1,278,318 1,271,591 -0.53%
packages/@rescript/belt/src/Belt_SortArrayInt.res 1,276,495 1,269,796 -0.52%
packages/@rescript/belt/src/Belt_SortArray.res 1,374,004 1,367,334 -0.49%
packages/@rescript/belt/src/Belt_Array.res 2,042,965 2,034,185 -0.43%
packages/@rescript/runtime/Primitive_hash.res 925,333 921,996 -0.36%
16 more changes
Benchmark Base Head Change
packages/@rescript/runtime/Stdlib_Int.res 445,261 443,936 -0.30%
build/shared-edit 84,100,966 83,950,807 -0.18%
packages/@rescript/belt/src/Belt_internalAVLset.res 2,762,124 2,757,349 -0.17%
stress/ObjectFields.res 20,552,089 20,519,677 -0.16%
packages/@rescript/belt/src/Belt_internalAVLtree.res 3,365,852 3,360,883 -0.15%
packages/@rescript/belt/src/Belt_Range.res 279,967 279,569 -0.14%
packages/@rescript/belt/src/Belt_HashSetString.res 824,498 823,443 -0.13%
packages/@rescript/belt/src/Belt_HashSetInt.res 824,183 823,130 -0.13%
packages/@rescript/belt/src/Belt_MutableQueue.res 764,303 763,401 -0.12%
packages/@rescript/runtime/Stdlib_Array.res 1,238,375 1,236,975 -0.11%
packages/@rescript/belt/src/Belt_HashSet.res 947,314 946,257 -0.11%
packages/@rescript/belt/src/Belt_HashMapInt.res 1,028,373 1,027,239 -0.11%
packages/@rescript/belt/src/Belt_HashMapString.res 1,029,083 1,027,951 -0.11%
packages/@rescript/belt/src/Belt_internalBuckets.res 935,830 934,801 -0.11%
build/clean 253,950,736 253,688,865 -0.10%
stress/Record.res 24,087,754 24,063,454 -0.10%

Instructions

In total: 9,128,411,164 → 8,961,649,825 (-1.83%).

🎉 Improvement: 3 benchmarks execute more than 1% fewer instructions.

Benchmark Base Head Change
stress/LetRec.res 1,348,692,090 1,222,188,819 -9.38%
stress/RefsInLoop.res 388,449,252 363,907,450 -6.32%
stress/NestedClosures.res 517,306,852 503,934,621 -2.58%
stress/InlineChain.res 137,338,530 136,731,260 -0.44%
packages/@rescript/belt/src/Belt_internalAVLtree.res 134,650,975 134,479,192 -0.13%

Output size

In total: 27,368,788 → 27,371,412 (+0.01%).

None of the 13 benchmarks changed by 0.1% or more.

Pull request 2b8872c compared with base 69e11bf in this run.

@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.47368% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.49%. Comparing base (69e11bf) to head (2b8872c).

Files with missing lines Patch % Lines
compiler/typing/typecore.ml 89.47% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9501      +/-   ##
==========================================
- Coverage   82.49%   82.49%   -0.01%     
==========================================
  Files         467      467              
  Lines       66125    66120       -5     
==========================================
- Hits        54553    54548       -5     
  Misses      11572    11572              
Files with missing lines Coverage Δ
compiler/typing/typecore.ml 89.22% <89.47%> (+0.05%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

The fix keeps an operand's first typed tree when its type fits, so each
way an operator takes its type needs a test that compiles and runs: from
the left operand, from the right operand, the fallback to int, and
nested operators. Each function only compiles if its operands get the
right type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt

cknitt commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 65b6611d86

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@pkg-pr-new

pkg-pr-new Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9501

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9501

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9501

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9501

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9501

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9501

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9501

commit: 2b8872c

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

A review found two gaps in the tests. Typing an operand again is also how
an operand whose type is int only inside a GADT branch type checks, and no
test covered that. And the left operand in ppwarning_operand_once was an
int, which was never typed twice, so it did not test the fix. It is
replaced by left operands that take their type from the right operand or
fall back to int. The changelog no longer says the cost grew by one
typing per level: it doubled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt requested a review from cristianoc December 1, 2026 21:15
@cknitt
cknitt merged commit 0fe7cd9 into master Dec 2, 2026
23 checks passed
@cknitt
cknitt deleted the fix-unified-ops-typed-twice branch December 2, 2026 06:11
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.

Operands of arithmetic operators are type checked twice

2 participants