dart-archive / kernel

Dart IR (Intermediate Representation) -- moved to dart-lang/sdk
BSD 3-Clause "New" or "Revised" License
20 stars 3 forks source link

Closure conversion: for loops #32

Closed peter-ahe-google closed 8 years ago

peter-ahe-google commented 8 years ago

Closure conversion needs to support capturing the for-loop variable and pay special attention to the fact that a new variable is introduced per iteration.

peter-ahe-google commented 8 years ago

Proposed fix in CL 520497013.

peter-ahe-google commented 8 years ago

Fixed by 34036c5aac323fe251db558ab9986eaa06408602.

peter-ahe-google commented 8 years ago

The fix wasn't correct.

@asgerf has made a test that demonstrates why.

The problem is that other initializers can capture previously defined loop variables, and that updates can also capture loop variables. For example:

  var closure;
  for (var i=0, fn = () => i; i < 3; i++) {
    i += 1;
    closure = fn;
  }
  Expect.equals(1, closure());
peter-ahe-google commented 8 years ago

Fixed by 6b464934860f5898ea71285306096ff60bad4563.