You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If a proc-holding var is assigned a value using a case statement, it's "noSideEffect" state is set to the one from the first arm of the case statement. i.e. if the second or later arm of the case has a proc with side effects, it will fail compilation.
So can one of these be made possible:
Force the "side effect state" of a proc even if it actually has no side effects
Force the "side effect state" of a proc holding var
If a var is assigned from a case statement as in this example, its "side effect state" should be "has side effects" irrespective of the order of the case statement cases.
# fails -> nim c t12642.nim# passes -> nim c -d:workaround t12642.nimlet
choice =0procwithoutSideEffects(): int=0procwithSideEffects(): int=echo"foo"# the echo causes the side effectwhendefined(workaround):
# Workaround is to put the proc *without* side effects at the end of the# case statement.let procPtr =case choice
of0: withSideEffects
else: withoutSideEffects
else:
let procPtr =case choice
of0: withoutSideEffects
else: withSideEffects
echo procPtr.repr
Run nim c t12642.nim.
Current Output
Error: type mismatch: got <proc (): int{.gcsafe, locks: 0.}> but expected 'proc (): int{.noSideEffect, gcsafe, locks: 0.}'
Expected Output
Should have printed the addr of the procPtr.
Workaround
Re-arrange the case statement so that the first case is the one where the proc with side effects is assigned,
But this workaround is not very robust, because tomorrow if the withSideEffects proc gets refactored to have no side effects, the case statement might need to be shuffled again.
So running nim c -d:workaround t12642.nim will work.
$ nim -v
Nim Compiler Version 1.1.1 [Linux: amd64]
Compiled at 2019-11-11
Copyright (c) 2006-2019 by Andreas Rumpf
git hash: 7e689873e203620c3a14f1697129cd0e3fd548e4
active boot switches: -d:release
Update (2021/06/10):
I am re-confirming this issue on the latest devel:
Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-06-10
Copyright (c) 2006-2021 by Andreas Rumpf
git hash: 19918ceb2b099440cd3e43b6d7fff313de6a84a2
active boot switches: -d:release
The text was updated successfully, but these errors were encountered:
If a proc-holding var is assigned a value using a
case
statement, it's "noSideEffect" state is set to the one from the first arm of the case statement. i.e. if the second or later arm of thecase
has a proc with side effects, it will fail compilation.So can one of these be made possible:
Example
t12642.nim
https://play.nim-lang.org/#ix=21tP
Run
nim c t12642.nim
.Current Output
Expected Output
Should have printed the addr of the
procPtr
.Workaround
Re-arrange the case statement so that the first case is the one where the proc with side effects is assigned,
But this workaround is not very robust, because tomorrow if the
withSideEffects
proc gets refactored to have no side effects, the case statement might need to be shuffled again.So running
nim c -d:workaround t12642.nim
will work.Additional Information
Update (2021/06/10):
I am re-confirming this issue on the latest devel:
The text was updated successfully, but these errors were encountered: