Debian Codegraph @

resolve/testdata/resolve.star

raw
1 # Tests of resolver errors.
2 #
3 # The initial environment contains the predeclared names "M"
4 # (module-specific) and "U" (universal). This distinction
5 # should be unobservable to the Starlark program.
6
7 # use of declared global
8 x = 1
9 _ = x
10
11 ---
12 # premature use of global is not a static error;
13 # see github.com/google/skylark/issues/116.
14 _ = x
15 x = 1
16
17 ---
18 # use of undefined global
19 _ = x ### "undefined: x"
20
21 ---
22 # redeclaration of global
23 x = 1
24 x = 2 ### "cannot reassign global x declared at .*resolve.star:23:1"
25
26 ---
27 # Redeclaration of predeclared names is allowed.
28 #
29 # This rule permits tool maintainers to add members to the predeclared
30 # environment without breaking existing programs.
31
32 # module-specific predeclared name
33 M = 1 # ok
34 M = 2 ### "cannot reassign global M declared at .*/resolve.star"
35
36 # universal predeclared name
37 U = 1 # ok
38 U = 1 ### "cannot reassign global U declared at .*/resolve.star"
39
40 ---
41 # A global declaration shadows all references to a predeclared;
42 # see github.com/google/skylark/issues/116.
43
44 a = U # ok: U is a reference to the global defined on the next line.
45 U = 1
46
47 ---
48 # reference to predeclared name
49 M()
50
51 ---
52 # locals may be referenced before they are defined
53
54 def f():
55 M(x) # dynamic error
56 x = 1
57
58 ---
59 # Various forms of assignment:
60
61 def f(x): # parameter
62 M(x)
63 M(y) ### "undefined: y"
64
65 (a, b) = 1, 2
66 M(a)
67 M(b)
68 M(c) ### "undefined: c"
69
70 [p, q] = 1, 2
71 M(p)
72 M(q)
73 M(r) ### "undefined: r"
74
75 ---
76 # a comprehension introduces a separate lexical block
77
78 _ = [x for x in "abc"]
79 M(x) ### "undefined: x"
80
81 ---
82 # Functions may have forward refs.
83 def f():
84 g()
85 h() ### "undefined: h"
86 def inner():
87 i()
88 i = lambda: 0
89
90 def g():
91 f()
92
93 ---
94 # It is not permitted to rebind a global using a += assignment.
95
96 x = [1]
97 x.extend([2]) # ok
98 x += [3] ### `cannot reassign global x`
99
100 def f():
101 x += [4] # x is local to f
102
103 y = 1
104 y += 2 ### `cannot reassign global y`
105 z += 3 # ok (but fails dynamically because z is undefined)
106
107 ---
108 def f(a):
109 if 1==1:
110 b = 1
111 c = 1
112 M(a) # ok: param
113 M(b) # ok: maybe bound local
114 M(c) # ok: bound local
115 M(d) # NB: we don't do a use-before-def check on local vars!
116 M(e) # ok: global
117 M(f) # ok: global
118 d = 1
119
120 e = 1
121
122 ---
123 # This program should resolve successfully but fail dynamically.
124 x = 1
125
126 def f():
127 M(x) # dynamic error: reference to undefined local
128 x = 2
129
130 f()
131
132 ---
133 load("module", "name") # ok
134
135 def f():
136 load("foo", "bar") ### "load statement within a function"
137
138 load("foo",
139 "", ### "load: empty identifier"
140 "_a", ### "load: names with leading underscores are not exported: _a"
141 b="", ### "load: empty identifier"
142 c="_d", ### "load: names with leading underscores are not exported: _d"
143 _e="f") # ok
144
145 ---
146 # option:toplevelcontrol
147 if M:
148 load("foo", "bar") ### "load statement within a conditional"
149
150 ---
151 # option:toplevelcontrol
152 for x in M:
153 load("foo", "bar") ### "load statement within a loop"
154
155 ---
156 # option:toplevelcontrol option:while
157 while M:
158 load("foo", "bar") ### "load statement within a loop"
159
160 ---
161 # return statements must be within a function
162
163 return ### "return statement not within a function"
164
165 ---
166 # if-statements and for-loops at top-level are forbidden
167 # (without globalreassign option)
168
169 for x in "abc": ### "for loop not within a function"
170 pass
171
172 if x: ### "if statement not within a function"
173 pass
174
175 ---
176 # option:toplevelcontrol
177
178 for x in "abc": # ok
179 pass
180
181 if x: # ok
182 pass
183
184 ---
185 # while loops are forbidden (without -recursion option)
186
187 def f():
188 while U: ### "dialect does not support while loops"
189 pass
190
191 ---
192 # option:while
193
194 def f():
195 while U: # ok
196 pass
197
198 while U: ### "while loop not within a function"
199 pass
200
201 ---
202 # option:toplevelcontrol option:while
203
204 while U: # ok
205 pass
206
207 ---
208 # The parser allows any expression on the LHS of an assignment.
209
210 1 = 0 ### "can't assign to literal"
211 1+2 = 0 ### "can't assign to binaryexpr"
212 f() = 0 ### "can't assign to callexpr"
213
214 [a, b] = 0
215 [c, d] += 0 ### "can't use list expression in augmented assignment"
216 (e, f) += 0 ### "can't use tuple expression in augmented assignment"
217
218 [] = 0 # ok
219 () = 0 # ok
220
221 ---
222 # break and continue statements must appear within a loop
223
224 break ### "break not in a loop"
225
226 continue ### "continue not in a loop"
227
228 pass
229
230 ---
231 # Test for bug #598: break statement inside nested function within a loop
232 def fun1():
233 for a in [1, 2, 3]:
234 def nested():
235 break ### "break not in a loop"
236
237 def fun2():
238 for a in [1, 2, 3]:
239 def nested():
240 continue ### "continue not in a loop"
241
242 ---
243 # Positional arguments (and required parameters)
244 # must appear before named arguments (and optional parameters).
245
246 M(x=1, 2) ### `positional argument may not follow named`
247
248 def f(x=1, y): pass ### `required parameter may not follow optional`
249 ---
250 # No parameters may follow **kwargs in a declaration.
251
252 def f(**kwargs, x): ### `parameter may not follow \*\*kwargs`
253 pass
254
255 def g(**kwargs, *args): ### `\* parameter may not follow \*\*kwargs`
256 pass
257
258 def h(**kwargs1, **kwargs2): ### `multiple \*\* parameters not allowed`
259 pass
260
261 ---
262 # Only keyword-only params and **kwargs may follow *args in a declaration.
263
264 def f(*args, x): # ok
265 pass
266
267 def g(*args1, *args2): ### `multiple \* parameters not allowed`
268 pass
269
270 def h(*, ### `bare \* must be followed by keyword-only parameters`
271 *): ### `multiple \* parameters not allowed`
272 pass
273
274 def i(*args, *): ### `multiple \* parameters not allowed`
275 pass
276
277 def j(*, ### `bare \* must be followed by keyword-only parameters`
278 *args): ### `multiple \* parameters not allowed`
279 pass
280
281 def k(*, **kwargs): ### `bare \* must be followed by keyword-only parameters`
282 pass
283
284 def l(*): ### `bare \* must be followed by keyword-only parameters`
285 pass
286
287 def m(*args, a=1, **kwargs): # ok
288 pass
289
290 def n(*, a=1, **kwargs): # ok
291 pass
292
293 ---
294 # No arguments may follow **kwargs in a call.
295 def f(*args, **kwargs):
296 pass
297
298 f(**{}, 1) ### `argument may not follow \*\*kwargs`
299 f(**{}, x=1) ### `argument may not follow \*\*kwargs`
300 f(**{}, *[]) ### `\*args may not follow \*\*kwargs`
301 f(**{}, **{}) ### `multiple \*\*kwargs not allowed`
302
303 ---
304 # Only **kwargs may follow *args in a call.
305 def f(*args, **kwargs):
306 pass
307
308 f(*[], 1) ### `positional argument may not follow \*args`
309 f(*[], a=1) ### `keyword argument may not follow \*args`
310 f(*[], *[]) ### `multiple \*args not allowed`
311 f(*[], **{}) # ok
312
313 ---
314 # Parameter names must be unique.
315
316 def f(a, b, a): pass ### "duplicate parameter: a"
317 def g(args, b, *args): pass ### "duplicate parameter: args"
318 def h(kwargs, a, **kwargs): pass ### "duplicate parameter: kwargs"
319 def i(*x, **x): pass ### "duplicate parameter: x"
320
321 ---
322 # Floating-point support is now standard.
323 a = float("3.141")
324 b = 1 / 2
325 c = 3.141
326
327 ---
328 # option:globalreassign
329 # Legacy Bazel (and Python) semantics: def must precede use even for globals.
330
331 _ = x ### `undefined: x`
332 x = 1
333
334 ---
335 # option:globalreassign
336 # Legacy Bazel (and Python) semantics: reassignment of globals is allowed.
337 x = 1
338 x = 2 # ok
339
340 ---
341 # option:globalreassign
342 # Redeclaration of predeclared names is allowed.
343
344 # module-specific predeclared name
345 M = 1 # ok
346 M = 2 # ok (legacy)
347
348 # universal predeclared name
349 U = 1 # ok
350 U = 1 # ok (legacy)
351
352 ---
353 # https://github.com/bazelbuild/starlark/starlark/issues/21
354 def f(**kwargs): pass
355 f(a=1, a=1) ### `keyword argument "a" is repeated`
356
357
358 ---
359 # spelling
360
361 print = U
362
363 hello = 1
364 print(hollo) ### `undefined: hollo \(did you mean hello\?\)`
365
366 def f(abc):
367 print(abd) ### `undefined: abd \(did you mean abc\?\)`
368 print(goodbye) ### `undefined: goodbye$`
369
370 ---
371 load("module", "x") # ok
372 x = 1 ### `cannot reassign local x`
373 load("module", "x") ### `cannot reassign top-level x`
374
375 ---
376 # option:loadbindsglobally
377 load("module", "x") # ok
378 x = 1 ### `cannot reassign global x`
379 load("module", "x") ### `cannot reassign global x`
380
381 ---
382 # option:globalreassign
383 load("module", "x") # ok
384 x = 1 # ok
385 load("module", "x") # ok
386
387 ---
388 # option:globalreassign option:loadbindsglobally
389 load("module", "x") # ok
390 x = 1
391 load("module", "x") # ok
392
393 ---
394 _ = x # forward ref to file-local
395 load("module", "x") # ok