Skip to content

Fetch operands of binary operators in left-to-right order #23108

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

Merged
merged 3 commits into from
Jul 15, 2025
Merged
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
19 changes: 19 additions & 0 deletions pod/perldelta.pod
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ XXX Changes (i.e. rewording) of diagnostic messages go here

XXX Describe change here

=item *

L<Use of uninitialized value%s|perldiag/"Use of uninitialized value%s">

This warning was issued in the reverse order (right-to-left) when both
operands of a binary operator are uninitialized values. This is now
fixed to be consistent with evaluation order of operands.

=back

=head1 Utility Changes
Expand Down Expand Up @@ -368,6 +376,17 @@ manager will later use a regex to expand these into links.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the commit message s/thange/change/

XXX

=item *

When both operands of arithmetic operators (C<+>, C<->, etc.) are
L<overload>ed objects which have no method for that operator but have
a C<0+> method and the C<fallback> option set to TRUE,
perl will normally call the C<0+> method for the left operand first
and then call one for the right operand, i.e. in the same order
with operand evaluation order.
But if C<S<use integer;>> is in effect, the C<0+> methods used to be
called in wrong (reverse) order.

=back

=head1 Known Problems
Expand Down
54 changes: 26 additions & 28 deletions pp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1483,8 +1483,8 @@ PP(pp_multiply)
} /* SvIOK(svr) */
#endif
{
NV right = SvNV_nomg(svr);
NV left = SvNV_nomg(svl);
NV right = SvNV_nomg(svr);
NV result = left * right;

#if defined(__sgi) && defined(USE_LONG_DOUBLE) && LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_BE && NVSIZE == 16
Expand Down Expand Up @@ -1611,8 +1611,8 @@ PP(pp_divide)
} /* one operand wasn't SvIOK */
#endif /* PERL_TRY_UV_DIVIDE */
{
NV right = SvNV_nomg(svr);
NV left = SvNV_nomg(svl);
NV right = SvNV_nomg(svr);
#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
if (! Perl_isnan(right) && right == 0.0)
#else
Expand Down Expand Up @@ -1919,7 +1919,7 @@ PP(pp_subtract)

SV *svr = PL_stack_sp[0];
SV *svl = PL_stack_sp[-1];

NV nv;

#ifdef PERL_PRESERVE_IVUV

Expand Down Expand Up @@ -2050,7 +2050,8 @@ PP(pp_subtract)
TARGi(NEGATE_2IV(result), 1);
else {
/* result valid, but out of range for IV. */
TARGn(-(NV)result, 1);
nv = -(NV)result;
goto ret_nv;
}
}
goto ret;
Expand All @@ -2060,17 +2061,14 @@ PP(pp_subtract)
#else
useleft = USE_LEFT(svl);
#endif
{
NV value = SvNV_nomg(svr);

if (!useleft) {
/* left operand is undef, treat as zero - value */
TARGn(-value, 1);
goto ret;
}
TARGn(SvNV_nomg(svl) - value, 1);
goto ret;
}
/* If left operand is undef, treat as zero - value */
nv = useleft ? SvNV_nomg(svl) : 0.0;
/* Separate statements here to ensure SvNV_nomg(svl) is evaluated
before SvNV_nomg(svr) */
nv -= SvNV_nomg(svr);
ret_nv:
TARGn(nv, 1);

ret:
rpp_replace_2_1_NN(targ);
Expand Down Expand Up @@ -2356,8 +2354,8 @@ Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
}
#endif
{
NV const rnv = SvNV_nomg(right);
NV const lnv = SvNV_nomg(left);
NV const rnv = SvNV_nomg(right);

#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
Expand Down Expand Up @@ -2888,8 +2886,8 @@ PP(pp_i_multiply)
if (rpp_try_AMAGIC_2(mult_amg, AMGf_assign))
return NORMAL;

IV right = SvIV_nomg(PL_stack_sp[0]);
IV left = SvIV_nomg(PL_stack_sp[-1]);
IV right = SvIV_nomg(PL_stack_sp[0]);

TARGi((IV)((UV)left * (UV)right), 1);
rpp_replace_2_1_NN(targ);
Expand All @@ -2910,10 +2908,10 @@ PP(pp_i_divide)
SV *left = PL_stack_sp[-1];

{
IV num = SvIV_nomg(left);
IV value = SvIV_nomg(right);
if (value == 0)
DIE(aTHX_ "Illegal division by zero");
IV num = SvIV_nomg(left);

/* avoid FPE_INTOVF on some platforms when num is IV_MIN */
if (value == -1)
Expand All @@ -2936,8 +2934,8 @@ PP(pp_i_modulo)
if (rpp_try_AMAGIC_2(modulo_amg, AMGf_assign))
return NORMAL;

IV right = SvIV_nomg(PL_stack_sp[0]);
IV left = SvIV_nomg(PL_stack_sp[-1]);
IV right = SvIV_nomg(PL_stack_sp[0]);

{
if (!right)
Expand All @@ -2962,9 +2960,9 @@ PP(pp_i_add)
if (rpp_try_AMAGIC_2(add_amg, AMGf_assign))
return NORMAL;

IV right = SvIV_nomg(PL_stack_sp[0]);
SV *leftsv = PL_stack_sp[-1];
IV left = USE_LEFT(leftsv) ? SvIV_nomg(leftsv) : 0;
IV right = SvIV_nomg(PL_stack_sp[0]);

TARGi((IV)((UV)left + (UV)right), 1);
rpp_replace_2_1_NN(targ);
Expand All @@ -2981,9 +2979,9 @@ PP(pp_i_subtract)
if (rpp_try_AMAGIC_2(subtr_amg, AMGf_assign))
return NORMAL;

IV right = SvIV_nomg(PL_stack_sp[0]);
SV *leftsv = PL_stack_sp[-1];
IV left = USE_LEFT(leftsv) ? SvIV_nomg(leftsv) : 0;
IV right = SvIV_nomg(PL_stack_sp[0]);

TARGi((IV)((UV)left - (UV)right), 1);
rpp_replace_2_1_NN(targ);
Expand All @@ -2996,8 +2994,8 @@ PP(pp_i_lt)
if (rpp_try_AMAGIC_2(lt_amg, 0))
return NORMAL;

IV right = SvIV_nomg(PL_stack_sp[0]);
IV left = SvIV_nomg(PL_stack_sp[-1]);
IV right = SvIV_nomg(PL_stack_sp[0]);

rpp_replace_2_IMM_NN(boolSV(left < right));
return NORMAL;
Expand All @@ -3009,8 +3007,8 @@ PP(pp_i_gt)
if (rpp_try_AMAGIC_2(gt_amg, 0))
return NORMAL;

IV right = SvIV_nomg(PL_stack_sp[0]);
IV left = SvIV_nomg(PL_stack_sp[-1]);
IV right = SvIV_nomg(PL_stack_sp[0]);

rpp_replace_2_IMM_NN(boolSV(left > right));
return NORMAL;
Expand All @@ -3022,8 +3020,8 @@ PP(pp_i_le)
if (rpp_try_AMAGIC_2(le_amg, 0))
return NORMAL;

IV right = SvIV_nomg(PL_stack_sp[0]);
IV left = SvIV_nomg(PL_stack_sp[-1]);
IV right = SvIV_nomg(PL_stack_sp[0]);

rpp_replace_2_IMM_NN(boolSV(left <= right));
return NORMAL;
Expand All @@ -3035,8 +3033,8 @@ PP(pp_i_ge)
if (rpp_try_AMAGIC_2(ge_amg, 0))
return NORMAL;

IV right = SvIV_nomg(PL_stack_sp[0]);
IV left = SvIV_nomg(PL_stack_sp[-1]);
IV right = SvIV_nomg(PL_stack_sp[0]);

rpp_replace_2_IMM_NN(boolSV(left >= right));
return NORMAL;
Expand All @@ -3048,8 +3046,8 @@ PP(pp_i_eq)
if (rpp_try_AMAGIC_2(eq_amg, 0))
return NORMAL;

IV right = SvIV_nomg(PL_stack_sp[0]);
IV left = SvIV_nomg(PL_stack_sp[-1]);
IV right = SvIV_nomg(PL_stack_sp[0]);

rpp_replace_2_IMM_NN(boolSV(left == right));
return NORMAL;
Expand All @@ -3061,8 +3059,8 @@ PP(pp_i_ne)
if (rpp_try_AMAGIC_2(ne_amg, 0))
return NORMAL;

IV right = SvIV_nomg(PL_stack_sp[0]);
IV left = SvIV_nomg(PL_stack_sp[-1]);
IV right = SvIV_nomg(PL_stack_sp[0]);

rpp_replace_2_IMM_NN(boolSV(left != right));
return NORMAL;
Expand All @@ -3075,8 +3073,8 @@ PP(pp_i_ncmp)
if (rpp_try_AMAGIC_2(ncmp_amg, 0))
return NORMAL;

IV right = SvIV_nomg(PL_stack_sp[0]);
IV left = SvIV_nomg(PL_stack_sp[-1]);
IV right = SvIV_nomg(PL_stack_sp[0]);


{
Expand Down Expand Up @@ -3122,8 +3120,8 @@ PP(pp_atan2)
if (rpp_try_AMAGIC_2(atan2_amg, 0))
return NORMAL;

NV right = SvNV_nomg(PL_stack_sp[0]);
NV left = SvNV_nomg(PL_stack_sp[-1]);
NV right = SvNV_nomg(PL_stack_sp[0]);

TARGn(Perl_atan2(left, right), 1);
rpp_replace_2_1_NN(targ);
Expand Down
21 changes: 10 additions & 11 deletions pp_hot.c
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,7 @@ PP(pp_add)
SV *targ = (PL_op->op_flags & OPf_STACKED)
? PL_stack_sp[-1]
: PAD_SV(PL_op->op_targ);
NV nv;

if (rpp_try_AMAGIC_2(add_amg, AMGf_assign|AMGf_numeric))
return NORMAL;
Expand Down Expand Up @@ -1994,7 +1995,8 @@ PP(pp_add)
TARGi(NEGATE_2IV(result), 1);
else {
/* result valid, but out of range for IV. */
TARGn(-(NV)result, 1);
nv = -(NV)result;
goto ret_nv;
}
}
goto ret;
Expand All @@ -2006,16 +2008,13 @@ PP(pp_add)
useleft = USE_LEFT(svl);
#endif

{
NV value = SvNV_nomg(svr);
if (!useleft) {
/* left operand is undef, treat as zero. + 0.0 is identity. */
TARGn(value, 1);
}
else {
TARGn(value + SvNV_nomg(svl), 1);
}
}
/* If left operand is undef, treat as zero. */
nv = useleft ? SvNV_nomg(svl) : 0.0;
/* Separate statements here to ensure SvNV_nomg(svl) is evaluated
before SvNV_nomg(svr) */
nv += SvNV_nomg(svr);
ret_nv:
TARGn(nv, 1);

ret:
rpp_replace_2_1_NN(targ);
Expand Down
Loading
Loading