-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
make const a toplevel expr or error #12010
Comments
I think this is still really useful for some packages that load shared libraries. |
IIRC, the plan in #11456 (or a related one) is that the compiler should look inside the function and see if there's a global const defined inside and reserve a slot for it. (Looking for the specific comment now) Edit: Comment about this is here #11456 (comment) |
#11456 is about making global variable bindings resolved early, this thread is about disallowing the addition of new global const bindings. #11456 (comment) is exactly the subtle but serious issue that can happen if global const is allowed (since ccall will precompile a call to a static pointer, rather than a runtime lookup) |
Interesting. What if we define an EDIT: |
Is this an issue if the pointer is used in the same module and |
@timholy that's just: module MyModule
__init_pre__() = "here1"
__init_post__() = "there2"
__init__() = __init_post__()
__init_pre__()
end @yuyichao less so, but only because we don't run |
Sorry I forgot to mention that this should also be combined with letting typeinf/precompile look into functions (especially |
I think I get it now. Presumably "only" affects functions that get JITted before the cache is written? |
From an inference perspective, I'm guessing that much of the benefit of what we now use |
@carnaval is that something typeinference could handle?
JIT isn't really the right term, since this is AOT compiling. But since this is AOT, it's generally beneficial to do as much work as possible to build a good / complete cache. |
module OfConstType
const binding = Ref{T}()
binding[] = 1
Base.Test.@test binding[] === 1
end |
Sorry, I don't understand your example. |
I guess I wouldn't mind only allowing real const in global scope if we can making |
that seems to be Jeff's plan #11456 (comment) |
|
Would still be nice if accessing them doesn't require adding |
Got it. Yes, that's a workable strategy. |
I'm still hoping that const will some day be respected in local scope,
please don't make that an error (though I understand it might be considered
a different feature than const at the top level).
|
Hmm, even for typed global, it still won't cover this __init__() = global const lib_path = find_library(...)
ccall((:sym, lib_path), ...) |
This doesn't seem like a high priority given the lack of comment since July 2015. |
Might also be helped by #18933 ? |
Resolved: we should do this. |
Jameson was right in JuliaLang/julia#12010, this can lead to subtle bugs. Fixes #49. Came to this because in julia 0.7 this is no longer allowed.
const
can cause serious, subtle bugs when combined with precompilation. it is valid to use from the:toplevel
scope, but it can cause problems if used at runtime (and it's already ignored when specified at the local scope. this could be fixed by warning for and ignoring the const declaration outside of the toplevel scope. or this could be fixed by internally distinguishing betweenconst
(as it is currently defined) and a newassigned once
attribute (that provides none of the inference speed benefits of const, but just doesn't allow reassignment).the following is fine:
the following can confuse precompilation (and isn't great for type inference either):
the following currently is ignored by parsing, so is neither bad nor good:
The text was updated successfully, but these errors were encountered: