forked from aviatesk/JET.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_jetanalyzer.jl
1154 lines (1039 loc) · 37.9 KB
/
test_jetanalyzer.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
module test_jetanalyzer
include("../setup.jl")
@testset "configurations" begin
@test_throws JET.JETConfigError report_call(function () end; mode=:badmode)
@test_throws JET.JETConfigError report_call(function () end; report_pass=JET.BasicPass(), mode=:sound)
# cache key should be same for the same configurations
let cache1 = JET.AnalysisCache(JETAnalyzer()),
cache2 = JET.AnalysisCache(JETAnalyzer())
@test cache1 == cache2
end
# cache key should be different for different configurations
let analyzer1 = JETAnalyzer(; max_methods=3),
analyzer2 = JETAnalyzer(; max_methods=4)
cache1 = JET.AnalysisCache(analyzer1)
cache2 = JET.AnalysisCache(analyzer2)
@test cache1 ≠ cache2
end
# configurations other than `InferenceParams` and `ReportPass`
# shouldn't affect the cache key identity
let analyzer1 = JETAnalyzer(; toplevel_logger=nothing),
analyzer2 = JETAnalyzer(; toplevel_logger=IOBuffer())
cache1 = JET.AnalysisCache(analyzer1)
cache2 = JET.AnalysisCache(analyzer2)
@test cache1 == cache2
end
# cache key should be different for different report passes
let analyzer1 = JETAnalyzer(; report_pass=JET.BasicPass()),
analyzer2 = JETAnalyzer(; report_pass=JET.SoundPass())
cache1 = JET.AnalysisCache(analyzer1)
cache2 = JET.AnalysisCache(analyzer2)
@test cache1 ≠ cache2
end
# end to end test
let m = Module()
@eval m begin
foo(a::Val{1}) = 1
foo(a::Val{2}) = 2
foo(a::Val{3}) = 3
foo(a::Val{4}) = undefvar
end
# run first analysis and cache
result = @eval m $report_call((Int,); max_methods=3) do a
foo(Val(a))
end
@test isempty(get_reports_with_test(result))
# should use the cached result
result = @eval m $report_call((Int,); max_methods=3) do a
foo(Val(a))
end
@test isempty(get_reports_with_test(result))
# should re-run analysis, and should get a report
result = @eval m $report_call((Int,); max_methods=4) do a
foo(Val(a))
end
@test any(r->is_global_undef_var(r, :undefvar), get_reports_with_test(result))
# should run the cached previous result
result = @eval m $report_call((Int,); max_methods=4) do a
foo(Val(a))
end
@test any(r->is_global_undef_var(r, :undefvar), get_reports_with_test(result))
end
end
@testset "MethodErrorReport" begin
# report no match case
# --------------------
# if there is no method matching case, it should be reported
let m = Module()
result = Core.eval(m, quote
foo(a::Integer) = :Integer
$report_call((AbstractString,)) do a
foo(a)
end
end)
report = only(get_reports_with_test(result))
@test report isa MethodErrorReport
@test report.t === Tuple{typeof(m.foo), AbstractString}
end
let result = report_call(()->sum([]))
report = only(get_reports_with_test(result))
@test report isa SeriousExceptionReport
@test report.err isa MethodError
@test report.err.f === zero
end
# if there is no method matching case in union-split, it should be reported
let m = Module()
result = Core.eval(m, quote
foo(a::Integer) = :Integer
foo(a::AbstractString) = "AbstractString"
$report_call(a->foo(a), (Union{Nothing,Int},))
end)
report = only(get_reports_with_test(result))
@test report isa MethodErrorReport
@test report.t == Any[Tuple{typeof(m.foo), Nothing}]
end
# report uncovered match
# ----------------------
# only in :sound mode
let (basic, sound) = @eval Module() begin
onlyint(::Int) = :ok
basic = $report_call((Any,)) do x
onlyint(x)
end
sound = $report_call((Any,); mode=:sound) do x
onlyint(x)
end
basic, sound
end
@test isempty(get_reports_with_test(basic))
report = only(get_reports_with_test(sound))
@test report isa MethodErrorReport
@test report.uncovered
@test report.sig[end] === Symbol
end
let (basic, sound) = @eval Module() begin
integer_or_nothing(::Integer) = :ok1
integer_or_nothing(::Nothing) = :ok2
basic = $report_call((Union{Number,Nothing},)) do x
integer_or_nothing(x)
end
sound = $report_call((Union{Number,Nothing},); mode=:sound) do x
integer_or_nothing(x)
end
basic, sound
end
@test isempty(get_reports_with_test(basic))
report = only(get_reports_with_test(sound))
@test report isa MethodErrorReport
@test report.uncovered
@test report.sig[end] === Symbol
end
# report both no match error and uncovered match error
let result = @eval Module() begin
onlyint(::Int) = :ok
$report_call((Union{Integer,Nothing},); mode=:sound) do x
onlyint(x)
end
end
@test length(get_reports_with_test(result)) == 2
@test any(get_reports_with_test(result)) do report
# no match for `onlyint(::Nothing)`
report isa MethodErrorReport &&
!report.uncovered &&
report.sig[end] === Union{}
end
@test any(get_reports_with_test(result)) do report
# uncovered match for `onlyint(::Integer)`
report isa MethodErrorReport &&
report.uncovered &&
report.sig[end] === Symbol
end
end
# unanalyzed calls
# only in :sound mode
test_call((Any,Any)) do x, y
x + y
end
let result = report_call((Any,Any); mode=:sound) do x, y
x + y
end
report = only(get_reports_with_test(result))
@test report isa UnanalyzedCallReport
report.type === Tuple{typeof(+),Any,Any}
end
end
issue285(x, y::Vararg{T}) where {T} = T
issue586(t::Vararg{Type{<:T}}) where {T} = T
@testset "UndefVarErrorReport" begin
@testset "global" begin
let result = report_call(()->foo)
r = only(get_reports_with_test(result))
@test is_global_undef_var(r, :foo)
end
# deeper level
let m = @fixturedef begin
foo(bar) = bar + baz
qux(a) = foo(a)
end
result = Core.eval(m, :($report_call(qux, (Int,))))
r = only(get_reports_with_test(result))
@test is_global_undef_var(r, :baz)
# works when cached
result = Core.eval(m, :($report_call(qux, (Int,))))
r = only(get_reports_with_test(result))
@test is_global_undef_var(r, :baz)
end
let result = @eval Module() begin
$report_call() do
getfield(@__MODULE__, :undefvar)
end
end
r = only(get_reports_with_test(result))
@test is_global_undef_var(r, :undefvar)
end
let result = @eval Module() begin
$report_call() do
getglobal(@__MODULE__, :undefvar)
end
end
r = only(get_reports_with_test(result))
@test is_global_undef_var(r, :undefvar)
end
# if a global variable is type-declared, it will likely get assigned somewhere
let res = @analyze_toplevel analyze_from_definitions=true begin
global var::String
function __init__()
global var
var = "init"
end
getvar() = (global var; var)
end
@test isempty(res.res.inference_error_reports)
end
# but the sound mode should still be sound
let res = @analyze_toplevel mode=:sound analyze_from_definitions=true begin
global var::String
function __init__()
global var
var = "init"
end
getvar() = (global var; var)
end
r = only(get_reports_with_test(res))
@test is_global_undef_var(r, :var)
end
end
@static false && @testset "local" begin
let result = report_call((Bool,)) do b
if b
bar = rand(Int)
return bar
end
return bar # undefined in this pass
end
r = only(get_reports_with_test(result))
@test is_local_undef_var(r, :bar)
@test last(r.vst).line == (@__LINE__)-4
end
# deeper level
let m = @fixturedef begin
function foo(b)
if b
bar = rand(Int)
return bar
end
return bar # undefined in this pass
end
baz(a) = foo(a)
end
result = Core.eval(m, :($report_call(baz, (Bool,))))
r = only(get_reports_with_test(result))
@test is_local_undef_var(r, :bar)
@test last(r.vst).line == (@__LINE__)-8
# works when cached
result = Core.eval(m, :($report_call(baz, (Bool,))))
r = only(get_reports_with_test(result))
@test is_local_undef_var(r, :bar)
@test last(r.vst).line == (@__LINE__)-14
end
# try to exclude false negatives as possible (by collecting reports in after-optimization pass)
let result = report_call((Bool,)) do b
if b
bar = rand()
end
return if b
return bar # this shouldn't be reported
else
return nothing
end
end
@test isempty(get_reports_with_test(result))
end
let result = report_call((Bool,)) do b
if b
bar = rand()
end
return if b
return nothing
else
# ideally we want to have report for this pass, but tons of work will be
# needed to report this pass
return bar
end
end
@test_broken length(get_reports_with_test(result)) === 1 &&
is_local_undef_var(get_reports_with_test(result), :bar)
end
# TODO better closure handling
let result = report_call() do
local a
function inner(n)
if n > 0
a = n
end
end
inner(rand(Int))
return a
end
@test_broken isempty(get_reports_with_test(result))
end
let # should work for top-level analysis
res = @analyze_toplevel begin
foo = let
if rand(Bool)
bar = rand(Int)
else
bar # undefined in this pass
end
end
end
let r = only(res.res.inference_error_reports)
@test is_local_undef_var(r, :bar)
end
end
end
@testset "static parameter" begin
@test_call issue285(1, 2)
let result = @report_call issue285(1)
r = only(get_reports_with_test(result))
@test r isa UndefVarErrorReport && r.var isa TypeVar && r.var.name == :T
end
test_call((Vector{Any},)) do xs
issue285(xs...)
end
let result = report_call((Vector{Any},); mode=:sound) do xs
issue285(xs...)
end
@test any(get_reports_with_test(result)) do r
r isa UndefVarErrorReport && r.var isa TypeVar && r.var.name == :T
end
end
@test_call issue586(Int, Int)
let result = @report_call issue586(Int, String)
r = only(get_reports_with_test(result))
@test r isa UndefVarErrorReport && r.var isa TypeVar && r.var.name == :T
end
test_call((Vector{Type},)) do ts
issue586(ts...)
end
let result = report_call((Vector{Type},); mode=:sound) do ts
issue586(ts...)
end
@test any(get_reports_with_test(result)) do r
r isa UndefVarErrorReport && r.var isa TypeVar && r.var.name == :T
end
end
end
end
global __int_globalvar__::Int
let result = report_call((Nothing,)) do x
setglobal!(@__MODULE__, :__int_globalvar__, x)
end
report = only(get_reports_with_test(result))
@test report isa InvalidGlobalAssignmentError
@test report.mod === @__MODULE__
@test report.name === :__int_globalvar__
end
@testset "report non-boolean condition error" begin
# simple case
let result = report_call((Int,)) do a
a ? a : nothing
end
@test length(get_reports_with_test(result)) === 1
er = first(get_reports_with_test(result))
@test er isa NonBooleanCondErrorReport
@test er.t === Int
end
# don't report when a type can be `Bool` (Bool ⊑ type)
let result = report_call((Integer,)) do a
a ? a : nothing
end
@test isempty(get_reports_with_test(result))
end
# report union split case
let result = report_call((Union{Nothing,Bool},)) do a
a ? a : false
end
@test length(get_reports_with_test(result)) === 1
let r = first(get_reports_with_test(result))
@test r isa NonBooleanCondErrorReport
@test r.t == [Nothing]
@test occursin("(1/2 union split)", get_msg(r))
end
end
# sound mode
let result = report_call() do
anyary = Any[1,2,3]
first(anyary) ? first(anyary) : nothing
end
@test isempty(get_reports_with_test(result)) # very untyped, we don't report this by default
end
let result = report_call(; mode=:sound) do
anyary = Any[1,2,3]
first(anyary) ? first(anyary) : nothing
end
@test any(get_reports_with_test(result)) do @nospecialize report # very untyped, the sound mode should report this
report isa NonBooleanCondErrorReport &&
report.uncovered &&
report.t === Any
end
end
end
func_undef_keyword(a; kw) = a, kw # `kw` can be undefined
@testset "UndefKeywordError" begin
result = report_call(func_undef_keyword, (Any,))
@test !isempty(get_reports_with_test(result))
@test any(get_reports_with_test(result)) do r
r isa SeriousExceptionReport || return false
err = r.err
err isa UndefKeywordError && err.var === :kw
end
# there shouldn't be duplicated report for the `throw` call
@test !any(r->isa(r,UncaughtExceptionReport), get_reports_with_test(result))
end
func_invalid_index(xs, i) = xs[i]
func_invalid_index(xs::Vector{Int}) = xs[findfirst(>(0), xs)]
@testset "report invalid index" begin
let result = report_call(func_invalid_index, (Vector{Int},Nothing))
@test count(get_reports_with_test(result)) do r
r isa SeriousExceptionReport
end == 1
end
let result = report_call(func_invalid_index, (Vector{Int},))
@test count(get_reports_with_test(result)) do r
r isa SeriousExceptionReport
end == 1
end
end
@testset "DivideError" begin
let result = report_call() do
div(1, 0)
end
report = only(get_reports_with_test(result))
@test report isa BuiltinErrorReport
@test report.msg == JET.DIVIDE_ERROR_MSG
end
let result = report_call() do
rem(1, 0)
end
report = only(get_reports_with_test(result))
@test report isa BuiltinErrorReport
@test report.msg == JET.DIVIDE_ERROR_MSG
end
# JETAnalyzer isn't sound
let result = report_call((Int,Int)) do a, b
div(a, b)
end
@test isempty(get_reports_with_test(result))
end
end
@testset "report `throw` calls" begin
# simplest case
let
result = report_call(()->throw("foo"))
@test !isempty(get_reports_with_test(result))
@test first(get_reports_with_test(result)) isa UncaughtExceptionReport
end
# throws in deep level
let
foo(a) = throw(a)
result = report_call(()->foo("foo"))
@test !isempty(get_reports_with_test(result))
@test first(get_reports_with_test(result)) isa UncaughtExceptionReport
end
# don't report possibly false negative `throw`s
let
foo(a) = a ≤ 0 ? throw("a is $(a)") : a
result = report_call(foo, (Int,))
@test isempty(get_reports_with_test(result))
end
# constant prop sometimes helps exclude false negatives
let
foo(a) = a ≤ 0 ? throw("a is $(a)") : a
result = report_call(()->foo(0))
@test !isempty(get_reports_with_test(result))
@test first(get_reports_with_test(result)) isa UncaughtExceptionReport
end
# report even if there're other "critical" error exist
let
m = gen_virtual_module()
result = Core.eval(m, quote
foo(a) = sum(a) # should be reported
bar(a) = throw(a) # shouldn't be reported first
$report_call((Bool, String)) do b, s
b && foo(s)
bar(s)
end
end)
@test length(get_reports_with_test(result)) === 3
test_sum_over_string(get_reports_with_test(result))
end
# end to end
let
# this should report `throw(ArgumentError("Sampler for this object is not defined")`
result = report_call(rand, (Char,))
@test !isempty(get_reports_with_test(result))
@test first(get_reports_with_test(result)) isa UncaughtExceptionReport
# this should not report `throw(DomainError(x, "sin(x) is only defined for finite x."))`
result = report_call(sin, (Int,))
@test isempty(get_reports_with_test(result))
# again, constant prop sometimes can exclude false negatives
result = report_call(()->sin(Inf))
@test !isempty(get_reports_with_test(result))
@test first(get_reports_with_test(result)) isa UncaughtExceptionReport
end
end
@testset "keyword argument methods" begin
result = Core.eval(gen_virtual_module(), quote
f(a; b = nothing, c = nothing) = return
$report_call((Any,)) do b
f(1; b)
end
end)
@test isempty(get_reports_with_test(result))
end
@testset "don't early escape if type grows up to `Any`" begin
vmod = gen_virtual_module()
res = @analyze_toplevel context = vmod virtualize = false begin
abstract type Foo end
struct Foo1 <: Foo
bar
end
struct Foo2 <: Foo
baz # typo
end
struct Foo3 <: Foo
qux # typo
end
bar(foo::Foo) = foo.bar
f = rand(Bool) ? Foo1(1) : rand(Bool) ? Foo2(1) : Foo3(1)
bar(f)
end
# `bar(::Foo1)` is valid but its return type is `Any`, and so we can't collect possible
# error points for `bar(::Foo2)` and `bar(::Foo3)` if we bail out on `Any`-grew return type
@test length(res.res.inference_error_reports) === 2
@test all(res.res.inference_error_reports) do report
report isa BuiltinErrorReport && report.f === getfield
end
end
abstract_invoke1(i::Integer) = throw(string(i))
#== LINE SENSITIVITY START ===#
const _ABSTRACT_INVOKE2_LINE = (@__LINE__) + 2
const ABSTRACT_INVOKE2_LINE = (@__LINE__) + 4
_abstract_invoke2(a) = return a + undefvar
function abstract_invoke2(a)
a += 1
return @invoke _abstract_invoke2(a::Int) # `invoke` is valid, but error should happen within `bar`
end
#== LINE SENSITIVITY END ===#
@testset "abstract_invoke" begin
# non-`Type` `argtypes`
result = report_call() do
invoke(sin, :this_should_be_type, 1.0)
end
@test length(get_reports_with_test(result)) == 1
r = first(get_reports_with_test(result))
@test isa(r, InvalidInvokeErrorReport)
# invalid `argtype`
result = report_call() do
@invoke sin(1.0::Int)
end
@test length(get_reports_with_test(result)) == 1
r = first(get_reports_with_test(result))
@test isa(r, InvalidInvokeErrorReport)
# don't report errors collected in `invoke`d functions
result = report_call((Int,)) do a
@invoke abstract_invoke1(a::Integer)
end
@test !isempty(get_reports_with_test(result))
@test !any(r->isa(r, InvalidInvokeErrorReport), get_reports_with_test(result))
result = report_call(abstract_invoke2, (Any,))
#== LINE SENSITIVITY END ===#
@test !isempty(get_reports_with_test(result))
@test !any(r->isa(r, InvalidInvokeErrorReport), get_reports_with_test(result))
# virtual stack trace should include frames of both `bar` and `baz`
# we already `@assert` it within `typeinf` when `JET_DEV_MODE` is enabled,
# but test it explicitly here just to make sure it's working
@test all(get_reports_with_test(result)) do r
any(r.vst) do vf
vf.file === Symbol(@__FILE__) &&
vf.line == _ABSTRACT_INVOKE2_LINE && # `_abstract_invoke2`
vf.linfo.def.name === :_abstract_invoke2
end || return false
any(r.vst) do vf
vf.file === Symbol(@__FILE__) &&
vf.line == ABSTRACT_INVOKE2_LINE && # `abstract_invoke2`
vf.linfo.def.name === :abstract_invoke2
end || return false
return true
end
end
@generated function staged_func(a)
if a <: Integer
return :(a)
elseif a <: Number
return :(undefvar) # report me this case
end
throw("invalid argument")
end
call_staged_func(args...) = staged_func(args...)
@testset "staged programming" begin
let # successful code generation, valid code
result = report_call(staged_func, (Int,))
@test isempty(get_reports_with_test(result))
end
let # successful code generation, invalid code
result = report_call(staged_func, (Float64,))
@test length(get_reports_with_test(result)) == 1
r = only(get_reports_with_test(result))
@test is_global_undef_var(r, :undefvar)
end
let # unsuccessful code generation
result = report_call(staged_func, (String,))
@test length(get_reports_with_test(result)) == 1
r = only(get_reports_with_test(result))
@test isa(r, GeneratorErrorReport) && r.err == "invalid argument"
end
let # should work if cached
result = report_call(call_staged_func, (String,))
@test length(get_reports_with_test(result)) == 1
r = first(get_reports_with_test(result))
@test isa(r, GeneratorErrorReport) && r.err == "invalid argument"
end
end
struct InvalidBuiltinStruct; v; end
access_field(x::InvalidBuiltinStruct, sym) = getfield(x, sym)
@testset "report invalid builtin call" begin
let result = report_call((Int, Type{Int}, Any)) do a, b, c
isa(a, b, c)
end
report = only(get_reports_with_test(result))
@test report isa BuiltinErrorReport && report.f === isa
end
@testset "constant propagation" begin
let result = report_call(x::InvalidBuiltinStruct->access_field(x,:v))
@test isempty(get_reports_with_test(result))
end
let result = report_call(x::InvalidBuiltinStruct->access_field(x,:w))
report = only(get_reports_with_test(result))
@test report isa BuiltinErrorReport && report.f === getfield
end
let result = report_call(x::InvalidBuiltinStruct->access_field(x,:v))
@test isempty(get_reports_with_test(result))
end
end
end
function test_builtinerror_compatibility(@nospecialize(reproducer), result)
buf = IOBuffer()
try
reproducer()
@assert false "reproducer doesn't produce a target error"
catch err
Base.showerror(buf, err)
end
expected = String(take!(buf))
print(buf, result)
got = String(take!(buf))
@test occursin(expected, got)
end
mutable struct SingleField
x
end
@testset "no field error report" begin
let result = report_call() do
x = SingleField("foo")
getfield(x, :y)
end
@test only(get_reports_with_test(result)) isa BuiltinErrorReport
test_builtinerror_compatibility(result) do
x = SingleField("foo")
getfield(x, :y)
end
end
let result = report_call() do
x = SingleField("foo")
getfield(x, 2)
end
@test only(get_reports_with_test(result)) isa BuiltinErrorReport
test_builtinerror_compatibility(result) do
x = SingleField("foo")
getfield(x, 2)
end
end
let result = report_call() do
fieldtype(SingleField, :y)
end
@test only(get_reports_with_test(result)) isa BuiltinErrorReport
test_builtinerror_compatibility(result) do
fieldtype(SingleField, :y)
end
end
let result = report_call() do
fieldtype(SingleField, 2)
end
@test only(get_reports_with_test(result)) isa BuiltinErrorReport
# XXX Julia doesn't render this error nicely
# test_builtinerror_compatibility(result) do
# fieldtype(SingleField, 2)
# end
end
let result = report_call() do
x = SingleField("foo")
setfield!(x, :y, "bar")
end
@test only(get_reports_with_test(result)) isa BuiltinErrorReport
test_builtinerror_compatibility(result) do
x = SingleField("foo")
setfield!(x, :y, "bar")
end
end
let result = report_call() do
x = SingleField("foo")
setfield!(x, :y, 2)
end
@test only(get_reports_with_test(result)) isa BuiltinErrorReport
test_builtinerror_compatibility(result) do
x = SingleField("foo")
setfield!(x, :y, 2)
end
end
let result = report_call() do
x = SingleField("foo")
x.y
end
@test only(get_reports_with_test(result)) isa BuiltinErrorReport
test_builtinerror_compatibility(result) do
x = SingleField("foo")
x.y
end
end
let result = report_call() do
x = SingleField("foo")
x.y = "bar"
end
@test only(get_reports_with_test(result)) isa BuiltinErrorReport
test_builtinerror_compatibility(result) do
x = SingleField("foo")
x.y = "bar"
end
end
end
@testset "getfield analysis" begin
# analysis on malformed `getfield` shouldn't error
let result = report_call((Any,)) do a
getfield(a)
end
report = only(get_reports_with_test(result))
@test report isa BuiltinErrorReport && report.f === getfield
end
let result = report_call() do
getfield((1,2,3), :x)
end
report = only(get_reports_with_test(result))
@test report isa BuiltinErrorReport && report.f === getfield
test_builtinerror_compatibility(result) do
getfield((1,2,3), :x)
end
end
let result = report_call() do
getfield(@__MODULE__, 42)
end
report = only(get_reports_with_test(result))
@test report isa BuiltinErrorReport && report.f === getglobal
# XXX Julia raises `BoundsError` when ran in the compiler
# test_builtinerror_compatibility(result) do
# getfield(@__MODULE__, 42)
# end
end
end
@testset "setfield! analysis" begin
# analysis on malformed `setfield!` shouldn't error
let result = report_call((Any,)) do a
setfield!(a)
end
report = only(get_reports_with_test(result))
@test report isa BuiltinErrorReport && report.f === setfield!
end
# `setfield!` to a module raises an error
let result = report_call() do
setfield!(@__MODULE__, :___xxx___, 42)
end
report = only(get_reports_with_test(result))
@test report isa BuiltinErrorReport && report.f === setfield!
test_builtinerror_compatibility(result) do
setfield!(@__MODULE__, :___xxx___, 42)
end
end
# mutability check
let result = report_call((String,)) do s
x = Some("julia")
x.value = s
end
report = only(get_reports_with_test(result))
@test report isa BuiltinErrorReport && report.f === setfield!
end
end
@testset "special case `return_type`" begin
# don't report invalid method calls simulated in `return_type_tfunc`
let
result = report_call(()->CC.return_type(sum, Tuple{String}))
@test isempty(get_reports_with_test(result))
end
# report invalid call of `return_type` itself
let
result = report_call(()->CC.return_type(sum))
@test length(get_reports_with_test(result)) == 1
@test isa(first(get_reports_with_test(result)), InvalidReturnTypeCall)
end
# end to end
let # this shouldn't report "no matching method found for call signature: Base.iterate(itr::DataType)",
# which otherwise will be caught in `abstract_call` in `return_type_tfunc`
result = report_call(() -> Dict('a' => 1, :b => 2))
@test isempty(get_reports_with_test(result))
end
end
@testset "configured_reports" begin
M = Module()
@eval M begin
function foo(a)
r1 = sum(a) # => Base: MethodError(+(::Char, ::Char)), MethodError(zero(::Type{Any}))
r2 = undefsum(a) # => @__MODULE__: UndefVarError(:undefsum)
return r1, r2
end
end
let result = @report_call M.foo("julia")
test_sum_over_string(result)
@test any(r->is_global_undef_var(r, :undefsum), get_reports_with_test(result))
end
let result = @report_call target_modules=(M,) M.foo("julia")
r = only(get_reports_with_test(result))
@test is_global_undef_var(r, :undefsum)
end
let result = @report_call target_modules=(AnyFrameModule(M),) M.foo("julia")
test_sum_over_string(result)
@test any(r->is_global_undef_var(r, :undefsum), get_reports_with_test(result))
end
let result = @report_call ignored_modules=(Base,) M.foo("julia")
r = only(get_reports_with_test(result))
@test is_global_undef_var(r, :undefsum)
end
let result = @report_call ignored_modules=(M,) M.foo("julia")
test_sum_over_string(result)
@test !any(r->is_global_undef_var(r, :undefsum), get_reports_with_test(result))
end
end
issue363(f, args...) = f(args...)
@testset "BasicPass" begin
@testset "basic_filter" begin
# skip errors on abstract dispatch
# https://github.com/aviatesk/JET.jl/issues/154
let res = @analyze_toplevel analyze_from_definitions=true begin
struct Foo
x::AbstractVector{<:AbstractString}
end
end
@test isempty(res.res.inference_error_reports)
end
# https://github.com/aviatesk/JET.jl/issues/363
let res = report_call() do
issue363(sin, "42")
end
@test only(get_reports_with_test(res)) isa MethodErrorReport
end
# should still report anything within entry frame
let res = @eval Module() begin
foo(a::Int) = "hello"
$report_call((AbstractString,)) do a # this abstract call isn't concrete dispatch
foo(a)
end
end
@test !isempty(get_reports_with_test(res))
@test any(r->isa(r,MethodErrorReport), get_reports_with_test(res))
end
end
end
@testset "SoundPass" begin
let # `:basicfilter`
m = Module()
# `==(::Missing, ::Any) -> Missing` will be reported
@eval m foo(a, b) = a == b ? 0 : 1
result = @eval m $report_call((Any,Symbol)) do a, b
foo(a, b)
end
@test isempty(get_reports_with_test(result))
result = report_call((Any,Symbol); mode = :sound) do a, b
a == b ? 0 : 1
end
@test any(get_reports_with_test(result)) do r
isa(r, NonBooleanCondErrorReport) &&
r.t == Type[Missing]
end
end
let # `!(t ⊑ Bool)` -> error
basic = report_call((Integer,)) do cond
cond ? 0 : 1
end
@test isempty(get_reports_with_test(basic))
sound = report_call((Integer,); mode=:sound) do cond
cond ? 0 : 1
end
@test any(get_reports_with_test(sound)) do r
isa(r, NonBooleanCondErrorReport) &&
r.t == Integer
end
end