Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static compilation doesn't use __init__ #18115

Closed
omus opened this issue Aug 18, 2016 · 6 comments
Closed

Static compilation doesn't use __init__ #18115

omus opened this issue Aug 18, 2016 · 6 comments
Assignees
Labels
docs This change adds or pertains to documentation
Milestone

Comments

@omus
Copy link
Member

omus commented Aug 18, 2016

It appears that static compilation does not call the __init__. Take the two modules below as an example:

module StaticFail

export @process

# global ENABLED = true  # static compilation works if I uncomment out this line
function __init__()
    global ENABLED = true
end

macro process(expr)
    ENABLED && return esc(expr)
    error("ENABLED is not set")
end

end
module StaticFailDemo

using StaticFail
export demo
demo() = @process 1 + 1

end

Calling demo() from within the REPL works as expected with and without pre-compilation but static compilation produces the following exception:

...
LoadError("sysimg.jl",381,LoadError("~/Development/Julia/0.5/base/userimg.jl",1,LoadError("~/.julia/v0.5/StaticFailDemo/src/StaticFailDemo.jl",5,UndefVarError(:ENABLED))))
ERROR: LoadError: failed process: Process(`~/Development/Julia/0.5/usr/bin/julia -C native --output-ji /tmp/sys.ji --output-o /tmp/sys.o -J /tmp/inference.ji --startup-file=no sysimg.jl`, ProcessExited(1)) [1]
@JeffBezanson
Copy link
Member

This is intentional; when code is only being compiled we don't consider it to be at run time. The reasoning was that __init__ is often used by a library to discover something about its run time environment on startup, and we didn't want compiled libraries to bake-in information about the compile time environment instead.

It's also worth nothing that the same error would happen if you used @process within the StaticFail module, since macros run before the module is closed and so before __init__ runs. So having macros depend on __init__ is a bit dodgy in general.

@vtjnash
Copy link
Member

vtjnash commented Aug 18, 2016

Yes. I also confirmed this works with incremental compilation, as intended.

@vtjnash vtjnash closed this as completed Aug 18, 2016
@omus
Copy link
Member Author

omus commented Aug 18, 2016

For some reason I was under the impression that __init__ would run at compile time as well as run time. The global I defined should actually be defined at the top level. Thanks for clearing things up.

@StefanKarpinski
Copy link
Member

Should this be documented somewhere?

@vtjnash
Copy link
Member

vtjnash commented Aug 18, 2016

yes, it does seem like http://docs.julialang.org/en/latest/manual/modules/#module-initialization-and-precompilation may lead someone to expect otherwise

@StefanKarpinski StefanKarpinski added the docs This change adds or pertains to documentation label Aug 18, 2016
@StefanKarpinski StefanKarpinski added this to the 0.5.x milestone Aug 18, 2016
@StefanKarpinski
Copy link
Member

Reopened as a documentation issue.

vtjnash added a commit that referenced this issue Aug 22, 2016
vtjnash added a commit that referenced this issue Aug 23, 2016
vtjnash added a commit that referenced this issue Aug 23, 2016
vtjnash added a commit that referenced this issue Aug 24, 2016
vtjnash added a commit that referenced this issue Aug 24, 2016
tkelman pushed a commit that referenced this issue Aug 29, 2016
vtjnash added a commit that referenced this issue Aug 30, 2016
mfasi pushed a commit to mfasi/julia that referenced this issue Sep 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation
Projects
None yet
Development

No branches or pull requests

4 participants