Skip to content

Commit

Permalink
Force latebound lexical lookups in dyncomp'd code
Browse files Browse the repository at this point in the history
When we compile some code to run it at BEGIN time, and a closure is
taken and retained there, then it may have some connections to the
faked-up lexical environment it was compiled in. In such cases, force
lookups of lexicals to be late-bound, so that we do not get wrong
indexes. Fixes the issue exposed in
rakudo/rakudo#2684.
  • Loading branch information
jnthn committed Feb 12, 2019
1 parent 6bf0654 commit c267ae4
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/vm/moar/QAST/QASTCompilerMAST.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -1630,17 +1630,19 @@ my class MASTCompilerInstance {
my $lexref;
my $outer := 0;
my $block := $*BLOCK;
my $must_be_late_bound;
# find the block where the lexical was declared, if any
while nqp::istype($block, BlockInfo) {
last if $block.qast.ann('DYN_COMP_WRAPPER');
$must_be_late_bound := 1 if $block.qast.ann('DYNAMICALLY_COMPILED');
$lex := $block.lexical($name);
last if $lex;
$lexref := $block.lexicalref($name);
last if $lexref;
$block := $block.outer;
$outer++;
}
if $lex {
if $lex && !$must_be_late_bound {
$res_kind := $block.lexical_kind($name);
if $outer {
# need to create lex that knows how many frames to go out
Expand All @@ -1666,7 +1668,7 @@ my class MASTCompilerInstance {
%core_op_generators{'getlex'}($res_reg, $lex);
}
}
elsif $lexref {
elsif $lexref && !$must_be_late_bound{
if $*BINDVAL {
nqp::die('Cannot bind to QAST::Var resolving to a lexicalref');
}
Expand All @@ -1681,7 +1683,9 @@ my class MASTCompilerInstance {
$*REGALLOC.release_register($tmp_reg, $MVM_reg_obj);
}
else {
$res_kind := self.type_to_register_kind($node.returns);
$res_kind := $lex
?? $block.lexical_kind($name)
!! self.type_to_register_kind($node.returns);
if $*BINDVAL {
my $valmast := self.as_mast_clear_bindval($*BINDVAL, :want($res_kind));
$res_reg := $valmast.result_reg;
Expand Down

0 comments on commit c267ae4

Please sign in to comment.