1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
#!./perl
#
# test OP_MULTIDEREF.
#
# This optimising op is used when one or more array or hash aggregate
# lookups / derefs are performed, and where each key/index is a simple
# constant or scalar var; e.g.
#
# $r->{foo}[0]{$k}[$i]
BEGIN {
chdir 't';
require './test.pl';
set_up_inc("../lib");
}
use warnings;
use strict;
plan 65;
# check that strict refs hint is handled
{
package strict_refs;
our %foo;
my @a = ('foo');
eval {
$a[0]{k} = 7;
};
::like($@, qr/Can't use string/, "strict refs");
::ok(!exists $foo{k}, "strict refs, not exist");
no strict 'refs';
$a[0]{k} = 13;
::is($foo{k}, 13, "no strict refs, exist");
}
# check the basics of multilevel lookups
{
package basic;
# build up the multi-level structure piecemeal to try and avoid
# relying on what we're testing
my @a;
my $r = \@a;
my $rh = {};
my $ra = [];
my %h = qw(a 1 b 2 c 3 d 4 e 5 f 6);
push @a, 66, 77, 'abc', $rh;
%$rh = (foo => $ra, bar => 'BAR');
push @$ra, 'def', \%h;
our ($i1, $i2, $k1, $k2) = (3, 1, 'foo', 'c');
my ($li1, $li2, $lk1, $lk2) = (3, 1, 'foo', 'c');
my $z = 0;
# fetch
::is($a[3]{foo}[1]{c}, 3, 'fetch: const indices');
::is($a[$i1]{$k1}[$i2]{$k2}, 3, 'fetch: pkg indices');
::is($r->[$i1]{$k1}[$i2]{$k2}, 3, 'fetch: deref pkg indices');
::is($a[$li1]{$lk1}[$li2]{$lk2}, 3, 'fetch: lexical indices');
::is($r->[$li1]{$lk1}[$li2]{$lk2}, 3, 'fetch: deref lexical indices');
::is(($r//0)->[$li1]{$lk1}[$li2+$z]{$lk2}, 3,
'fetch: general expression and index');
# store
::is($a[3]{foo}[1]{c} = 5, 5, 'store: const indices');
::is($a[3]{foo}[1]{c}, 5, 'store: const indices 2');
::is($a[$i1]{$k1}[$i2]{$k2} = 7, 7, 'store: pkg indices');
::is($a[$i1]{$k1}[$i2]{$k2}, 7, 'store: pkg indices 2');
::is($r->[$i1]{$k1}[$i2]{$k2} = 9, 9, 'store: deref pkg indices');
::is($r->[$i1]{$k1}[$i2]{$k2}, 9, 'store: deref pkg indices 2');
::is($a[$li1]{$lk1}[$li2]{$lk2} = 11, 11, 'store: lexical indices');
::is($a[$li1]{$lk1}[$li2]{$lk2}, 11, 'store: lexical indices 2');
::is($r->[$li1]{$lk1}[$li2]{$lk2} = 13, 13, 'store: deref lexical indices');
::is($r->[$li1]{$lk1}[$li2]{$lk2}, 13, 'store: deref lexical indices 2');
::is(($r//0)->[$li1]{$lk1}[$li2+$z]{$lk2} = 15, 15,
'store: general expression and index');
::is(($r//0)->[$li1]{$lk1}[$li2+$z]{$lk2}, 15,
'store: general expression and index 2');
# local
{
::is(local $a[3]{foo}[1]{c} = 19, 19, 'local const indices');
::is($a[3]{foo}[1]{c}, 19, 'local const indices 2');
}
::is($a[3]{foo}[1]{c}, 15, 'local const indices 3');
{
::is(local $a[$i1]{$k1}[$i2]{$k2} = 21, 21, 'local pkg indices');
::is($a[$i1]{$k1}[$i2]{$k2}, 21, 'local pkg indices 2');
}
::is($a[$i1]{$k1}[$i2]{$k2}, 15, 'local pkg indices 3');
{
::is(local $a[$li1]{$lk1}[$li2]{$lk2} = 23, 23, 'local lexical indices');
::is($a[$li1]{$lk1}[$li2]{$lk2}, 23, 'local lexical indices 2');
}
::is($a[$li1]{$lk1}[$li2]{$lk2}, 15, 'local lexical indices 3');
{
::is(local+($r//0)->[$li1]{$lk1}[$li2+$z]{$lk2} = 25, 25,
'local general');
::is(($r//0)->[$li1]{$lk1}[$li2+$z]{$lk2}, 25, 'local general 2');
}
::is(($r//0)->[$li1]{$lk1}[$li2+$z]{$lk2}, 15, 'local general 3');
# exists
::ok(exists $a[3]{foo}[1]{c}, 'exists: const indices');
::ok(exists $a[$i1]{$k1}[$i2]{$k2}, 'exists: pkg indices');
::ok(exists $r->[$i1]{$k1}[$i2]{$k2}, 'exists: deref pkg indices');
::ok(exists $a[$li1]{$lk1}[$li2]{$lk2}, 'exists: lexical indices');
::ok(exists $r->[$li1]{$lk1}[$li2]{$lk2}, 'exists: deref lexical indices');
::ok(exists +($r//0)->[$li1]{$lk1}[$li2+$z]{$lk2}, 'exists: general');
# delete
our $k3 = 'a';
my $lk4 = 'b';
::is(delete $a[3]{foo}[1]{c}, 15, 'delete: const indices');
::is(delete $a[$i1]{$k1}[$i2]{$k3}, 1, 'delete: pkg indices');
::is(delete $r->[$i1]{$k1}[$i2]{d}, 4, 'delete: deref pkg indices');
::is(delete $a[$li1]{$lk1}[$li2]{$lk4}, 2, 'delete: lexical indices');
::is(delete $r->[$li1]{$lk1}[$li2]{e}, 5, 'delete: deref lexical indices');
::is(delete +($r//0)->[$li1]{$lk1}[$li2+$z]{f}, 6, 'delete: general');
# !exists
::ok(!exists $a[3]{foo}[1]{c}, '!exists: const indices');
::ok(!exists $a[$i1]{$k1}[$i2]{$k3}, '!exists: pkg indices');
::ok(!exists $r->[$i1]{$k1}[$i2]{$k3}, '!exists: deref pkg indices');
::ok(!exists $a[$li1]{$lk1}[$li2]{$lk4}, '!exists: lexical indices');
::ok(!exists $r->[$li1]{$lk1}[$li2]{$lk4},'!exists: deref lexical indices');
::ok(!exists +($r//0)->[$li1]{$lk1}[$li2+$z]{$lk4},'!exists: general');
}
# weird "constant" keys
{
use constant my_undef => undef;
use constant my_ref => [];
no warnings 'uninitialized';
my %h1;
$h1{+my_undef} = 1;
is(join(':', keys %h1), '', "+my_undef");
my %h2;
$h2{+my_ref} = 1;
like(join(':', keys %h2), qr/x/, "+my_ref");
}
{
# test that multideref is marked OA_DANGEROUS, i.e. its one of the ops
# that should set the OPpASSIGN_COMMON flag in list assignments
my $x = {};
$x->{a} = [ 1 ];
$x->{b} = [ 2 ];
($x->{a}, $x->{b}) = ($x->{b}, $x->{a});
is($x->{a}[0], 2, "OA_DANGEROUS a");
is($x->{b}[0], 1, "OA_DANGEROUS b");
}
# defer
sub defer {}
{
my %h;
$h{foo} = {};
defer($h{foo}{bar});
ok(!exists $h{foo}{bar}, "defer");
}
# RT #123609
# don't evaluate a const array index unless it's really a const array
# index
{
my $warn = '';
local $SIG{__WARN__} = sub { $warn .= $_[0] };
ok(
eval q{
my @a = (1);
my $arg = 0;
my $x = $a[ 'foo' eq $arg ? 1 : 0 ];
1;
},
"#123609: eval"
)
or diag("eval gave: $@");
is($warn, "", "#123609: warn");
}
# RT #130727
# a [ah]elem op can be both OPpLVAL_INTRO and OPpDEREF. It may not make
# much sense, but it shouldn't fail an assert.
{
my @x;
eval { @{local $x[0][0]} = 1; };
like $@, qr/Can't use an undefined value as an ARRAY reference/,
"RT #130727 error";
ok !defined $x[0][0],"RT #130727 array not autovivified";
eval { @{1, local $x[0][0]} = 1; };
like $@, qr/Can't use an undefined value as an ARRAY reference/,
"RT #130727 part 2: error";
ok !defined $x[0][0],"RT #130727 part 2: array not autovivified";
}
# RT #131627: assertion failure on OPf_PAREN on OP_GV
{
my @x = (10..12);
our $rt131627 = 1;
no strict qw(refs vars);
is $x[qw(rt131627)->$*], 11, 'RT #131627: $a[qw(var)->$*]';
}
# this used to leak - run the code for ASan to spot any problems
{
package Foo;
our %FIELDS = ();
my Foo $f;
eval q{ my $x = $f->{c}; };
::pass("S_maybe_multideref() shouldn't leak on croak");
}
fresh_perl_is('0for%{scalar local$0[0]}', '', {},
"RT #134045 assertion on the OP_SCALAR");
|