Permalink
Browse files

typed exception for import name clashes

  • Loading branch information...
1 parent 54030c3 commit 962b9b1e4d581ad041b4222ad5e0433e31fb7f8a @moritz moritz committed Sep 2, 2012
Showing with 28 additions and 9 deletions.
  1. +7 −7 src/Perl6/Grammar.pm
  2. +10 −2 src/Perl6/World.pm
  3. +11 −0 src/core/Exception.pm
View
@@ -513,7 +513,7 @@ grammar Perl6::Grammar is HLL::Grammar {
my $module := $*W.load_module($/,
$longname,
$*GLOBALish);
- do_import($module, $longname);
+ do_import($/, $module, $longname);
$/.CURSOR.import_EXPORTHOW($module);
}
}
@@ -750,7 +750,7 @@ grammar Perl6::Grammar is HLL::Grammar {
$arglist := nqp::getattr($arglist.list.eager,
$*W.find_symbol(['List']), '$!items');
}
- do_import($module.WHO, ~$<module_name><longname>, $arglist);
+ do_import($/, $module.WHO, ~$<module_name><longname>, $arglist);
}
else {
$/.CURSOR.panic("Could not find module " ~ ~$<module_name> ~
@@ -800,7 +800,7 @@ grammar Perl6::Grammar is HLL::Grammar {
my $module := $*W.load_module($/,
~$longname,
$*GLOBALish);
- do_import($module, ~$longname, $arglist);
+ do_import($/, $module, ~$longname, $arglist);
$/.CURSOR.import_EXPORTHOW($module);
}
|| {
@@ -809,7 +809,7 @@ grammar Perl6::Grammar is HLL::Grammar {
my $module := $*W.load_module($/,
~$longname,
$*GLOBALish);
- do_import($module, ~$longname);
+ do_import($/, $module, ~$longname);
$/.CURSOR.import_EXPORTHOW($module);
}
}
@@ -819,7 +819,7 @@ grammar Perl6::Grammar is HLL::Grammar {
<.ws>
}
- sub do_import($module, $package_source_name, $arglist?) {
+ sub do_import($/, $module, $package_source_name, $arglist?) {
if nqp::existskey($module, 'EXPORT') {
my $EXPORT := $module<EXPORT>.WHO;
my @to_import := ['MANDATORY'];
@@ -830,7 +830,7 @@ grammar Perl6::Grammar is HLL::Grammar {
if nqp::istype($tag, $Pair) {
$tag := nqp::unbox_s($tag.key);
if nqp::existskey($EXPORT, $tag) {
- $*W.import($EXPORT{$tag}, $package_source_name);
+ $*W.import($/, $EXPORT{$tag}, $package_source_name);
}
else {
nqp::die("Error while importing from '$package_source_name': no such tag '$tag'");
@@ -847,7 +847,7 @@ grammar Perl6::Grammar is HLL::Grammar {
}
for @to_import -> $tag {
if nqp::existskey($EXPORT, $tag) {
- $*W.import($EXPORT{$tag}, $package_source_name);
+ $*W.import($/, $EXPORT{$tag}, $package_source_name);
}
}
if +@positional_imports {
View
@@ -251,7 +251,7 @@ class Perl6::World is HLL::World {
}
# Imports symbols from the specified package into the current lexical scope.
- method import($package, $source_package_name) {
+ method import($/, $package, $source_package_name) {
# We'll do this in two passes, since at the start of CORE.setting we import
# StaticLexPad, which of course we need to use when importing. Since we still
# keep the authoritative copy of stuff from the compiler's view in QAST::Block's
@@ -262,16 +262,24 @@ class Perl6::World is HLL::World {
# First pass: QAST::Block symbol table installation. Also detect any
# outright conflicts, and handle any situations where we need to merge.
my %to_install;
+ my @clash;
for %stash {
if $target.symbol($_.key) {
# XXX TODO: Merge handling.
- nqp::die("Cannot import symbol '" ~ $_.key ~ "' from package '$source_package_name', since it already exists in the lexpad"); }
+ nqp::push(@clash, $_.key);
+ }
else {
$target.symbol($_.key, :scope('lexical'), :value($_.value));
$target[0].push(QAST::Var.new( :scope('lexical'), :name($_.key), :decl('var') ));
%to_install{$_.key} := $_.value;
}
}
+ if +@clash {
+ self.throw($/, 'X::Import::Redeclaration',
+ symbols => @clash,
+ source-package-name => $source_package_name,
+ );
+ }
# Second pass: stick everything we still need to install in the
# actual static lexpad.
View
@@ -420,6 +420,17 @@ my class X::Redeclaration does X::Comp {
}
}
+my class X::Import::Redeclaration does X::Comp {
+ has @.symbols;
+ has $.source-package-name;
+ method message() {
+ @.symbols == 1
+ ?? "Cannot import symbol @.symbols[0] from $.source-package-name, because it already exists in this lexical scope"
+ !! "Cannot import the following symbols from $.source-package-name, becase they aready exist in this lexical scope: ", @.symbols.join(', ');
+ }
+
+}
+
my class X::Phaser::Multiple does X::Comp {
has $.block;
method message() { "Only one $.block block is allowed" }

0 comments on commit 962b9b1

Please sign in to comment.