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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
| | # Tests for the zsh/param/static module
%prep
zmodload zsh/param/static
%test
typeset scalar_test=toplevel
() {
print $scalar_test
static scalar_test
print $+scalar_test
unset scalar_test
print $+scalar_test
}
print $scalar_test
0:basic scope hiding
>toplevel
>1
>0
>toplevel
typeset scalar_test=toplevel
print $scalar_test
() {
static scalar_test=function
print $scalar_test
}
print $scalar_test
0:enter and exit a scope
>toplevel
>function
>toplevel
print $+unset_test
() {
static unset_test
print $+unset_test
unset_test=setme
print $unset_test
}
print $+unset_test
0:variable defined only in scope
>0
>1
>setme
>0
# Depends on zsh-5.0.9 typeset keyword
typeset -a array_test=(top level)
() {
local -Sa array_test=(in function)
() {
static array_test
print $+array_test
}
print $array_test
}
print $array_test
0:nested scope with different type, correctly restored
>1
>in function
>top level
typeset -a array_test=(top level)
() {
static array_test
array_test=(in function)
}
1:type of static may not be changed by assignment
?(anon):2: array_test: attempt to assign array value to non-array
typeset -A hash_test=(top level)
() {
setopt localoptions noglob
static hash_test[top]
}
1:associative array fields may not be static
?(anon):static:2: hash_test[top]: can't create local array elements
() {
static path
}
1:tied params may not be static, part 1
?(anon):static:1: can't change scope of existing param: path
() {
static PATH
}
1:tied params may not be static, part 2
?(anon):static:1: can't change scope of existing param: PATH
() {
static -h path
print X$path
}
0:statics may hide tied paramters
>X
# Deliberate type mismatch here
typeset -a hash_test=(top level)
typeset -p hash_test
inner () {
static -p hash_test
print ${(t)hash_test} ${(kv)hash_test}
}
outer () {
local -SA hash_test=(in function)
typeset -p hash_test
inner
}
outer
print ${(kv)hash_test}
0:static hides value from surrounding scope in nested scope
>typeset -a hash_test
>hash_test=( top level )
>typeset -A hash_test
>hash_test=( in function )
>typeset -a hash_test
>hash_test=( top level )
>array-local top level
>top level
F:note "typeset" rather than "static" in output from outer
() {
static -a array_test
local array_test=scalar
}
1:static cannot be re-declared as local
?(anon):local:2: array_test: inconsistent type for assignment
() {
local hash_test=scalar
static -A hash_test
}
1:local cannot be re-declared as static
?(anon):static:2: can't change scope of existing param: hash_test
inner () {
print $+scalar_test
$ZTST_testdir/../Src/zsh -fc 'print X $scalar_test'
}
() {
static -x scalar_test=whaat
$ZTST_testdir/../Src/zsh -fc 'print X $scalar_test'
inner
print Y $scalar_test
}
0:exported static behaves like a local, part 1
>X whaat
>0
>X whaat
>Y whaat
inner () {
typeset -p array_test
$ZTST_testdir/../Src/zsh -fc 'print X $array_test'
}
() {
local -Sax array_test=(whaat)
print Y $array_test
$ZTST_testdir/../Src/zsh -fc 'print X $array_test'
inner
}
0:exported static behaves like a local, part 2 (arrays do not export)
?inner:typeset:1: no such variable: array_test
>Y whaat
>X
>X
inner () {
print $+scalar_test
$ZTST_testdir/../Src/zsh -fc 'print X $scalar_test'
}
() {
static scalar_test=whaat
export scalar_test
$ZTST_testdir/../Src/zsh -fc 'print X $scalar_test'
inner
() {
print $+scalar_test
$ZTST_testdir/../Src/zsh -fc 'print X $scalar_test'
}
print Y $scalar_test
}
0:exported static behaves like a local, part 3 (export does not change scope)
>X whaat
>0
>X whaat
>0
>X whaat
>Y whaat
typeset -A hash_test=(top level)
() {
local -SA hash_test=(in function)
() {
print X ${(kv)hash_test}
}
print Y ${(kv)hash_test}
}
print ${(kv)hash_test}
0:statics are not visible in anonymous functions, part 1
>X top level
>Y in function
>top level
typeset -A hash_test=(top level)
() {
local -SA hash_test=(in function)
() {
print X ${(kv)hash_test}
hash_test[in]=deeper
}
print Y ${(kv)hash_test}
}
print ${(okv)hash_test}
0:statics are not visible in anonymous functions, part 2
>X top level
>Y in function
>deeper in level top
typeset -A hash_test=(top level)
() {
local -Sa array_test=(in function)
local -SA hash_test=($array_test)
() {
print X ${(kv)hash_test}
hash_test=(even deeper)
array_test+=(${(kv)hash_test})
}
print Y ${(kv)hash_test} Z $array_test
}
print ${(kv)hash_test}
0:statics are not visible in anonymous functions, part 3
>X top level
>Y in function Z in function
>even deeper
typeset -A hash_test=(top level)
() {
local -SA hash_test=(in function)
() {
print X ${(kv)hash_test}
unset hash_test
}
print Y ${(kv)hash_test}
}
print ${(t)hash_test} ${(kv)hash_test}
0:statics are not visible in anonymous functions, part 4
>X top level
>Y in function
>
# Subshell because otherwise this silently dumps core when broken
( () { static SECONDS } )
1:special parameters cannot be made static
?(anon):static: can't change scope of existing param: SECONDS
() { static -h SECONDS }
0:static parameter may hide a special parameter
|