Skip to content
Permalink
Browse files

Fix missing redeclaration of bound outer error

The code `my $a = 42; { say $a; my ($a) }` would fail to give an error
that `$a` cannot be redeclared because it is already bound. The error
was correctly reported in the non-signature case of a declaration (e.g.
`my $a = 42; { say $a; my $a }`). This makes sure it reports in both
cases, fixing #2570.
  • Loading branch information...
jnthn committed Jan 2, 2019
1 parent c11e7e6 commit 23663609a7dfd22fdd81a912fc7e094dcb2ac3b9
Showing with 26 additions and 12 deletions.
  1. +26 −12 src/Perl6/Actions.nqp
@@ -3413,13 +3413,20 @@ class Perl6::Actions is HLL::Actions does STDActions {

my $post := $_<post_constraints> ?? $_<post_constraints> !! [];
if $_<variable_name> {
my $past := QAST::Var.new( :name($_<variable_name>) );
$past := declare_variable($/, $past, $_<sigil>, $_<twigil>,
$_<desigilname>, $<trait>, :$post);
my $name := $_<variable_name>;
my $sigil := $_<sigil>;
my $twigil := $_<twigil>;
my $desigilname := $_<desigilname>;
if $desigilname {
ensure_unused_in_scope($/, $name, $twigil)
}
my $past := QAST::Var.new( :$name );
$past := declare_variable($/, $past, $sigil, $twigil,
$desigilname, $<trait>, :$post);
unless nqp::istype($past, QAST::Op) && $past.op eq 'null' {
$list.push($past);
if $_<sigil> eq '' {
nqp::push(@nosigil, ~$_<desigilname>);
if $sigil eq '' {
nqp::push(@nosigil, ~$desigilname);
}
}
}
@@ -3567,13 +3574,8 @@ class Perl6::Actions is HLL::Actions does STDActions {
if $lex.symbol($name) {
$/.typed_worry('X::Redeclaration', symbol => $name);
}
elsif $lex.ann('also_uses') && $lex.ann('also_uses'){$name} {
if ~$twigil eq '*' {
$/.typed_sorry('X::Dynamic::Postdeclaration', symbol => $name);
}
else {
$/.typed_sorry('X::Redeclaration::Outer', symbol => $name);
}
else {
ensure_unused_in_scope($/, $name, $twigil);
}
}
if nqp::elems($<semilist>) > 1 {
@@ -3582,6 +3584,18 @@ class Perl6::Actions is HLL::Actions does STDActions {
make declare_variable($/, $past, ~$sigil, ~$twigil, $desigilname, $<trait>, $<semilist>, :@post);
}

sub ensure_unused_in_scope($/, $name, $twigil) {
my $lex := $*W.cur_lexpad();
if $lex.ann('also_uses') && $lex.ann('also_uses'){$name} {
if ~$twigil eq '*' {
$/.typed_sorry('X::Dynamic::Postdeclaration', symbol => $name);
}
else {
$/.typed_sorry('X::Redeclaration::Outer', symbol => $name);
}
}
}

sub declare_variable($/, $past, $sigil, $twigil, $desigilname, $trait_list, $shape?, :@post) {
my $name := $sigil ~ $twigil ~ $desigilname;
my $BLOCK := $*W.cur_lexpad();

0 comments on commit 2366360

Please sign in to comment.
You can’t perform that action at this time.