Skip to content

Commit 25688cd

Browse files
committed
fixup forbid mandatory named params after optional positional ones
1 parent 33c5d63 commit 25688cd

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

op.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16990,6 +16990,11 @@ Perl_subsignature_append_named(pTHX_ const char *paramname, PADOFFSET padix, OPC
1699016990
return;
1699116991
}
1699216992

16993+
if(!defexpr && signature->opt_params) {
16994+
yyerror("Mandatory parameter follows optional parameter");
16995+
return;
16996+
}
16997+
1699316998
SV *namesv = newSVpvn_utf8(paramname, strlen(paramname), true);
1699416999

1699517000
for(size_t parami = 0; parami < signature->named_params; parami++) {

pod/perldiag.pod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3959,6 +3959,10 @@ caller to omit an earlier one and pass a later one. If you want to act
39593959
as if the parameters are filled from right to left, declare the rightmost
39603960
optional and then shuffle the parameters around in the subroutine's body.
39613961

3962+
When using named parameters, no named parameter may be made mandatory if
3963+
there exist any optional I<positional> parameters, though any named
3964+
parameters may be freely mixed between mandatory and optional.
3965+
39623966
=item Matched non-Unicode code point 0x%X against Unicode property; may
39633967
not be portable
39643968

pod/perlsub.pod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,14 @@ The defaulting expressions for optional named parameters are invoked in order
506506
left to right, as required. As with positional parameters, each expression
507507
can see and make use of the values assigned to any previous parameter.
508508

509+
If the subroutine already declares any optional positional parameters, then
510+
all named parameters must also be optional. It would not make sense to
511+
require a mandatory named parameter after any optional positional ones, as
512+
there would be no way for the caller to provide a value for it. However, if
513+
the subroutine does not declare any optional positional parameters, then any
514+
named ones may be freely mixed between mandatory and optional, since they are
515+
processed by name and not position.
516+
509517
After positional or named parameters, additional arguments may be captured
510518
in a slurpy parameter. The simplest form of this is just an array variable:
511519

t/lib/croak/signatures

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ sub f (:$x, :$x) { }
8787
EXPECT
8888
Duplicated subroutine parameter name at - line 3, near ":$x) "
8989
########
90+
# NAME mandatory named after optional positional
91+
no warnings 'experimental::signature_named_parameters';
92+
sub f ($x = 1, :$y) { }
93+
EXPECT
94+
Mandatory parameter follows optional parameter at - line 3, near ":$y) "
95+
########
9096
sub t061 (@a, @b) { }
9197
EXPECT
9298
Multiple slurpy parameters not allowed at - line 2, near "@b) "

0 commit comments

Comments
 (0)