Skip to content

add custom error for 'catch (my $e)' #23228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: blead
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions op.c
Original file line number Diff line number Diff line change
Expand Up @@ -4141,7 +4141,7 @@ S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
} else if (attrs) {
GV * const gv = cGVOPx_gv(cUNOPo->op_first);
assert(PL_parser);
PL_parser->in_my = FALSE;
PL_parser->in_my = KEY_NULL;
PL_parser->in_my_stash = NULL;
apply_attrs(GvSTASH(gv),
(type == OP_RV2SV ? GvSVn(gv) :
Expand Down Expand Up @@ -4174,7 +4174,7 @@ S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
HV *stash;

assert(PL_parser);
PL_parser->in_my = FALSE;
PL_parser->in_my = KEY_NULL;
PL_parser->in_my_stash = NULL;

/* check for C<my Dog $spot> when deciding package */
Expand Down Expand Up @@ -4232,7 +4232,7 @@ Perl_my_attrs(pTHX_ OP *o, OP *attrs)
o = op_append_list(OP_LIST, o, rops);
}
}
PL_parser->in_my = FALSE;
PL_parser->in_my = KEY_NULL;
PL_parser->in_my_stash = NULL;
return o;
}
Expand Down Expand Up @@ -4857,7 +4857,7 @@ Perl_localize(pTHX_ OP *o, I32 lex)
o = my(o);
else
o = op_lvalue(o, OP_NULL); /* a bit kludgey */
PL_parser->in_my = FALSE;
PL_parser->in_my = KEY_NULL;
PL_parser->in_my_stash = NULL;
return o;
}
Expand Down
4 changes: 2 additions & 2 deletions perly.act

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion perly.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion perly.tab

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion perly.y
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ catch_paren: empty
/* not really valid grammar but we detect it in the
* action block to throw a nicer error message */
| PERLY_PAREN_OPEN
{ parser->in_my = 1; }
{ parser->in_my = KEY_catch; }
scalar
{ parser->in_my = 0; intro_my(); }
PERLY_PAREN_CLOSE
Expand Down
16 changes: 16 additions & 0 deletions pod/perldelta.pod
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,22 @@ and L</New Warnings>

XXX L<message|perldiag/"message">

=item *

L<Can't redeclare catch variable as "%s"|perldiag/"Can't redeclare catch variable as "%s"">

(F) A C<my>, C<our> or C<state> keyword was used with the exception variable in
a C<catch> block:

try { ... }
catch (my $e) { ... }
# or catch (our $e) { ... }
# or catch (state $e) { ... }

This is not valid syntax. C<catch> takes a bare variable name, which is
automatically lexically declared.
[GH #23222]

=back

=head3 New Warnings
Expand Down
17 changes: 15 additions & 2 deletions pod/perldiag.pod
Original file line number Diff line number Diff line change
Expand Up @@ -1485,10 +1485,23 @@ missing. You need to figure out where your CRTL misplaced its environ
or define F<PERL_ENV_TABLES> (see L<perlvms>) so that environ is not
searched.

=item Can't redeclare catch variable as "%s"

(F) A C<my>, C<our> or C<state> keyword was used with the exception variable in
a C<catch> block:

try { ... }
catch (my $e) { ... }
# or catch (our $e) { ... }
# or catch (state $e) { ... }

This is not valid syntax. C<catch> takes a bare variable name, which is
automatically lexically declared.

=item Can't redeclare "%s" in "%s"

(F) A "my", "our" or "state" declaration was found within another declaration,
such as C<my ($x, my($y), $z)> or C<our (my $x)>.
(F) A C<my>, C<our> or C<state> declaration was found within another
declaration, such as C<my ($x, my($y), $z)> or C<our (my $x)>.

=item Can't "redo" outside a loop block

Expand Down
24 changes: 24 additions & 0 deletions t/lib/croak/toke
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,30 @@ EXPECT
Can't redeclare "state" in "state" at - line 2, near ", state"
Execution of - aborted due to compilation errors.
########
# NAME catch (my $x) errors
use feature 'try';
try {} catch (my $x) {}
EXPECT
Can't redeclare catch variable as "my" at - line 2, near "(my"
syntax error at - line 2, near "(my "
Execution of - aborted due to compilation errors.
########
# NAME catch (our $x) errors
use feature 'try';
try {} catch (our $x) {}
EXPECT
Can't redeclare catch variable as "our" at - line 2, near "(our"
syntax error at - line 2, near "(our "
Execution of - aborted due to compilation errors.
########
# NAME catch (state $x) errors
use feature 'try', 'state';
try {} catch (state $x) {}
EXPECT
Can't redeclare catch variable as "state" at - line 2, near "(state"
syntax error at - line 2, near "(state "
Execution of - aborted due to compilation errors.
########
# NAME BEGIN <> [perl #125341]
BEGIN <>
EXPECT
Expand Down
32 changes: 26 additions & 6 deletions toke.c
Original file line number Diff line number Diff line change
Expand Up @@ -7186,17 +7186,37 @@ yyl_do(pTHX_ char *s, I32 orig_keyword)
OPERATOR(KW_DO);
}

static const char *
declarator_name(I32 k) {
switch (k) {
case KEY_my: return "my";
case KEY_state: return "state";
case KEY_our: return "our";
case KEY_field: return "field";
case KEY_catch: return "catch";
default: return "???";
}
}

static int
yyl_my(pTHX_ char *s, I32 my)
{
assert(my == KEY_my || my == KEY_state || my == KEY_our);
if (PL_in_my) {
PL_bufptr = s;
yyerror(form(
"Can't redeclare \"%s\" in \"%s\"",
my == KEY_my ? "my" :
my == KEY_state ? "state" : "our",
PL_in_my == KEY_my ? "my" :
PL_in_my == KEY_state ? "state" : "our"));
if (PL_in_my == KEY_catch) {
yyerror(form(
"Can't redeclare catch variable as \"%s\"",
declarator_name(my)
));
} else {
assert(PL_in_my == KEY_my || PL_in_my == KEY_state || PL_in_my == KEY_our);
yyerror(form(
"Can't redeclare \"%s\" in \"%s\"",
declarator_name(my),
declarator_name(PL_in_my)
));
}
}
PL_in_my = (U16)my;
s = skipspace(s);
Expand Down
Loading