Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get “Lexical with name '$x' does not exist in this frame” when using “will leave”?

Tags:

raku

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.

like image 382
Nikola Benes Avatar asked Jun 12 '21 14:06

Nikola Benes


1 Answers

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.

like image 109
Elizabeth Mattijsen Avatar answered Oct 15 '22 11:10

Elizabeth Mattijsen