Allow lvalue subs to return TEMPs
authorFather Chrysostomos <sprout@cpan.org>
Wed, 1 Jun 2011 00:09:15 +0000 (17:09 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Wed, 1 Jun 2011 00:09:15 +0000 (17:09 -0700)
commitfd6c41ce5607a262d2124271d289aa2a0213c049
tree491ece948e641e2b9584450ac33cf7417078e58c
parentc73030b8a71ef564254d3a858775de115ce7f6b5
Allow lvalue subs to return TEMPs

This is perhaps not ideal, but it fixes (or allows to be fixed) seve-
ral bugs.

I was hoping that the cases that this perhaps erroneously allows
through would fall back to the warning I added in commit 8fe85e3, but,
unfortunately, in all these cases the refcount is greater than 1 when
pp_sassign is reached.

To be less vague: ‘foo() = 3’ warns if foo() returns a TEMP with no
set-magic and a refcount of 1 (meaning it will be freed shortly). But
truly temporary values returned by pure-Perl lvalue subs have a refer-
ence count of at least 2, and as many entries on the mortals stack.

I cannot distinguish between truly temporary values and those that
are but nominally temporary (marked TEMP because the refcount will go
down, but not to zero) by checking for a refcount <= 2 in pp_sassign,
because this example returns with a refcount of 2:

+sub :lvalue { return delete($_[0]), $x }->($x) = 3; # returns a TEMP

There’s no logical reason why that shouldn’t work, if this does:

+sub :lvalue { return foo(), $x }->($x) = 3; # not TEMP

as they are conceptually identical.

The advantages to this change:

• The delete example above will work.
• It allows XS lvalue subs that return TEMPs to work in the debugger
  [perl #71172], restoring the bug fix that b724cc1 implemented but
  c73030b reverted.
• It makes these three cases identical, as they should be. Note that
  only two of them return TEMPs:
    +sub :lvalue { return shift }->($x) = 3;
    +sub :lvalue { \@_; return shift }->($x) = 3; # returns a TEMP
    +sub :lvalue { return delete $_[0] }->($x) = 3; # returns a TEMP

So I think the advantages outweigh the disadvantages.
pp_hot.c
t/op/sub_lval.t