diff --git a/changes/unified-ops-typed-once.fix.md b/changes/unified-ops-typed-once.fix.md new file mode 100644 index 0000000000..bfe1c3dd4b --- /dev/null +++ b/changes/unified-ops-typed-once.fix.md @@ -0,0 +1 @@ +- 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/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/compiler_tests/src/unified_ops_operand_test.mjs b/tests/compiler_tests/src/unified_ops_operand_test.mjs new file mode 100644 index 0000000000..5d74bb851c --- /dev/null +++ b/tests/compiler_tests/src/unified_ops_operand_test.mjs @@ -0,0 +1,65 @@ +// 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 intInBranch(w, x) { + return 1 + x | 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 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 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 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 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); + }); +}); + +export { + floatFromLeft, + stringFromLeft, + floatFromRight, + bigintFromRight, + intByDefault, + intInBranch, + 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..9332b99a8b --- /dev/null +++ b/tests/compiler_tests/src/unified_ops_operand_test.res @@ -0,0 +1,57 @@ +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 + +// 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) + +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("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 new file mode 100644 index 0000000000..a92ebdca3f --- /dev/null +++ b/tests/error_tests/single/expected/ppwarning_operand_once.res.expected @@ -0,0 +1,60 @@ + + 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 │ + + 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 │ + 6 │ // A left operand that takes the type of the right operand + + float operand + + + Warning number 22 + /.../fixtures/ppwarning_operand_once.res:7:34-69 + + 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 │ + + 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 new file mode 100644 index 0000000000..c6acead2e5 --- /dev/null +++ b/tests/error_tests/single/fixtures/ppwarning_operand_once.res @@ -0,0 +1,11 @@ +// 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) + +// 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)