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
{.experimental: "strictFuncs".}
typeFoo=refobjectfunctwice(foo: Foo) =var a =newSeq[Foo](2)
a[0] = foo # No error.
a[1] = foo # Error: 'twice' can have side effects.let foo =Foo()
twice(foo)
Current behavior
/tmp/foo.nim(6, 6) Error: 'twice' can have side effects
an object reachable from 'foo' is potentially mutated
/tmp/foo.nim(9, 4) the mutation is here
/tmp/foo.nim(8, 4) is the statement that connected the mutation to the parameter
Expected behavior
Compiles without error.
Nim version
Occurs in Nim devel (3d692d0), Nim 1.6.8, and every Nim version back to 1.4.0 (which introduced strictFuncs).
Additional information
This issue is a follow-up to #18998 (comment) and #16305. I'm attempting to make the latter more actionable by tracking the maybe-simplest subset of problems in a separate issue, with a single clear reduction. But feel free to close this as a duplicate if we prefer a single issue that's probably harder to close.
We see no error if we make Foo a non-ref object, or change twice to any of the below:
functwice(foo: Foo) =var a =newSeq[Foo](2)
a[0] = foo
functwice(foo: Foo) =var a =newSeq[Foo](2)
a[0] = foo
var b = foo
functwice(foo: Foo) =var a =newSeqOfCap[Foo](2)
a.add foo
a.add foo
The example at the top of this post is a reduction of the problem that keeps procedures like sequtils.zip,sequtils.unzip, and sequtils.repeat as a proc:
Since version 1.4, a stricter definition of "side effect" is available. In addition to the existing rule that a side effect is calling a function with side effects, the following rule is also enforced:
Any mutation to an object does count as a side effect if that object is reachable via a parameter that is not declared as a var parameter.
So we shouldn't see an error in any of the above cases, because there is no mutation.
The text was updated successfully, but these errors were encountered:
Uh oh!
There was an error while loading. Please reload this page.
Example
Current behavior
Expected behavior
Compiles without error.
Nim version
Occurs in Nim
devel
(3d692d0), Nim 1.6.8, and every Nim version back to 1.4.0 (which introducedstrictFuncs
).Additional information
This issue is a follow-up to #18998 (comment) and #16305. I'm attempting to make the latter more actionable by tracking the maybe-simplest subset of problems in a separate issue, with a single clear reduction. But feel free to close this as a duplicate if we prefer a single issue that's probably harder to close.
We see no error if we make
Foo
a non-ref object, or changetwice
to any of the below:The example at the top of this post is a reduction of the problem that keeps procedures like
sequtils.zip
,sequtils.unzip
, andsequtils.repeat
as aproc
:Nim/lib/pure/collections/sequtils.nim
Lines 176 to 187 in e03a178
Because when we made each a
func
, something like the below would produce an error:Background
The docs for
strictFuncs
say:So we shouldn't see an error in any of the above cases, because there is no mutation.
The text was updated successfully, but these errors were encountered: