-
-
Notifications
You must be signed in to change notification settings - Fork 1.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
allow generic compileTime proc folding #22022
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
41f851c
to
c1fae98
Compare
This comment was marked as outdated.
This comment was marked as outdated.
c1fae98
to
399c7fd
Compare
I've changed it so non-magic calls do not fold at compile time in |
@@ -49,7 +49,9 @@ proc semTypeOf(c: PContext; n: PNode): PNode = | |||
else: | |||
m = mode.intVal | |||
result = newNodeI(nkTypeOfExpr, n.info) | |||
inc c.inTypeofContext | |||
let typExpr = semExprWithType(c, n[1], if m == 1: {efInTypeof} else: {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you can see here we already have efInTypeof
. Can this be used instead of c.inTypeofContext
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what I tried first, it doesn't always propagate, even in simple cases like typeof(foo(compileTimeProc()))
where it's nested in another call.
Thanks for your hard work on this PR! Hint: mm: orc; opt: speed; options: -d:release |
fixes #10753, fixes #22021, refs #19365 (was fixed by #22029, but more faithful test added) For whatever reason `compileTime` proc calls did not fold if the proc was generic ([since this folding was introduced](c25ffbf#diff-539da3a63df08fa987f1b0c67d26cdc690753843d110b6bf0805a685eeaffd40)). I'm guessing the intention was for *unresolved* generic procs to not fold, which is now the logic. Non-magic `compileTime` procs also now don't fold at compile time in `typeof` contexts to avoid possible runtime errors (only the important) and prevent double/needless evaluation. (cherry picked from commit f7c11a8)
…port:2.0] (#24152) fixes #24150, refs #22022 An exception is raised in the `semExprWithType` call, which means `dec c.inTypeofContext` is never called, but `compiles` allows compilation to continue. This means `c.inTypeofContext` is left perpetually nonzero, which prevents `compileTime` evaluation for the rest of the program. To fix this, `defer:` is used for the `dec c.inTypeofContext` call, as is done for [`instCounter`](https://github.com/nim-lang/Nim/blob/d51d88700b2fb3bd228d5e8f7385e2e4a2e2880c/compiler/seminst.nim#L374) in other parts of the compiler.
…port:2.0] (#24152) fixes #24150, refs #22022 An exception is raised in the `semExprWithType` call, which means `dec c.inTypeofContext` is never called, but `compiles` allows compilation to continue. This means `c.inTypeofContext` is left perpetually nonzero, which prevents `compileTime` evaluation for the rest of the program. To fix this, `defer:` is used for the `dec c.inTypeofContext` call, as is done for [`instCounter`](https://github.com/nim-lang/Nim/blob/d51d88700b2fb3bd228d5e8f7385e2e4a2e2880c/compiler/seminst.nim#L374) in other parts of the compiler. (cherry picked from commit a177720)
fixes #24228, refs #22022 As described in #24228 (comment), instantiating generic routines inside `typeof` causes all code inside to be treated as being in a typeof context, and thus preventing compile time proc folding, causing issues when code is generated for the instantiated routine. Now, instantiated generic procs are treated as never being inside a `typeof` context. This is probably an arbitrary special case and more issues with the `typeof` behavior from #22022 are likely. Ideally this behavior would be removed but it's necessary to accomodate the current [proc `declval` in the package `stew`](status-im/nim-stew#190), at least without changes to `compileTime` that would either break other code (making it not eagerly fold by default) or still require a change in stew (adding an option to disable the eager folding). Alternatively we could also make the eager folding opt-in only for generic compileTime procs so that #22022 breaks nothing whatsoever, but a universal solution would be better. Edit: Done in #24230 via experimental switch
fixes #24228, refs #22022 As described in #24228 (comment), instantiating generic routines inside `typeof` causes all code inside to be treated as being in a typeof context, and thus preventing compile time proc folding, causing issues when code is generated for the instantiated routine. Now, instantiated generic procs are treated as never being inside a `typeof` context. This is probably an arbitrary special case and more issues with the `typeof` behavior from #22022 are likely. Ideally this behavior would be removed but it's necessary to accomodate the current [proc `declval` in the package `stew`](status-im/nim-stew#190), at least without changes to `compileTime` that would either break other code (making it not eagerly fold by default) or still require a change in stew (adding an option to disable the eager folding). Alternatively we could also make the eager folding opt-in only for generic compileTime procs so that #22022 breaks nothing whatsoever, but a universal solution would be better. Edit: Done in #24230 via experimental switch (cherry picked from commit ea9811a)
fixes #10753, fixes #22021, refs #19365 (was fixed by #22029, but more faithful test added)
For whatever reason
compileTime
proc calls did not fold if the proc was generic (since this folding was introduced). I'm guessing the intention was for unresolved generic procs to not fold, which is now the logic.Non-magic
compileTime
procs also now don't fold at compile time intypeof
contexts to avoid possible runtime errors (only the type is important) and prevent double/needless evaluation.