-
-
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
strictFuncs/views
design issue
#16305
Comments
[moved to top post] |
strictFuncs
(or views) reports error on valid code (mutation through proc param)strictFuncs
(or views
) reports error on valid code (mutation through proc param)
strictFuncs
(or views
) reports error on valid code (mutation through proc param)strictFuncs
(or views
) reports error on valid code (mutation through var copy)
strictFuncs
(or views
) reports error on valid code (mutation through var copy)strictFuncs
(or views
) design issue?
strictFuncs
(or views
) design issue?strictFuncs/views
design issue?
This was outlined when we announced strict funcs on the forum, yes. It's well known to me. I don't have a solution beyond making the writetracking smarter. |
this causes regressions that are hard to fix, see example 6 I just added, which shows valid code that has stopped working after #16293, because I don't see how this can be fixed with current
I do have a solution. I've worked hard on #14976 and IMO we should re-open it, it offers a better and more general approach to safe views/1st class openarray:
(whereas
I've done more work on that branch to fix remaining edge cases for detection of escapes but these haven't been pushed as the PR was closed. extensions of interprocedural analysis from #14976memory tracking in that PR could be adapted/generalized to other problems eg sideEffect tracking but that's a separate issue from what's needed for escape safe views, whereas, as shown in this issue, Example:
and for all these, giving warning/error when compiler cannot prove that these guarantees don't hold |
Interprocedural analysis is a dead-end. Been there, done that, error messages are not understandable and tend to point into code that you didn't write yourself. Interprocedural analysis is the opposite of "robust", it's fragile by construction.
That's a huge design problem. It means that some usages of Also, you don't even have an RFC for your work. |
strictFuncs/views
design issue?strictFuncs/views
design issue
Should we revert some generic My understanding is that upgrading Nim 1.4.x to 1.6.0 RC2 breaks code that:
as seen in e.g. @timotheecour's example 5: {.experimental: "strictFuncs".}
import std/sequtils
type Foo = ref object
x: int
let a1 = zip(@[1,2], @[1,2]) # ok
let a2 = zip(@[Foo(x: 1)], @[Foo(x: 2)]) # error in 1.6.0 RC2, but not 1.4.x Or do we consider this error when using an "experimental" feature as sufficiently minor/obscure to be acceptable, and that we should just wait until the |
Nim 1.4.x compiled the below code without error when using `--experimental:strictFuncs` import std/sequtils type Foo = ref object let foo1 = Foo() let foo2 = Foo() let foos = @[foo1, foo2] let fooTuples = @[(foo1, 1), (foo2, 2)] discard repeat(foo1, 3) discard zip(foos, foos) discard unzip(fooTuples) However, since 2020-12-09, devel Nim produced errors like /tmp/strict.nim(11, 15) template/generic instantiation of `repeat` from here /foo/nim/pure/collections/sequtils.nim(172, 6) Error: 'repeat' can have side effects an object reachable from 'x' is potentially mutated /foo/nim/pure/collections/sequtils.nim(183, 15) the mutation is here /foo/nim/pure/collections/sequtils.nim(183, 15) is the statement that connected the mutation to the parameter This commit reverts some `proc` to `func` changes so that code that: - calls `repeat`, `zip`, or `unzip` - and instantiates them with types containing `ref` is able to compile with `--experimental:strictFuncs`. Otherwise, a user might be forced to drop their `strictFuncs` use when upgrading from 1.4.x, or when writing new code that uses these procedures (at least for now, with the current `strictFuncs` implementation). This commit also adds tests to assert that the remaining funcs in this module can be compiled with `strictFuncs` when used with types containing `ref`. The original batch of `proc` to `func` changes in `sequtils.nim` was in commit 6f57eba, which was partially reverted in 38eb021. See also: nim-lang#16305
Nim 1.4.x compiled the below code without error when using `--experimental:strictFuncs` import std/sequtils type Foo = ref object let foo1 = Foo() let foo2 = Foo() let foos = @[foo1, foo2] let fooTuples = @[(foo1, 1), (foo2, 2)] discard repeat(foo1, 3) discard zip(foos, foos) discard unzip(fooTuples) However, since 2020-12-09, devel Nim produced errors like /tmp/bar.nim(11, 15) template/generic instantiation of `repeat` from here /foo/nim/pure/collections/sequtils.nim(172, 6) Error: 'repeat' can have side effects an object reachable from 'x' is potentially mutated /foo/nim/pure/collections/sequtils.nim(183, 15) the mutation is here /foo/nim/pure/collections/sequtils.nim(183, 15) is the statement that connected the mutation to the parameter This commit reverts some `proc` to `func` changes so that code that: - calls `repeat`, `zip`, or `unzip` - and instantiates them with types containing `ref` can once again be compiled with `strictFuncs`. Otherwise, a user might be forced to drop or alter their `strictFuncs` use when upgrading from Nim 1.4.x, or when writing new code that uses these procedures (at least for now, with the current `strictFuncs` implementation). This commit also adds tests to assert that the remaining funcs in this module can be compiled with `strictFuncs` when used with types containing `ref`. The original batch of `proc` to `func` changes in `sequtils.nim` was in commit 6f57eba, which was partially reverted in 38eb021. See also: nim-lang#16305
I opened #18998 to workaround the rest of the strictFuncs regressions in sequtils, finding all the problematic Aside from the already mentioned @Araq wrote:
There was previously a reduction in this post, but I've moved it to #20863. |
Nim 1.4.x compiled the below code without error when using `--experimental:strictFuncs` import std/sequtils type Foo = ref object let foo1 = Foo() let foo2 = Foo() let foos = @[foo1, foo2] let fooTuples = @[(foo1, 1), (foo2, 2)] discard repeat(foo1, 3) discard zip(foos, foos) discard unzip(fooTuples) However, since 2020-12-09, devel Nim produced errors like /tmp/bar.nim(11, 15) template/generic instantiation of `repeat` from here /foo/nim/pure/collections/sequtils.nim(172, 6) Error: 'repeat' can have side effects an object reachable from 'x' is potentially mutated /foo/nim/pure/collections/sequtils.nim(183, 15) the mutation is here /foo/nim/pure/collections/sequtils.nim(183, 15) is the statement that connected the mutation to the parameter This commit reverts some `proc` to `func` changes so that code that: - calls `repeat`, `zip`, or `unzip` - and instantiates them with types containing `ref` can once again be compiled with `strictFuncs`. Otherwise, a user might be forced to drop or alter their `strictFuncs` use when upgrading from Nim 1.4.x, or when writing new code that uses these procedures (at least for now, with the current `strictFuncs` implementation). This commit also adds tests to assert that the remaining funcs in this module can be compiled with `strictFuncs` when used with types containing `ref`. The original batch of `proc` to `func` changes in `sequtils.nim` was in commit 6f57eba, which was partially reverted in 38eb021. See also: #16305
Nim 1.4.x compiled the below code without error when using `--experimental:strictFuncs` import std/sequtils type Foo = ref object let foo1 = Foo() let foo2 = Foo() let foos = @[foo1, foo2] let fooTuples = @[(foo1, 1), (foo2, 2)] discard repeat(foo1, 3) discard zip(foos, foos) discard unzip(fooTuples) However, since 2020-12-09, devel Nim produced errors like /tmp/bar.nim(11, 15) template/generic instantiation of `repeat` from here /foo/nim/pure/collections/sequtils.nim(172, 6) Error: 'repeat' can have side effects an object reachable from 'x' is potentially mutated /foo/nim/pure/collections/sequtils.nim(183, 15) the mutation is here /foo/nim/pure/collections/sequtils.nim(183, 15) is the statement that connected the mutation to the parameter This commit reverts some `proc` to `func` changes so that code that: - calls `repeat`, `zip`, or `unzip` - and instantiates them with types containing `ref` can once again be compiled with `strictFuncs`. Otherwise, a user might be forced to drop or alter their `strictFuncs` use when upgrading from Nim 1.4.x, or when writing new code that uses these procedures (at least for now, with the current `strictFuncs` implementation). This commit also adds tests to assert that the remaining funcs in this module can be compiled with `strictFuncs` when used with types containing `ref`. The original batch of `proc` to `func` changes in `sequtils.nim` was in commit 6f57eba, which was partially reverted in 38eb021. See also: nim-lang#16305
Resolved by #21066 |
…g#21066) * alternative, much simpler algorithm for strict func checking * forgot to git add new compiler module * new spec is incredibly simple to describe * fixes bigints regression * typos * closes nim-lang#16305; closes nim-lang#17387; closes nim-lang#20863
…g#21066) * alternative, much simpler algorithm for strict func checking * forgot to git add new compiler module * new spec is incredibly simple to describe * fixes bigints regression * typos * closes nim-lang#16305; closes nim-lang#17387; closes nim-lang#20863
…g#21066) * alternative, much simpler algorithm for strict func checking * forgot to git add new compiler module * new spec is incredibly simple to describe * fixes bigints regression * typos * closes nim-lang#16305; closes nim-lang#17387; closes nim-lang#20863
strictFuncs/views
seems to have a design issue, which preventsalgorithm.sorted
and many other procs from beingfunc
as I noticed when I started investigating the initial CI failures in in #16304 (comment)What's worse is that, when using generic func's, the issue only manifests itself when the func is instantiated with types containing ref, so that generic code that's seemingly valid when used in some project may fail when used in other contexts, causing regressions when proc's are changed to func's in a seemingly innocuous way (see example 6 for an example of such regression).
Example 1
with
--experimental:strictFuncs
this gives:Error: 'sortedFake2' can have side effects
Example 2
after reduction of the CI error I saw in #16304 (comment):
Example 3
this works fine, which is a reduction of the example in #16303:
Example 4
reduction of Example 2:
also gives:
Example 5
shows the kind of regressions you get:
before #16293
after #16293
EDIT: likewise with other procs, eg
sequtils.map
, eg:Error: 'map' can have side effects
an object reachable from 's' is potentially mutated
Example 6
this valid code stops to work after #16293:
--experimental:views
, it hitsError: 'zip' can have side effects
Error: invalid type: 'openArray[int]' for let
ditto with
algorithm.map
:Example 7
see #18065 (comment), because of
--experimental:strictFuncs
, a cast was used:which is IMO a sign of defeat; and on top of that it masks actual side effects for code not built with
--experimental:strictFuncs
Additional Information
func
doesn't check for side effects in proc parameters #16303The text was updated successfully, but these errors were encountered: