aboutsummaryrefslogtreecommitdiff
path: root/resolve/testdata/resolve.star
blob: ce6711050ecba72f20843d819170ea3c732a4f29 (plain)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# Tests of resolver errors.
#
# The initial environment contains the predeclared names "M"
# (module-specific) and "U" (universal). This distinction
# should be unobservable to the Starlark program.

# use of declared global
x = 1
_ = x

---
# premature use of global is not a static error;
# see github.com/google/skylark/issues/116.
_ = x
x = 1

---
# use of undefined global
_ = x ### "undefined: x"

---
# redeclaration of global
x = 1
x = 2 ### "cannot reassign global x declared at .*resolve.star:23:1"

---
# Redeclaration of predeclared names is allowed.
#
# This rule permits tool maintainers to add members to the predeclared
# environment without breaking existing programs.

# module-specific predeclared name
M = 1 # ok
M = 2 ### "cannot reassign global M declared at .*/resolve.star"

# universal predeclared name
U = 1 # ok
U = 1 ### "cannot reassign global U declared at .*/resolve.star"

---
# A global declaration shadows all references to a predeclared;
# see github.com/google/skylark/issues/116.

a = U # ok: U is a reference to the global defined on the next line.
U = 1

---
# reference to predeclared name
M()

---
# locals may be referenced before they are defined

def f():
   M(x) # dynamic error
   x = 1

---
# Various forms of assignment:

def f(x): # parameter
    M(x)
    M(y) ### "undefined: y"

(a, b) = 1, 2
M(a)
M(b)
M(c) ### "undefined: c"

[p, q] = 1, 2
M(p)
M(q)
M(r) ### "undefined: r"

---
# a comprehension introduces a separate lexical block

_ = [x for x in "abc"]
M(x) ### "undefined: x"

---
# Functions may have forward refs.
def f():
   g()
   h() ### "undefined: h"
   def inner():
     i()
     i = lambda: 0

def g():
  f()

---
# It is not permitted to rebind a global using a += assignment.

x = [1]
x.extend([2]) # ok
x += [3] ### `cannot reassign global x`

def f():
   x += [4] # x is local to f

y = 1
y += 2 ### `cannot reassign global y`
z += 3 # ok (but fails dynamically because z is undefined)

---
def f(a):
  if 1==1:
    b = 1
  c = 1
  M(a) # ok: param
  M(b) # ok: maybe bound local
  M(c) # ok: bound local
  M(d) # NB: we don't do a use-before-def check on local vars!
  M(e) # ok: global
  M(f) # ok: global
  d = 1

e = 1

---
# This program should resolve successfully but fail dynamically.
x = 1

def f():
  M(x) # dynamic error: reference to undefined local
  x = 2

f()

---
load("module", "name") # ok

def f():
  load("foo", "bar") ### "load statement within a function"

load("foo",
     "",     ### "load: empty identifier"
     "_a",   ### "load: names with leading underscores are not exported: _a"
     b="",   ### "load: empty identifier"
     c="_d", ### "load: names with leading underscores are not exported: _d"
     _e="f") # ok

---
# option:globalreassign
if M:
    load("foo", "bar") ### "load statement within a conditional"

---
# option:globalreassign
for x in M:
    load("foo", "bar") ### "load statement within a loop"

---
# option:recursion option:globalreassign
while M:
    load("foo", "bar") ### "load statement within a loop"

---
# return statements must be within a function

return ### "return statement not within a function"

---
# if-statements and for-loops at top-level are forbidden
# (without globalreassign option)

for x in "abc": ### "for loop not within a function"
  pass

if x: ### "if statement not within a function"
  pass

---
# option:globalreassign

for x in "abc": # ok
  pass

if x: # ok
  pass

---
# while loops are forbidden (without -recursion option)

def f():
  while U: ### "dialect does not support while loops"
    pass

---
# option:recursion

def f():
  while U: # ok
    pass

while U: ### "while loop not within a function"
  pass

---
# option:globalreassign option:recursion

while U: # ok
  pass

---
# The parser allows any expression on the LHS of an assignment.

1 = 0 ### "can't assign to literal"
1+2 = 0 ### "can't assign to binaryexpr"
f() = 0 ### "can't assign to callexpr"

[a, b] = 0
[c, d] += 0 ### "can't use list expression in augmented assignment"
(e, f) += 0 ### "can't use tuple expression in augmented assignment"

[] = 0 # ok
() = 0 # ok

---
# break and continue statements must appear within a loop

break ### "break not in a loop"

continue ### "continue not in a loop"

pass

---
# Positional arguments (and required parameters)
# must appear before named arguments (and optional parameters).

M(x=1, 2) ### `positional argument may not follow named`

def f(x=1, y): pass ### `required parameter may not follow optional`
---
# No parameters may follow **kwargs in a declaration.

def f(**kwargs, x): ### `parameter may not follow \*\*kwargs`
  pass

def g(**kwargs, *args): ### `\* parameter may not follow \*\*kwargs`
  pass

def h(**kwargs1, **kwargs2): ### `multiple \*\* parameters not allowed`
  pass

---
# Only keyword-only params and **kwargs may follow *args in a declaration.

def f(*args, x): # ok
  pass

def g(*args1, *args2): ### `multiple \* parameters not allowed`
  pass

def h(*, ### `bare \* must be followed by keyword-only parameters`
      *): ### `multiple \* parameters not allowed`
  pass

def i(*args, *): ### `multiple \* parameters not allowed`
  pass

def j(*,      ### `bare \* must be followed by keyword-only parameters`
      *args): ### `multiple \* parameters not allowed`
  pass

def k(*, **kwargs): ### `bare \* must be followed by keyword-only parameters`
  pass

def l(*): ### `bare \* must be followed by keyword-only parameters`
  pass

def m(*args, a=1, **kwargs): # ok
  pass

def n(*, a=1, **kwargs): # ok
  pass

---
# No arguments may follow **kwargs in a call.
def f(*args, **kwargs):
  pass

f(**{}, 1) ### `argument may not follow \*\*kwargs`
f(**{}, x=1) ### `argument may not follow \*\*kwargs`
f(**{}, *[]) ### `\*args may not follow \*\*kwargs`
f(**{}, **{}) ### `multiple \*\*kwargs not allowed`

---
# Only **kwargs may follow *args in a call.
def f(*args, **kwargs):
  pass

f(*[], 1) ### `positional argument may not follow \*args`
f(*[], a=1) ### `keyword argument may not follow \*args`
f(*[], *[]) ### `multiple \*args not allowed`
f(*[], **{}) # ok

---
# Parameter names must be unique.

def f(a, b, a): pass ### "duplicate parameter: a"
def g(args, b, *args): pass ### "duplicate parameter: args"
def h(kwargs, a, **kwargs): pass ### "duplicate parameter: kwargs"
def i(*x, **x): pass ### "duplicate parameter: x"

---
# Floating-point support is now standard.
a = float("3.141")
b = 1 / 2
c = 3.141

---
# option:globalreassign
# Legacy Bazel (and Python) semantics: def must precede use even for globals.

_ = x ### `undefined: x`
x = 1

---
# option:globalreassign
# Legacy Bazel (and Python) semantics: reassignment of globals is allowed.
x = 1
x = 2 # ok

---
# option:globalreassign
# Redeclaration of predeclared names is allowed.

# module-specific predeclared name
M = 1 # ok
M = 2 # ok (legacy)

# universal predeclared name
U = 1 # ok
U = 1 # ok (legacy)

---
# https://github.com/bazelbuild/starlark/starlark/issues/21
def f(**kwargs): pass
f(a=1, a=1) ### `keyword argument a repeated`


---
# spelling

print = U

hello = 1
print(hollo) ### `undefined: hollo \(did you mean hello\?\)`

def f(abc):
   print(abd) ### `undefined: abd \(did you mean abc\?\)`
   print(goodbye) ### `undefined: goodbye$`

---
load("module", "x") # ok
x = 1 ### `cannot reassign local x`
load("module", "x") ### `cannot reassign top-level x`

---
# option:loadbindsglobally
load("module", "x") # ok
x = 1 ### `cannot reassign global x`
load("module", "x") ### `cannot reassign global x`

---
# option:globalreassign
load("module", "x") # ok
x = 1 # ok
load("module", "x") # ok

---
# option:globalreassign option:loadbindsglobally
load("module", "x") # ok
x = 1
load("module", "x") # ok

---
_ = x # forward ref to file-local
load("module", "x") # ok