make strict funcs analysis smarter (#18219)

* make strict funcs analysis smarter: varParam[i] = v is different from varParam[i][] = v
* added a test case
* Update compiler/varpartitions.nim

Co-authored-by: Clyybber <darkmine956@gmail.com>
This commit is contained in:
Andreas Rumpf 2021-06-09 17:33:19 +02:00 • committed by GitHub
commit 47acc80f4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 95 additions and 36 deletions

View file

@ -0,0 +1,27 @@
discard """
errormsg: "'copy' can have side effects"
nimout: '''an object reachable from 'y' is potentially mutated
tfuncs_cannot_mutate2.nim(15, 7) the mutation is here
tfuncs_cannot_mutate2.nim(13, 10) is the statement that connected the mutation to the parameter
'''
"""
{.experimental: "strictFuncs".}
func copy[T](x: var openArray[T]; y: openArray[T]) =
for i in 0..high(x):
x[i] = y[i]
x[0].a = nil
type
R = ref object
a, b: R
data: string
proc main =
var a, b: array[3, R]
b = [R(data: "a"), R(data: "b"), R(data: "c")]
copy a, b
main()

View file

@ -27,3 +27,20 @@ block:
var x = @[0, 1]
let z = x &&& 2
func copy[T](x: var openArray[T]; y: openArray[T]) =
for i in 0..high(x):
x[i] = y[i]
type
R = ref object
a, b: R
data: string
proc main =
var a, b: array[3, R]
b = [R(data: "a"), R(data: "b"), R(data: "c")]
copy a, b
main()