Semantic Diff

#9501 Fix operands of arithmetic operators being type checked twice

3 commits

69e11bfe7acda5984c1bf6bb2e7b10c320cd7df6..pr-9501

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

Fix operands of arithmetic operators being type checked twice

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.

modifiedDiagnostic @ppwarning on a nested operand

let x = 1 + (1 + (1 + (@ppwarning("operand") 2)))
diagnostic
Before
After
Warning number 22 (reported 8 times)Warning number 22 (reported once)operandoperand

Fixes #9500

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

Test the typing rules of arithmetic operators on variable operands

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.

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

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

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.