From d7eb3f38ef23ab426266282e71e886860ecb4187 Mon Sep 17 00:00:00 2001 From: Tony Cook Date: Wed, 2 Jun 2021 12:16:42 +1000 Subject: [PATCH] only croak on undef = ... not on (complex... undef) = ... The previous check would trigger on code like: ($x ? $y : undef) = 1; I also moved the test from t/run/fresh_perl.t to t/run/croak/op and added a test for the broken case. --- op.c | 8 ++++++-- t/lib/croak/op | 13 +++++++++++++ t/run/fresh_perl.t | 5 ----- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/op.c b/op.c index c17d1b07d2a7..f68e9fd582ef 100644 --- a/op.c +++ b/op.c @@ -4291,8 +4291,6 @@ Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags) switch (o->op_type) { case OP_UNDEF: - if (type == OP_SASSIGN) - goto nomod; PL_modcount++; goto do_next; @@ -13675,6 +13673,12 @@ Perl_ck_sassign(pTHX_ OP *o) if (OpHAS_SIBLING(kid)) { OP *kkid = OpSIBLING(kid); + + if (kkid->op_type == OP_UNDEF) { + /* diag_listed_as: Can't modify %s in %s */ + yyerror("Can't modify undef operator in scalar assignment"); + } + /* For state variable assignment with attributes, kkid is a list op whose op_last is a padsv. */ if ((kkid->op_type == OP_PADSV || diff --git a/t/lib/croak/op b/t/lib/croak/op index 5f314788602a..3699e6424ec7 100644 --- a/t/lib/croak/op +++ b/t/lib/croak/op @@ -251,3 +251,16 @@ Type of arg 1 to pop must be array (not private hash) at - line 4, near "%a;" Type of arg 1 to shift must be array (not private hash) at - line 5, near "%a;" Type of arg 1 to unshift must be array (not private hash) at - line 6, near "1;" Execution of - aborted due to compilation errors. +######## +# NAME detect scalar assignment to undef during compilation +BEGIN { undef = 0 } +EXPECT +Can't modify undef operator in scalar assignment at - line 1, near "0 }" +BEGIN not safe after errors--compilation aborted at - line 1. +######## +# NAME but don't trigger on a more complex left-hand expression involving undef +my $x = 1; +my $y; +($x ? $y : undef) = 0; +EXPECT +OPTIONS nonfatal diff --git a/t/run/fresh_perl.t b/t/run/fresh_perl.t index 88a64ad58391..4717e1d04969 100644 --- a/t/run/fresh_perl.t +++ b/t/run/fresh_perl.t @@ -192,11 +192,6 @@ EXPECT Illegal division by zero at - line 1. BEGIN failed--compilation aborted at - line 1. ######## -BEGIN { undef = 0 } -EXPECT -Can't modify undef operator in scalar assignment at - line 1, near "0 }" -BEGIN not safe after errors--compilation aborted at - line 1. -######## { package foo; sub PRINT {