Skip to content
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

8.0 regression: unused list variables that include side effects generate invalid JS #4491

Closed
TheSpyder opened this issue Jun 29, 2020 · 1 comment

Comments

@TheSpyder
Copy link
Contributor

TheSpyder commented Jun 29, 2020

Here's a simple function with an unused variable that creates a list (playground link):

let f xs =
  let unused =
    match xs with
    | Some (l) -> [l; l]
    | None  -> [1; 2]
  in
  Js.log2 "nothing to see here" xs

In the generated JS, the variable is removed completely:

function f(xs) {
  console.log("nothing to see here", xs);
}

However, if this unused variable has a side effect, the list is created but never assigned to a variable. JS arrays are allowed on lines by themselves (a weird edge case of the syntax, I guess) but objects are not.

Function with a side effect (playground link):

let f xs =
  let unused =
    match xs with
    | Some (l) -> Js.log "side effect"; [l; l]
    | None  -> [1; 2]
  in
  Js.log2 "nothing to see here" xs

v7 generates:

function f(xs) {
  if (xs !== undefined) {
    console.log("side effect");
    /* :: */[
      xs,
      /* :: */[
        xs,
        /* [] */0
      ]
    ];
  } else {
    /* :: */[
      1,
      /* :: */[
        2,
        /* [] */0
      ]
    ];
  }
  console.log("nothing to see here", xs);
}

Which is weird, but valid and works.

v8 generates:

function f(xs) {
  if (xs !== undefined) {
    console.log("side effect");
    {
      hd: xs,
      tl: {
        hd: xs,
        tl: /* [] */0
      }
    };
  } else {
    {
      hd: 1,
      tl: {
        hd: 2,
        tl: /* [] */0
      }
    };
  }
  console.log("nothing to see here", xs); 
}

Which triggers SyntaxError: Unexpected token on the tl: { line.

bobzhang added a commit that referenced this issue Jun 29, 2020
bobzhang added a commit that referenced this issue Jun 29, 2020
bobzhang added a commit that referenced this issue Jun 29, 2020
bobzhang added a commit that referenced this issue Jun 29, 2020
@bobzhang
Copy link
Member

the fix is released in 8.0.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants