reasonml / reasonml/reason

unhygienic expansion and unintended normalization in refmt

Open
#2,334 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Printer
Dominant language
OCaml
Stars
10.3k
Forks
438
PR merge metrics
No merged PRs in 30d

Description

Consider:

let f = (x, p) => p(x);
let g = (a, b) => a + b;

let ___x = 42;

Js.log(7->f(g(_, ___x))); /* output 49 */

Now, change ___x to __x:

let f = (x, p) => p(x);
let g = (a, b) => a + b;

let __x = 42;

Js.log(7->f(g(_, __x))); /* output 14 */

The reason is that 7->f(g(_, __x)) is rewritten to 7->f(__x => g(__x, __x)) in the compiler. This causes an unintended variable capture akin to what we can see from unhygienic macros.

A related bug is that refmt normalizes 7->f(g(_, foo)) to 7->f(__x => g(__x, foo)). I argue that it should not do this. Even if the compiler is now correct, that is, it generates a fresh identifier that avoids unintended variable capture, outputting the generated identifier to users doesn't seem to be a good normalization. Furthermore, it doesn't look good aesthetically (although this might be subjective).

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the compiler rewrite of partial application and the refmt normalization described in the issue. Reproduce both ___x and __x examples, then verify that expansion avoids unintended variable capture and that refmt does not expose an unnecessary generated identifier.

Written by the indexing model from the issue text.

Assessment

Tech stack
ocaml
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.