Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix error reporting for anon vars [RT #123584]
`$; my $b;` now gives correctly: Useless use of variable $ in sink context (line 1)
  • Loading branch information
FROGGS committed Feb 7, 2015
1 parent 1b24050 commit d728669
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/Perl6/Actions.nqp
Expand Up @@ -2263,6 +2263,7 @@ class Perl6::Actions is HLL::Actions does STDActions {
}
elsif $*SCOPE eq 'my' || $*SCOPE eq 'our' || $*SCOPE eq 'state' {
# Some things can't be done to our vars.
my $varname;
if $*SCOPE eq 'our' {
if $*OFTYPE {
$/.CURSOR.panic("Cannot put a type constraint on an 'our'-scoped variable");
Expand All @@ -2278,14 +2279,15 @@ class Perl6::Actions is HLL::Actions does STDActions {
if $twigil {
$/.CURSOR.panic("Cannot have an anonymous variable with a twigil");
}
$name := QAST::Node.unique($sigil ~ 'ANON_VAR_');
$name := QAST::Node.unique($sigil ~ 'ANON_VAR_');
$varname := $sigil;
}

# Create a container descriptor. Default to rw and set a
# type if we have one; a trait may twiddle with that later.
my %cont_info := $*W.container_type_info($/, $sigil, $*OFTYPE ?? [$*OFTYPE.ast] !! [], $shape);
my $descriptor := $*W.create_container_descriptor(
%cont_info<value_type>, 1, $name, %cont_info<default_value>);
%cont_info<value_type>, 1, $varname || $name, %cont_info<default_value>);

# Install the container.
my $cont := $*W.install_lexical_container($BLOCK, $name, %cont_info, $descriptor,
Expand Down
20 changes: 16 additions & 4 deletions src/Perl6/Optimizer.nqp
Expand Up @@ -173,6 +173,17 @@ my class Symbols {
nqp::die("Optimizer: No lexical $name found");
}

method find_lexical_symbol($name) {
my int $i := +@!block_stack;
while $i > 0 {
$i := $i - 1;
my $block := @!block_stack[$i];
my %sym := $block.symbol($name);
return %sym if +%sym;
}
nqp::die("Optimizer: No lexical $name found");
}

# Checks if a given lexical is declared, though it needn't have a compile
# time known value.
method is_lexical_declared($name) {
Expand Down Expand Up @@ -1508,14 +1519,15 @@ class Perl6::Optimizer {
if $!void_context && !$!in_declaration && $var.name && !$var.ann('sink_ok') {
# stuff like Nil is also stored in a QAST::Var, but
# we certainly don't want to warn about that one.
my str $sigil := nqp::substr($var.name, 0, 1);
my str $name := nqp::substr($var.name, 1);
my str $name := try $!symbols.find_lexical_symbol($var.name)<descriptor>.name;
$name := $var.name unless $name;
my str $sigil := nqp::substr($name, 0, 1);
if $sigil eq '$' || $sigil eq '@' || $sigil eq '%' {
$!problems.add_worry(
$var,
$name eq ''
$name eq $sigil
?? "Useless use of unnamed $sigil variable in sink context"
!! "Useless use of variable $sigil$name in sink context"
!! "Useless use of variable $name in sink context"
);
return $NULL;
}
Expand Down

0 comments on commit d728669

Please sign in to comment.