Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
forbid to put variable in initializer of its declaration
This fixes RT #125371. Variables in initializers in subsequent blocks are allowed though.
  • Loading branch information
FROGGS committed Aug 17, 2015
1 parent ccfb5fb commit 3754356
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/Perl6/Grammar.nqp
Expand Up @@ -1233,8 +1233,10 @@ grammar Perl6::Grammar is HLL::Grammar does STD {
:my $*POD_BLOCK;
:my $*DOC := $*DECLARATOR_DOCS;
:my $*LINE_NO := HLL::Compiler.lineof(self.orig(), self.from(), :cache(1));
{ $*DECLARATOR_DOCS := '' }
{
$*DECLARATOR_DOCS := '';
$*VARIABLE := '' if $*VARIABLE;

if $*PRECEDING_DECL_LINE < $*LINE_NO {
$*PRECEDING_DECL_LINE := $*LINE_NO;
$*PRECEDING_DECL := $*DECLARAND;
Expand Down Expand Up @@ -1951,6 +1953,8 @@ grammar Perl6::Grammar is HLL::Grammar does STD {
[
| :dba('infix noun') '&[' ~ ']' <infixish('[]')>
| <sigil> <twigil>? <desigilname>
[ <?{ !$*IN_DECL && $*VARIABLE && $*VARIABLE eq $<sigil> ~ $<twigil> ~ $<desigilname> }>
{ self.typed_panic: 'X::Syntax::Variable::Initializer', name => $*VARIABLE } ]?
| <special_variable>
| <sigil> $<index>=[\d+] [<?{ $*IN_DECL }> <.typed_panic: "X::Syntax::Variable::Numeric">]?
| <sigil> <?[<]> <postcircumfix> [<?{ $*IN_DECL }> <.typed_panic('X::Syntax::Variable::Match')>]?
Expand Down Expand Up @@ -2259,6 +2263,7 @@ grammar Perl6::Grammar is HLL::Grammar does STD {

token declarator {
:my $*LEFTSIGIL := '';
:my $*VARIABLE := '';
[
# STD.pm6 uses <defterm> here, but we need different
# action methods
Expand Down Expand Up @@ -2364,19 +2369,18 @@ grammar Perl6::Grammar is HLL::Grammar does STD {

token variable_declarator {
:my $*IN_DECL := 'variable';
:my $var;
<variable>
{
$var := $<variable>.Str;
$/.CURSOR.add_variable($var);
$*VARIABLE := $<variable>.Str;
$/.CURSOR.add_variable($*VARIABLE);
$*IN_DECL := '';
}
[
<.unsp>?
$<shape>=[
| '(' ~ ')' <signature>
{
my $sigil := nqp::substr($var, 0, 1);
my $sigil := nqp::substr($*VARIABLE, 0, 1);
if $sigil eq '&' {
self.typed_sorry('X::Syntax::Reserved',
reserved => '() shape syntax in routine declarations',
Expand Down
5 changes: 5 additions & 0 deletions src/core/Exception.pm
Expand Up @@ -1087,6 +1087,11 @@ my class X::Syntax::Variable::Match does X::Syntax {
method message() { 'Cannot declare a match variable' }
}

my class X::Syntax::Variable::Initializer does X::Syntax {
has $.name = '<anon>';
method message() { "Cannot use variable $!name in declaration to initialize itself" }
}


my class X::Syntax::Variable::Twigil does X::Syntax {
has $.what = 'variable';
Expand Down

0 comments on commit 3754356

Please sign in to comment.