Skip to content

Commit

Permalink
Fix issue with uncurried function with 1 arg being a variable
Browse files Browse the repository at this point in the history
Fixes #6504

Uncurried functions with 1 unit argument are emitted without the parameter. This leaves a possible undefined id in the emitted code if the parameter is a variable.
This PR limits the optimization to cases where the parameter is explicitly `()`, as opposed to e.g. a variable whose type is inferred to be unit.
  • Loading branch information
cristianoc committed Dec 5, 2023
1 parent 363257e commit 3ec3479
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#### :bug: Bug Fix
- Fix issue where an inline record with attributes did not parse. https://github.com/rescript-lang/rescript-compiler/pull/6499
- Fix issue with uncurried function with 1 arg being a variable where an undefined variable could be emitted. https://github.com/rescript-lang/rescript-compiler/pull/6507

# 11.0.0-rc.6

Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/lam_pass_alpha_conversion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ let alpha_conversion (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
| None -> Lam.prim ~primitive ~args:[ simpl arg ] loc)
| Lprim { primitive = Pjs_fn_make_unit; args = [ arg ]; loc } ->
let arg = match arg with
| Lfunction ({arity=1; params=[x]; attr; body}) ->
| Lfunction ({arity=1; params=[x]; attr; body}) when Ident.name x = "param" (* "()" *) ->
Lam.function_ ~params:[x] ~attr:{attr with oneUnitArg=true} ~body ~arity:1
| _ -> arg in
simpl arg
Expand Down
25 changes: 24 additions & 1 deletion jscomp/test/UncurriedAlways.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions jscomp/test/UncurriedAlways.res
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,13 @@ module OptMixed = {
let c3 = ptl(~x="x", ~z="z", ~d2="d2<-200", ~d4="d4<-400")
Js.log2("c3:", c3)
}

let fn = cb => {
cb()
}

fn(s => Js.log(#foo(s)))

let fn1 = (a, b, ()) => a() + b

let a = fn1(() => 1, 2, _)

0 comments on commit 3ec3479

Please sign in to comment.