From 21490390d04cb678b8d45e5fac77f931294c0460 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Tue, 1 Dec 2026 20:26:50 +0000 Subject: [PATCH 1/3] 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. 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 Claude-Session: https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du Signed-off-by: Christoph Knittel --- changes/unified-ops-typed-once.fix.md | 1 + compiler/typing/typecore.ml | 62 ++++++++----------- .../ppwarning_operand_once.res.expected | 34 ++++++++++ .../fixtures/ppwarning_operand_once.res | 5 ++ 4 files changed, 66 insertions(+), 36 deletions(-) create mode 100644 changes/unified-ops-typed-once.fix.md create mode 100644 tests/error_tests/single/expected/ppwarning_operand_once.res.expected create mode 100644 tests/error_tests/single/fixtures/ppwarning_operand_once.res diff --git a/changes/unified-ops-typed-once.fix.md b/changes/unified-ops-typed-once.fix.md new file mode 100644 index 0000000000..34282b5336 --- /dev/null +++ b/changes/unified-ops-typed-once.fix.md @@ -0,0 +1 @@ +- Type check the operands of arithmetic operators once instead of once per nesting level, which also reported their diagnostics again. diff --git a/compiler/typing/typecore.ml b/compiler/typing/typecore.ml index ecc8bb1d93..394f383d87 100644 --- a/compiler/typing/typecore.ml +++ b/compiler/typing/typecore.ml @@ -4416,78 +4416,68 @@ and translate_unified_ops (env : Env.t) (funct : Typedtree.expression) let lhs_type = expand_head env lhs.exp_type in let rhs = type_exp ~context:None env rhs_expr in let rhs_type = expand_head env rhs.exp_type in + (* Gives the operand [typed], typed from [sexp], the type [ty]. Typing it + again would type nested operands once more at each level, and report + their diagnostics again. Only when [ty] does not fit is it typed + again, against [ty], for the error message that gives. *) + let expect typed sexp ty = + let snapshot = Btype.snapshot () in + try + (* An instance: [ty] is the shared predefined type *) + unify env typed.exp_type (instance_def ty); + typed + with Ctype.Unify _ -> + Btype.backtrack snapshot; + type_expect ~context:None env sexp ty + in let lhs, rhs, result_type = (* Rule 1. Try unifying to lhs *) match (lhs_type.desc, specialization) with | Tconstr (path, _, _), _ when Path.same path Predef.path_int -> - let rhs = - type_expect ~context:None env rhs_expr (Predef.type_int ()) - in + let rhs = expect rhs rhs_expr (Predef.type_int ()) in (lhs, rhs, instance_def (Predef.type_int ())) | Tconstr (path, _, _), {bool = Some _} when Path.same path Predef.path_bool -> - let rhs = - type_expect ~context:None env rhs_expr (Predef.type_bool ()) - in + let rhs = expect rhs rhs_expr (Predef.type_bool ()) in (lhs, rhs, instance_def (Predef.type_bool ())) | Tconstr (path, _, _), {float = Some _} when Path.same path Predef.path_float -> - let rhs = - type_expect ~context:None env rhs_expr (Predef.type_float ()) - in + let rhs = expect rhs rhs_expr (Predef.type_float ()) in (lhs, rhs, instance_def (Predef.type_float ())) | Tconstr (path, _, _), {bigint = Some _} when Path.same path Predef.path_bigint -> - let rhs = - type_expect ~context:None env rhs_expr (Predef.type_bigint ()) - in + let rhs = expect rhs rhs_expr (Predef.type_bigint ()) in (lhs, rhs, instance_def (Predef.type_bigint ())) | Tconstr (path, _, _), {string = Some _} when Path.same path Predef.path_string -> - let rhs = - type_expect ~context:None env rhs_expr (Predef.type_string ()) - in + let rhs = expect rhs rhs_expr (Predef.type_string ()) in (lhs, rhs, instance_def (Predef.type_string ())) | _ -> ( (* Rule 2. Try unifying to rhs *) match (rhs_type.desc, specialization) with | Tconstr (path, _, _), _ when Path.same path Predef.path_int -> - let lhs = - type_expect ~context:None env lhs_expr (Predef.type_int ()) - in + let lhs = expect lhs lhs_expr (Predef.type_int ()) in (lhs, rhs, instance_def (Predef.type_int ())) | Tconstr (path, _, _), {bool = Some _} when Path.same path Predef.path_bool -> - let lhs = - type_expect ~context:None env lhs_expr (Predef.type_bool ()) - in + let lhs = expect lhs lhs_expr (Predef.type_bool ()) in (lhs, rhs, instance_def (Predef.type_bool ())) | Tconstr (path, _, _), {float = Some _} when Path.same path Predef.path_float -> - let lhs = - type_expect ~context:None env lhs_expr (Predef.type_float ()) - in + let lhs = expect lhs lhs_expr (Predef.type_float ()) in (lhs, rhs, instance_def (Predef.type_float ())) | Tconstr (path, _, _), {bigint = Some _} when Path.same path Predef.path_bigint -> - let lhs = - type_expect ~context:None env lhs_expr (Predef.type_bigint ()) - in + let lhs = expect lhs lhs_expr (Predef.type_bigint ()) in (lhs, rhs, instance_def (Predef.type_bigint ())) | Tconstr (path, _, _), {string = Some _} when Path.same path Predef.path_string -> - let lhs = - type_expect ~context:None env lhs_expr (Predef.type_string ()) - in + let lhs = expect lhs lhs_expr (Predef.type_string ()) in (lhs, rhs, instance_def (Predef.type_string ())) | _ -> (* Rule 3. Fallback to int *) - let lhs = - type_expect ~context:None env lhs_expr (Predef.type_int ()) - in - let rhs = - type_expect ~context:None env rhs_expr (Predef.type_int ()) - in + let lhs = expect lhs lhs_expr (Predef.type_int ()) in + let rhs = expect rhs rhs_expr (Predef.type_int ()) in (lhs, rhs, instance_def (Predef.type_int ()))) in let targs = [(lhs_label, Some lhs); (rhs_label, Some rhs)] in diff --git a/tests/error_tests/single/expected/ppwarning_operand_once.res.expected b/tests/error_tests/single/expected/ppwarning_operand_once.res.expected new file mode 100644 index 0000000000..62568db8ee --- /dev/null +++ b/tests/error_tests/single/expected/ppwarning_operand_once.res.expected @@ -0,0 +1,34 @@ + + Warning number 22 + /.../fixtures/ppwarning_operand_once.res:3:37-49 + + 1 │ // The operands of an arithmetic operator are type checked once, so a + 2 │ // @ppwarning on one is reported once, however deeply it is nested. + 3 │ let int = 1 + (1 + (1 + (@ppwarning("int operand") 2))) + 4 │ let float = 1.5 + (@ppwarning("float operand") 2.5) + 5 │ let left = (@ppwarning("left operand") 1) + 2 + + int operand + + + Warning number 22 + /.../fixtures/ppwarning_operand_once.res:4:31-45 + + 2 │ // @ppwarning on one is reported once, however deeply it is nested. + 3 │ let int = 1 + (1 + (1 + (@ppwarning("int operand") 2))) + 4 │ let float = 1.5 + (@ppwarning("float operand") 2.5) + 5 │ let left = (@ppwarning("left operand") 1) + 2 + 6 │ + + float operand + + + Warning number 22 + /.../fixtures/ppwarning_operand_once.res:5:24-37 + + 3 │ let int = 1 + (1 + (1 + (@ppwarning("int operand") 2))) + 4 │ let float = 1.5 + (@ppwarning("float operand") 2.5) + 5 │ let left = (@ppwarning("left operand") 1) + 2 + 6 │ + + left operand \ No newline at end of file diff --git a/tests/error_tests/single/fixtures/ppwarning_operand_once.res b/tests/error_tests/single/fixtures/ppwarning_operand_once.res new file mode 100644 index 0000000000..c2fea20853 --- /dev/null +++ b/tests/error_tests/single/fixtures/ppwarning_operand_once.res @@ -0,0 +1,5 @@ +// The operands of an arithmetic operator are type checked once, so a +// @ppwarning on one is reported once, however deeply it is nested. +let int = 1 + (1 + (1 + (@ppwarning("int operand") 2))) +let float = 1.5 + (@ppwarning("float operand") 2.5) +let left = (@ppwarning("left operand") 1) + 2 From 65b6611d86e7392b5c7147ba34aa8158477b3628 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Tue, 1 Dec 2026 20:35:17 +0000 Subject: [PATCH 2/3] 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. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du Signed-off-by: Christoph Knittel --- .../src/unified_ops_operand_test.mjs | 59 +++++++++++++++++++ .../src/unified_ops_operand_test.res | 43 ++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 tests/compiler_tests/src/unified_ops_operand_test.mjs create mode 100644 tests/compiler_tests/src/unified_ops_operand_test.res diff --git a/tests/compiler_tests/src/unified_ops_operand_test.mjs b/tests/compiler_tests/src/unified_ops_operand_test.mjs new file mode 100644 index 0000000000..e3807ebed4 --- /dev/null +++ b/tests/compiler_tests/src/unified_ops_operand_test.mjs @@ -0,0 +1,59 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Mocha from "mocha"; +import * as Test_utils from "./test_utils.mjs"; + +function floatFromLeft(x) { + return 1.5 + x; +} + +function stringFromLeft(x) { + return "a" + x; +} + +function floatFromRight(x) { + return x + 2.5; +} + +function bigintFromRight(x) { + return x + 2n; +} + +function intByDefault(x, y) { + return x + y | 0; +} + +function nestedFloat(x) { + return 1.5 + (x + (x + 0.5)); +} + +function nestedFromRight(x) { + return x - (x - 1.5); +} + +Mocha.describe("Unified_ops_operand_test", () => { + Mocha.test("the type comes from the left operand", () => { + Test_utils.eq("File \"unified_ops_operand_test.res\", line 26, characters 7-14", 1.5 + 1.0, 2.5); + Test_utils.eq("File \"unified_ops_operand_test.res\", line 27, characters 7-14", "ab", "ab"); + }); + Mocha.test("the type comes from the right operand", () => { + Test_utils.eq("File \"unified_ops_operand_test.res\", line 31, characters 7-14", 1.0 + 2.5, 3.5); + Test_utils.eq("File \"unified_ops_operand_test.res\", line 32, characters 7-14", 1n + 2n, 3n); + }); + Mocha.test("without a known type the operator is on ints", () => Test_utils.eq("File \"unified_ops_operand_test.res\", line 36, characters 7-14", 3, 3)); + Mocha.test("nested operands", () => { + Test_utils.eq("File \"unified_ops_operand_test.res\", line 40, characters 7-14", 1.5 + (1.0 + (1.0 + 0.5)), 4.0); + Test_utils.eq("File \"unified_ops_operand_test.res\", line 41, characters 7-14", 2.0 - (2.0 - 1.5), 1.5); + }); +}); + +export { + floatFromLeft, + stringFromLeft, + floatFromRight, + bigintFromRight, + intByDefault, + nestedFloat, + nestedFromRight, +} +/* Not a pure module */ diff --git a/tests/compiler_tests/src/unified_ops_operand_test.res b/tests/compiler_tests/src/unified_ops_operand_test.res new file mode 100644 index 0000000000..85f318e0e7 --- /dev/null +++ b/tests/compiler_tests/src/unified_ops_operand_test.res @@ -0,0 +1,43 @@ +open Mocha +open Test_utils + +// An operator such as `+` takes its type from the left operand if that has a +// known type, else from the right one, else int. The operand that gets the +// type is given it after it has been typed once. Each function below only +// compiles if its operands get the right type. + +// From the left operand +let floatFromLeft = x => 1.5 + x +let stringFromLeft = x => "a" + x + +// From the right operand +let floatFromRight = x => x + 2.5 +let bigintFromRight = x => x + 2n + +// Neither operand has a known type: int +let intByDefault = (x, y) => x + y + +// Nested operators take the type through each level +let nestedFloat = x => 1.5 + (x + (x + 0.5)) +let nestedFromRight = x => x - (x - 1.5) + +describe(__MODULE__, () => { + test("the type comes from the left operand", () => { + eq(__LOC__, floatFromLeft(1.0), 2.5) + eq(__LOC__, stringFromLeft("b"), "ab") + }) + + test("the type comes from the right operand", () => { + eq(__LOC__, floatFromRight(1.0), 3.5) + eq(__LOC__, bigintFromRight(1n), 3n) + }) + + test("without a known type the operator is on ints", () => { + eq(__LOC__, intByDefault(1, 2), 3) + }) + + test("nested operands", () => { + eq(__LOC__, nestedFloat(1.0), 4.0) + eq(__LOC__, nestedFromRight(2.0), 1.5) + }) +}) From 2b8872c889abc9917e5b8a9c4b376c94f50e2186 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Tue, 1 Dec 2026 20:58:21 +0000 Subject: [PATCH 3/3] 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. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du Signed-off-by: Christoph Knittel --- changes/unified-ops-typed-once.fix.md | 2 +- .../src/unified_ops_operand_test.mjs | 20 ++++++--- .../src/unified_ops_operand_test.res | 14 ++++++ .../ppwarning_operand_once.res.expected | 44 +++++++++++++++---- .../fixtures/ppwarning_operand_once.res | 8 +++- 5 files changed, 70 insertions(+), 18 deletions(-) diff --git a/changes/unified-ops-typed-once.fix.md b/changes/unified-ops-typed-once.fix.md index 34282b5336..bfe1c3dd4b 100644 --- a/changes/unified-ops-typed-once.fix.md +++ b/changes/unified-ops-typed-once.fix.md @@ -1 +1 @@ -- Type check the operands of arithmetic operators once instead of once per nesting level, which also reported their diagnostics again. +- 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. diff --git a/tests/compiler_tests/src/unified_ops_operand_test.mjs b/tests/compiler_tests/src/unified_ops_operand_test.mjs index e3807ebed4..5d74bb851c 100644 --- a/tests/compiler_tests/src/unified_ops_operand_test.mjs +++ b/tests/compiler_tests/src/unified_ops_operand_test.mjs @@ -23,6 +23,10 @@ function intByDefault(x, y) { return x + y | 0; } +function intInBranch(w, x) { + return 1 + x | 0; +} + function nestedFloat(x) { return 1.5 + (x + (x + 0.5)); } @@ -33,17 +37,18 @@ function nestedFromRight(x) { Mocha.describe("Unified_ops_operand_test", () => { Mocha.test("the type comes from the left operand", () => { - Test_utils.eq("File \"unified_ops_operand_test.res\", line 26, characters 7-14", 1.5 + 1.0, 2.5); - Test_utils.eq("File \"unified_ops_operand_test.res\", line 27, characters 7-14", "ab", "ab"); + Test_utils.eq("File \"unified_ops_operand_test.res\", line 36, characters 7-14", 1.5 + 1.0, 2.5); + Test_utils.eq("File \"unified_ops_operand_test.res\", line 37, characters 7-14", "ab", "ab"); }); Mocha.test("the type comes from the right operand", () => { - Test_utils.eq("File \"unified_ops_operand_test.res\", line 31, characters 7-14", 1.0 + 2.5, 3.5); - Test_utils.eq("File \"unified_ops_operand_test.res\", line 32, characters 7-14", 1n + 2n, 3n); + Test_utils.eq("File \"unified_ops_operand_test.res\", line 41, characters 7-14", 1.0 + 2.5, 3.5); + Test_utils.eq("File \"unified_ops_operand_test.res\", line 42, characters 7-14", 1n + 2n, 3n); }); - Mocha.test("without a known type the operator is on ints", () => Test_utils.eq("File \"unified_ops_operand_test.res\", line 36, characters 7-14", 3, 3)); + Mocha.test("without a known type the operator is on ints", () => Test_utils.eq("File \"unified_ops_operand_test.res\", line 46, characters 7-14", 3, 3)); + Mocha.test("an operand whose type is int only in a GADT branch", () => Test_utils.eq("File \"unified_ops_operand_test.res\", line 50, characters 7-14", 3, 3)); Mocha.test("nested operands", () => { - Test_utils.eq("File \"unified_ops_operand_test.res\", line 40, characters 7-14", 1.5 + (1.0 + (1.0 + 0.5)), 4.0); - Test_utils.eq("File \"unified_ops_operand_test.res\", line 41, characters 7-14", 2.0 - (2.0 - 1.5), 1.5); + Test_utils.eq("File \"unified_ops_operand_test.res\", line 54, characters 7-14", 1.5 + (1.0 + (1.0 + 0.5)), 4.0); + Test_utils.eq("File \"unified_ops_operand_test.res\", line 55, characters 7-14", 2.0 - (2.0 - 1.5), 1.5); }); }); @@ -53,6 +58,7 @@ export { floatFromRight, bigintFromRight, intByDefault, + intInBranch, nestedFloat, nestedFromRight, } diff --git a/tests/compiler_tests/src/unified_ops_operand_test.res b/tests/compiler_tests/src/unified_ops_operand_test.res index 85f318e0e7..9332b99a8b 100644 --- a/tests/compiler_tests/src/unified_ops_operand_test.res +++ b/tests/compiler_tests/src/unified_ops_operand_test.res @@ -17,6 +17,16 @@ let bigintFromRight = x => x + 2n // Neither operand has a known type: int let intByDefault = (x, y) => x + y +// The operand's type is int only inside the GADT branch, so it is typed +// again against int, which succeeds +type rec intWitness<_> = IsInt: intWitness + +let intInBranch = (type a, w: intWitness, x: a) => + 1 + + switch w { + | IsInt => x + } + // Nested operators take the type through each level let nestedFloat = x => 1.5 + (x + (x + 0.5)) let nestedFromRight = x => x - (x - 1.5) @@ -36,6 +46,10 @@ describe(__MODULE__, () => { eq(__LOC__, intByDefault(1, 2), 3) }) + test("an operand whose type is int only in a GADT branch", () => { + eq(__LOC__, intInBranch(IsInt, 2), 3) + }) + test("nested operands", () => { eq(__LOC__, nestedFloat(1.0), 4.0) eq(__LOC__, nestedFromRight(2.0), 1.5) diff --git a/tests/error_tests/single/expected/ppwarning_operand_once.res.expected b/tests/error_tests/single/expected/ppwarning_operand_once.res.expected index 62568db8ee..a92ebdca3f 100644 --- a/tests/error_tests/single/expected/ppwarning_operand_once.res.expected +++ b/tests/error_tests/single/expected/ppwarning_operand_once.res.expected @@ -6,7 +6,7 @@ 2 │ // @ppwarning on one is reported once, however deeply it is nested. 3 │ let int = 1 + (1 + (1 + (@ppwarning("int operand") 2))) 4 │ let float = 1.5 + (@ppwarning("float operand") 2.5) - 5 │ let left = (@ppwarning("left operand") 1) + 2 + 5 │ int operand @@ -17,18 +17,44 @@ 2 │ // @ppwarning on one is reported once, however deeply it is nested. 3 │ let int = 1 + (1 + (1 + (@ppwarning("int operand") 2))) 4 │ let float = 1.5 + (@ppwarning("float operand") 2.5) - 5 │ let left = (@ppwarning("left operand") 1) + 2 - 6 │ + 5 │ + 6 │ // A left operand that takes the type of the right operand float operand Warning number 22 - /.../fixtures/ppwarning_operand_once.res:5:24-37 + /.../fixtures/ppwarning_operand_once.res:7:34-69 - 3 │ let int = 1 + (1 + (1 + (@ppwarning("int operand") 2))) - 4 │ let float = 1.5 + (@ppwarning("float operand") 2.5) - 5 │ let left = (@ppwarning("left operand") 1) + 2 - 6 │ + 5 │ + 6 │ // A left operand that takes the type of the right operand + 7 │ let fromRight = x => (@ppwarning("left operand, float from the right") x + │ ) + 1.5 + 8 │ + 9 │ // Neither operand has a known type, so both are ints + + left operand, float from the right + + + Warning number 22 + /.../fixtures/ppwarning_operand_once.res:11:15-44 + + 9 │ // Neither operand has a known type, so both are ints + 10 │ let byDefault = (x, y) => + 11 │ (@ppwarning("left operand, int by default") x) + (@ppwarning("right o + │ perand, int by default") y) + 12 │ + + left operand, int by default + + + Warning number 22 + /.../fixtures/ppwarning_operand_once.res:11:64-94 + + 9 │ // Neither operand has a known type, so both are ints + 10 │ let byDefault = (x, y) => + 11 │ (@ppwarning("left operand, int by default") x) + (@ppwarning("right o + │ perand, int by default") y) + 12 │ - left operand \ No newline at end of file + right operand, int by default \ No newline at end of file diff --git a/tests/error_tests/single/fixtures/ppwarning_operand_once.res b/tests/error_tests/single/fixtures/ppwarning_operand_once.res index c2fea20853..c6acead2e5 100644 --- a/tests/error_tests/single/fixtures/ppwarning_operand_once.res +++ b/tests/error_tests/single/fixtures/ppwarning_operand_once.res @@ -2,4 +2,10 @@ // @ppwarning on one is reported once, however deeply it is nested. let int = 1 + (1 + (1 + (@ppwarning("int operand") 2))) let float = 1.5 + (@ppwarning("float operand") 2.5) -let left = (@ppwarning("left operand") 1) + 2 + +// A left operand that takes the type of the right operand +let fromRight = x => (@ppwarning("left operand, float from the right") x) + 1.5 + +// Neither operand has a known type, so both are ints +let byDefault = (x, y) => + (@ppwarning("left operand, int by default") x) + (@ppwarning("right operand, int by default") y)