strict func: much better error messages (#15068)

* strict func: much better error messages
* documented the 'strict funcs' mode
This commit is contained in:
Andreas Rumpf 2020-07-25 19:36:53 +02:00 • committed by GitHub
commit 2aca748ddd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 133 additions and 46 deletions

View file

@ -1844,3 +1844,41 @@ via ``.noSideEffect``. The rules 3 and 4 can also be approximated by a different
5. A global or thread local variable (or a location derived from such a location)
can only passed to a parameter of a ``.noSideEffect`` proc.
Strict funcs
============
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.
For example:
.. code-block:: nim
{.experimental: "strictFuncs".}
type
Node = ref object
le, ri: Node
data: string
func len(n: Node): int =
# valid: len does not have side effects
var it = n
while it != nil:
inc result
it = it.ri
func mut(n: Node) =
let m = n # is the statement that connected the mutation to the parameter
m.data = "yeah" # the mutation is here
# Error: 'mut' can have side effects
# an object reachable from 'n' is potentially mutated
The algorithm behind this analysis is currently not documented.