3 # Regression tests for attributes.pm and the C< : attrs> syntax.
9 skip_all_if_miniperl("miniperl can't load attributes");
14 $SIG{__WARN__} = sub { die @_ };
21 fresh_perl_is 'use attributes; print "ok"', 'ok',
22 'attributes.pm can load without warnings.pm already loaded';
24 our $anon1; eval_ok '$anon1 = sub : method { $_[0]++ }';
26 eval 'sub e1 ($) : plugh ;';
27 like $@, qr/^Invalid CODE attributes?: ["']?plugh["']? at/;
29 eval 'sub e2 ($) : plugh(0,0) xyzzy ;';
30 like $@, qr/^Invalid CODE attributes: ["']?plugh\(0,0\)["']? /;
32 eval 'sub e3 ($) : plugh(0,0 xyzzy ;';
33 like $@, qr/Unterminated attribute parameter in attribute list at/;
35 eval 'sub e4 ($) : plugh + XYZZY ;';
36 like $@, qr/Invalid separator character '[+]' in attribute list at/;
38 eval_ok 'my main $x : = 0;';
39 eval_ok 'my $x : = 0;';
41 eval_ok 'my ($x) : = 0;';
43 eval_ok 'my ($x) : ;';
44 eval_ok 'my ($x,$y) : = 0;';
45 eval_ok 'my ($x,$y) ;';
46 eval_ok 'my ($x,$y) : ;';
48 eval 'my ($x,$y) : plugh;';
49 like $@, qr/^Invalid SCALAR attribute: ["']?plugh["']? at/;
52 eval '{my $x : plugh}';
53 like $@, qr/^Invalid SCALAR attribute: ["']?plugh["']? at/;
54 eval '{my ($x,$y) : plugh(})}';
55 like $@, qr/^Invalid SCALAR attribute: ["']?plugh\(}\)["']? at/;
57 # More syntax tests from the attributes manpage
58 eval 'my $x : switch(10,foo(7,3)) : expensive;';
59 like $@, qr/^Invalid SCALAR attributes: ["']?switch\(10,foo\(7,3\)\) : expensive["']? at/;
60 eval q/my $x : Ugly('\(") :Bad;/;
61 like $@, qr/^Invalid SCALAR attributes: ["']?Ugly\('\\\("\) : Bad["']? at/;
63 like $@, qr/^Invalid SCALAR attribute: ["']?_5x5["']? at/;
64 eval 'my $x : locked method;';
65 like $@, qr/^Invalid SCALAR attributes: ["']?locked : method["']? at/;
66 eval 'my $x : switch(10,foo();';
67 like $@, qr/^Unterminated attribute parameter in attribute list at/;
68 eval q/my $x : Ugly('(');/;
69 like $@, qr/^Unterminated attribute parameter in attribute list at/;
72 eval 'my $x : Y2::north;';
73 like $@, qr/Invalid separator character ':' in attribute list at/;
75 sub A::MODIFY_SCALAR_ATTRIBUTES { return }
76 eval 'my A $x : plugh;';
77 like $@, qr/^SCALAR package attribute may clash with future reserved word: ["']?plugh["']? at/;
79 eval 'my A $x : plugh plover;';
80 like $@, qr/^SCALAR package attributes may clash with future reserved words: ["']?plugh["']? /;
82 no warnings 'reserved';
83 eval 'my A $x : plugh;';
86 eval 'package Cat; my Cat @socks;';
92 sub X::MODIFY_CODE_ATTRIBUTES { die "$_[0]" }
95 *Y::bar = \&X::foo; # second time for -w
96 eval 'package Z; sub Y::bar : foo';
99 @attrs = eval 'attributes::get $anon1';
100 is "@attrs", "method";
103 sub Z::FETCH_CODE_ATTRIBUTES { return 'Z' }
104 my $thunk = eval 'bless +sub : method { 1 }, "Z"';
107 @attrs = eval 'attributes::get $thunk';
108 is "@attrs", "method Z";
110 # Test attributes on predeclared subroutines:
111 eval 'package A; sub PS : lvalue';
112 @attrs = eval 'attributes::get \&A::PS';
113 is "@attrs", "lvalue";
115 # Test attributes on predeclared subroutines, after definition
116 eval 'package A; sub PS : lvalue; sub PS { }';
117 @attrs = eval 'attributes::get \&A::PS';
118 is "@attrs", "lvalue";
120 # Test ability to modify existing sub's (or XSUB's) attributes.
121 eval 'package A; sub X { $_[0] } sub X : method';
122 @attrs = eval 'attributes::get \&A::X';
123 is "@attrs", "method";
125 # Above not with just 'pure' built-in attributes.
126 sub Z::MODIFY_CODE_ATTRIBUTES { (); }
127 eval 'package Z; sub L { $_[0] } sub L : Z method';
128 @attrs = eval 'attributes::get \&Z::L';
129 is "@attrs", "method Z";
131 # Begin testing attributes that tie
136 sub TIESCALAR { my $x = $_[1]; bless \$x, $_[0]; }
137 sub FETCH { ${$_[0]} }
143 sub MODIFY_SCALAR_ATTRIBUTES { tie ${$_[1]}, 'Ttie', -1; (); }
149 my $x : TieLoop = $i;
150 $x != $i*2 and ::is $x, $i*2;
155 eval 'our ${""} : foo = 1';
156 like $@, qr/Can't declare scalar dereference in "our"/;
157 eval 'my $$foo : bar = 1';
158 like $@, qr/Can't declare scalar dereference in "my"/;
161 my @code = qw(lvalue method);
162 my @other = qw(shared);
163 my @deprecated = qw(locked unique);
165 $valid{CODE} = {map {$_ => 1} @code};
166 $valid{SCALAR} = {map {$_ => 1} @other};
167 $valid{ARRAY} = $valid{HASH} = $valid{SCALAR};
169 $deprecated{CODE} = { locked => 1 };
170 $deprecated{ARRAY} = $deprecated{HASH} = $deprecated{SCALAR} = { unique => 1 };
172 our ($scalar, @array, %hash);
173 foreach my $value (\&foo, \$scalar, \@array, \%hash) {
174 my $type = ref $value;
175 foreach my $negate ('', '-') {
176 foreach my $attr (@code, @other, @deprecated) {
177 my $attribute = $negate . $attr;
178 eval "use attributes __PACKAGE__, \$value, '$attribute'";
179 if ($deprecated{$type}{$attr}) {
180 like $@, qr/^Attribute "$attr" is deprecated at \(eval \d+\)/,
181 "$type attribute $attribute deprecated";
182 } elsif ($valid{$type}{$attr}) {
183 if ($attribute eq '-shared') {
184 like $@, qr/^A variable may not be unshared/;
186 is( $@, '', "$type attribute $attribute");
189 like $@, qr/^Invalid $type attribute: $attribute/,
190 "Bogus $type attribute $attribute should fail";
196 # this will segfault if it fails
197 sub PVBM () { 'foo' }
198 { my $dummy = index 'foo', PVBM }
200 ok !defined(eval 'attributes::get(\PVBM)'),
201 'PVBMs don\'t segfault attributes::get';
204 # [perl #49472] Attributes + Unknown Error
207 sub MODIFY_CODE_ATTRIBUTE{}
208 sub f:Blah {$nosuchvar};
212 like ($err, qr/Global symbol "\$nosuchvar" requires /, 'perl #49472');
215 # Test that code attributes always get applied to the same CV that
216 # we're left with at the end (bug#66970).
220 sub MODIFY_CODE_ATTRIBUTES { $c = $_[1]; () }
221 $c=undef; eval 'sub t0 :Foo';
222 main::ok $c == \&{"t0"};
223 $c=undef; eval 'sub t1 :Foo { }';
224 main::ok $c == \&{"t1"};
225 $c=undef; eval 'sub t2';
227 $c=undef; eval 'sub t2 :Foo';
228 main::ok $c == \&{"t2"} && $c == $t2a;
229 $c=undef; eval 'sub t3';
231 $c=undef; eval 'sub t3 :Foo { }';
232 main::ok $c == \&{"t3"} && $c == $t3a;
233 $c=undef; eval 'sub t4 :Foo';
236 $c=undef; eval 'sub t4 :Foo';
237 main::ok $c == \&{"t4"} && $c == $t4b && $c == $t4a;
238 $c=undef; eval 'sub t5 :Foo';
241 $c=undef; eval 'sub t5 :Foo { }';
242 main::ok $c == \&{"t5"} && $c == $t5b && $c == $t5a;
245 my @tests = grep {/^[^#]/} split /\n/, <<'EOT';
246 # This one is fine as an empty attribute list
247 my $holy_Einstein : = '';
248 # This one is deprecated
250 our $FWISK_FWISK_FWIZZACH_FWACH_ZACHITTY_ZICH_SHAZZATZ_FWISK := '';
251 state $thump := 'Trumpets';
252 # Lather rinse repeat in my usual obsessive style
253 my @holy_perfect_pitch : = ();
256 # state @widget_mark := ();
257 my %holy_seditives : = ();
261 my $holy_giveaways : = '';
263 our $TWOYYOYYOING_THUK_UGH := 1 == 1;
264 state $octothorn := 'Tinky Winky';
265 my @holy_Taj_Mahal : = ();
267 our @PLAK_DAK_THUK_FRIT := ();
268 # state @hash_mark := ();
269 my %holy_priceless_collection_of_Etruscan_snoods : = ();
272 # state %octalthorpe := ();
273 my $holy_sewer_pipe : = '';
276 state $crunch := 'Laa Laa';
278 my @holy_harem : = ();
280 # state @square := ();
281 my %holy_pin_cushions : = ();
284 # state %scratchmark := ();
287 foreach my $test (@tests) {
291 like $@, qr/Use of := for an empty attribute list is not allowed/,
292 "Parse error for q{$test}";
294 is $@, '', "No error for q{$test}";
298 # [perl #68560] Calling closure prototypes (only accessible via :attr)
302 sub MODIFY_CODE_ATTRIBUTES { $proto = $_[1]; _: }
308 eval { $proto->() }; # used to crash in pp_entersub
309 like $@, qr/^Closure prototype called/,
310 "Calling closure proto with (no) args";
311 eval { () = &$proto }; # used to crash in pp_leavesub
312 like $@, qr/^Closure prototype called/,
313 'Calling closure proto with no @_ that returns a lexical';
316 # Referencing closure prototypes
320 sub MODIFY_CODE_ATTRIBUTES { push @proto, $_[1], \&{$_[1]}; _: }
322 () = sub :buck {$id};
323 &::is(@proto, 'referencing closure prototype');
326 # [perl #68658] Attributes on stately variables
329 sub MODIFY_SCALAR_ATTRIBUTES { () }
332 eval 'sub foo { use 5.01; state $x :A0 = $i++; $x_values .= $x }';
335 is $x_values, '00', 'state with attributes';
339 package ningnangnong;
340 sub MODIFY_SCALAR_ATTRIBUTES{}
341 sub MODIFY_ARRAY_ATTRIBUTES{ }
342 sub MODIFY_HASH_ATTRIBUTES{ }
343 my ($cows, @go, %bong) : teapots = qw[ jibber jabber joo ];
344 ::is $cows, 'jibber', 'list assignment to scalar with attrs';
345 ::is "@go", 'jabber joo', 'list assignment to array with attrs';
350 local $SIG{__WARN__} = sub { $w = shift };
354 'lvalue attribute applied to already-defined subroutine at '
357 'lvalue attribute removed from already-defined subroutine at '
359 eval 'use attributes __PACKAGE__, \&ent, "lvalue"';
360 like $w, qr/^$posmsg/, 'lvalue attr warning on def sub';
361 is join("",&attributes::get(\&ent)), "lvalue",':lvalue applied anyway';
363 eval 'use attributes __PACKAGE__, \&lent, "lvalue"; 1' or die;
364 is $w, "", 'no lvalue warning on def lvalue sub';
365 eval 'use attributes __PACKAGE__, \&lent, "-lvalue"';
366 like $w, qr/^$negmsg/, '-lvalue attr warning on def sub';
367 is join("",&attributes::get(\&lent)), "",
368 'lvalue attribute removed anyway';
370 eval 'use attributes __PACKAGE__, \&lent, "-lvalue"; 1' or die;
371 is $w, "", 'no -lvalue warning on def non-lvalue sub';
373 eval 'use attributes __PACKAGE__, \&lent, "lvalue"';
374 is $w, "", 'no lvalue warnings under no warnings misc';
375 eval 'use attributes __PACKAGE__, \&ent, "-lvalue"';
376 is $w, "", 'no -lvalue warnings under no warnings misc';
380 prog => 'BEGIN {$^H{a}=b} sub foo:bar{1}',
384 'attribute errors do not cause op trees to leak';