Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit 3e0d533

Browse files
author
Reini Urban
committed
iter: new Invalid for range iterator (%d .. %d) error
we dont support reverse loops with constant ints (9..0). now we exit earlier, already at compile-time.
1 parent f97ed5f commit 3e0d533

File tree

6 files changed

+53
-8
lines changed

6 files changed

+53
-8
lines changed

op.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7933,8 +7933,9 @@ Perl_newFOROP(pTHX_ I32 flags, OP *sv, OP *expr, OP *block, OP *cont)
79337933
&& SvIOK(leftsv = cSVOPx_sv(left))
79347934
&& SvIOK(rightsv = cSVOPx_sv(right)))
79357935
{
7936-
if (SvIV(rightsv) - SvIV(leftsv) < 0)
7937-
DIE("Invalid for range (%"IVdf"..%"IVdf")", SvIV(leftsv), SvIV(rightsv));
7936+
if (UNLIKELY(SvIV(rightsv) < SvIV(leftsv)))
7937+
DIE(aTHX_ "Invalid for range iterator (%"IVdf" .. %"IVdf")",
7938+
SvIV(leftsv), SvIV(rightsv));
79387939
/* TODO: unroll loop for small constant ranges, if the body is not too big */
79397940
if (SvIV(rightsv)-SvIV(leftsv) <= PERL_MAX_UNROLL_LOOP_COUNT) {
79407941
DEBUG_kv(Perl_deb(aTHX_ "TODO unroll loop (%"IVdf"..%"IVdf")\n",

pod/perlcdelta.pod

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ L</Selected Bug Fixes> section.
3535

3636
=item *
3737

38+
for loops got several enhancements:
39+
40+
new special B<iter_ary> C<for (@ary)> and B<iter_lazyiv> C<for (0..9)>
41+
ops to avoid a run-time switch in the generic iter op.
42+
43+
more aelem_u optimizations, less run-time out of bounds checks for
44+
shaped arrays in loops. E.g. in C<my @a[5]; $a[$_] for (0..4);> the
45+
compilers knows that the max index for C<@a> will be C<4>, which is
46+
within the allowed shape of C<@a>.
47+
48+
=item *
49+
3850
The C<multideref> OP has a new C<MDEREF_INDEX_uoob> flag. This is
3951
used for unchecked out-of-bounds checks for arrays, to use the
4052
previous AvSHAPED array optimizations (aelem_u, aelemfast_lex_u) or
@@ -79,7 +91,34 @@ dual-life modules would have a F<Changes> file that could be cribbed.
7991

8092
=item *
8193

82-
L<XXX> has been upgraded from version A.xx to B.yy.
94+
L<Opcode> has been upgraded from version 1.34c to 1.35c, with the new
95+
iter_ary and iter_lazyiv ops.
96+
97+
=back
98+
99+
=head1 Diagnostics
100+
101+
The following additions or changes have been made to diagnostic output,
102+
including warnings and fatal error messages. For the complete list of
103+
diagnostic messages, see L<perldiag>.
104+
105+
=head2 New Diagnostics
106+
107+
=head3 New Errors
108+
109+
=over 4
110+
111+
=item Invalid for range iterator (%d .. %d)
112+
113+
(F) A range with constant integers as a for loop cannot be reversed.
114+
115+
We check now at compile-time that a range with C<for> loops with
116+
constant integers is incremental, the 2nd number must be higher. We
117+
don't support reverse loops with ranges, i.e. C<for (9..0)> is
118+
invalid, but C<for (0..$#ary)> with C<$#ary> being C<-1> is valid.
119+
120+
Reverse constant strings ranges are still valid and lead to an empty
121+
loop. i.e. C<for ('z'..'a')> is currently valid.
83122

84123
=back
85124

pod/perldiag.pod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2939,6 +2939,10 @@ instead, except within S<C<(?[ ])>>, where it is a fatal error.
29392939
The S<<-- HERE> shows whereabouts in the regular expression the
29402940
escape was discovered.
29412941

2942+
=item Invalid for range iterator (%d .. %d)
2943+
2944+
(F) A range with constant integers as a for loop cannot be reversed.
2945+
29422946
=item Invalid function type %s
29432947

29442948
(F) The declared type for the function must be a coretype or an existing

pp_ctl.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,11 +2204,12 @@ PP(pp_enteriter)
22042204
cx->blk_loop.state_u.lazyiv.end = SvIV_nomg(right);
22052205
}
22062206
else {
2207+
SV *left = newSVsv(sv);
22072208
cx->cx_type |= CXt_LOOP_LAZYSV;
2208-
cx->blk_loop.state_u.lazysv.cur = newSVsv(sv);
2209+
cx->blk_loop.state_u.lazysv.cur = left;
22092210
cx->blk_loop.state_u.lazysv.end = right;
22102211
SvREFCNT_inc_simple_void_NN(right);
2211-
(void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur);
2212+
(void) SvPV_force_nolen(left);
22122213
/* This will do the upgrade to SVt_PV, and warn if the value
22132214
is uninitialised. */
22142215
(void) SvPV_nolen_const(right);
@@ -2218,7 +2219,7 @@ PP(pp_enteriter)
22182219
SvREFCNT_dec(right);
22192220
cx->blk_loop.state_u.lazysv.end = &PL_sv_no;
22202221
}
2221-
}
2222+
}
22222223
}
22232224
else /* SvTYPE(maybe_ary) == SVt_PVAV */ {
22242225
/* for (@array) {} */

pp_hot.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2747,6 +2747,7 @@ PP(pp_iter_lazyiv)
27472747
itersvp = CxITERVAR(cx);
27482748
assert(itersvp);
27492749

2750+
/* Note: no reverse support. (9..0) */
27502751
cur = cx->blk_loop.state_u.lazyiv.cur;
27512752
if (UNLIKELY(cur > cx->blk_loop.state_u.lazyiv.end)) {
27522753
assert(PL_stack_sp < PL_stack_max);

t/porting/known_pod_issues.dat

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,7 @@ pod/cperl.pod Verbatim line length including indents exceeds 79 by 8
336336
pod/perl.pod Verbatim line length including indents exceeds 79 by 8
337337
pod/perl5222cdelta.pod ? Should you be using F<...> or maybe L<...> instead of 1
338338
pod/perl5240cdelta.pod Apparent broken link 1
339-
pod/perl5250cdelta.pod Apparent internal link is missing its forward slash 4
340-
pod/perl5250cdelta.pod unresolved internal link 1
339+
pod/perl5250cdelta.pod Apparent internal link is missing its forward slash 3
341340
pod/perlandroid.pod Verbatim line length including indents exceeds 79 by 3
342341
pod/perlapi.pod Verbatim line length including indents exceeds 79 by 2
343342
pod/perlbook.pod Verbatim line length including indents exceeds 79 by 1

0 commit comments

Comments
 (0)