diff --git a/src/Raku/Grammar.nqp b/src/Raku/Grammar.nqp index 088c0be8fc9..da82b65cc5f 100644 --- a/src/Raku/Grammar.nqp +++ b/src/Raku/Grammar.nqp @@ -534,6 +534,7 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common { :my $*LITERALS; :my $*EXPORT; :my $*NEXT_STATEMENT_ID := 1; # to give each statement an ID + :my $*begin_compunit := 1; # whether we're at start of a compilation unit <.comp_unit_stage0> <.lang_setup($outer-cu)> @@ -1911,9 +1912,14 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common { * <.enter-package-scope> [ - || + || { $*begin_compunit := 0; } || ';' - + [ + || + { $*begin_compunit := 0; } + + || { $/.typed_panic("X::UnitScope::TooLate", what => $*PKGDECL); } + ] || <.panic("Unable to parse $*PKGDECL definition")> ] <.leave-block-scope> @@ -2103,6 +2109,27 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common { * :!s { $*IN_DECL := ''; } [ + || ';' + { + if $ ne 'MAIN' { + $/.typed_panic("X::UnitScope::Invalid", what => "sub", + where => "except on a MAIN sub", suggestion => + 'Please use the block form. If you did not mean to ' + ~ "declare a unit-scoped sub,\nperhaps you accidentally " + ~ "placed a semicolon after routine's definition?" + ); + } + unless $*begin_compunit { + $/.typed_panic("X::UnitScope::TooLate", what => "sub"); + } + unless $*MULTINESS eq '' || $*MULTINESS eq 'only' { + $/.typed_panic("X::UnitScope::Invalid", what => "sub", where => "on a $*MULTINESS sub"); + } + unless $*R.outer-scope =:= $*UNIT { + $/.typed_panic("X::UnitScope::Invalid", what => "sub", where => "in a subscope"); + } + $*begin_compunit := 0; + } || || ]