[perl #7946] Lvalue subs do not autovivify
authorFather Chrysostomos <sprout@cpan.org>
Sat, 4 Jun 2011 03:06:24 +0000 (20:06 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Sat, 4 Jun 2011 03:06:24 +0000 (20:06 -0700)
commit767eda446920b18c91ad2d91822428141c99301f
treeb09e6e97b02d1fbd51b456eba16ae55a6aaac4d3
parent339c6c60f3062528eca01e318da233bdf2b57ff2
[perl #7946] Lvalue subs do not autovivify

This commit makes autovivification work with lvalue subs. It follows
the same technique used by other autovivifiable ops (aelem, helem,
tc.), except that, due to flag constraints, it uses a single flag and
instead checks the op tree at run time to find out what sort of thing
to vivify.

The flag constraints are that these two flags:

#define OPpENTERSUB_HASTARG 32 /* Called from OP tree. */
#define OPpENTERSUB_NOMOD 64 /* Immune to op_lvalue() for :attrlist. */

conflict with these:

#define OPpDEREF (32|64) /* autovivify: Want ref to something: */
#define OPpDEREF_AV 32 /*   Want ref to AV. */
#define OPpDEREF_HV 64 /*   Want ref to HV. */
#define OPpDEREF_SV (32|64) /*   Want ref to SV. */

Renumbering HASTARG and NOMOD is problematic, as there are places in
op.c that change entersubs into rv2cvs, and the entersub and rv2cv
flags would conflict. Setting the flags correctly when changing the
type is hard and would result in subtle bugs if not done perfectly.

Ops like ${...} don’t actually autovivify; it’s the op inside that
does it. In those cases, the parent op is flagged with OPpDEREFed, and
it skips get-magic, as it has already been called by the inner op.

Since entersub is now marked as being an autovivifying op, ${...} in
lvalue context ends up skipping get-magic if there is a foo() inside.
And this affects even regular subs. So pp_leavesub and pp_return have
to call get-magic; hence the new tests in gmagic.t.
cop.h
ext/B/B/Concise.pm
ext/B/t/f_map.t
ext/B/t/optree_samples.t
op.c
op.h
pp_ctl.c
pp_hot.c
t/op/gmagic.t
t/op/sub_lval.t