mirror of
https://github.com/nim-lang/Nim.git
synced 2026-08-28 09:31:38 +00:00
writing to a location counts as "side effect"; implements https://github.com/nim-lang/RFCs/issues/234 (#15030)
This commit is contained in:
31
tests/effects/tfuncs_cannot_mutate.nim
Normal file
31
tests/effects/tfuncs_cannot_mutate.nim
Normal file
@@ -0,0 +1,31 @@
|
||||
discard """
|
||||
errormsg: "'mutate' can have side effects"
|
||||
line: 25
|
||||
"""
|
||||
|
||||
{.experimental: "strictFuncs".}
|
||||
|
||||
type
|
||||
Node = ref object
|
||||
le, ri: Node
|
||||
data: string
|
||||
|
||||
func len(n: Node): int =
|
||||
var it = n
|
||||
while it != nil:
|
||||
inc result
|
||||
it = it.ri
|
||||
|
||||
func doNotDistract(n: Node) =
|
||||
var m = Node()
|
||||
m.data = "abc"
|
||||
|
||||
func select(a, b: Node): Node = b
|
||||
|
||||
func mutate(n: Node) =
|
||||
var it = n
|
||||
let x = it
|
||||
let y = x
|
||||
let z = y
|
||||
|
||||
select(x, z).data = "tricky"
|
||||
Reference in New Issue
Block a user