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:
parent
51ab7ccec1
commit
47acc80f4e
3 changed files with 95 additions and 36 deletions
27
tests/effects/tfuncs_cannot_mutate2.nim
Normal file
27
tests/effects/tfuncs_cannot_mutate2.nim
Normal 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()
|
||||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue