11

I have the following Raku code:

class Thing {
    method close {
        say "closed";
    }
};

for 1..1000 {
    my $x will leave { .close } = Thing.new;
}

Running it, I get the error:

Lexical with name '$x' does not exist in this frame
  in block <unit> at c.raku line 7

Interestingly, this only happens if the number of iterations is high enough (with 500 or 100, I do not get any error messages).

If I change the body of the cycle to

my $x = Thing.new;
LEAVE $x.close;

then everything works without errors as well.

What is happening here? Did I misunderstand the will leave construct? (It seems to me the two versions should be equivalent.)

EDIT: Further observation – when running the code multiple times, the error appears nondeterministically. This suggests that the problem is somehow connected to garbage collection, but I am still confused as to what actually happens here.

I am using Rakudo v2021.03.

Elizabeth Mattijsen
  • 25,654
  • 3
  • 75
  • 105
Nikola Benes
  • 2,372
  • 1
  • 20
  • 33
  • 3
    Thank you for your report. FWIW, I cannot reproduce this on Rakudo 2021.05. Could you try this code in that version and tell us the result? – Elizabeth Mattijsen Jun 12 '21 at 20:33
  • 3
    I have tried running the same code using Rakudo 2021.05. The original code works fine, but if I increase the number of iterations to 10000, the error reappears. If I change the body of the `close` method to `say "closed ", $++;` I observe the error happening nondeterministically somewhere around 1500 to 2000 iterations. – Nikola Benes Jun 12 '21 at 20:48

1 Answers1

6

This is a bug. Have made an issue for it: https://github.com/rakudo/rakudo/issues/4403

I suggest using the workaround in the meantime.

Elizabeth Mattijsen
  • 25,654
  • 3
  • 75
  • 105