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
Remove runtime access to {.compileTime.} variable #338
Comments
I agree with this. Very related (and compatible with this RFC) is this proposal: #283 which makes globals variables (eg {.threadvar.} and {.global.}) at module level work at CT, just like {.threadvar.} and {.global.} at proc scope already do, without cross-over between RT and CT. It simplifies many use cases and makes code work at CT just like it does at RT. There is no cross-over between RT and CT (you need const or static for that), which would cause all sorts of issues. furthermore, code like this: proc main =
echo getCurrentCompilerExe() should generate a warning, and eventually an error when used in a RT context. Instead, what should be used is: |
As I said from the very beginning. |
I started to use Is there any use case were if no, then (I am asking, not confirming, I am neutral, but in favor of whatever is less bug prone) |
proc foo(arg: Foo): Foo {.compileTime.} =
# a function that can only be called at compile time. Trying to generate a call to this procedure at runtime generates an error
discard
var a: Foo # a global variable that can be modified from procedures tagged with {.compileTime.}. Currently it does something wonkey if you try to access it at runtime. This RFC wants to make it emit an error again to work like the {.compileTime.} pragma on procs, like it used to. |
Makes sense, I am wrong, should be Fixed, if that includes reverting it back so be it. This is a Bug more than rfc. |
Did anybody read the spec and the reason behind this feature? Oh well. |
yes, and the spec is either wrong or badly worded:
when true:
var a {.compileTime.}: int
proc main()=
echo a
a.inc
echo a
static: main()
main() prints:
furthermore the example given in spec is also misleading:
because and indeed, the example given would still pass if using instead:
{.compileTime.} should require calling it via a static context eg static or const, otherwise you end up with phase ordering problems. One aspect not mentioned in this RFC is your remark here: #283 (comment)
and indeed, that "after whole program compilation" conversion should be opt-in and explicit (via |
The PR nim-lang/Nim#12128 allowed runtime access to compile-time variables to solve nim-lang/Nim#10990
This is a controversial change:
that opened a can of worms:
I don't think there is any code that rely on that feature because it does not work.
I suggest removing it and make
static(myExpression)
the standard way to pass data from{.compileTime.}
variable to runtime variable with an appropriate error message when acompileTime
variable is used at runtime.This has the following benefits:
{.compileTime.}
only exist in the Nim VM and would be hardcoded in the binarystatic
magic would be one codepath only to handle the runtime/compilation time differences, for example if we want to keep using different hash functions between runtime and VM https://github.com/nim-lang/Nim/blob/d1210a3/lib/pure/hashes.nim#L352-L355The text was updated successfully, but these errors were encountered: