Skip to content

Commit

Permalink
RakuAST: make redeclarations a typed worry / panic
Browse files Browse the repository at this point in the history
Until now, with RakUAST, lexical entries that were defined more than
once, would fail at the MAST generation stage.  Now, they will
properly worry / panic at the stage when being parsed.

This is achieved by having the .declare-lexical return a Bool that
indicates whether the name of the declaration already existed or not.
And then add the worry / throw the panic if it did exist already.

This does *not* however completely mimic the behaviour of the old
grammar, as in the case of a single worry, MAST generation still
complains of finding duplicate lexical symbols.  So I guess this
needs more investigation.
  • Loading branch information
lizmat committed Feb 27, 2023
1 parent 95e0a17 commit f2fb7b4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
20 changes: 13 additions & 7 deletions src/Raku/Actions.nqp
Expand Up @@ -383,9 +383,11 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
}

method label($/) {
my $label := self.r('Label').new(~$<identifier>);
$*R.declare-lexical($label);
self.attach: $/, $label;
my $name := ~$<identifier>;
my $decl := self.r('Label').new($name);
$/.typed_panic('X::Redeclaration', :symbol($name))
if $*R.declare-lexical($decl);
self.attach: $/, $decl;
}

method pblock($/) {
Expand Down Expand Up @@ -1573,7 +1575,8 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
$decl := self.r('VarDeclaration', 'Simple').new:
:$scope, :$type, :$name, :$initializer, :$shape;
if $scope eq 'my' || $scope eq 'state' || $scope eq 'our' {
$*R.declare-lexical($decl);
$/.typed_worry('X::Redeclaration', :symbol($name))
if $*R.declare-lexical($decl);
}
}
else {
Expand Down Expand Up @@ -1704,7 +1707,8 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
for $<trait> {
$decl.add-trait($_.ast);
}
$*R.declare-lexical($decl);
$/.typed_panic('X::Redeclaration', :symbol($name))
if $*R.declare-lexical($decl);
self.attach: $/, $decl;
}

Expand Down Expand Up @@ -2254,8 +2258,10 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
method param_term($/) {
if $<defterm> {
# Create sigilless target to bind into
my $decl := self.r('ParameterTarget', 'Term').new($<defterm>.ast);
$*R.declare-lexical($decl);
my $name := $<defterm>.ast;
my $decl := self.r('ParameterTarget', 'Term').new($name);
$/.typed_panic('X::Redeclaration', :symbol($name))
if $*R.declare-lexical($decl);
self.attach: $/, self.r('Parameter').new(target => $decl);
}
else {
Expand Down
7 changes: 5 additions & 2 deletions src/Raku/ast/resolver.rakumod
Expand Up @@ -983,9 +983,12 @@ class RakuAST::Resolver::Compile::Scope
}

method declare-lexical(RakuAST::Declaration $decl) {
nqp::die('Should not be calling declare-lexical in batch mode') if $!batch-mode;
nqp::die('Should not be calling declare-lexical in batch mode')
if $!batch-mode;
my $name := $decl.lexical-name;
my $existed := nqp::existskey($!live-decl-map, $name);
$!live-decl-map{$decl.lexical-name} := $decl;
Nil
$existed
}

method create-implicits() {
Expand Down

0 comments on commit f2fb7b4

Please sign in to comment.