Skip to content

Commit

Permalink
RakuAST: throw correct exception type on redeclaration of packages
Browse files Browse the repository at this point in the history
  • Loading branch information
niner committed Mar 10, 2023
1 parent 5d7e385 commit f49b108
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Raku/Grammar.nqp
Expand Up @@ -3158,6 +3158,7 @@ if $*COMPILING_CORE_SETTING {
self.set_actions($actions);
$*R.outer-scope.merge-generated-lexical-declaration(
:resolver($*R),
self.actions.r('VarDeclaration', 'Implicit', 'Constant').new(
:name('%?LANG'),
:value(%*LANG),
Expand Down
16 changes: 11 additions & 5 deletions src/Raku/ast/scoping.rakumod
Expand Up @@ -114,7 +114,7 @@ class RakuAST::LexicalScope
Nil
}

method merge-generated-lexical-declaration(RakuAST::Declaration $declaration) {
method merge-generated-lexical-declaration(RakuAST::Declaration $declaration, RakuAST::Resolver :$resolver!) {
unless $!generated-lexical-declarations {
nqp::bindattr(self, RakuAST::LexicalScope, '$!generated-lexical-declarations', []);
}
Expand All @@ -124,7 +124,7 @@ class RakuAST::LexicalScope
return Nil
}
else {
$_.merge($declaration);
$_.merge($declaration, :$resolver);
return;
}
}
Expand Down Expand Up @@ -388,7 +388,7 @@ class RakuAST::Declaration::Mergeable {
$name eq 'Perl6::Metamodel::PackageHOW' || $name eq 'KnowHOW'
}

method merge(RakuAST::Declaration $other) {
method merge(RakuAST::Declaration $other, RakuAST::Resolver :$resolver!) {
my $target := self.compile-time-value;
my $source := $other.compile-time-value;

Expand Down Expand Up @@ -434,8 +434,12 @@ class RakuAST::Declaration::Mergeable {
self.set-value($source);
}
else {
nqp::die('Unsupported case trying to merge symbols or duplicate definition'
~ (nqp::can(self, 'lexical-name') ?? ' trying to merge ' ~ self.lexical-name !! ''));
$resolver.panic(
$resolver.build-exception(
'X::Redeclaration',
:symbol(nqp::can(self, 'lexical-name') ?? self.lexical-name !! '')
)
);
}
}

Expand Down Expand Up @@ -686,6 +690,7 @@ class RakuAST::PackageInstaller {
$lexical := $resolver.resolve-lexical-constant($final);
if $pure-package-installation || !$lexical {
$resolver.current-scope.merge-generated-lexical-declaration:
:$resolver,
self.IMPL-GENERATE-LEXICAL-DECLARATION($final, $type-object);
}
# If `our`-scoped, also put it into the current package.
Expand Down Expand Up @@ -723,6 +728,7 @@ class RakuAST::PackageInstaller {
$target := Perl6::Metamodel::PackageHOW.new_type(name => $first);
$target.HOW.compose($target);
$resolver.current-scope.merge-generated-lexical-declaration:
:$resolver,
RakuAST::Declaration::LexicalPackage.new:
:lexical-name($first),
:compile-time-value($target),
Expand Down
2 changes: 1 addition & 1 deletion src/Raku/ast/statements.rakumod
Expand Up @@ -1419,7 +1419,7 @@ class RakuAST::Statement::Use
}
my $declarand := RakuAST::Declaration::Import.new:
:lexical-name($key), :compile-time-value($value);
$target-scope.merge-generated-lexical-declaration: $declarand;
$target-scope.merge-generated-lexical-declaration: $declarand, :$resolver;

my $categorical := $key ~~ /^ '&' (\w+) [ ':<' (.+) '>' | ':«' (.+) '»' ] $/;
if $categorical {
Expand Down

0 comments on commit f49b108

Please sign in to comment.