I'm attaching this bug report to an existing ticket (RT#56184) because
I believe they are in fact the same problem. But I think this
description may make the problem more clear.
Summary: Lexical access from immediate blocks in closures don't work.
Consider the following Perl 6 code:
sub foo() {
my $a = 'hello';
my sub bar($b) { say $a, $b; { say $a, $b; } }
return &bar;
}
my $x = foo();
$x('world');
The C<foo> sub returns a C<bar> closure that is then invoked
from main. Within C<bar> there is an immediate block, and this
is where lexicals don't work.
Here's a PIR translation of the above Perl 6:
.sub 'main' :main
.local pmc x
x = 'foo'()
x('world')
.end
.sub 'foo' :outer('main')
.local pmc a, bar
a = new 'String'
a = 'hello '
.lex '$a', a
$P0 = get_global 'bar'
bar = newclosure $P0
.return (bar)
.end
.sub 'bar' :outer('foo')
.param pmc b
.lex '$b', b
.local pmc a
a = find_lex '$a'
print a
say b
'bar_inner'()
.end
.sub 'bar_inner' :outer('bar')
.local pmc a, b
a = find_lex '$a'
b = find_lex '$b'
print a
say b
.end
When run, Parrot r28767 produces the following:
$ ./parrot lex3.pir
hello world
hello Null PMC access in get_string()
current instr.: 'bar_inner' pc -86686 ((unknown file):-1)
called from Sub 'bar_inner' pc 85 (lex3.pir:43)
called from Sub 'bar' pc 63 (lex3.pir:34)
called from Sub 'main' pc 15 (lex3.pir:14)
$
As you can see, the first "say $a, $b" in the closure works fine,
but the lexical accesses in the nested immediate block do not.
Comments and suggestions welcomed.
Pm