Skip to content

Commit

Permalink
Improve X::ControlFlow::Return error
Browse files Browse the repository at this point in the history
Don't say we're outside of "any routine" when we're inside of
one and the error is 'cause we're out of dyn scope.

Fixes #1216
  • Loading branch information
zoffixznet committed Oct 27, 2017
1 parent e513f19 commit c936020
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Perl6/Metamodel/BOOTSTRAP.nqp
Expand Up @@ -3340,7 +3340,7 @@ nqp::sethllconfig('perl6', nqp::hash(
if $cat == nqp::const::CONTROL_RETURN {
my %ex := nqp::gethllsym('perl6', 'P6EX');
if !nqp::isnull(%ex) && nqp::existskey(%ex,'X::ControlFlow::Return') {
nqp::atkey(%ex, 'X::ControlFlow::Return')();
nqp::atkey(%ex, 'X::ControlFlow::Return')($out_of_dyn_scope);
}
nqp::die('Attempt to return outside of any Routine');
}
Expand Down
16 changes: 13 additions & 3 deletions src/core/Exception.pm
Expand Up @@ -2132,9 +2132,19 @@ my class X::ControlFlow is Exception {
method message() { "$.illegal without $.enclosing" }
}
my class X::ControlFlow::Return is X::ControlFlow {
has Bool $.out-of-dynamic-scope;
submethod BUILD(Bool() :$!out-of-dynamic-scope) {}

method illegal() { 'return' }
method enclosing() { 'Routine' }
method message() { 'Attempt to return outside of any Routine' }
method message() {
'Attempt to return outside of ' ~ (
$!out-of-dynamic-scope
?? 'immediatelly-enclosing Routine (i.e. `return` execution is'
~ ' outside the dynamic scope of the Routine where `return` was used)'
!! 'any Routine'
)
}
}

my class X::Composition::NotComposable does X::Comp {
Expand Down Expand Up @@ -2627,8 +2637,8 @@ nqp::bindcurhllsym('P6EX', BEGIN nqp::hash(
X::Assignment::RO.new(:$value).throw;
},
'X::ControlFlow::Return',
{
X::ControlFlow::Return.new().throw;
-> $out-of-dynamic-scope = False {
X::ControlFlow::Return.new(:$out-of-dynamic-scope).throw;
},
'X::NoDispatcher',
-> $redispatcher {
Expand Down

0 comments on commit c936020

Please sign in to comment.