Skip to content

Commit 69baeda

Browse files
committed
Treat uncurried application of primitives like curried application
Primitives are treated specially in the type checker, so that when fully applied, no function is generated. With uncurried application, primitives are wrapped with eta expansion before being used, because the encoding via `Js.Internal.opaqueFullApply` disables the special treatment by the type checker. This PR treats uncurried application of primitives like curried application, to help `Lam` compilation produce better code instead of leaving eta-expanded functions in the output.
1 parent 4a94992 commit 69baeda

7 files changed

+28
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ These are only breaking changes for unformatted code.
5454
- Remove method application via operator `##`, which does not exist in `.res` syntax https://github.com/rescript-lang/rescript-compiler/pull/5844
5555
- Treat `@meth` annotation as making the type uncurried for backwards compatibitly with some examples https://github.com/rescript-lang/rescript-compiler/pull/5845
5656
- Process `@set` annotation for field update as generating an uncurried function https://github.com/rescript-lang/rescript-compiler/pull/5846
57+
- Treat uncurried application of primitives like curried application, which produces better output https://github.com/rescript-lang/rescript-compiler/pull/5851
5758

5859
# 10.1.0-rc.6
5960

jscomp/ml/typecore.ml

+5-1
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,11 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
20412041
exp_attributes = sexp.pexp_attributes;
20422042
exp_env = env } in
20432043

2044-
if fully_applied then
2044+
let is_primitive = match funct.exp_desc with
2045+
| Texp_ident (_, _, {val_kind = Val_prim _}) -> true
2046+
| _ -> false in
2047+
2048+
if fully_applied && not is_primitive then
20452049
rue (apply_internal "opaqueFullApply" (mk_apply (apply_internal "opaque" funct) args))
20462050
else
20472051
rue (mk_apply funct args)

jscomp/test/UncurriedExternals.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ function tg(arr) {
2626

2727
var tc = Object.assign({}, "abc");
2828

29-
var te = (function (prim) {
30-
return prim;
31-
})({
32-
RE_EXN_ID: "Not_found"
33-
});
29+
var te = {
30+
RE_EXN_ID: "Not_found"
31+
};
3432

3533
var tcr = {};
3634

@@ -87,11 +85,9 @@ function tg$1(arr) {
8785

8886
var tc$1 = Object.assign({}, "abc");
8987

90-
var te$1 = (function (prim) {
91-
return prim;
92-
})({
93-
RE_EXN_ID: "Not_found"
94-
});
88+
var te$1 = {
89+
RE_EXN_ID: "Not_found"
90+
};
9591

9692
var tcr$1 = {};
9793

jscomp/test/bs_splice_partial.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function test_cb(x) {
3232
}
3333

3434
function f(x) {
35-
v(x);
35+
Curry._1(v, x);
3636
}
3737

3838
function testUndefined(param) {

lib/4.06.1/unstable/js_compiler.ml

+5-1
Original file line numberDiff line numberDiff line change
@@ -42771,7 +42771,11 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
4277142771
exp_attributes = sexp.pexp_attributes;
4277242772
exp_env = env } in
4277342773

42774-
if fully_applied then
42774+
let is_primitive = match funct.exp_desc with
42775+
| Texp_ident (_, _, {val_kind = Val_prim _}) -> true
42776+
| _ -> false in
42777+
42778+
if fully_applied && not is_primitive then
4277542779
rue (apply_internal "opaqueFullApply" (mk_apply (apply_internal "opaque" funct) args))
4277642780
else
4277742781
rue (mk_apply funct args)

lib/4.06.1/unstable/js_playground_compiler.ml

+5-1
Original file line numberDiff line numberDiff line change
@@ -42771,7 +42771,11 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
4277142771
exp_attributes = sexp.pexp_attributes;
4277242772
exp_env = env } in
4277342773

42774-
if fully_applied then
42774+
let is_primitive = match funct.exp_desc with
42775+
| Texp_ident (_, _, {val_kind = Val_prim _}) -> true
42776+
| _ -> false in
42777+
42778+
if fully_applied && not is_primitive then
4277542779
rue (apply_internal "opaqueFullApply" (mk_apply (apply_internal "opaque" funct) args))
4277642780
else
4277742781
rue (mk_apply funct args)

lib/4.06.1/whole_compiler.ml

+5-1
Original file line numberDiff line numberDiff line change
@@ -97765,7 +97765,11 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
9776597765
exp_attributes = sexp.pexp_attributes;
9776697766
exp_env = env } in
9776797767

97768-
if fully_applied then
97768+
let is_primitive = match funct.exp_desc with
97769+
| Texp_ident (_, _, {val_kind = Val_prim _}) -> true
97770+
| _ -> false in
97771+
97772+
if fully_applied && not is_primitive then
9776997773
rue (apply_internal "opaqueFullApply" (mk_apply (apply_internal "opaque" funct) args))
9777097774
else
9777197775
rue (mk_apply funct args)

0 commit comments

Comments
 (0)