Repository navigation
Rename a for loop's start expression once when inlining #9396
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
Merged
Changes from all commits
Commits
Show all changes
2 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 @@ | ||
| - Rename the variables in a for loop's start expression once instead of twice when inlining a function. |
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,37 @@ | ||
| let ( >:: ), ( >::: ) = OUnit.(( >:: ), ( >::: )) | ||
|
|
||
| let int i = Lambda.const (Lambda.const_int i) | ||
|
|
||
| (* [for i in {let s = 1; s} to 10 { i }]: a binder in the start expression *) | ||
| let i = Ident.create "i" | ||
|
|
||
| let s = Ident.create "s" | ||
|
|
||
| let loop = | ||
| Lambda.for_ i | ||
| (Lambda.let_ Strict s (int 1) (Lambda.var s)) | ||
| (int 10) Upto (Lambda.var i) | ||
|
|
||
| (* [rewrite] gives each bound variable one fresh name and records it in the | ||
| map: the binder in the result, its uses and the map agree, and the map has | ||
| one entry per binder. Renaming the start expression twice gave its binder | ||
| a second name the map didn't record. *) | ||
| let test_for_start_is_renamed_once _ = | ||
| let map = Hash_ident.create 4 in | ||
| match Lam_bounded_vars.rewrite map loop with | ||
| | Lfor (i', Llet (_, s', _, Lvar s_use), _, _, Lvar i_use) -> | ||
| OUnit.assert_bool "the loop variable is renamed" (not (Ident.same i' i)); | ||
| OUnit.assert_bool "its use follows" (Ident.same i_use i'); | ||
| OUnit.assert_bool "the start binder is renamed" (not (Ident.same s' s)); | ||
| OUnit.assert_bool "its use follows" (Ident.same s_use s'); | ||
| (match Hash_ident.find_opt map s with | ||
| | Some (Lvar recorded) -> | ||
| OUnit.assert_bool "the map records the binder's name" | ||
| (Ident.same recorded s') | ||
| | _ -> OUnit.assert_failure "the start binder is in the map"); | ||
| OUnit.assert_equal ~printer:string_of_int ~msg:"one entry per binder" 2 | ||
| (Hash_ident.length map) | ||
| | _ -> OUnit.assert_failure "a for loop with a let in its start" | ||
|
|
||
| let suites = | ||
| __FILE__ >::: ["for_start_is_renamed_once" >:: test_for_start_is_renamed_once] |
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,36 @@ | ||
| // Generated by ReScript, PLEASE EDIT WITH CARE | ||
|
|
||
| import * as Mocha from "mocha"; | ||
| import * as Test_utils from "./test_utils.mjs"; | ||
| import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.mjs"; | ||
|
|
||
| function fromBlock(x) { | ||
| let acc = 0; | ||
| let s = (x << 1); | ||
| for (let i = s - x | 0, i_finish = x + 3 | 0; i <= i_finish; ++i) { | ||
| acc = acc + i | 0; | ||
| } | ||
| return acc; | ||
| } | ||
|
|
||
| function fromCallback(xs, x) { | ||
| let acc = 0; | ||
| for (let i = Stdlib_Array.reduce(xs, 0, (a, b) => a + b | 0); i <= x; ++i) { | ||
| acc = acc + i | 0; | ||
| } | ||
| return acc; | ||
| } | ||
|
|
||
| Mocha.describe("Inline_for_start_test", () => { | ||
| Mocha.test("a let in the start expression", () => Test_utils.eq("File \"inline_for_start_test.res\", line 35, characters 7-14", fromBlock(2), 14)); | ||
| Mocha.test("a callback in the start expression", () => Test_utils.eq("File \"inline_for_start_test.res\", line 39, characters 7-14", fromCallback([ | ||
| 1, | ||
| 2 | ||
| ], 5), 12)); | ||
| }); | ||
|
|
||
| export { | ||
| fromBlock, | ||
| fromCallback, | ||
| } | ||
| /* 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,41 @@ | ||
| open Mocha | ||
| open Test_utils | ||
|
|
||
| // Applying a function literal right away inlines it, which renames the | ||
| // variables bound in its body, including those in a for loop's start | ||
| // expression: a let in a block, and a callback's parameters. | ||
| let fromBlock = x => | ||
| ( | ||
| n => { | ||
| let acc = ref(0) | ||
| for i in { | ||
| let s = n * 2 | ||
| s - n | ||
| } to n + 3 { | ||
| acc := acc.contents + i | ||
| } | ||
| acc.contents | ||
| } | ||
| )(x) | ||
|
|
||
| let fromCallback = (xs, x) => | ||
| ( | ||
| n => { | ||
| let acc = ref(0) | ||
| for i in xs->Array.reduce(0, (a, b) => a + b) to n { | ||
| acc := acc.contents + i | ||
| } | ||
| acc.contents | ||
| } | ||
| )(x) | ||
|
|
||
| describe(__MODULE__, () => { | ||
| test("a let in the start expression", () => { | ||
| // 2 + 3 + 4 + 5 | ||
| eq(__LOC__, fromBlock(2), 14) | ||
| }) | ||
| test("a callback in the start expression", () => { | ||
| // 3 + 4 + 5 | ||
| eq(__LOC__, fromCallback([1, 2], 5), 12) | ||
| }) | ||
| }) |
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.