Skip to content

Commit

Permalink
Throw typed Exception for 'is rw' on optnl param
Browse files Browse the repository at this point in the history
Added a new X::Trait::Invalid in Exception.pm that is then thrown in
BOOTSTRAP.nqp.

The new Exception might be generic enough to be used in other cases.
  • Loading branch information
MasterDuke17 committed Apr 14, 2017
1 parent 135ddee commit 8370675
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Perl6/Metamodel/BOOTSTRAP.nqp
Expand Up @@ -1541,7 +1541,13 @@ BEGIN {
}
my int $flags := nqp::getattr_i($dcself, Parameter, '$!flags');
if $flags +& $SIG_ELEM_IS_OPTIONAL {
nqp::die("Cannot use 'is rw' on optional parameter '$varname'");
my %ex := nqp::gethllsym('perl6', 'P6EX');
if nqp::isnull(%ex) || !nqp::existskey(%ex, 'X::Trait::Invalid') {
nqp::die("Cannot use 'is rw' on optional parameter '$varname'");
}
else {
nqp::atkey(%ex, 'X::Trait::Invalid')('is', 'rw', 'optional parameter', $varname);
}
}
my $cd := nqp::getattr($dcself, Parameter, '$!container_descriptor');
if nqp::defined($cd) { $cd.set_rw(1) }
Expand Down
14 changes: 14 additions & 0 deletions src/core/Exception.pm
Expand Up @@ -781,6 +781,16 @@ my class X::Worry::P5::LeadingZero is X::Worry::P5 {
}
}

my class X::Trait::Invalid is Exception {
has $.type; # is, will, of etc.
has $.subtype; # wrong subtype being tried
has $.declaring; # variable, sub, parameter, etc.
has $.name; # '$foo', '@bar', etc.
method message () {
"Cannot use '$.type $.subtype' on $.declaring '$.name'."
}
}

my class X::Trait::Unknown is Exception {
has $.type; # is, will, of etc.
has $.subtype; # wrong subtype being tried
Expand Down Expand Up @@ -2497,6 +2507,10 @@ nqp::bindcurhllsym('P6EX', nqp::hash(
X::PhaserExceptions.new(exceptions =>
@exceptions.map(-> Mu \e { EXCEPTION(e) })).throw;
},
'X::Trait::Invalid',
sub ($type, $subtype, $declaring, $name) {
X::Trait::Invalid.new(:$type, :$subtype, :$declaring, :$name).throw;
},
));

my class X::HyperWhatever::Multiple is Exception {
Expand Down

0 comments on commit 8370675

Please sign in to comment.