Repository navigation
Fix operands of arithmetic operators being type checked twice #9501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+220
−36
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<int> | ||
|
|
||
| let intInBranch = (type a, w: intWitness<a>, 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) | ||
| }) | ||
| }) |
60 changes: 60 additions & 0 deletions
60
tests/error_tests/single/expected/ppwarning_operand_once.res.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
|
|
||
| [1;33mWarning number 22[0m | ||
| [36m/.../fixtures/ppwarning_operand_once.res[0m:[2m3:37-49[0m | ||
|
|
||
| 1 [2m│[0m // The operands of an arithmetic operator are type checked once, so a | ||
| 2 [2m│[0m // @ppwarning on one is reported once, however deeply it is nested. | ||
| [1;33m3[0m [2m│[0m let int = 1 + (1 + (1 + (@ppwarning([1;33m"int operand"[0m) 2))) | ||
| 4 [2m│[0m let float = 1.5 + (@ppwarning("float operand") 2.5) | ||
| 5 [2m│[0m | ||
|
|
||
| int operand | ||
|
|
||
|
|
||
| [1;33mWarning number 22[0m | ||
| [36m/.../fixtures/ppwarning_operand_once.res[0m:[2m4:31-45[0m | ||
|
|
||
| 2 [2m│[0m // @ppwarning on one is reported once, however deeply it is nested. | ||
| 3 [2m│[0m let int = 1 + (1 + (1 + (@ppwarning("int operand") 2))) | ||
| [1;33m4[0m [2m│[0m let float = 1.5 + (@ppwarning([1;33m"float operand"[0m) 2.5) | ||
| 5 [2m│[0m | ||
| 6 [2m│[0m // A left operand that takes the type of the right operand | ||
|
|
||
| float operand | ||
|
|
||
|
|
||
| [1;33mWarning number 22[0m | ||
| [36m/.../fixtures/ppwarning_operand_once.res[0m:[2m7:34-69[0m | ||
|
|
||
| 5 [2m│[0m | ||
| 6 [2m│[0m // A left operand that takes the type of the right operand | ||
| [1;33m7[0m [2m│[0m let fromRight = x => (@ppwarning([1;33m"left operand, float from the right"[0m) x | ||
| [2m│[0m [1;33m) + 1.5[0m | ||
| 8 [2m│[0m | ||
| 9 [2m│[0m // Neither operand has a known type, so both are ints | ||
|
|
||
| left operand, float from the right | ||
|
|
||
|
|
||
| [1;33mWarning number 22[0m | ||
| [36m/.../fixtures/ppwarning_operand_once.res[0m:[2m11:15-44[0m | ||
|
|
||
| 9 [2m│[0m // Neither operand has a known type, so both are ints | ||
| 10 [2m│[0m let byDefault = (x, y) => | ||
| [1;33m11[0m [2m│[0m (@ppwarning([1;33m"left operand, int by default"[0m) x) + (@ppwarning("right o | ||
| [2m│[0m [1;33mperand, int by default") y)[0m | ||
| 12 [2m│[0m | ||
|
|
||
| left operand, int by default | ||
|
|
||
|
|
||
| [1;33mWarning number 22[0m | ||
| [36m/.../fixtures/ppwarning_operand_once.res[0m:[2m11:64-94[0m | ||
|
|
||
| 9 [2m│[0m // Neither operand has a known type, so both are ints | ||
| 10 [2m│[0m let byDefault = (x, y) => | ||
| [1;33m11[0m [2m│[0m (@ppwarning("left operand, int by default") x) + (@ppwarning([1;33m"right o[0m | ||
| [2m│[0m [1;33mperand, int by default") y)[0m | ||
| 12 [2m│[0m | ||
|
|
||
| right operand, int by default |
11 changes: 11 additions & 0 deletions
11
tests/error_tests/single/fixtures/ppwarning_operand_once.res
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.