strict func: much better error messages (#15068)
* strict func: much better error messages * documented the 'strict funcs' mode
This commit is contained in:
parent
7ca32c86bb
commit
2aca748ddd
5 changed files with 133 additions and 46 deletions
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue