forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.jl
455 lines (402 loc) · 15.3 KB
/
compile.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
# This file is a part of Julia. License is MIT: https://julialang.org/license
using Base.Test
function redirected_stderr(expected)
rd, wr = redirect_stderr()
t = @async begin
read = readstring(rd) # also makes sure the kernel isn't being forced to buffer the output
if !contains(read, expected)
@show expected
@show read
@test false
end
nothing
end
return t
end
Foo_module = :Foo4b3a94a1a081a8cb
FooBase_module = :FooBase4b3a94a1a081a8cb
@eval module ConflictingBindings
export $Foo_module, $FooBase_module
$Foo_module = 232
$FooBase_module = 9134
end
using ConflictingBindings
# this environment variable would affect some error messages being tested below
# so we disable it for the tests below
withenv( "JULIA_DEBUG_LOADING" => nothing ) do
olderr = STDERR
dir = mktempdir()
dir2 = mktempdir()
insert!(LOAD_PATH, 1, dir)
insert!(Base.LOAD_CACHE_PATH, 1, dir)
try
Foo_file = joinpath(dir, "$Foo_module.jl")
FooBase_file = joinpath(dir, "$FooBase_module.jl")
write(FooBase_file,
"""
__precompile__(true)
module $FooBase_module
end
""")
write(Foo_file,
"""
__precompile__(true)
module $Foo_module
using $FooBase_module
# test that docs get reconnected
@doc "foo function" foo(x) = x + 1
include_dependency("foo.jl")
include_dependency("foo.jl")
module Bar
@doc "bar function" bar(x) = x + 2
include_dependency("bar.jl")
end
# test for creation of some reasonably complicated type
immutable MyType{T} end
const t17809s = Any[
Tuple{
Type{Ptr{MyType{i}}},
Array{Ptr{MyType{MyType{:sym}()}}(0), 0},
Val{Complex{Int}(1, 2)},
Val{3},
Val{nothing}}
for i = 0:25]
# test that types and methods get reconnected correctly
# issue 16529 (adding a method to a type with no instances)
(::Task)(::UInt8, ::UInt16, ::UInt32) = 2
# issue 16471 (capturing references to a kwfunc)
Base.Test.@test_throws ErrorException Core.kwfunc(Base.nothing)
Base.nothing(::UInt8, ::UInt16, ::UInt32; x = 52) = x
const nothingkw = Core.kwfunc(Base.nothing)
# issue 16908 (some complicated types and external method definitions)
abstract CategoricalPool{T, R <: Integer, V}
abstract CategoricalValue{T, R <: Integer}
immutable NominalPool{T, R <: Integer, V} <: CategoricalPool{T, R, V}
index::Vector{T}
invindex::Dict{T, R}
order::Vector{R}
ordered::Vector{T}
valindex::Vector{V}
end
immutable NominalValue{T, R <: Integer} <: CategoricalValue{T, R}
level::R
pool::NominalPool{T, R, NominalValue{T, R}}
end
immutable OrdinalValue{T, R <: Integer} <: CategoricalValue{T, R}
level::R
pool::NominalPool{T, R, NominalValue{T, R}}
end
(::Union{Type{NominalValue}, Type{OrdinalValue}})() = 1
(::Union{Type{NominalValue{T}}, Type{OrdinalValue{T}}}){T}() = 2
(::Type{Vector{NominalValue{T, R}}}){T, R}() = 3
(::Type{Vector{NominalValue{T, T}}}){T}() = 4
(::Type{Vector{NominalValue{Int, Int}}})() = 5
#const some_method = @which Base.include("string") // FIXME: support for serializing a direct reference to an external Method not implemented
const some_linfo = @code_typed Base.include("string")
# more tests for method signature involving a complicated type
# issue 18343
immutable Pool18343{R, V}
valindex::Vector{V}
end
immutable Value18343{T, R}
pool::Pool18343{R, Value18343{T, R}}
end
Base.convert{S}(::Type{Nullable{S}}, ::Value18343{Nullable}) = 2
Base.convert(::Type{Nullable{Value18343}}, ::Value18343{Nullable}) = 2
Base.convert{T}(::Type{Ref}, ::Value18343{T}) = 3
infer_rational() = 1//2
Test.@inferred infer_rational()
end
""")
@test_throws ErrorException Core.kwfunc(Base.nothing) # make sure `nothing` didn't have a kwfunc (which would invalidate the attempted test)
# Issue #12623
@test __precompile__(true) === nothing
Base.require(Foo_module)
cachefile = joinpath(dir, "$Foo_module.ji")
# use _require_from_serialized to ensure that the test fails if
# the module doesn't reload from the image:
t = redirected_stderr("WARNING: replacing module Foo4b3a94a1a081a8cb.\nWARNING: Method definition ")
try
@test isa(Base._require_from_serialized(myid(), Foo_module, cachefile, #=broadcast-load=#false), Array{Any,1})
finally
close(STDERR)
redirect_stderr(olderr)
end
wait(t)
let Foo = getfield(Main, Foo_module)
@test Foo.foo(17) == 18
@test Foo.Bar.bar(17) == 19
# issue #12284:
@test stringmime("text/plain", Base.Docs.doc(Foo.foo)) == "foo function\n"
@test stringmime("text/plain", Base.Docs.doc(Foo.Bar.bar)) == "bar function\n"
modules, deps, required_modules = Base.parse_cache_header(cachefile)
@test modules == Dict(Foo_module => Base.module_uuid(Foo))
@test map(x -> x[1], sort(deps)) == [Foo_file, joinpath(dir, "bar.jl"), joinpath(dir, "foo.jl")]
modules, deps1 = Base.cache_dependencies(cachefile)
@test modules == Dict(s => Base.module_uuid(getfield(Foo, s)) for s in
[:Base, :Core, FooBase_module, :Main])
@test deps == deps1
@test current_task()(0x01, 0x4000, 0x30031234) == 2
@test nothing(0x01, 0x4000, 0x30031234) == 52
@test nothing(0x01, 0x4000, 0x30031234; x = 9142) == 9142
@test Foo.nothingkw === Core.kwfunc(Base.nothing)
@test Foo.NominalValue() == 1
@test Foo.OrdinalValue() == 1
@test Foo.NominalValue{Int}() == 2
@test Foo.OrdinalValue{Int}() == 2
let T = Vector{Foo.NominalValue{Int}}
@test isa(T(), T)
end
@test Vector{Foo.NominalValue{Int32, Int64}}() == 3
@test Vector{Foo.NominalValue{UInt, UInt}}() == 4
@test Vector{Foo.NominalValue{Int, Int}}() == 5
@test all(i -> Foo.t17809s[i + 1] ===
Tuple{
Type{Ptr{Foo.MyType{i}}},
Array{Ptr{Foo.MyType{Foo.MyType{:sym}()}}(0), 0},
Val{Complex{Int}(1, 2)},
Val{3},
Val{nothing}},
0:25)
@eval begin
@inferred 1//2 # use @eval to prevent this from being inferred before this point
@inferred $Foo.infer_rational()
end
@test Foo.some_linfo === @code_typed Base.include("string")
PV = Foo.Value18343{Nullable}.types[1]
VR = PV.types[1].parameters[1]
@test PV.types[1] === Array{VR,1}
@test pointer_from_objref(PV.types[1]) ===
pointer_from_objref(PV.types[1].parameters[1].types[1].types[1]) ===
pointer_from_objref(Array{VR,1})
@test PV === PV.types[1].parameters[1].types[1]
@test pointer_from_objref(PV) !== pointer_from_objref(PV.types[1].parameters[1].types[1])
end
Baz_file = joinpath(dir, "Baz.jl")
write(Baz_file,
"""
__precompile__(false)
module Baz
end
""")
t = redirected_stderr("ERROR: LoadError: Declaring __precompile__(false) is not allowed in files that are being precompiled.\n in __precompile__")
try
Base.compilecache("Baz") # from __precompile__(false)
error("__precompile__ disabled test failed")
catch exc
close(STDERR)
redirect_stderr(olderr)
isa(exc, ErrorException) || rethrow(exc)
!isempty(search(exc.msg, "__precompile__(false)")) && rethrow(exc)
end
wait(t)
# Issue #12720
FooBar1_file = joinpath(dir, "FooBar1.jl")
write(FooBar1_file,
"""
__precompile__(true)
module FooBar1
using FooBar
end
""")
sleep(2) # give FooBar and FooBar1 different timestamps, in reverse order too
FooBar_file = joinpath(dir, "FooBar.jl")
write(FooBar_file,
"""
__precompile__(true)
module FooBar
end
""")
Base.compilecache("FooBar")
@test isfile(joinpath(dir, "FooBar.ji"))
@test !Base.stale_cachefile(FooBar_file, joinpath(dir, "FooBar.ji"))
@test !isdefined(Main, :FooBar)
@test !isdefined(Main, :FooBar1)
relFooBar_file = joinpath(dir, "subfolder", "..", "FooBar.jl")
@test Base.stale_cachefile(relFooBar_file, joinpath(dir, "FooBar.ji")) == !is_windows() # `..` is not a symlink on Windows
mkdir(joinpath(dir, "subfolder"))
@test !Base.stale_cachefile(relFooBar_file, joinpath(dir, "FooBar.ji"))
@eval using FooBar
fb_uuid = Base.module_uuid(Main.FooBar)
sleep(2); touch(FooBar_file)
insert!(Base.LOAD_CACHE_PATH, 1, dir2)
@test Base.stale_cachefile(FooBar_file, joinpath(dir, "FooBar.ji"))
@eval using FooBar1
@test !isfile(joinpath(dir2, "FooBar.ji"))
@test !isfile(joinpath(dir, "FooBar1.ji"))
@test isfile(joinpath(dir2, "FooBar1.ji"))
@test Base.stale_cachefile(FooBar_file, joinpath(dir, "FooBar.ji"))
@test !Base.stale_cachefile(FooBar1_file, joinpath(dir2, "FooBar1.ji"))
@test fb_uuid == Base.module_uuid(Main.FooBar)
fb_uuid1 = Base.module_uuid(Main.FooBar1)
@test fb_uuid != fb_uuid1
t = redirected_stderr("WARNING: replacing module FooBar.")
try
reload("FooBar")
finally
close(STDERR)
redirect_stderr(olderr)
end
wait(t)
@test fb_uuid != Base.module_uuid(Main.FooBar)
@test fb_uuid1 == Base.module_uuid(Main.FooBar1)
fb_uuid = Base.module_uuid(Main.FooBar)
@test isfile(joinpath(dir2, "FooBar.ji"))
@test Base.stale_cachefile(FooBar_file, joinpath(dir, "FooBar.ji"))
@test !Base.stale_cachefile(FooBar1_file, joinpath(dir2, "FooBar1.ji"))
@test !Base.stale_cachefile(FooBar_file, joinpath(dir2, "FooBar.ji"))
t = redirected_stderr("WARNING: replacing module FooBar1.")
try
reload("FooBar1")
finally
close(STDERR)
redirect_stderr(olderr)
end
wait(t)
@test fb_uuid == Base.module_uuid(Main.FooBar)
@test fb_uuid1 != Base.module_uuid(Main.FooBar1)
@test isfile(joinpath(dir2, "FooBar.ji"))
@test isfile(joinpath(dir2, "FooBar1.ji"))
@test Base.stale_cachefile(FooBar_file, joinpath(dir, "FooBar.ji"))
@test !Base.stale_cachefile(FooBar_file, joinpath(dir2, "FooBar.ji"))
@test !Base.stale_cachefile(FooBar1_file, joinpath(dir2, "FooBar1.ji"))
# test behavior of precompile modules that throw errors
write(FooBar_file,
"""
__precompile__(true)
module FooBar
error("break me")
end
""")
t = redirected_stderr("ERROR: LoadError: break me\n in error")
try
Base.require(:FooBar)
error("\"LoadError: break me\" test failed")
catch exc
close(STDERR)
redirect_stderr(olderr)
isa(exc, ErrorException) || rethrow(exc)
!isempty(search(exc.msg, "ERROR: LoadError: break me")) && rethrow(exc)
end
wait(t)
# Test transitive dependency for #21266
FooBarT_file = joinpath(dir, "FooBarT.jl")
write(FooBarT_file,
"""
__precompile__(true)
module FooBarT
end
""")
FooBarT1_file = joinpath(dir, "FooBarT1.jl")
write(FooBarT1_file,
"""
__precompile__(true)
module FooBarT1
using FooBarT
end
""")
FooBarT2_file = joinpath(dir, "FooBarT2.jl")
write(FooBarT2_file,
"""
__precompile__(true)
module FooBarT2
using FooBarT1
end
""")
Base.compilecache("FooBarT2")
write(FooBarT1_file,
"""
__precompile__(true)
module FooBarT1
end
""")
rm(FooBarT_file)
@test Base.stale_cachefile(FooBarT2_file, joinpath(dir2, "FooBarT2.ji"))
@test Base.require(:FooBarT2) === nothing
finally
if STDERR != olderr
close(STDERR)
redirect_stderr(olderr)
end
splice!(Base.LOAD_CACHE_PATH, 1:2)
splice!(LOAD_PATH, 1)
rm(dir, recursive=true)
rm(dir2, recursive=true)
end
# test --compilecache=no command line option
let dir = mktempdir(),
Time_module = :Time4b3a94a1a081a8cb
try
write(joinpath(dir, "$Time_module.jl"),
"""
module $Time_module
__precompile__(true)
time = Base.time()
end
""")
eval(quote
insert!(LOAD_PATH, 1, $(dir))
insert!(Base.LOAD_CACHE_PATH, 1, $(dir))
Base.compilecache(:Time4b3a94a1a081a8cb)
end)
exename = `$(Base.julia_cmd()) --precompiled=yes --startup-file=no`
testcode = """
insert!(LOAD_PATH, 1, $(repr(dir)))
insert!(Base.LOAD_CACHE_PATH, 1, $(repr(dir)))
using $Time_module
getfield($Time_module, :time)
"""
t1_yes = readchomp(`$exename --compilecache=yes -E $(testcode)`)
t2_yes = readchomp(`$exename --compilecache=yes -E $(testcode)`)
@test t1_yes == t2_yes
t1_no = readchomp(`$exename --compilecache=no -E $(testcode)`)
t2_no = readchomp(`$exename --compilecache=no -E $(testcode)`)
@test t1_no != t2_no
@test parse(Float64, t1_no) < parse(Float64, t2_no)
finally
splice!(Base.LOAD_CACHE_PATH, 1)
splice!(LOAD_PATH, 1)
rm(dir, recursive=true)
end
end
# test loading a package with conflicting namespace
let dir = mktempdir()
Test_module = :Test6c92f26
try
write(joinpath(dir, "Iterators.jl"),
"""
module Iterators
__precompile__(true)
end
""")
write(joinpath(dir, "$Test_module.jl"),
"""
module $Test_module
__precompile__(true)
using Iterators
end
""")
testcode = """
insert!(LOAD_PATH, 1, $(repr(dir)))
insert!(Base.LOAD_CACHE_PATH, 1, $(repr(dir)))
using $Test_module
"""
exename = `$(Base.julia_cmd()) --startup-file=no`
@test readchomp(`$exename -E $(testcode)`) == "nothing"
@test readchomp(`$exename -E $(testcode)`) == "nothing"
finally
rm(dir, recursive=true)
end
end
let module_name = string("a",randstring())
insert!(LOAD_PATH, 1, pwd())
file_name = string(module_name, ".jl")
sleep(2); touch(file_name)
code = """module $(module_name)\nend\n"""
write(file_name, code)
reload(module_name)
@test typeof(eval(Symbol(module_name))) == Module
deleteat!(LOAD_PATH,1)
rm(file_name)
end
end # !withenv