Skip to content

let a = ref(None) should not compile to var a; in a for loop #6647

Closed
@aaronz0

Description

@aaronz0

The following code:

   for i in 1 to 5 {
     let a = ref(None)
     if i == 3 {
       a := Some(3)
     }
     Js.log(a.contents)
   }

gets compiled to:

for(var i = 1; i <= 5; ++i){
  var a;
  if (i === 3) {
    a = 3;
  }
  console.log(a);
}

rescript playground

reason playground

Expected result:
Variable a is re-initialized to undefined in each iteration of the loop.

undefined
undefined
3
undefined
undefined

Actual result:

undefined
undefined
3
3
3

Correct compilation result should be:

for(var i = 1; i <= 5; ++i){
  var a = undefined;
  if (i === 3) {
    a = 3;
  }
  console.log(a);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions