Skip to content

Precompilation affects function call in @generated function #19942

@maleadt

Description

@maleadt

Running into some #17057 complexity on latest master, where depending on the position in the source file / precompilation is on or off, a generated function selects a different method to call.

Foo/src/Foo.jl:

__precompile__()

module Foo

type Original end

end

Bar/src/Bar.jl:

__precompile__()

module Bar

using Foo

transform{T}(::Type{T}) = T

@generated function test()
    return transform(Foo.Original)
end

immutable Transformed end
transform(::Type{Foo.Original}) = Transformed

end

test.jl:

using Bar
@show Bar.test()

If I run this code as-is, I get the expected (to me) behaviour of the Original type getting transformed to Transformed:

$ JULIA_LOAD_PATH=. JULIA_PKGDIR=. julia test.jl
Bar.test() = Bar.Transformed

However, if I disable precompilation, it matches the first definition of transform:

$ JULIA_LOAD_PATH=. JULIA_PKGDIR=. julia --compilecache=no test.jl
Bar.test() = Foo.Original

This is further exemplified by removing the first transform definition:

$ JULIA_LOAD_PATH=. JULIA_PKGDIR=. julia --compilecache=no test.jl
ERROR: LoadError: MethodError: no method matching transform(::Type{Foo.Original})
The applicable method may be too new: running in world age 20564, while current world is 20566.
Closest candidates are:
  transform(::Type{Foo.Original}) at Bar/src/Bar.jl:14 (method too new to be called from this world context.)
Stacktrace:
 [1] test(...) at Bar/src/Bar.jl:10
 [2] include_from_node1(::String) at ./loading.jl:532
 [3] include(::String) at ./sysimg.jl:14
 [4] process_options(::Base.JLOptions) at ./client.jl:308
 [5] _start() at ./client.jl:374
while loading test.jl, in expression starting on line 229

This error does not show if I run with precompilation enabled. Furthermore, if I move the @generated function test below the additional definition of transform the issue disappears.
Also, on 0.5 everything works 'as expected' (ie. Transformed is returned in all cases, without errors)

Metadata

Metadata

Assignees

Labels

bugIndicates an unexpected problem or unintended behaviortypes and dispatchTypes, subtyping and method dispatch

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions