Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/lam-bounded-vars-for.internal.md
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.
2 changes: 1 addition & 1 deletion compiler/backend/lam_bounded_vars.ml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ let rewrite (map : _ Hash_ident.t) (lam : Lambda.t) : Lambda.t =
let l1 = aux l1 in
let l2 = aux l2 in
let l3 = aux l3 in
Lambda.for_ ident (aux l1) l2 dir l3
Lambda.for_ ident l1 l2 dir l3
Comment thread
cknitt marked this conversation as resolved.
Resolved
| Lfor_of (ident, l1, l2) ->
let ident = rebind ident in
let l1 = aux l1 in
Expand Down
37 changes: 37 additions & 0 deletions tests/ounit_tests/ounit_lam_bounded_vars_tests.ml
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]
1 change: 1 addition & 0 deletions tests/ounit_tests/ounit_tests_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let suites =
Ounit_deep_flatten_tests.suites;
Ounit_exits_tests.suites;
Ounit_lam_size_tests.suites;
Ounit_lam_bounded_vars_tests.suites;
Ounit_sroa_tests.suites;
Ounit_ast_mapper0_tests.suites;
Ounit_ast_helper_tests.suites;
Expand Down
36 changes: 36 additions & 0 deletions tests/tests/src/inline_for_start_test.mjs
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 */
41 changes: 41 additions & 0 deletions tests/tests/src/inline_for_start_test.res
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)
})
})
Loading