-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathconst_spec.cr
495 lines (398 loc) · 8.18 KB
/
const_spec.cr
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
require "../../spec_helper"
describe "Semantic: const" do
it "types a constant" do
input = parse("CONST = 1").as(Assign)
semantic input
input.target.type?.should be_nil # Don't type value until needed
end
it "types a constant reference" do
assert_type("CONST = 1; CONST") { int32 }
end
it "types a nested constant" do
assert_type("class Foo; A = 1; end; Foo::A") { int32 }
end
it "types a constant using Path" do
assert_type(%(
Foo::Bar = 1
Foo::Bar
)) { int32 }
end
it "types a nested constant using Path" do
assert_type(%(
class Foo
Bar::Baz = 1
end
Foo::Bar::Baz
)) { int32 }
end
it "types a nested type with same name" do
assert_type(%(
class Foo
class Foo
A = 1
end
end
Foo::Foo::A
)) { int32 }
end
it "creates container module if not exist when using Path" do
assert_type(%(
Foo::Bar = 1
Foo
)) do
foo = types["Foo"]
foo.module?.should be_true
foo.metaclass
end
end
it "keeps type of container when using Path" do
assert_type(%(
class Foo
end
Foo::Const = 1
Foo
)) do
foo = types["Foo"]
foo.class?.should be_true
foo.metaclass
end
assert_type(%(
struct Foo
end
Foo::Const = 1
Foo
)) do
foo = types["Foo"]
foo.struct?.should be_true
foo.metaclass
end
assert_type(%(
module Foo
end
Foo::Const = 1
Foo
)) do
foo = types["Foo"]
foo.module?.should be_true
foo.metaclass
end
end
it "types a constant inside a def" do
assert_type("
class Foo
A = 1
def foo
A
end
end
Foo.new.foo
") { int32 }
end
it "finds nearest constant first" do
assert_type("
CONST = 1
class Foo
CONST = 2.5
def foo
CONST
end
end
Foo.new.foo
") { float64 }
end
it "finds current type first" do
assert_type("
class Foo
class Bar
def self.foo
Bar.new.foo
end
def foo
1
end
end
end
Foo::Bar.foo
") { int32 }
end
it "finds current type before parents (#4086)" do
assert_type(%(
class Foo
class Bar
class Baz < Foo
def self.foo
Baz.new.foo
end
def foo
1
end
end
end
class Baz
end
end
Foo::Bar::Baz.foo
)) { int32 }
end
it "doesn't count parent types as current type" do
assert_type(%(
class Foo
end
class Bar
class Foo
def foo
1
end
end
class Baz < Foo
def self.bar
Foo.new
end
end
end
Bar::Baz.bar.foo
)) { int32 }
end
it "finds current type only for first path item (1)" do
assert_error %(
class Foo
def self.foo
Foo::Foo
end
end
Foo.foo
),
"undefined constant Foo::Foo"
end
it "finds current type only for first path item (2)" do
assert_error %(
class Foo
class Foo
end
def self.foo
Foo::Foo
end
end
Foo.foo
),
"undefined constant Foo::Foo"
end
it "types a global constant reference in method" do
assert_type("
FOO = 2.5
class Bar
FOO = 1
def foo
::FOO
end
end
Bar.new.foo
") { float64 }
end
it "types a global constant reference in static method" do
assert_type("
CONST = 2.5
class Bar
CONST = 1
def self.foo
CONST
end
end
Bar.foo
") { int32 }
end
it "doesn't share variables with global scope" do
assert_error "a = 1; CONST = a; CONST",
"undefined local variable or method 'a'"
end
it "finds const from restriction" do
assert_type("
struct Int32
FOO = 'a'
end
def foo(x : U) forall U
U::FOO
end
foo 1
") { char }
end
it "doesn't crash with const used in initialize (bug)" do
assert_type("
COCO = init_coco
def init_coco
1
end
class Foo
def initialize
COCO
end
end
Foo.new
COCO
") { int32 }
end
it "finds constant in module that includes module (#205)" do
assert_type(%(
module Foo
CONSTANT = true
end
module Moo
include Foo
end
Moo::CONSTANT
)) { bool }
end
it "finds constant in class that extends class (#205)" do
assert_type(%(
class Foo
CONSTANT = true
end
class Bar < Foo
end
Bar::CONSTANT
)) { bool }
end
["nil", "true", "1", "'a'", %("foo"), "+ 1", "- 2", "~ 2",
"1 + 2", "1 + ZED", "ZED - 1", "ZED * 2", "ZED // 2",
"1 &+ ZED", "ZED &- 1", "ZED &* 2"].each do |node|
it "doesn't errors if constant depends on another one defined later through method, but constant is simple (#{node})" do
assert_no_errors <<-CRYSTAL, inject_primitives: true
ZED = 10
struct Int32
def +; 0; end
def ~; 0; end
def -; 0; end
def //(other); 0; end
end
CONST1 = foo
CONST2 = #{node}
def foo
CONST2
end
CONST1
CRYSTAL
end
end
it "doesn't error if using c enum" do
assert_type(%(
lib LibC
enum Foo
A = 1
end
end
LibC::Foo::A
)) { types["LibC"].types["Foo"] }
end
it "errors on dynamic constant assignment inside block" do
assert_error %(
def foo
yield
end
foo do
CONST = 1
end
),
"can't declare constant dynamically"
end
it "errors on dynamic constant assignment inside if" do
assert_error %(
if 1 == 1
CONST = 1
end
),
"can't declare constant dynamically"
end
it "can use constant defined later (#2906)" do
assert_type(%(
FOO = Foo.new
class Foo
A = Bar.new
def initialize
A
end
end
class Bar
end
FOO
)) { types["Foo"] }
end
it "errors if can't infer constant type (#3240, #3948)" do
assert_error %(
A = A.b
A
),
"can't infer type of constant A"
end
it "errors if using constant as generic type (#3240)" do
assert_error %(
Foo = Foo(Int32).new
Foo
),
"Foo is not a type, it's a constant"
end
it "errors if using const in type declaration" do
assert_error %(
A = 1
class Foo
@x : A
end
),
"A is not a type, it's a constant"
end
it "errors if using const in uninitialized" do
assert_error %(
A = 1
x = uninitialized A
),
"A is not a type, it's a constant"
end
it "errors if using const in var declaration" do
assert_error %(
A = 1
x : A
),
"A is not a type, it's a constant"
end
it "errors if using const in restriction" do
assert_error %(
A = 1
def foo(x : A)
end
foo(1)
),
"A is not a type, it's a constant"
end
it "errors if using const in proc notation parameter type" do
assert_error <<-CRYSTAL, "A is not a type, it's a constant"
A = 1
x : A ->
CRYSTAL
end
it "errors if using const in proc notation return type" do
assert_error <<-CRYSTAL, "A is not a type, it's a constant"
A = 1
x : -> A
CRYSTAL
end
it "errors if using return inside constant value (#5391)" do
assert_error %(
class Foo
A = begin
return if 1 == 2
end
end
Foo::A
),
"can't return from constant", inject_primitives: true
end
it "errors if constant has NoReturn type (#6139)" do
assert_error %(
lib LibFoo
fun foo : NoReturn
end
FOO = LibFoo.foo
FOO
),
"constant FOO has illegal type NoReturn"
end
end