forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.jl
5116 lines (4566 loc) · 115 KB
/
core.jl
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is a part of Julia. License is MIT: https://julialang.org/license
# test core language features
const Bottom = Union{}
# For curmod_*
include("testenv.jl")
f47(x::Vector{Vector{T}}) where {T} = 0
@test_throws MethodError f47(Array{Vector}(0))
@test f47(Array{Vector{Int}}(0)) == 0
# checking unionall and typevar components
@test_throws TypeError ([] where T)
@test_throws TypeError ([T] where T)
@test_throws TypeError (Array{T} where T<:[])
@test_throws TypeError (Array{T} where T>:[])
@test_throws TypeError (Array{T} where T<:Vararg)
@test_throws TypeError (Array{T} where T>:Vararg)
@test_throws TypeError (Array{T} where T<:Vararg{Int})
@test_throws TypeError (Array{T} where T<:Vararg{Int,2})
# issue #12939
module Issue12939
abstract type Abs; end
struct Foo <: Abs; end
struct Bar; val::Int64; end
struct Baz; val::Int64; end
f(::Type{T}, x::T) where {T} = T(3)
f(::Type{Bar}, x::T) where {T <: Abs} = Bar(2)
f(::Type{Bar}, x) = Bar(1)
f(::Type{Baz}, x) = Baz(1)
f(::Type{Baz}, x::T) where {T <: Abs} = Baz(2)
end
@test Issue12939.f(Issue12939.Baz,Issue12939.Foo()) === Issue12939.Baz(2)
@test Issue12939.f(Issue12939.Bar,Issue12939.Foo()) === Issue12939.Bar(2)
# issue #11840
TT11840{T} = Tuple{T,T}
f11840(::Type) = "Type"
f11840(::DataType) = "DataType"
f11840(::UnionAll) = "UnionAll"
f11840(::Type{T}) where {T<:Tuple} = "Tuple"
@test f11840(Type) == "UnionAll"
@test f11840(Type.body) == "DataType"
@test f11840(Union{Int,Int8}) == "Type"
@test f11840(Tuple) == "Tuple"
@test f11840(TT11840) == "Tuple"
g11840(::DataType) = 1
g11840(::Type) = 2
g11840(sig::Type{T}) where {T<:Tuple} = 3
@test g11840(Vector.body) == 1
@test g11840(Vector) == 2
@test g11840(Vector.body) == 1
@test g11840(Vector) == 2
@test g11840(Tuple) == 3
@test g11840(TT11840) == 3
g11840b(::DataType) = 1
g11840b(::Type) = 2
# FIXME (needs a test): how to compute that the guard entry is still required,
# even though Type{Vector} ∩ DataType = Bottom and this method would set
# cache_with_orig = true
g11840b(sig::Type{T}) where {T<:Tuple} = 3
@test g11840b(Vector) == 2
@test g11840b(Vector.body) == 1
@test g11840b(Vector) == 2
@test g11840b(Vector.body) == 1
@test g11840b(Tuple) == 3
@test g11840b(TT11840) == 3
h11840(::DataType) = '1'
h11840(::Type) = '2'
h11840(::UnionAll) = '3'
h11840(::Type{T}) where {T<:Tuple} = '4'
@test h11840(Vector) == '3'
@test h11840(Vector.body) == '1'
@test h11840(Vector) == '3'
@test h11840(Union{Vector, Matrix}) == '2'
@test h11840(Union{Vector.body, Matrix.body}) == '2'
@test h11840(Tuple) == '4'
@test h11840(TT11840) == '4'
# issue #20511
f20511(x::DataType) = 0
f20511(x) = 1
Type{Integer} # cache this
@test f20511(Union{Integer,T} where T <: Unsigned) == 1
# join
@test typejoin(Int8,Int16) === Signed
@test typejoin(Int,AbstractString) === Any
@test typejoin(Array{Float64},BitArray) <: AbstractArray
@test typejoin(Array{Bool},BitArray) <: AbstractArray{Bool}
@test typejoin(Tuple{Int,Int8},Tuple{Int8,Float64}) === Tuple{Signed,Real}
@test Base.typeseq(typejoin(Tuple{String,String},Tuple{DirectIndexString,String},
Tuple{String,DirectIndexString},Tuple{Int,String,Int}),
Tuple{Any,AbstractString,Vararg{Int}})
@test Base.typeseq(typejoin(Tuple{Int8,Vararg{Int}},Tuple{Int8,Int8}),
Tuple{Int8,Vararg{Signed}})
@test Base.typeseq(typejoin(Tuple{Int8,Vararg{Int}},Tuple{Int8,Vararg{Int8}}),
Tuple{Int8,Vararg{Signed}})
@test Base.typeseq(typejoin(Tuple{Int8,UInt8,Vararg{Int}},Tuple{Int8,Vararg{Int8}}),
Tuple{Int8,Vararg{Integer}})
@test Base.typeseq(typejoin(Union{Int,AbstractString},Int), Union{Int,AbstractString})
@test Base.typeseq(typejoin(Union{Int,AbstractString},Int8), Any)
# typejoin associativity
abstract type Foo____{K} end
mutable struct Wow____{K,V} <: Foo____{K} end
mutable struct Bar____{K,V} <: Foo____{K} end
let
a = Wow____{Int64, Int64}
b = Wow____{Int64, Float64}
c = Bar____{Int64, Int64}
@test typejoin(typejoin(b,c), a) == typejoin(typejoin(b,a), c) == Foo____{Int64}
end
# typejoin with Vararg{T,N}
@test typejoin(Tuple{Vararg{Int,2}}, Tuple{Int,Int,Int}) === Tuple{Int,Int,Vararg{Int}}
@test typejoin(Tuple{Vararg{Int,2}}, Tuple{Vararg{Int}}) === Tuple{Vararg{Int}}
@test promote_type(Bool,Bottom) === Bool
# ntuples
nttest1(x::NTuple{n,Int}) where {n} = n
@test nttest1(()) == 0
@test nttest1((1,2)) == 2
@test NTuple <: Tuple
@test (NTuple{T,Int32} where T) <: Tuple{Vararg{Int32}}
@test !((NTuple{T,Int32} where T) <: Tuple{Int32,Vararg{Int32}})
@test Tuple{Vararg{Int32}} <: (NTuple{T,Int32} where T)
@test Tuple{Int32,Vararg{Int32}} <: (NTuple{T,Int32} where T)
# #17198
@test_throws MethodError convert(Tuple{Int}, (1.0, 2.0, 3.0))
# #21238
@test_throws MethodError convert(Tuple{Int,Int,Int}, (1, 2))
# type declarations
abstract type Sup_{A,B} end
abstract type Qux_{T} <: Sup_{Qux_{Int},T} end
@test Qux_{Int}.super <: Sup_
@test ===(Qux_{Int}, Qux_{Int}.super.parameters[1])
@test ===(Qux_{Int}.super.parameters[2], Int)
@test Qux_{Char}.super <: Sup_
@test ===(Qux_{Int}, Qux_{Char}.super.parameters[1])
@test ===(Qux_{Char}.super.parameters[2], Char)
@test Qux_.body.super.parameters[1].super <: Sup_
@test ===(Qux_{Int}, Qux_.body.super.parameters[1].super.parameters[1])
@test ===(Int, Qux_.body.super.parameters[1].super.parameters[2])
mutable struct Foo_{T} x::Foo_{Int} end
@test ===(Foo_.body.types[1], Foo_{Int})
@test ===(Foo_.body.types[1].types[1], Foo_{Int})
mutable struct Circ_{T} x::Circ_{T} end
@test ===(Circ_{Int}, Circ_{Int}.types[1])
abstract type Sup2a_ end
abstract type Sup2b_{A <: Sup2a_, B} <: Sup2a_ end
@test_throws ErrorException @eval abstract type Qux2_{T} <: Sup2b_{Qux2_{Int}, T} end # wrapped in eval to avoid #16793
# issue #21923
struct A21923{T,N}; v::Vector{A21923{T}}; end
@test fieldtype(A21923,1) == Vector{A21923{T}} where T
struct B21923{T,N}; v::Vector{B21923{T,M} where M}; end
@test fieldtype(B21923, 1) == Vector{B21923{T,M} where M} where T
struct C21923{T,N}; v::C21923{T,M} where M; end
@test fieldtype(C21923, 1) == C21923
struct D21923{T,N}; v::D21923{T}; end
@test fieldtype(D21923, 1) == D21923
# issue #22624, more circular definitions
struct T22624{A,B,C}; v::Vector{T22624{Int64,A}}; end
let elT = T22624.body.body.body.types[1].parameters[1]
@test elT == T22624{Int64, T22624.var, C} where C
elT2 = elT.body.types[1].parameters[1]
@test elT2 == T22624{Int64, Int64, C} where C
@test elT2.body.types[1].parameters[1] === elT2
@test isleaftype(elT2.body.types[1])
end
# issue #3890
mutable struct A3890{T1}
x::Matrix{Complex{T1}}
end
@test A3890{Float64}.types[1] === Array{Complex{Float64},2}
# make sure the field type Matrix{Complex{T1}} isn't cached
mutable struct B3890{T2}
x::Matrix{Complex{T2}}
end
@test B3890{Float64}.types[1] === Array{Complex{Float64},2}
# issue #786
mutable struct Node{T}
v::Vector{Node}
end
@test ===(Node{Int}.types[1].parameters[1], Node)
mutable struct Node2{T}
v::Vector{Node2{T}}
end
@test ===(Node2{Int}.types[1].parameters[1], Node2{Int})
mutable struct FooFoo{A,B} y::FooFoo{A} end
@test FooFoo{Int} <: FooFoo{Int,AbstractString}.types[1]
let x = (2,3)
@test +(x...) == 5
end
# conversions
function fooo()
local x::Int8
x = 100
x
end
@test fooo() === convert(Int8,100)
function fooo_2()
local x::Int8
x = 100
end
@test fooo_2() === 100
function fooo_3()
local x::Int8
y = x = 100
@test isa(x,Int8)
y
end
@test fooo_3() === 100
let
function foo()
local x::Int8
function bar()
x = 100
end
bar()
x
end
@test foo() === convert(Int8,100)
end
function bar(x::T) where T
local z::Complex{T}
z = x
z
end
@test bar(3.0) == Complex(3.0,0.0)
z = convert(Complex{Float64},2)
@test z == Complex(2.0,0.0)
function typeassert_instead_of_decl()
local x
x = 1
x::Float64
return 0
end
@test_throws TypeError typeassert_instead_of_decl()
# type declarations on globals not implemented yet
@test_throws ErrorException eval(parse("global x20327::Int"))
y20327 = 1
@test_throws TypeError y20327::Float64
# misc
fib(n) = n < 2 ? n : fib(n-1) + fib(n-2)
@test fib(20) == 6765
# static parameters
sptest1(x::T, y::T) where {T} = 42
sptest1(x::T, y::S) where {T,S} = 43
@test sptest1(1,2) == 42
@test sptest1(1,"b") == 43
sptest2(x::T) where {T} = T
@test ===(sptest2(:a),Symbol)
sptest3(x::T) where {T} = y->T
let m = sptest3(:a)
@test ===(m(0),Symbol)
end
sptest4(x::T, y::T) where {T} = 42
sptest4(x::T, y) where {T} = 44
@test sptest4(1,2) == 42
@test sptest4(1, "cat") == 44
# closures
function clotest()
c = 0
function inc()
c += 1
end
function dec()
c -= 1
end
inc(); inc()
@test c == 2
dec()
@test c == 1
@test (()->c)() == 1
fibb(n) = n < 2 ? n : fibb(n-1)+fibb(n-2)
@test fibb(10) == 55
return (n->(c+=n), ()->c)
end
let T = clotest()
(inc, C) = T
inc(11)
@test C() == 12
end
Yc(f) = (h->f(x->h(h)(x)))(h->f(x->h(h)(x)))
yfib = Yc(fib->(n->(n < 2 ? n : fib(n-1) + fib(n-2))))
@test yfib(20) == 6765
function capt_before_def()
f() = y
y = 2
f
end
@test capt_before_def()() == 2
function i18408()
local i
x->i
end
let f = i18408()
@test_throws UndefRefError f(0)
end
# variable scope, globals
glob_x = 23
function glotest()
global glob_x
glob_x = 24
loc_x = 8
function inner()
global loc_x = 10
glob_x = 88
end
function inner2()
local glob_x # override
global loc_x
glob_x = 2
@test glob_x == 2
@test loc_x == 10
end
inner()
inner2()
@test glob_x == 88
@test loc_x == 8
end
glotest()
@test glob_x == 88
@test loc_x == 10
# issue #7234
f7234_cnt = 0
begin
glob_x2 = 24
function f7234_a()
global f7234_cnt += 1
glob_x2 += 1
global f7234_cnt += -10000
end
end
@test_throws UndefVarError f7234_a()
@test f7234_cnt == 1
begin
global glob_x2 = 24
function f7234_b()
global f7234_cnt += 1
glob_x2 += 1
global f7234_cnt += -10000
end
end
@test_throws UndefVarError f7234_b()
@test f7234_cnt == 2
# existing globals can be inherited by non-function blocks
for i = 1:2
glob_x2 += 1
end
@test glob_x2 == 26
# globals declared as such in a non-global scope are inherited
let
global glob_x3 = 11
f7234_2() = (glob_x3 += 1)
f7234_2()
end
@test glob_x3 == 12
# interaction between local variable renaming and nested globals (#19333)
x19333 = 1
function f19333(x19333)
return let x19333 = x19333
g19333() = (global x19333 += 2)
g19333() + (x19333 += 1)
end + (x19333 += 1)
end
@test f19333(0) == 5
@test f19333(0) == 7
@test x19333 == 5
function h19333()
s = 0
for (i, j) in ((1, 2),)
s += i + j # use + as a global
end
for (k, +) in ((3, 4),)
s -= (k - +) # use + as a local
end
return s
end
@test h19333() == 4
# let - new variables, including undefinedness
let_undef_cnt = 0
function let_undef()
first = true
for i = 1:2
let x # new x
if first # not defined on second pass
x = 1
first = false
end
global let_undef_cnt += 1
x + 1
global let_undef_cnt += 23
end
end
end
@test_throws UndefVarError let_undef()
@test let_undef_cnt == 25
# const implies local in a local scope block
function const_implies_local()
let
x = 1
local y
let
const x = 0
y = x
end
x, y
end
end
@test const_implies_local() === (1, 0)
a = Vector{Any}(3)
for i=1:3
let ii = i
a[i] = x->x+ii
end
end
@test a[1](10) == 11
@test a[2](10) == 12
@test a[3](10) == 13
# issue #22032
let a = [], fs = []
for f() in 1:3
push!(a, f())
push!(fs, f)
end
@test a == [1,2,3]
@test [f() for f in fs] == [1,2,3]
end
let t = (22,33)
(g(), x) = t
@test g() == 22
@test x == 33
end
# issue #23091
let (f(), x) = (1, 2)
@test f() == 1
@test x == 2
end
# issue #21900
f21900_cnt = 0
function f21900()
for i = 1:1
x = 0
end
global f21900_cnt += 1
x
global f21900_cnt += -1000
nothing
end
@test_throws UndefVarError f21900()
@test f21900_cnt == 1
@test_throws UndefVarError @eval begin
for i21900 = 1:10
for j21900 = 1:10
foo21900 = 10
end
bar21900 = 0
bar21900 = foo21900 + 1
end
end
@test !@isdefined(foo21900)
@test !@isdefined(bar21900)
bar21900 = 0
@test_throws UndefVarError @eval begin
for i21900 = 1:10
for j21900 = 1:10
foo21900 = 10
end
bar21900 = -1
bar21900 = foo21900 + 1
end
end
@test bar21900 == -1
@test !@isdefined foo21900
foo21900 = 0
@test nothing === @eval begin
for i21900 = 1:10
for j21900 = 1:10
foo21900 = 10
end
bar21900 = -1
bar21900 = foo21900 + 1
end
end
@test foo21900 == 10
@test bar21900 == 11
# ? syntax
@test (true ? 1 : false ? 2 : 3) == 1
# issue #7252
let
local a
1 > 0 ? a=2 : a=3
@test a == 2
1 < 0 ? a=2 : a=3
@test a == 3
end
# tricky space sensitive syntax cases
@test [-1 ~1] == [(-1) (~1)]
# undefinedness
mutable struct UndefField
field
UndefField() = new()
end
let
local a
a = UndefField()
@test !isdefined(a, :field)
@test !isdefined(a, :foo)
@test !isdefined(2, :a)
@test_throws TypeError isdefined(2)
end
let
local a
a = Vector{Any}(2)
@test !isassigned(a,1) && !isassigned(a,2)
a[1] = 1
@test isassigned(a,1) && !isassigned(a,2)
a = Array{Float64}(1)
@test isassigned(a,1)
@test isassigned(a)
@test !isassigned(a,2)
end
# isassigned, issue #11167
mutable struct Type11167{T,N} end
Type11167{Int,2}
let tname = Type11167.body.body.name
@test !isassigned(tname.cache, 0)
@test isassigned(tname.cache, 1)
@test !isassigned(tname.cache, 2)
Type11167{Float32,5}
@test isassigned(tname.cache, 2)
@test !isassigned(tname.cache, 3)
end
# dispatch
let
local foo, foo2, fooN, bar, baz
foo(x::Tuple{Vararg{Any}}) = 0
foo(x::Tuple{Vararg{Integer}}) = 1
@test foo((:a,)) == 0
@test foo(( 2,)) == 1
foo2(x::Vararg{Any,2}) = 2
@test foo2(1,2) == 2
@test_throws MethodError foo2(1)
@test_throws MethodError foo2(1,2,3)
fooN(A::Array{T,N}, x::Vararg{Any,N}) where {T,N} = -1
@test fooN([1,2], 1) == -1
@test_throws MethodError fooN([1,2], 1, 2) == -1
@test fooN([1 2; 3 4], 1, 2) == -1
@test_throws MethodError fooN([1 2; 3 4], 1)
@test_throws MethodError fooN([1 2; 3 4], 1, 2, 3)
bar(x::Tuple{T,T,T,T}) where {T} = 1
bar(x::Tuple{Any,Any,Any,Any})=2
@test bar((1,1,1,1)) == 1
@test bar((1,1,1,"a")) == 2
@test bar((:a,:a,:a,:a)) == 1
baz(::Type{Rational}) = 1
baz(::Type{Rational{T}}) where {T} = 2
@test baz(Rational) == 1
@test baz(Rational{Int}) == 2
end
let
local mytype
function mytype(vec)
convert(Vector{Tuple{String, DataType}}, vec)
end
some_data = Any[("a", Int32), ("b", Int32)]
@test isa(mytype(some_data),Vector{Tuple{String, DataType}})
end
mutable struct MyArray{N} <: AbstractArray{Int, N}
end
let
local x
x = MyArray{1}()
foob(x::AbstractArray) = 0
foob(x::AbstractVector{T}) where {T} = 1
@test foob(x) == 1
end
let
local f, g, a
f(a::Vector{Vector{T}}) where {T} = a
g(a::Vector{Vector{T}}) where {T} = a
a = Vector{Int}[]
@test ===(f(a), a)
@test ===(g(a), a)
end
mutable struct _AA{T}; a::T; end
_AoA{T} = _AA{_AA{T}}
let
local g, a
g(a::_AA{_AA{T}}) where {T} = a
a = _AA(_AA(1))
@test ===(g(a),a)
end
# dispatch using Val{T}. See discussion in #9452, #22475 for instances vs types
let
local firstlast
firstlast(::Val{true}) = "First"
firstlast(::Val{false}) = "Last"
@test firstlast(Val(true)) == "First"
@test firstlast(Val(false)) == "Last"
end
# x::Vararg{Any} declarations
let
local f1, f2, f3
f1(x...) = [x...]
f2(x::Vararg{Any}) = [x...]
f3(x::Vararg) = [x...]
@test f1(1,2,3) == [1,2,3]
@test f2(1,2,3) == [1,2,3]
@test f3(1,2,3) == [1,2,3]
end
# try/finally
begin
after = 0
b = try
1+2
finally
after = 1
end
@test b == 3
@test after == 1
after = 0
gothere = 0
try
try
error(" ")
finally
after = 1
end
gothere = 1
end
@test after == 1
@test gothere == 0
after = 0
b = try
error(" ")
catch
42
finally
after = 1
end
@test b == 42
@test after == 1
glo = 0
function retfinally()
try
return 5
finally
global glo = 18
end
end
@test retfinally() == 5
@test glo == 18
@test try error() end === nothing
end
# finalizers
let A = [1]
local x = 0
finalizer(A, a->(x+=1))
finalize(A)
@test x == 1
A = 0
gc(); gc()
@test x == 1
end
# Module() constructor
@test names(Module(:anonymous), true, true) == [:anonymous]
@test names(Module(:anonymous, false), true, true) == [:anonymous]
# exception from __init__()
let didthrow =
try
include_string(
@__MODULE__,
"""
module TestInitError
__init__() = error()
end
""")
false
catch ex
@test isa(ex, LoadError)
@test isa(ex.error, InitError)
true
end
@test didthrow
end
# issue #7307
function test7307(a, ret)
try
try
ret && return a
finally
push!(a, "inner")
end
finally
push!(a, "outer")
end
return a
end
@test test7307([], true) == ["inner","outer"]
@test test7307([], false) == ["inner","outer"]
# issue #8277
function test8277(a)
i = 0
for j=1:2
try
if i == 0
push!(a,0)
end
i += 1
error()
catch
end
end
end
let a = []
test8277(a)
@test length(a) == 1
end
# chained and multiple assignment behavior (issue #2913)
let
local x, a, b, c, d, e
x = (a,b,b,b,e) = (1,2,3,4,5)
@test x === (1,2,3,4,5)
@test a == 1
@test b == 4
@test e == 5
x = (a,b,b,e) = (1,2,3,4,5)
@test x === (1,2,3,4,5)
@test a == 1
@test b == 3
@test e == 4
a = complex(1,2)
b = 3
b, a = a.re, b
@test b == 1
@test a == 3
a = complex(1,2)
b = 3
a, b = b, a.re
@test a == 3
@test b == 1
end
# accessing fields by index
let
local z = complex(3, 4)
v = Int[0,0]
for i=1:2
v[i] = getfield(z, i)
end
@test v == [3,4]
@test_throws BoundsError getfield(z, -1)
@test_throws BoundsError getfield(z, 0)
@test_throws BoundsError getfield(z, 3)
strct = LoadError("", 0, "")
setfield!(strct, 2, 8)
@test strct.line == 8
setfield!(strct, 3, "hi")
@test strct.error == "hi"
setfield!(strct, 1, "yo")
@test strct.file == "yo"
@test_throws BoundsError getfield(strct, 10)
@test_throws BoundsError setfield!(strct, 0, "")
@test_throws BoundsError setfield!(strct, 4, "")
end
# allow typevar in Union to match as long as the arguments contain
# sufficient information
# issue #814
let
local MatOrNot, my_func, M
MatOrNot{T} = Union{AbstractMatrix{T}, Vector{Union{}}}
my_func(A::MatOrNot{T}, B::MatOrNot{T}, C::MatOrNot{T}) where {T<:Real} = 0
M = [ 2. 1. ; 1. 1. ]
@test my_func(Union{}[], M, M) == 0
end
let
local my_func, a, c
my_func(P::Vector{T}, Q::Vector{T}) where {T} = 0
my_func(x::T, P::Vector{T}) where {T} = 1
my_func(P::Vector{T}, x::T) where {T} = 2
a = Int[3]
c = Vector[a]
@test my_func(c,c)==0
@test my_func(a,c)==1
end
let
local baar, foor, boor
# issue #1131
baar(x::DataType) = 0
baar(x::Union) = 1
baar(x::UnionAll) = 2
@test baar(StridedArray) == 2
@test baar(Base.unwrap_unionall(StridedArray)) == 1
@test baar(Vector) == 2
@test baar(Vector.body) == 0
boor(x) = 0
boor(x::Union) = 1
@test boor(StridedArray) == 0
@test boor(Base.unwrap_unionall(StridedArray)) == 1
# issue #1202
foor(x::Union) = 1
@test_throws MethodError foor(StridedArray)
@test foor(Base.unwrap_unionall(StridedArray)) == 1
@test_throws MethodError foor(StridedArray)
end
# issue #1153
mutable struct SI{m, s, kg}
value::AbstractFloat
end
import Base.*
*(x::SI{m1, s1, kg1}, y::SI{m2, s2, kg2}) where {m1, m2, s1, s2, kg1, kg2} = SI{m1 + m2, s1 + s2, kg1 + kg2}(x.value * y.value)
let
local a,b
a = SI{0,0,1}(1.0) * SI{1,2,0}(2.0)
b = SI{0,0,1}(1.0) * SI{1,-2,0}(2.0)
@test typeof(a) === SI{1,2,1}
@test typeof(b) === SI{1,-2,1}
end
# pointer arithmetic
let
local a,b,c
a = C_NULL
b = C_NULL + 1
c = C_NULL - 1
d = 1 + C_NULL
@test eltype(a) == Void
@test a != b != c
@test b == d
@test UInt(a) == 0
@test UInt(b) == 1
@test UInt(c) == typemax(UInt)
@test b - a == -(a - b) == 1
@test c - a == -(a - c) == typemax(UInt)
@test c - b == -(b - c) == typemax(UInt) - 1
@test a < b < c
end
# pull request 1270
let
local a,p, a2,p2
a = [11,12,13]
p = pointer(a)
@test unsafe_load(p, 1) == 11
unsafe_store!(p, 99, 2)
@test a == [11,99,13]
a2 = Any[101,102,103]
p2 = pointer(a2)
@test unsafe_load(p2) == 101
unsafe_store!(p2, 909, 3)
@test a2 == [101,102,909]
end
@test unsafe_pointer_to_objref(ccall(:jl_call1, Ptr{Void}, (Any,Any),
x -> x+1, 314158)) == 314159
@test unsafe_pointer_to_objref(pointer_from_objref(e+pi)) == e+pi
let
local a, aa
a = [1,2,3]
aa = unsafe_wrap(Array, pointer(a), length(a))
@test aa == a
aa = unsafe_wrap(Array, pointer(a), (length(a),))
@test aa == a
aa = unsafe_wrap(Array, pointer(a), UInt(length(a)))
@test aa == a
aa = unsafe_wrap(Array, pointer(a), UInt16(length(a)))
@test aa == a
aaa = unsafe_wrap(Array, pointer(a), (1, 1))
@test size(aaa) == (1, 1)
@test aaa[1] == a[1]
@test_throws InexactError unsafe_wrap(Array, pointer(a), -3)
# Misaligned pointer
res = @test_throws ArgumentError unsafe_wrap(Array, pointer(a) + 1, length(a))
@test contains(res.value.msg, "is not properly aligned to $(sizeof(Int)) bytes")
res = @test_throws ArgumentError unsafe_wrap(Array, pointer(a) + 1, (1, 1))
@test contains(res.value.msg, "is not properly aligned to $(sizeof(Int)) bytes")
end
struct FooBar2515
foo::Int
bar::Int
end
let
local X, p
X = FooBar2515[ FooBar2515(3,1), FooBar2515(4,4) ]
p = pointer(X)
@test unsafe_load(p) == FooBar2515(3,1)
@test unsafe_load(p, 2) == FooBar2515(4,4)
unsafe_store!(p, FooBar2515(8,4))
@test X[1] == FooBar2515(8,4)
unsafe_store!(p, FooBar2515(7,3), 1)
@test X[1] == FooBar2515(7,3)
end
# issue #1287, combinations of try, catch, return
let