External Issue: Compiler bug with lambdas capturing variables defined inside functions

18703, "rj-jesus", "Compiler bug with lambdas capturing variables defined inside functions", "2021-11-09T18:09:31Z"

It seems there's a compiler bug with lambdas that implicitly capture variables scoped inside functions. For example, the code below

proc f() {
  // if this is moved to outside of `f' (above it), `g1' works
  var x = 1.0;

  // with `x` defined inside `f`, this raises a compiler bug
  var g1 = lambda(i: int) { return i:real+x; };
  writeln(+reduce g1(0..9));

  // this works, the result is 55.0
  //var g2 = lambda(i: int, x: real) { return i:real+x; };
  //writeln(+reduce g2(0..9, x));
}

f();

// this also works fine
//var x = 1.0;
//var g3 = lambda(i: int) { return i:real+x; };
//writeln(+reduce g3(0..9));

results in

zzz.chpl:208: internal error: RES-ADD-LLS-0803 chpl version 1.25.0

As mentioned in the comments, if x is moved to outside of f, the code compiles fine. Alternatively, if x is passed to the lambda as an explicit argument, as in g2, the code also works fine. If everything is defined outside of f, as in g3, everything also works as expected.