3 # Test the various op trees that turn sub () { ... } into a constant, and
4 # some variants that don’t.
13 # @tests is an array of hash refs, each of which can have various keys:
15 # nickname - name of the sub to use in test names
16 # generator - a sub returning a code ref to test
17 # finally - sub to run after the other tests
19 # Each of the following gives expected test results. If the key is
20 # omitted, the test is skipped:
22 # retval - the returned code ref’s return value
23 # same_retval - whether the same scalar is returned each time
24 # inlinable - whether the sub is inlinable
25 # deprecated - whether the sub returning a code ref will emit a depreca-
26 # tion warning when called
27 # method - whether the sub has the :method attribute
29 # [perl #63540] Don’t treat sub { if(){.....}; "constant" } as a constant
30 sub blonk { ++$blonk_was_called }
32 nickname => 'sub with null+kids (if-block), then constant',
34 # This used to turn into a constant with the value of $x
36 sub() { if($x){ () = "tralala"; blonk() }; 0 }
43 finally => sub { ok($blonk_was_called, 'RT #63540'); },
48 nickname => 'sub with simple lexical modified elsewhere',
49 generator => sub { my $x = 5; my $ret = sub(){$x}; $x = 7; $ret },
50 retval => 5, # change to 7 when the deprecation cycle is over
58 nickname => 'sub () { 0; $x } with $x modified elsewhere',
59 generator => sub { my $x = 5; my $ret = sub(){0;$x}; $x = 8; $ret },
68 nickname => 'sub () { 0; $x } with $x unmodified elsewhere',
69 generator => sub { my $x = 5; my $y = $x; sub(){0;$x} },
77 use feature 'state', 'lexical_subs';
78 no warnings 'experimental::lexical_subs';
81 nickname => 'sub () { my $x; state sub z { $x } $outer }',
84 sub () { my $x; state sub z { $x } $outer }
94 nickname => 'sub:method with simple lexical',
95 generator => sub { my $y; sub():method{$y} },
104 use feature 'refaliasing';
105 no warnings 'experimental::refaliasing';
107 my $nickname = $_{nickname};
109 local $SIG{__WARN__} = sub { $w = shift };
110 my $sub = &{$_{generator}};
111 if (exists $_{deprecated}) {
112 if ($_{deprecated}) {
113 like $w, qr/^Constants from lexical variables potentially (?x:
114 )modified elsewhere are deprecated at /,
115 "$nickname is deprecated";
118 is $w, undef, "$nickname is not deprecated";
121 if (exists $_{retval}) {
122 is &$sub, $_{retval}, "retval of $nickname";
124 if (exists $_{same_retval}) {
125 my $same = $_{same_retval} ? "same" : "different";
126 &{$_{same_retval} ? \&is : \&isnt}(
127 \scalar &$sub(), \scalar &$sub(),
128 "$nickname gives $same retval each call"
131 if (exists $_{inlinable}) {
132 local *temp_inlinability_test = $sub;
134 use warnings 'redefine';
135 *temp_inlinability_test = sub (){};
136 my $S = $_{inlinable} ? "Constant s" : "S";
137 my $not = " not" x! $_{inlinable};
138 like $w, qr/^${S}ubroutine .* redefined at /,
139 "$nickname is$not inlinable";
141 if (exists $_{method}) {
144 use warnings 'ambiguous';
147 is $w, undef, "$nickname has :method attribute";
150 like $w, qr/^Ambiguous call resolved as CORE::time\(\), (?x:
151 )qualify as such or use & at /,
152 "$nickname has no :method attribute";
156 &{$_{finally} or next}