writing to a location counts as "side effect"; implements https://github.com/nim-lang/RFCs/issues/234 (#15030)

This commit is contained in:
Andreas Rumpf 2020-07-25 15:14:28 +02:00 • committed by GitHub
commit 7ca32c86bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 283 additions and 5 deletions

View file

@ -10,7 +10,7 @@
import
intsets, ast, astalgo, msgs, renderer, magicsys, types, idents, trees,
wordrecg, strutils, options, guards, lineinfos, semfold, semdata,
modulegraphs
modulegraphs, varpartitions
when defined(useDfa):
import dfa
@ -36,9 +36,6 @@ For every sink parameter of type T T is marked.
For every call f() the return type of f() is marked.
]#
# ------------------------ exception and tag tracking -------------------------
@ -73,6 +70,7 @@ type
guards: TModel # nested guards
locked: seq[PNode] # locked locations
gcUnsafe, isRecursive, isTopLevel, hasSideEffect, inEnforcedGcSafe: bool
hasDangerousAssign: bool
inEnforcedNoSideEffects: bool
maxLockLevel, currLockLevel: TLockLevel
currOptions: TOptions
@ -885,6 +883,8 @@ proc track(tracked: PEffects, n: PNode) =
createTypeBoundOps(tracked, n[0].typ, n.info)
if n[0].kind != nkSym or not isLocalVar(tracked, n[0].sym):
checkForSink(tracked.config, tracked.owner, n[1])
if not tracked.hasDangerousAssign and n[0].kind != nkSym:
tracked.hasDangerousAssign = true
of nkVarSection, nkLetSection:
for child in n:
let last = lastSon(child)
@ -1237,6 +1237,9 @@ proc trackProc*(c: PContext; s: PSym, body: PNode) =
patchResult(t, ensuresSpec)
effects[ensuresEffects] = ensuresSpec
if strictFuncs in c.features and not t.hasSideEffect and t.hasDangerousAssign:
t.hasSideEffect = mutatesNonVarParameters(s, body)
if sfThread in s.flags and t.gcUnsafe:
if optThreads in g.config.globalOptions and optThreadAnalysis in g.config.globalOptions:
#localError(s.info, "'$1' is not GC-safe" % s.name.s)