sempass2.nim 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. import
  10. intsets, ast, astalgo, msgs, renderer, magicsys, types, idents, trees,
  11. wordrecg, strutils, options, guards, lineinfos, semfold, semdata,
  12. modulegraphs, varpartitions, typeallowed, nilcheck, errorhandling, tables
  13. when defined(nimPreviewSlimSystem):
  14. import std/assertions
  15. when defined(useDfa):
  16. import dfa
  17. import liftdestructors
  18. include sinkparameter_inference
  19. #[ Second semantic checking pass over the AST. Necessary because the old
  20. way had some inherent problems. Performs:
  21. * effect+exception tracking
  22. * "usage before definition" checking
  23. * also now calls the "lift destructor logic" at strategic positions, this
  24. is about to be put into the spec:
  25. We treat assignment and sinks and destruction as identical.
  26. In the construct let/var x = expr() x's type is marked.
  27. In x = y the type of x is marked.
  28. For every sink parameter of type T T is marked.
  29. For every call f() the return type of f() is marked.
  30. ]#
  31. # ------------------------ exception and tag tracking -------------------------
  32. discard """
  33. exception tracking:
  34. a() # raises 'x', 'e'
  35. try:
  36. b() # raises 'e'
  37. except e:
  38. # must not undo 'e' here; hrm
  39. c()
  40. --> we need a stack of scopes for this analysis
  41. # XXX enhance the algorithm to care about 'dirty' expressions:
  42. lock a[i].L:
  43. inc i # mark 'i' dirty
  44. lock a[j].L:
  45. access a[i], a[j] # --> reject a[i]
  46. """
  47. type
  48. TEffects = object
  49. exc: PNode # stack of exceptions
  50. tags: PNode # list of tags
  51. forbids: PNode # list of tags
  52. bottom, inTryStmt, inExceptOrFinallyStmt, leftPartOfAsgn, inIfStmt, currentBlock: int
  53. owner: PSym
  54. ownerModule: PSym
  55. init: seq[int] # list of initialized variables
  56. scopes: Table[int, int] # maps var-id to its scope (see also `currentBlock`).
  57. guards: TModel # nested guards
  58. locked: seq[PNode] # locked locations
  59. gcUnsafe, isRecursive, isTopLevel, hasSideEffect, inEnforcedGcSafe: bool
  60. hasDangerousAssign, isInnerProc: bool
  61. inEnforcedNoSideEffects: bool
  62. currOptions: TOptions
  63. config: ConfigRef
  64. graph: ModuleGraph
  65. c: PContext
  66. escapingParams: IntSet
  67. PEffects = var TEffects
  68. proc createTypeBoundOps(tracked: PEffects, typ: PType; info: TLineInfo) =
  69. if typ == nil: return
  70. when false:
  71. let realType = typ.skipTypes(abstractInst)
  72. if realType.kind == tyRef and
  73. optSeqDestructors in tracked.config.globalOptions:
  74. createTypeBoundOps(tracked.graph, tracked.c, realType.lastSon, info)
  75. createTypeBoundOps(tracked.graph, tracked.c, typ, info, tracked.c.idgen)
  76. if (tfHasAsgn in typ.flags) or
  77. optSeqDestructors in tracked.config.globalOptions:
  78. tracked.owner.flags.incl sfInjectDestructors
  79. proc isLocalVar(a: PEffects, s: PSym): bool =
  80. s.typ != nil and (s.kind in {skVar, skResult} or (s.kind == skParam and isOutParam(s.typ))) and
  81. sfGlobal notin s.flags and s.owner == a.owner
  82. proc lockLocations(a: PEffects; pragma: PNode) =
  83. if pragma.kind != nkExprColonExpr:
  84. localError(a.config, pragma.info, "locks pragma without argument")
  85. return
  86. for x in pragma[1]:
  87. a.locked.add x
  88. proc guardGlobal(a: PEffects; n: PNode; guard: PSym) =
  89. # check whether the corresponding lock is held:
  90. for L in a.locked:
  91. if L.kind == nkSym and L.sym == guard: return
  92. # we allow accesses nevertheless in top level statements for
  93. # easier initialization:
  94. #if a.isTopLevel:
  95. # message(a.config, n.info, warnUnguardedAccess, renderTree(n))
  96. #else:
  97. if not a.isTopLevel:
  98. localError(a.config, n.info, "unguarded access: " & renderTree(n))
  99. # 'guard*' are checks which are concerned with 'guard' annotations
  100. # (var x{.guard: y.}: int)
  101. proc guardDotAccess(a: PEffects; n: PNode) =
  102. let ri = n[1]
  103. if ri.kind != nkSym or ri.sym.kind != skField: return
  104. var g = ri.sym.guard
  105. if g.isNil or a.isTopLevel: return
  106. # fixup guard:
  107. if g.kind == skUnknown:
  108. var field: PSym = nil
  109. var ty = n[0].typ.skipTypes(abstractPtrs)
  110. if ty.kind == tyTuple and not ty.n.isNil:
  111. field = lookupInRecord(ty.n, g.name)
  112. else:
  113. while ty != nil and ty.kind == tyObject:
  114. field = lookupInRecord(ty.n, g.name)
  115. if field != nil: break
  116. ty = ty[0]
  117. if ty == nil: break
  118. ty = ty.skipTypes(skipPtrs)
  119. if field == nil:
  120. localError(a.config, n.info, "invalid guard field: " & g.name.s)
  121. return
  122. g = field
  123. #ri.sym.guard = field
  124. # XXX unfortunately this is not correct for generic instantiations!
  125. if g.kind == skField:
  126. let dot = newNodeI(nkDotExpr, n.info, 2)
  127. dot[0] = n[0]
  128. dot[1] = newSymNode(g)
  129. dot.typ = g.typ
  130. for L in a.locked:
  131. #if a.guards.sameSubexprs(dot, L): return
  132. if guards.sameTree(dot, L): return
  133. localError(a.config, n.info, "unguarded access: " & renderTree(n))
  134. else:
  135. guardGlobal(a, n, g)
  136. proc makeVolatile(a: PEffects; s: PSym) {.inline.} =
  137. if a.inTryStmt > 0 and a.config.exc == excSetjmp:
  138. incl(s.flags, sfVolatile)
  139. proc varDecl(a: PEffects; n: PNode) {.inline.} =
  140. if n.kind == nkSym:
  141. a.scopes[n.sym.id] = a.currentBlock
  142. proc skipHiddenDeref(n: PNode): PNode {.inline.} =
  143. result = if n.kind == nkHiddenDeref: n[0] else: n
  144. proc initVar(a: PEffects, n: PNode; volatileCheck: bool) =
  145. let n = skipHiddenDeref(n)
  146. if n.kind != nkSym: return
  147. let s = n.sym
  148. if isLocalVar(a, s):
  149. if volatileCheck: makeVolatile(a, s)
  150. for x in a.init:
  151. if x == s.id: return
  152. a.init.add s.id
  153. if a.scopes.getOrDefault(s.id) == a.currentBlock:
  154. #[ Consider this case:
  155. var x: T
  156. while true:
  157. if cond:
  158. x = T() #1
  159. else:
  160. x = T() #2
  161. use x
  162. Even though both #1 and #2 are first writes we must use the `=copy`
  163. here so that the old value is destroyed because `x`'s destructor is
  164. run outside of the while loop. This is why we need the check here that
  165. the assignment is done in the same logical block as `x` was declared in.
  166. ]#
  167. n.flags.incl nfFirstWrite
  168. proc initVarViaNew(a: PEffects, n: PNode) =
  169. let n = skipHiddenDeref(n)
  170. if n.kind != nkSym: return
  171. let s = n.sym
  172. if {tfRequiresInit, tfNotNil} * s.typ.flags <= {tfNotNil}:
  173. # 'x' is not nil, but that doesn't mean its "not nil" children
  174. # are initialized:
  175. initVar(a, n, volatileCheck=true)
  176. elif isLocalVar(a, s):
  177. makeVolatile(a, s)
  178. proc warnAboutGcUnsafe(n: PNode; conf: ConfigRef) =
  179. #assert false
  180. message(conf, n.info, warnGcUnsafe, renderTree(n))
  181. proc markGcUnsafe(a: PEffects; reason: PSym) =
  182. if not a.inEnforcedGcSafe:
  183. a.gcUnsafe = true
  184. if a.owner.kind in routineKinds: a.owner.gcUnsafetyReason = reason
  185. proc markGcUnsafe(a: PEffects; reason: PNode) =
  186. if not a.inEnforcedGcSafe:
  187. a.gcUnsafe = true
  188. if a.owner.kind in routineKinds:
  189. if reason.kind == nkSym:
  190. a.owner.gcUnsafetyReason = reason.sym
  191. else:
  192. a.owner.gcUnsafetyReason = newSym(skUnknown, a.owner.name, nextSymId a.c.idgen,
  193. a.owner, reason.info, {})
  194. proc markSideEffect(a: PEffects; reason: PNode | PSym; useLoc: TLineInfo) =
  195. if not a.inEnforcedNoSideEffects:
  196. a.hasSideEffect = true
  197. if a.owner.kind in routineKinds:
  198. var sym: PSym
  199. when reason is PNode:
  200. if reason.kind == nkSym:
  201. sym = reason.sym
  202. else:
  203. let kind = if reason.kind == nkHiddenDeref: skParam else: skUnknown
  204. sym = newSym(kind, a.owner.name, nextSymId a.c.idgen, a.owner, reason.info, {})
  205. else:
  206. sym = reason
  207. a.c.sideEffects.mgetOrPut(a.owner.id, @[]).add (useLoc, sym)
  208. when false: markGcUnsafe(a, reason)
  209. proc listGcUnsafety(s: PSym; onlyWarning: bool; cycleCheck: var IntSet; conf: ConfigRef) =
  210. let u = s.gcUnsafetyReason
  211. if u != nil and not cycleCheck.containsOrIncl(u.id):
  212. let msgKind = if onlyWarning: warnGcUnsafe2 else: errGenerated
  213. case u.kind
  214. of skLet, skVar:
  215. if u.typ.skipTypes(abstractInst).kind == tyProc:
  216. message(conf, s.info, msgKind,
  217. "'$#' is not GC-safe as it calls '$#'" %
  218. [s.name.s, u.name.s])
  219. else:
  220. message(conf, s.info, msgKind,
  221. ("'$#' is not GC-safe as it accesses '$#'" &
  222. " which is a global using GC'ed memory") % [s.name.s, u.name.s])
  223. of routineKinds:
  224. # recursive call *always* produces only a warning so the full error
  225. # message is printed:
  226. if u.kind == skMethod and {sfBase, sfThread} * u.flags == {sfBase}:
  227. message(conf, u.info, msgKind,
  228. "Base method '$#' requires explicit '{.gcsafe.}' to be GC-safe" %
  229. [u.name.s])
  230. else:
  231. listGcUnsafety(u, true, cycleCheck, conf)
  232. message(conf, s.info, msgKind,
  233. "'$#' is not GC-safe as it calls '$#'" %
  234. [s.name.s, u.name.s])
  235. of skParam, skForVar:
  236. message(conf, s.info, msgKind,
  237. "'$#' is not GC-safe as it performs an indirect call via '$#'" %
  238. [s.name.s, u.name.s])
  239. else:
  240. message(conf, u.info, msgKind,
  241. "'$#' is not GC-safe as it performs an indirect call here" % s.name.s)
  242. proc listGcUnsafety(s: PSym; onlyWarning: bool; conf: ConfigRef) =
  243. var cycleCheck = initIntSet()
  244. listGcUnsafety(s, onlyWarning, cycleCheck, conf)
  245. proc listSideEffects(result: var string; s: PSym; cycleCheck: var IntSet;
  246. conf: ConfigRef; context: PContext; indentLevel: int) =
  247. template addHint(msg; lineInfo; sym; level = indentLevel) =
  248. result.addf("$# $# Hint: '$#' $#\n", repeat(">", level), conf $ lineInfo, sym, msg)
  249. if context.sideEffects.hasKey(s.id):
  250. for (useLineInfo, u) in context.sideEffects[s.id]:
  251. if u != nil and not cycleCheck.containsOrIncl(u.id):
  252. case u.kind
  253. of skLet, skVar:
  254. addHint("accesses global state '$#'" % u.name.s, useLineInfo, s.name.s)
  255. addHint("accessed by '$#'" % s.name.s, u.info, u.name.s, indentLevel + 1)
  256. of routineKinds:
  257. addHint("calls `.sideEffect` '$#'" % u.name.s, useLineInfo, s.name.s)
  258. addHint("called by '$#'" % s.name.s, u.info, u.name.s, indentLevel + 1)
  259. listSideEffects(result, u, cycleCheck, conf, context, indentLevel + 2)
  260. of skParam, skForVar:
  261. addHint("calls routine via hidden pointer indirection", useLineInfo, s.name.s)
  262. else:
  263. addHint("calls routine via pointer indirection", useLineInfo, s.name.s)
  264. proc listSideEffects(result: var string; s: PSym; conf: ConfigRef; context: PContext) =
  265. var cycleCheck = initIntSet()
  266. result.addf("'$#' can have side effects\n", s.name.s)
  267. listSideEffects(result, s, cycleCheck, conf, context, 1)
  268. proc useVarNoInitCheck(a: PEffects; n: PNode; s: PSym) =
  269. if {sfGlobal, sfThread} * s.flags != {} and s.kind in {skVar, skLet} and
  270. s.magic != mNimvm:
  271. if s.guard != nil: guardGlobal(a, n, s.guard)
  272. if {sfGlobal, sfThread} * s.flags == {sfGlobal} and
  273. (tfHasGCedMem in s.typ.flags or s.typ.isGCedMem):
  274. #if a.config.hasWarn(warnGcUnsafe): warnAboutGcUnsafe(n)
  275. markGcUnsafe(a, s)
  276. markSideEffect(a, s, n.info)
  277. if s.owner != a.owner and s.kind in {skVar, skLet, skForVar, skResult, skParam} and
  278. {sfGlobal, sfThread} * s.flags == {}:
  279. a.isInnerProc = true
  280. proc useVar(a: PEffects, n: PNode) =
  281. let s = n.sym
  282. if a.inExceptOrFinallyStmt > 0:
  283. incl s.flags, sfUsedInFinallyOrExcept
  284. if isLocalVar(a, s):
  285. if sfNoInit in s.flags:
  286. # If the variable is explicitly marked as .noinit. do not emit any error
  287. a.init.add s.id
  288. elif s.id notin a.init:
  289. if s.typ.requiresInit:
  290. message(a.config, n.info, warnProveInit, s.name.s)
  291. elif a.leftPartOfAsgn <= 0:
  292. if strictDefs in a.c.features:
  293. message(a.config, n.info, warnUninit, s.name.s)
  294. # prevent superfluous warnings about the same variable:
  295. a.init.add s.id
  296. useVarNoInitCheck(a, n, s)
  297. type
  298. TIntersection = seq[tuple[id, count: int]] # a simple count table
  299. proc addToIntersection(inter: var TIntersection, s: int) =
  300. for j in 0..<inter.len:
  301. if s == inter[j].id:
  302. inc inter[j].count
  303. return
  304. inter.add((id: s, count: 1))
  305. proc throws(tracked, n, orig: PNode) =
  306. if n.typ == nil or n.typ.kind != tyError:
  307. if orig != nil:
  308. let x = copyTree(orig)
  309. x.typ = n.typ
  310. tracked.add x
  311. else:
  312. tracked.add n
  313. proc getEbase(g: ModuleGraph; info: TLineInfo): PType =
  314. result = g.sysTypeFromName(info, "Exception")
  315. proc excType(g: ModuleGraph; n: PNode): PType =
  316. # reraise is like raising E_Base:
  317. let t = if n.kind == nkEmpty or n.typ.isNil: getEbase(g, n.info) else: n.typ
  318. result = skipTypes(t, skipPtrs)
  319. proc createRaise(g: ModuleGraph; n: PNode): PNode =
  320. result = newNode(nkType)
  321. result.typ = getEbase(g, n.info)
  322. if not n.isNil: result.info = n.info
  323. proc createTag(g: ModuleGraph; n: PNode): PNode =
  324. result = newNode(nkType)
  325. result.typ = g.sysTypeFromName(n.info, "RootEffect")
  326. if not n.isNil: result.info = n.info
  327. proc addRaiseEffect(a: PEffects, e, comesFrom: PNode) =
  328. #assert e.kind != nkRaiseStmt
  329. var aa = a.exc
  330. for i in a.bottom..<aa.len:
  331. # we only track the first node that can have the effect E in order
  332. # to safe space and time.
  333. if sameType(a.graph.excType(aa[i]), a.graph.excType(e)): return
  334. if e.typ != nil:
  335. if optNimV1Emulation in a.config.globalOptions or not isDefectException(e.typ):
  336. throws(a.exc, e, comesFrom)
  337. proc addTag(a: PEffects, e, comesFrom: PNode) =
  338. var aa = a.tags
  339. for i in 0..<aa.len:
  340. # we only track the first node that can have the effect E in order
  341. # to safe space and time.
  342. if sameType(aa[i].typ.skipTypes(skipPtrs), e.typ.skipTypes(skipPtrs)): return
  343. throws(a.tags, e, comesFrom)
  344. proc addNotTag(a: PEffects, e, comesFrom: PNode) =
  345. var aa = a.forbids
  346. for i in 0..<aa.len:
  347. if sameType(aa[i].typ.skipTypes(skipPtrs), e.typ.skipTypes(skipPtrs)): return
  348. throws(a.forbids, e, comesFrom)
  349. proc mergeRaises(a: PEffects, b, comesFrom: PNode) =
  350. if b.isNil:
  351. addRaiseEffect(a, createRaise(a.graph, comesFrom), comesFrom)
  352. else:
  353. for effect in items(b): addRaiseEffect(a, effect, comesFrom)
  354. proc mergeTags(a: PEffects, b, comesFrom: PNode) =
  355. if b.isNil:
  356. addTag(a, createTag(a.graph, comesFrom), comesFrom)
  357. else:
  358. for effect in items(b): addTag(a, effect, comesFrom)
  359. proc listEffects(a: PEffects) =
  360. for e in items(a.exc): message(a.config, e.info, hintUser, typeToString(e.typ))
  361. for e in items(a.tags): message(a.config, e.info, hintUser, typeToString(e.typ))
  362. for e in items(a.forbids): message(a.config, e.info, hintUser, typeToString(e.typ))
  363. proc catches(tracked: PEffects, e: PType) =
  364. let e = skipTypes(e, skipPtrs)
  365. var L = tracked.exc.len
  366. var i = tracked.bottom
  367. while i < L:
  368. # r supertype of e?
  369. if safeInheritanceDiff(tracked.graph.excType(tracked.exc[i]), e) <= 0:
  370. tracked.exc[i] = tracked.exc[L-1]
  371. dec L
  372. else:
  373. inc i
  374. if tracked.exc.len > 0:
  375. setLen(tracked.exc.sons, L)
  376. else:
  377. assert L == 0
  378. proc catchesAll(tracked: PEffects) =
  379. if tracked.exc.len > 0:
  380. setLen(tracked.exc.sons, tracked.bottom)
  381. proc track(tracked: PEffects, n: PNode)
  382. proc trackTryStmt(tracked: PEffects, n: PNode) =
  383. let oldBottom = tracked.bottom
  384. tracked.bottom = tracked.exc.len
  385. let oldState = tracked.init.len
  386. var inter: TIntersection = @[]
  387. inc tracked.inTryStmt
  388. track(tracked, n[0])
  389. dec tracked.inTryStmt
  390. for i in oldState..<tracked.init.len:
  391. addToIntersection(inter, tracked.init[i])
  392. var branches = 1
  393. var hasFinally = false
  394. inc tracked.inExceptOrFinallyStmt
  395. # Collect the exceptions caught by the except branches
  396. for i in 1..<n.len:
  397. let b = n[i]
  398. if b.kind == nkExceptBranch:
  399. inc branches
  400. if b.len == 1:
  401. catchesAll(tracked)
  402. else:
  403. for j in 0..<b.len - 1:
  404. if b[j].isInfixAs():
  405. assert(b[j][1].kind == nkType)
  406. catches(tracked, b[j][1].typ)
  407. createTypeBoundOps(tracked, b[j][2].typ, b[j][2].info)
  408. else:
  409. assert(b[j].kind == nkType)
  410. catches(tracked, b[j].typ)
  411. else:
  412. assert b.kind == nkFinally
  413. # Add any other exception raised in the except bodies
  414. for i in 1..<n.len:
  415. let b = n[i]
  416. if b.kind == nkExceptBranch:
  417. setLen(tracked.init, oldState)
  418. track(tracked, b[^1])
  419. for i in oldState..<tracked.init.len:
  420. addToIntersection(inter, tracked.init[i])
  421. else:
  422. setLen(tracked.init, oldState)
  423. track(tracked, b[^1])
  424. hasFinally = true
  425. tracked.bottom = oldBottom
  426. dec tracked.inExceptOrFinallyStmt
  427. if not hasFinally:
  428. setLen(tracked.init, oldState)
  429. for id, count in items(inter):
  430. if count == branches: tracked.init.add id
  431. proc isIndirectCall(tracked: PEffects; n: PNode): bool =
  432. # we don't count f(...) as an indirect call if 'f' is an parameter.
  433. # Instead we track expressions of type tyProc too. See the manual for
  434. # details:
  435. if n.kind != nkSym:
  436. result = true
  437. elif n.sym.kind == skParam:
  438. if laxEffects notin tracked.c.config.legacyFeatures:
  439. if tracked.owner == n.sym.owner and sfEffectsDelayed in n.sym.flags:
  440. result = false # it is not a harmful call
  441. else:
  442. result = true
  443. else:
  444. result = tracked.owner != n.sym.owner or tracked.owner == nil
  445. elif n.sym.kind notin routineKinds:
  446. result = true
  447. proc isForwardedProc(n: PNode): bool =
  448. result = n.kind == nkSym and sfForward in n.sym.flags
  449. proc trackPragmaStmt(tracked: PEffects, n: PNode) =
  450. for i in 0..<n.len:
  451. var it = n[i]
  452. let pragma = whichPragma(it)
  453. if pragma == wEffects:
  454. # list the computed effects up to here:
  455. listEffects(tracked)
  456. template notGcSafe(t): untyped = {tfGcSafe, tfNoSideEffect} * t.flags == {}
  457. proc importedFromC(n: PNode): bool =
  458. # when imported from C, we assume GC-safety.
  459. result = n.kind == nkSym and sfImportc in n.sym.flags
  460. proc propagateEffects(tracked: PEffects, n: PNode, s: PSym) =
  461. let pragma = s.ast[pragmasPos]
  462. let spec = effectSpec(pragma, wRaises)
  463. mergeRaises(tracked, spec, n)
  464. let tagSpec = effectSpec(pragma, wTags)
  465. mergeTags(tracked, tagSpec, n)
  466. if notGcSafe(s.typ) and sfImportc notin s.flags:
  467. if tracked.config.hasWarn(warnGcUnsafe): warnAboutGcUnsafe(n, tracked.config)
  468. markGcUnsafe(tracked, s)
  469. if tfNoSideEffect notin s.typ.flags:
  470. markSideEffect(tracked, s, n.info)
  471. proc procVarCheck(n: PNode; conf: ConfigRef) =
  472. if n.kind in nkSymChoices:
  473. for x in n: procVarCheck(x, conf)
  474. elif n.kind == nkSym and n.sym.magic != mNone and n.sym.kind in routineKinds:
  475. localError(conf, n.info, ("'$1' is a built-in and cannot be used as " &
  476. "a first-class procedure") % n.sym.name.s)
  477. proc notNilCheck(tracked: PEffects, n: PNode, paramType: PType) =
  478. let n = n.skipConv
  479. if paramType.isNil or paramType.kind != tyTypeDesc:
  480. procVarCheck skipConvCastAndClosure(n), tracked.config
  481. #elif n.kind in nkSymChoices:
  482. # echo "came here"
  483. let paramType = paramType.skipTypesOrNil(abstractInst)
  484. if paramType != nil and tfNotNil in paramType.flags and n.typ != nil:
  485. let ntyp = n.typ.skipTypesOrNil({tyVar, tyLent, tySink})
  486. if ntyp != nil and tfNotNil notin ntyp.flags:
  487. if isAddrNode(n):
  488. # addr(x[]) can't be proven, but addr(x) can:
  489. if not containsNode(n, {nkDerefExpr, nkHiddenDeref}): return
  490. elif (n.kind == nkSym and n.sym.kind in routineKinds) or
  491. (n.kind in procDefs+{nkObjConstr, nkBracket, nkClosure, nkStrLit..nkTripleStrLit}) or
  492. (n.kind in nkCallKinds and n[0].kind == nkSym and n[0].sym.magic == mArrToSeq) or
  493. n.typ.kind == tyTypeDesc:
  494. # 'p' is not nil obviously:
  495. return
  496. case impliesNotNil(tracked.guards, n)
  497. of impUnknown:
  498. message(tracked.config, n.info, errGenerated,
  499. "cannot prove '$1' is not nil" % n.renderTree)
  500. of impNo:
  501. message(tracked.config, n.info, errGenerated,
  502. "'$1' is provably nil" % n.renderTree)
  503. of impYes: discard
  504. proc assumeTheWorst(tracked: PEffects; n: PNode; op: PType) =
  505. addRaiseEffect(tracked, createRaise(tracked.graph, n), nil)
  506. addTag(tracked, createTag(tracked.graph, n), nil)
  507. proc isOwnedProcVar(tracked: PEffects; n: PNode): bool =
  508. # XXX prove the soundness of this effect system rule
  509. result = n.kind == nkSym and n.sym.kind == skParam and
  510. tracked.owner == n.sym.owner
  511. #if result and sfPolymorphic notin n.sym.flags:
  512. # echo tracked.config $ n.info, " different here!"
  513. if laxEffects notin tracked.c.config.legacyFeatures:
  514. result = result and sfEffectsDelayed in n.sym.flags
  515. proc isNoEffectList(n: PNode): bool {.inline.} =
  516. assert n.kind == nkEffectList
  517. n.len == 0 or (n[tagEffects] == nil and n[exceptionEffects] == nil and n[forbiddenEffects] == nil)
  518. proc isTrival(caller: PNode): bool {.inline.} =
  519. result = caller.kind == nkSym and caller.sym.magic in {mEqProc, mIsNil, mMove, mWasMoved, mSwap}
  520. proc trackOperandForIndirectCall(tracked: PEffects, n: PNode, formals: PType; argIndex: int; caller: PNode) =
  521. let a = skipConvCastAndClosure(n)
  522. let op = a.typ
  523. let param = if formals != nil and argIndex < formals.len and formals.n != nil: formals.n[argIndex].sym else: nil
  524. # assume indirect calls are taken here:
  525. if op != nil and op.kind == tyProc and n.skipConv.kind != nkNilLit and
  526. not isTrival(caller) and
  527. ((param != nil and sfEffectsDelayed in param.flags) or laxEffects in tracked.c.config.legacyFeatures):
  528. internalAssert tracked.config, op.n[0].kind == nkEffectList
  529. var effectList = op.n[0]
  530. var s = n.skipConv
  531. if s.kind == nkCast and s[1].typ.kind == tyProc:
  532. s = s[1]
  533. if s.kind == nkSym and s.sym.kind in routineKinds and isNoEffectList(effectList):
  534. propagateEffects(tracked, n, s.sym)
  535. elif isNoEffectList(effectList):
  536. if isForwardedProc(n):
  537. # we have no explicit effects but it's a forward declaration and so it's
  538. # stated there are no additional effects, so simply propagate them:
  539. propagateEffects(tracked, n, n.sym)
  540. elif not isOwnedProcVar(tracked, a):
  541. # we have no explicit effects so assume the worst:
  542. assumeTheWorst(tracked, n, op)
  543. # assume GcUnsafe unless in its type; 'forward' does not matter:
  544. if notGcSafe(op) and not isOwnedProcVar(tracked, a):
  545. if tracked.config.hasWarn(warnGcUnsafe): warnAboutGcUnsafe(n, tracked.config)
  546. markGcUnsafe(tracked, a)
  547. elif tfNoSideEffect notin op.flags and not isOwnedProcVar(tracked, a):
  548. markSideEffect(tracked, a, n.info)
  549. else:
  550. mergeRaises(tracked, effectList[exceptionEffects], n)
  551. mergeTags(tracked, effectList[tagEffects], n)
  552. if notGcSafe(op):
  553. if tracked.config.hasWarn(warnGcUnsafe): warnAboutGcUnsafe(n, tracked.config)
  554. markGcUnsafe(tracked, a)
  555. elif tfNoSideEffect notin op.flags:
  556. markSideEffect(tracked, a, n.info)
  557. let paramType = if formals != nil and argIndex < formals.len: formals[argIndex] else: nil
  558. if paramType != nil and paramType.kind in {tyVar}:
  559. invalidateFacts(tracked.guards, n)
  560. if n.kind == nkSym and isLocalVar(tracked, n.sym):
  561. makeVolatile(tracked, n.sym)
  562. if paramType != nil and paramType.kind == tyProc and tfGcSafe in paramType.flags:
  563. let argtype = skipTypes(a.typ, abstractInst)
  564. # XXX figure out why this can be a non tyProc here. See httpclient.nim for an
  565. # example that triggers it.
  566. if argtype.kind == tyProc and notGcSafe(argtype) and not tracked.inEnforcedGcSafe:
  567. localError(tracked.config, n.info, $n & " is not GC safe")
  568. notNilCheck(tracked, n, paramType)
  569. proc breaksBlock(n: PNode): bool =
  570. # semantic check doesn't allow statements after raise, break, return or
  571. # call to noreturn proc, so it is safe to check just the last statements
  572. var it = n
  573. while it.kind in {nkStmtList, nkStmtListExpr} and it.len > 0:
  574. it = it.lastSon
  575. result = it.kind in {nkBreakStmt, nkReturnStmt, nkRaiseStmt} or
  576. it.kind in nkCallKinds and it[0].kind == nkSym and sfNoReturn in it[0].sym.flags
  577. proc trackCase(tracked: PEffects, n: PNode) =
  578. track(tracked, n[0])
  579. inc tracked.inIfStmt
  580. let oldState = tracked.init.len
  581. let oldFacts = tracked.guards.s.len
  582. let stringCase = n[0].typ != nil and skipTypes(n[0].typ,
  583. abstractVarRange-{tyTypeDesc}).kind in {tyFloat..tyFloat128, tyString, tyCstring}
  584. let interesting = not stringCase and interestingCaseExpr(n[0]) and
  585. (tracked.config.hasWarn(warnProveField) or strictCaseObjects in tracked.c.features)
  586. var inter: TIntersection = @[]
  587. var toCover = 0
  588. for i in 1..<n.len:
  589. let branch = n[i]
  590. setLen(tracked.init, oldState)
  591. if interesting:
  592. setLen(tracked.guards.s, oldFacts)
  593. addCaseBranchFacts(tracked.guards, n, i)
  594. for i in 0..<branch.len:
  595. track(tracked, branch[i])
  596. if not breaksBlock(branch.lastSon): inc toCover
  597. for i in oldState..<tracked.init.len:
  598. addToIntersection(inter, tracked.init[i])
  599. setLen(tracked.init, oldState)
  600. if not stringCase or lastSon(n).kind == nkElse:
  601. for id, count in items(inter):
  602. if count >= toCover: tracked.init.add id
  603. # else we can't merge
  604. setLen(tracked.guards.s, oldFacts)
  605. dec tracked.inIfStmt
  606. proc trackIf(tracked: PEffects, n: PNode) =
  607. track(tracked, n[0][0])
  608. inc tracked.inIfStmt
  609. let oldFacts = tracked.guards.s.len
  610. addFact(tracked.guards, n[0][0])
  611. let oldState = tracked.init.len
  612. var inter: TIntersection = @[]
  613. var toCover = 0
  614. track(tracked, n[0][1])
  615. if not breaksBlock(n[0][1]): inc toCover
  616. for i in oldState..<tracked.init.len:
  617. addToIntersection(inter, tracked.init[i])
  618. for i in 1..<n.len:
  619. let branch = n[i]
  620. setLen(tracked.guards.s, oldFacts)
  621. for j in 0..i-1:
  622. addFactNeg(tracked.guards, n[j][0])
  623. if branch.len > 1:
  624. addFact(tracked.guards, branch[0])
  625. setLen(tracked.init, oldState)
  626. for i in 0..<branch.len:
  627. track(tracked, branch[i])
  628. if not breaksBlock(branch.lastSon): inc toCover
  629. for i in oldState..<tracked.init.len:
  630. addToIntersection(inter, tracked.init[i])
  631. setLen(tracked.init, oldState)
  632. if lastSon(n).len == 1:
  633. for id, count in items(inter):
  634. if count >= toCover: tracked.init.add id
  635. # else we can't merge as it is not exhaustive
  636. setLen(tracked.guards.s, oldFacts)
  637. dec tracked.inIfStmt
  638. proc trackBlock(tracked: PEffects, n: PNode) =
  639. if n.kind in {nkStmtList, nkStmtListExpr}:
  640. var oldState = -1
  641. for i in 0..<n.len:
  642. if hasSubnodeWith(n[i], nkBreakStmt):
  643. # block:
  644. # x = def
  645. # if ...: ... break # some nested break
  646. # y = def
  647. # --> 'y' not defined after block!
  648. if oldState < 0: oldState = tracked.init.len
  649. track(tracked, n[i])
  650. if oldState > 0: setLen(tracked.init, oldState)
  651. else:
  652. track(tracked, n)
  653. proc cstringCheck(tracked: PEffects; n: PNode) =
  654. if n[0].typ.kind == tyCstring and (let a = skipConv(n[1]);
  655. a.typ.kind == tyString and a.kind notin {nkStrLit..nkTripleStrLit}):
  656. message(tracked.config, n.info, warnUnsafeCode, renderTree(n))
  657. proc patchResult(c: PEffects; n: PNode) =
  658. if n.kind == nkSym and n.sym.kind == skResult:
  659. let fn = c.owner
  660. if fn != nil and fn.kind in routineKinds and fn.ast != nil and resultPos < fn.ast.len:
  661. n.sym = fn.ast[resultPos].sym
  662. else:
  663. localError(c.config, n.info, "routine has no return type, but .requires contains 'result'")
  664. else:
  665. for i in 0..<safeLen(n):
  666. patchResult(c, n[i])
  667. proc checkLe(c: PEffects; a, b: PNode) =
  668. case proveLe(c.guards, a, b)
  669. of impUnknown:
  670. #for g in c.guards.s:
  671. # if g != nil: echo "I Know ", g
  672. message(c.config, a.info, warnStaticIndexCheck,
  673. "cannot prove: " & $a & " <= " & $b)
  674. of impYes:
  675. discard
  676. of impNo:
  677. message(c.config, a.info, warnStaticIndexCheck,
  678. "can prove: " & $a & " > " & $b)
  679. proc checkBounds(c: PEffects; arr, idx: PNode) =
  680. checkLe(c, lowBound(c.config, arr), idx)
  681. checkLe(c, idx, highBound(c.config, arr, c.guards.g.operators))
  682. proc checkRange(c: PEffects; value: PNode; typ: PType) =
  683. let t = typ.skipTypes(abstractInst - {tyRange})
  684. if t.kind == tyRange:
  685. let lowBound = copyTree(t.n[0])
  686. lowBound.info = value.info
  687. let highBound = copyTree(t.n[1])
  688. highBound.info = value.info
  689. checkLe(c, lowBound, value)
  690. checkLe(c, value, highBound)
  691. #[
  692. proc passedToEffectsDelayedParam(tracked: PEffects; n: PNode) =
  693. let t = n.typ.skipTypes(abstractInst)
  694. if t.kind == tyProc:
  695. if n.kind == nkSym and tracked.owner == n.sym.owner and sfEffectsDelayed in n.sym.flags:
  696. discard "the arg is itself a delayed parameter, so do nothing"
  697. else:
  698. var effectList = t.n[0]
  699. if effectList.len == effectListLen:
  700. mergeRaises(tracked, effectList[exceptionEffects], n)
  701. mergeTags(tracked, effectList[tagEffects], n)
  702. if not importedFromC(n):
  703. if notGcSafe(t):
  704. if tracked.config.hasWarn(warnGcUnsafe): warnAboutGcUnsafe(n, tracked.config)
  705. markGcUnsafe(tracked, n)
  706. if tfNoSideEffect notin t.flags:
  707. markSideEffect(tracked, n, n.info)
  708. ]#
  709. proc checkForSink(tracked: PEffects; n: PNode) =
  710. if tracked.inIfStmt == 0:
  711. checkForSink(tracked.config, tracked.c.idgen, tracked.owner, n)
  712. proc trackCall(tracked: PEffects; n: PNode) =
  713. template gcsafeAndSideeffectCheck() =
  714. if notGcSafe(op) and not importedFromC(a):
  715. # and it's not a recursive call:
  716. if not (a.kind == nkSym and a.sym == tracked.owner):
  717. if tracked.config.hasWarn(warnGcUnsafe): warnAboutGcUnsafe(n, tracked.config)
  718. markGcUnsafe(tracked, a)
  719. if tfNoSideEffect notin op.flags and not importedFromC(a):
  720. # and it's not a recursive call:
  721. if not (a.kind == nkSym and a.sym == tracked.owner):
  722. markSideEffect(tracked, a, n.info)
  723. # p's effects are ours too:
  724. var a = n[0]
  725. #if canRaise(a):
  726. # echo "this can raise ", tracked.config $ n.info
  727. let op = a.typ
  728. if n.typ != nil:
  729. if tracked.owner.kind != skMacro and n.typ.skipTypes(abstractVar).kind != tyOpenArray:
  730. createTypeBoundOps(tracked, n.typ, n.info)
  731. if getConstExpr(tracked.ownerModule, n, tracked.c.idgen, tracked.graph) == nil:
  732. if a.kind == nkCast and a[1].typ.kind == tyProc:
  733. a = a[1]
  734. # XXX: in rare situations, templates and macros will reach here after
  735. # calling getAst(templateOrMacro()). Currently, templates and macros
  736. # are indistinguishable from normal procs (both have tyProc type) and
  737. # we can detect them only by checking for attached nkEffectList.
  738. if op != nil and op.kind == tyProc and op.n[0].kind == nkEffectList:
  739. if a.kind == nkSym:
  740. if a.sym == tracked.owner: tracked.isRecursive = true
  741. # even for recursive calls we need to check the lock levels (!):
  742. if sfSideEffect in a.sym.flags: markSideEffect(tracked, a, n.info)
  743. else:
  744. discard
  745. var effectList = op.n[0]
  746. if a.kind == nkSym and a.sym.kind == skMethod:
  747. if {sfBase, sfThread} * a.sym.flags == {sfBase}:
  748. if tracked.config.hasWarn(warnGcUnsafe): warnAboutGcUnsafe(n, tracked.config)
  749. markGcUnsafe(tracked, a)
  750. propagateEffects(tracked, n, a.sym)
  751. elif isNoEffectList(effectList):
  752. if isForwardedProc(a):
  753. propagateEffects(tracked, n, a.sym)
  754. elif isIndirectCall(tracked, a):
  755. assumeTheWorst(tracked, n, op)
  756. gcsafeAndSideeffectCheck()
  757. else:
  758. if laxEffects notin tracked.c.config.legacyFeatures and a.kind == nkSym and
  759. a.sym.kind in routineKinds:
  760. propagateEffects(tracked, n, a.sym)
  761. else:
  762. mergeRaises(tracked, effectList[exceptionEffects], n)
  763. mergeTags(tracked, effectList[tagEffects], n)
  764. gcsafeAndSideeffectCheck()
  765. if a.kind != nkSym or a.sym.magic notin {mNBindSym, mFinished, mExpandToAst, mQuoteAst}:
  766. for i in 1..<n.len:
  767. trackOperandForIndirectCall(tracked, n[i], op, i, a)
  768. if a.kind == nkSym and a.sym.magic in {mNew, mNewFinalize, mNewSeq}:
  769. # may not look like an assignment, but it is:
  770. let arg = n[1]
  771. initVarViaNew(tracked, arg)
  772. if arg.typ.len != 0 and {tfRequiresInit} * arg.typ.lastSon.flags != {}:
  773. if a.sym.magic == mNewSeq and n[2].kind in {nkCharLit..nkUInt64Lit} and
  774. n[2].intVal == 0:
  775. # var s: seq[notnil]; newSeq(s, 0) is a special case!
  776. discard
  777. else:
  778. message(tracked.config, arg.info, warnProveInit, $arg)
  779. # check required for 'nim check':
  780. if n[1].typ.len > 0:
  781. createTypeBoundOps(tracked, n[1].typ.lastSon, n.info)
  782. createTypeBoundOps(tracked, n[1].typ, n.info)
  783. # new(x, finalizer): Problem: how to move finalizer into 'createTypeBoundOps'?
  784. elif a.kind == nkSym and a.sym.magic in {mArrGet, mArrPut} and
  785. optStaticBoundsCheck in tracked.currOptions:
  786. checkBounds(tracked, n[1], n[2])
  787. if a.kind != nkSym or a.sym.magic notin {mRunnableExamples, mNBindSym, mExpandToAst, mQuoteAst}:
  788. for i in 0..<n.safeLen:
  789. track(tracked, n[i])
  790. if a.kind == nkSym and a.sym.name.s.len > 0 and a.sym.name.s[0] == '=' and
  791. tracked.owner.kind != skMacro:
  792. var opKind = find(AttachedOpToStr, a.sym.name.s.normalize)
  793. if a.sym.name.s == "=": opKind = attachedAsgn.int
  794. if opKind != -1:
  795. # rebind type bounds operations after createTypeBoundOps call
  796. let t = n[1].typ.skipTypes({tyAlias, tyVar})
  797. if a.sym != getAttachedOp(tracked.graph, t, TTypeAttachedOp(opKind)):
  798. createTypeBoundOps(tracked, t, n.info)
  799. let op = getAttachedOp(tracked.graph, t, TTypeAttachedOp(opKind))
  800. if op != nil:
  801. n[0].sym = op
  802. if op != nil and op.kind == tyProc:
  803. for i in 1..<min(n.safeLen, op.len):
  804. let paramType = op[i]
  805. case paramType.kind
  806. of tySink:
  807. createTypeBoundOps(tracked, paramType[0], n.info)
  808. checkForSink(tracked, n[i])
  809. of tyVar:
  810. tracked.hasDangerousAssign = true
  811. if isOutParam(paramType):
  812. # consider this case: p(out x, x); we want to remark that 'x' is not
  813. # initialized until after the call. Since we do this after we analysed the
  814. # call, this is fine.
  815. initVar(tracked, n[i].skipAddr, false)
  816. else: discard
  817. type
  818. PragmaBlockContext = object
  819. oldLocked: int
  820. enforcedGcSafety, enforceNoSideEffects: bool
  821. oldExc, oldTags, oldForbids: int
  822. exc, tags, forbids: PNode
  823. proc createBlockContext(tracked: PEffects): PragmaBlockContext =
  824. var oldForbidsLen = 0
  825. if tracked.forbids != nil: oldForbidsLen = tracked.forbids.len
  826. result = PragmaBlockContext(oldLocked: tracked.locked.len,
  827. enforcedGcSafety: false, enforceNoSideEffects: false,
  828. oldExc: tracked.exc.len, oldTags: tracked.tags.len,
  829. oldForbids: oldForbidsLen)
  830. proc applyBlockContext(tracked: PEffects, bc: PragmaBlockContext) =
  831. if bc.enforcedGcSafety: tracked.inEnforcedGcSafe = true
  832. if bc.enforceNoSideEffects: tracked.inEnforcedNoSideEffects = true
  833. proc unapplyBlockContext(tracked: PEffects; bc: PragmaBlockContext) =
  834. if bc.enforcedGcSafety: tracked.inEnforcedGcSafe = false
  835. if bc.enforceNoSideEffects: tracked.inEnforcedNoSideEffects = false
  836. setLen(tracked.locked, bc.oldLocked)
  837. if bc.exc != nil:
  838. # beware that 'raises: []' is very different from not saying
  839. # anything about 'raises' in the 'cast' at all. Same applies for 'tags'.
  840. setLen(tracked.exc.sons, bc.oldExc)
  841. for e in bc.exc:
  842. addRaiseEffect(tracked, e, e)
  843. if bc.tags != nil:
  844. setLen(tracked.tags.sons, bc.oldTags)
  845. for t in bc.tags:
  846. addTag(tracked, t, t)
  847. if bc.forbids != nil:
  848. setLen(tracked.forbids.sons, bc.oldForbids)
  849. for t in bc.forbids:
  850. addNotTag(tracked, t, t)
  851. proc castBlock(tracked: PEffects, pragma: PNode, bc: var PragmaBlockContext) =
  852. case whichPragma(pragma)
  853. of wGcSafe:
  854. bc.enforcedGcSafety = true
  855. of wNoSideEffect:
  856. bc.enforceNoSideEffects = true
  857. of wTags:
  858. let n = pragma[1]
  859. if n.kind in {nkCurly, nkBracket}:
  860. bc.tags = n
  861. else:
  862. bc.tags = newNodeI(nkArgList, pragma.info)
  863. bc.tags.add n
  864. of wForbids:
  865. let n = pragma[1]
  866. if n.kind in {nkCurly, nkBracket}:
  867. bc.forbids = n
  868. else:
  869. bc.forbids = newNodeI(nkArgList, pragma.info)
  870. bc.forbids.add n
  871. of wRaises:
  872. let n = pragma[1]
  873. if n.kind in {nkCurly, nkBracket}:
  874. bc.exc = n
  875. else:
  876. bc.exc = newNodeI(nkArgList, pragma.info)
  877. bc.exc.add n
  878. of wUncheckedAssign:
  879. discard "handled in sempass1"
  880. else:
  881. localError(tracked.config, pragma.info,
  882. "invalid pragma block: " & $pragma)
  883. proc trackInnerProc(tracked: PEffects, n: PNode) =
  884. case n.kind
  885. of nkSym:
  886. let s = n.sym
  887. if s.kind == skParam and s.owner == tracked.owner:
  888. tracked.escapingParams.incl s.id
  889. of nkNone..pred(nkSym), succ(nkSym)..nkNilLit:
  890. discard
  891. of nkProcDef, nkConverterDef, nkMethodDef, nkIteratorDef, nkLambda, nkFuncDef, nkDo:
  892. if n[0].kind == nkSym and n[0].sym.ast != nil:
  893. trackInnerProc(tracked, getBody(tracked.graph, n[0].sym))
  894. of nkTypeSection, nkMacroDef, nkTemplateDef, nkError,
  895. nkConstSection, nkConstDef, nkIncludeStmt, nkImportStmt,
  896. nkExportStmt, nkPragma, nkCommentStmt, nkBreakState,
  897. nkTypeOfExpr, nkMixinStmt, nkBindStmt:
  898. discard
  899. else:
  900. for ch in n: trackInnerProc(tracked, ch)
  901. proc allowCStringConv(n: PNode): bool =
  902. case n.kind
  903. of nkStrLit..nkTripleStrLit: result = true
  904. of nkSym: result = n.sym.kind in {skConst, skParam}
  905. of nkAddr: result = isCharArrayPtr(n.typ, true)
  906. of nkCallKinds:
  907. result = isCharArrayPtr(n.typ, n[0].kind == nkSym and n[0].sym.magic == mAddr)
  908. else: result = isCharArrayPtr(n.typ, false)
  909. proc track(tracked: PEffects, n: PNode) =
  910. case n.kind
  911. of nkSym:
  912. useVar(tracked, n)
  913. if n.sym.typ != nil and tfHasAsgn in n.sym.typ.flags:
  914. tracked.owner.flags.incl sfInjectDestructors
  915. # bug #15038: ensure consistency
  916. if not hasDestructor(n.typ) and sameType(n.typ, n.sym.typ): n.typ = n.sym.typ
  917. of nkHiddenAddr, nkAddr:
  918. if n[0].kind == nkSym and isLocalVar(tracked, n[0].sym):
  919. useVarNoInitCheck(tracked, n[0], n[0].sym)
  920. else:
  921. track(tracked, n[0])
  922. of nkRaiseStmt:
  923. if n[0].kind != nkEmpty:
  924. n[0].info = n.info
  925. #throws(tracked.exc, n[0])
  926. addRaiseEffect(tracked, n[0], n)
  927. for i in 0..<n.safeLen:
  928. track(tracked, n[i])
  929. createTypeBoundOps(tracked, n[0].typ, n.info)
  930. else:
  931. # A `raise` with no arguments means we're going to re-raise the exception
  932. # being handled or, if outside of an `except` block, a `ReraiseDefect`.
  933. # Here we add a `Exception` tag in order to cover both the cases.
  934. addRaiseEffect(tracked, createRaise(tracked.graph, n), nil)
  935. of nkCallKinds:
  936. trackCall(tracked, n)
  937. of nkDotExpr:
  938. guardDotAccess(tracked, n)
  939. for i in 0..<n.len: track(tracked, n[i])
  940. of nkCheckedFieldExpr:
  941. track(tracked, n[0])
  942. if tracked.config.hasWarn(warnProveField) or strictCaseObjects in tracked.c.features:
  943. checkFieldAccess(tracked.guards, n, tracked.config, strictCaseObjects in tracked.c.features)
  944. of nkTryStmt: trackTryStmt(tracked, n)
  945. of nkPragma: trackPragmaStmt(tracked, n)
  946. of nkAsgn, nkFastAsgn, nkSinkAsgn:
  947. track(tracked, n[1])
  948. initVar(tracked, n[0], volatileCheck=true)
  949. invalidateFacts(tracked.guards, n[0])
  950. inc tracked.leftPartOfAsgn
  951. track(tracked, n[0])
  952. dec tracked.leftPartOfAsgn
  953. addAsgnFact(tracked.guards, n[0], n[1])
  954. notNilCheck(tracked, n[1], n[0].typ)
  955. when false: cstringCheck(tracked, n)
  956. if tracked.owner.kind != skMacro and n[0].typ.kind notin {tyOpenArray, tyVarargs}:
  957. createTypeBoundOps(tracked, n[0].typ, n.info)
  958. if n[0].kind != nkSym or not isLocalVar(tracked, n[0].sym):
  959. checkForSink(tracked, n[1])
  960. if not tracked.hasDangerousAssign and n[0].kind != nkSym:
  961. tracked.hasDangerousAssign = true
  962. of nkVarSection, nkLetSection:
  963. for child in n:
  964. let last = lastSon(child)
  965. if last.kind != nkEmpty: track(tracked, last)
  966. if tracked.owner.kind != skMacro:
  967. if child.kind == nkVarTuple:
  968. createTypeBoundOps(tracked, child[^1].typ, child.info)
  969. for i in 0..<child.len-2:
  970. createTypeBoundOps(tracked, child[i].typ, child.info)
  971. else:
  972. createTypeBoundOps(tracked, skipPragmaExpr(child[0]).typ, child.info)
  973. if child.kind == nkIdentDefs:
  974. for i in 0..<child.len-2:
  975. let a = skipPragmaExpr(child[i])
  976. varDecl(tracked, a)
  977. if last.kind != nkEmpty:
  978. initVar(tracked, a, volatileCheck=false)
  979. addAsgnFact(tracked.guards, a, last)
  980. notNilCheck(tracked, last, a.typ)
  981. elif child.kind == nkVarTuple:
  982. for i in 0..<child.len-1:
  983. if child[i].kind == nkEmpty or
  984. child[i].kind == nkSym and child[i].sym.name.s == "_":
  985. continue
  986. varDecl(tracked, child[i])
  987. if last.kind != nkEmpty:
  988. initVar(tracked, child[i], volatileCheck=false)
  989. if last.kind in {nkPar, nkTupleConstr}:
  990. addAsgnFact(tracked.guards, child[i], last[i])
  991. notNilCheck(tracked, last[i], child[i].typ)
  992. # since 'var (a, b): T = ()' is not even allowed, there is always type
  993. # inference for (a, b) and thus no nil checking is necessary.
  994. of nkConstSection:
  995. for child in n:
  996. let last = lastSon(child)
  997. track(tracked, last)
  998. of nkCaseStmt: trackCase(tracked, n)
  999. of nkWhen, nkIfStmt, nkIfExpr: trackIf(tracked, n)
  1000. of nkBlockStmt, nkBlockExpr: trackBlock(tracked, n[1])
  1001. of nkWhileStmt:
  1002. # 'while true' loop?
  1003. inc tracked.currentBlock
  1004. if isTrue(n[0]):
  1005. trackBlock(tracked, n[1])
  1006. else:
  1007. # loop may never execute:
  1008. let oldState = tracked.init.len
  1009. let oldFacts = tracked.guards.s.len
  1010. addFact(tracked.guards, n[0])
  1011. track(tracked, n[0])
  1012. track(tracked, n[1])
  1013. setLen(tracked.init, oldState)
  1014. setLen(tracked.guards.s, oldFacts)
  1015. dec tracked.currentBlock
  1016. of nkForStmt, nkParForStmt:
  1017. # we are very conservative here and assume the loop is never executed:
  1018. inc tracked.currentBlock
  1019. let oldState = tracked.init.len
  1020. let oldFacts = tracked.guards.s.len
  1021. let iterCall = n[n.len-2]
  1022. if optStaticBoundsCheck in tracked.currOptions and iterCall.kind in nkCallKinds:
  1023. let op = iterCall[0]
  1024. if op.kind == nkSym and fromSystem(op.sym):
  1025. let iterVar = n[0]
  1026. case op.sym.name.s
  1027. of "..", "countup", "countdown":
  1028. let lower = iterCall[1]
  1029. let upper = iterCall[2]
  1030. # for i in 0..n means 0 <= i and i <= n. Countdown is
  1031. # the same since only the iteration direction changes.
  1032. addFactLe(tracked.guards, lower, iterVar)
  1033. addFactLe(tracked.guards, iterVar, upper)
  1034. of "..<":
  1035. let lower = iterCall[1]
  1036. let upper = iterCall[2]
  1037. addFactLe(tracked.guards, lower, iterVar)
  1038. addFactLt(tracked.guards, iterVar, upper)
  1039. else: discard
  1040. for i in 0..<n.len-2:
  1041. let it = n[i]
  1042. track(tracked, it)
  1043. if tracked.owner.kind != skMacro:
  1044. if it.kind == nkVarTuple:
  1045. for x in it:
  1046. createTypeBoundOps(tracked, x.typ, x.info)
  1047. else:
  1048. createTypeBoundOps(tracked, it.typ, it.info)
  1049. let loopBody = n[^1]
  1050. if tracked.owner.kind != skMacro and iterCall.safeLen > 1:
  1051. # XXX this is a bit hacky:
  1052. if iterCall[1].typ != nil and iterCall[1].typ.skipTypes(abstractVar).kind notin {tyVarargs, tyOpenArray}:
  1053. createTypeBoundOps(tracked, iterCall[1].typ, iterCall[1].info)
  1054. track(tracked, iterCall)
  1055. track(tracked, loopBody)
  1056. setLen(tracked.init, oldState)
  1057. setLen(tracked.guards.s, oldFacts)
  1058. dec tracked.currentBlock
  1059. of nkObjConstr:
  1060. when false: track(tracked, n[0])
  1061. let oldFacts = tracked.guards.s.len
  1062. for i in 1..<n.len:
  1063. let x = n[i]
  1064. track(tracked, x)
  1065. if x[0].kind == nkSym and sfDiscriminant in x[0].sym.flags:
  1066. addDiscriminantFact(tracked.guards, x)
  1067. if tracked.owner.kind != skMacro:
  1068. createTypeBoundOps(tracked, x[1].typ, n.info)
  1069. if x.kind == nkExprColonExpr:
  1070. if x[0].kind == nkSym:
  1071. notNilCheck(tracked, x[1], x[0].sym.typ)
  1072. checkForSink(tracked, x[1])
  1073. else:
  1074. checkForSink(tracked, x)
  1075. setLen(tracked.guards.s, oldFacts)
  1076. if tracked.owner.kind != skMacro:
  1077. # XXX n.typ can be nil in runnableExamples, we need to do something about it.
  1078. if n.typ != nil and n.typ.skipTypes(abstractInst).kind == tyRef:
  1079. createTypeBoundOps(tracked, n.typ.lastSon, n.info)
  1080. createTypeBoundOps(tracked, n.typ, n.info)
  1081. of nkTupleConstr:
  1082. for i in 0..<n.len:
  1083. track(tracked, n[i])
  1084. if tracked.owner.kind != skMacro:
  1085. if n[i].kind == nkExprColonExpr:
  1086. createTypeBoundOps(tracked, n[i][0].typ, n.info)
  1087. else:
  1088. createTypeBoundOps(tracked, n[i].typ, n.info)
  1089. checkForSink(tracked, n[i])
  1090. of nkPragmaBlock:
  1091. let pragmaList = n[0]
  1092. var bc = createBlockContext(tracked)
  1093. for i in 0..<pragmaList.len:
  1094. let pragma = whichPragma(pragmaList[i])
  1095. case pragma
  1096. of wLocks:
  1097. lockLocations(tracked, pragmaList[i])
  1098. of wGcSafe:
  1099. bc.enforcedGcSafety = true
  1100. of wNoSideEffect:
  1101. bc.enforceNoSideEffects = true
  1102. of wCast:
  1103. castBlock(tracked, pragmaList[i][1], bc)
  1104. else:
  1105. discard
  1106. applyBlockContext(tracked, bc)
  1107. track(tracked, n.lastSon)
  1108. unapplyBlockContext(tracked, bc)
  1109. of nkProcDef, nkConverterDef, nkMethodDef, nkIteratorDef, nkLambda, nkFuncDef, nkDo:
  1110. if n[0].kind == nkSym and n[0].sym.ast != nil:
  1111. trackInnerProc(tracked, getBody(tracked.graph, n[0].sym))
  1112. of nkTypeSection, nkMacroDef, nkTemplateDef:
  1113. discard
  1114. of nkCast:
  1115. if n.len == 2:
  1116. track(tracked, n[1])
  1117. if tracked.owner.kind != skMacro:
  1118. createTypeBoundOps(tracked, n.typ, n.info)
  1119. of nkHiddenStdConv, nkHiddenSubConv, nkConv:
  1120. if n.kind in {nkHiddenStdConv, nkHiddenSubConv} and
  1121. n.typ.skipTypes(abstractInst).kind == tyCstring and
  1122. not allowCStringConv(n[1]):
  1123. message(tracked.config, n.info, warnCstringConv,
  1124. "implicit conversion to 'cstring' from a non-const location: $1; this will become a compile time error in the future" %
  1125. $n[1])
  1126. if n.typ.skipTypes(abstractInst).kind == tyCstring and
  1127. isCharArrayPtr(n[1].typ, true):
  1128. message(tracked.config, n.info, warnPtrToCstringConv,
  1129. $n[1].typ)
  1130. let t = n.typ.skipTypes(abstractInst)
  1131. if t.kind == tyEnum:
  1132. if tfEnumHasHoles in t.flags:
  1133. message(tracked.config, n.info, warnHoleEnumConv, "conversion to enum with holes is unsafe: $1" % $n)
  1134. else:
  1135. message(tracked.config, n.info, warnAnyEnumConv, "enum conversion: $1" % $n)
  1136. if n.len == 2:
  1137. track(tracked, n[1])
  1138. if tracked.owner.kind != skMacro:
  1139. createTypeBoundOps(tracked, n.typ, n.info)
  1140. # This is a hacky solution in order to fix bug #13110. Hopefully
  1141. # a better solution will come up eventually.
  1142. if n[1].typ.kind != tyString:
  1143. createTypeBoundOps(tracked, n[1].typ, n[1].info)
  1144. if optStaticBoundsCheck in tracked.currOptions:
  1145. checkRange(tracked, n[1], n.typ)
  1146. of nkObjUpConv, nkObjDownConv, nkChckRange, nkChckRangeF, nkChckRange64:
  1147. if n.len == 1:
  1148. track(tracked, n[0])
  1149. if tracked.owner.kind != skMacro:
  1150. createTypeBoundOps(tracked, n.typ, n.info)
  1151. createTypeBoundOps(tracked, n[0].typ, n[0].info)
  1152. if optStaticBoundsCheck in tracked.currOptions:
  1153. checkRange(tracked, n[0], n.typ)
  1154. of nkBracket:
  1155. for i in 0..<n.safeLen:
  1156. track(tracked, n[i])
  1157. checkForSink(tracked, n[i])
  1158. if tracked.owner.kind != skMacro:
  1159. createTypeBoundOps(tracked, n.typ, n.info)
  1160. of nkBracketExpr:
  1161. if optStaticBoundsCheck in tracked.currOptions and n.len == 2:
  1162. if n[0].typ != nil and skipTypes(n[0].typ, abstractVar).kind != tyTuple:
  1163. checkBounds(tracked, n[0], n[1])
  1164. track(tracked, n[0])
  1165. dec tracked.leftPartOfAsgn
  1166. for i in 1 ..< n.len: track(tracked, n[i])
  1167. inc tracked.leftPartOfAsgn
  1168. of nkError:
  1169. localError(tracked.config, n.info, errorToString(tracked.config, n))
  1170. else:
  1171. for i in 0..<n.safeLen: track(tracked, n[i])
  1172. proc subtypeRelation(g: ModuleGraph; spec, real: PNode): bool =
  1173. if spec.typ.kind == tyOr:
  1174. for t in spec.typ.sons:
  1175. if safeInheritanceDiff(g.excType(real), t) <= 0:
  1176. return true
  1177. else:
  1178. return safeInheritanceDiff(g.excType(real), spec.typ) <= 0
  1179. proc checkRaisesSpec(g: ModuleGraph; emitWarnings: bool; spec, real: PNode, msg: string, hints: bool;
  1180. effectPredicate: proc (g: ModuleGraph; a, b: PNode): bool {.nimcall.};
  1181. hintsArg: PNode = nil; isForbids: bool = false) =
  1182. # check that any real exception is listed in 'spec'; mark those as used;
  1183. # report any unused exception
  1184. var used = initIntSet()
  1185. for r in items(real):
  1186. block search:
  1187. for s in 0..<spec.len:
  1188. if effectPredicate(g, spec[s], r):
  1189. if isForbids: break
  1190. used.incl(s)
  1191. break search
  1192. if isForbids:
  1193. break search
  1194. # XXX call graph analysis would be nice here!
  1195. pushInfoContext(g.config, spec.info)
  1196. var rr = if r.kind == nkRaiseStmt: r[0] else: r
  1197. while rr.kind in {nkStmtList, nkStmtListExpr} and rr.len > 0: rr = rr.lastSon
  1198. message(g.config, r.info, if emitWarnings: warnEffect else: errGenerated,
  1199. renderTree(rr) & " " & msg & typeToString(r.typ))
  1200. popInfoContext(g.config)
  1201. # hint about unnecessarily listed exception types:
  1202. if hints:
  1203. for s in 0..<spec.len:
  1204. if not used.contains(s):
  1205. message(g.config, spec[s].info, hintXCannotRaiseY,
  1206. "'$1' cannot raise '$2'" % [renderTree(hintsArg), renderTree(spec[s])])
  1207. proc checkMethodEffects*(g: ModuleGraph; disp, branch: PSym) =
  1208. ## checks for consistent effects for multi methods.
  1209. let actual = branch.typ.n[0]
  1210. if actual.len != effectListLen: return
  1211. let p = disp.ast[pragmasPos]
  1212. let raisesSpec = effectSpec(p, wRaises)
  1213. if not isNil(raisesSpec):
  1214. checkRaisesSpec(g, false, raisesSpec, actual[exceptionEffects],
  1215. "can raise an unlisted exception: ", hints=off, subtypeRelation)
  1216. let tagsSpec = effectSpec(p, wTags)
  1217. if not isNil(tagsSpec):
  1218. checkRaisesSpec(g, false, tagsSpec, actual[tagEffects],
  1219. "can have an unlisted effect: ", hints=off, subtypeRelation)
  1220. let forbidsSpec = effectSpec(p, wForbids)
  1221. if not isNil(forbidsSpec):
  1222. checkRaisesSpec(g, false, forbidsSpec, actual[tagEffects],
  1223. "has an illegal effect: ", hints=off, subtypeRelation, isForbids=true)
  1224. if sfThread in disp.flags and notGcSafe(branch.typ):
  1225. localError(g.config, branch.info, "base method is GC-safe, but '$1' is not" %
  1226. branch.name.s)
  1227. when defined(drnim):
  1228. if not g.compatibleProps(g, disp.typ, branch.typ):
  1229. localError(g.config, branch.info, "for method '" & branch.name.s &
  1230. "' the `.requires` or `.ensures` properties are incompatible.")
  1231. proc setEffectsForProcType*(g: ModuleGraph; t: PType, n: PNode; s: PSym = nil) =
  1232. var effects = t.n[0]
  1233. if t.kind != tyProc or effects.kind != nkEffectList: return
  1234. if n.kind != nkEmpty:
  1235. internalAssert g.config, effects.len == 0
  1236. newSeq(effects.sons, effectListLen)
  1237. let raisesSpec = effectSpec(n, wRaises)
  1238. if not isNil(raisesSpec):
  1239. effects[exceptionEffects] = raisesSpec
  1240. elif s != nil and (s.magic != mNone or {sfImportc, sfExportc} * s.flags == {sfImportc}):
  1241. effects[exceptionEffects] = newNodeI(nkArgList, effects.info)
  1242. let tagsSpec = effectSpec(n, wTags)
  1243. if not isNil(tagsSpec):
  1244. effects[tagEffects] = tagsSpec
  1245. elif s != nil and (s.magic != mNone or {sfImportc, sfExportc} * s.flags == {sfImportc}):
  1246. effects[tagEffects] = newNodeI(nkArgList, effects.info)
  1247. let forbidsSpec = effectSpec(n, wForbids)
  1248. if not isNil(forbidsSpec):
  1249. effects[forbiddenEffects] = forbidsSpec
  1250. elif s != nil and (s.magic != mNone or {sfImportc, sfExportc} * s.flags == {sfImportc}):
  1251. effects[forbiddenEffects] = newNodeI(nkArgList, effects.info)
  1252. let requiresSpec = propSpec(n, wRequires)
  1253. if not isNil(requiresSpec):
  1254. effects[requiresEffects] = requiresSpec
  1255. let ensuresSpec = propSpec(n, wEnsures)
  1256. if not isNil(ensuresSpec):
  1257. effects[ensuresEffects] = ensuresSpec
  1258. effects[pragmasEffects] = n
  1259. if s != nil and s.magic != mNone:
  1260. if s.magic != mEcho:
  1261. t.flags.incl tfNoSideEffect
  1262. proc rawInitEffects(g: ModuleGraph; effects: PNode) =
  1263. newSeq(effects.sons, effectListLen)
  1264. effects[exceptionEffects] = newNodeI(nkArgList, effects.info)
  1265. effects[tagEffects] = newNodeI(nkArgList, effects.info)
  1266. effects[forbiddenEffects] = newNodeI(nkArgList, effects.info)
  1267. effects[requiresEffects] = g.emptyNode
  1268. effects[ensuresEffects] = g.emptyNode
  1269. effects[pragmasEffects] = g.emptyNode
  1270. proc initEffects(g: ModuleGraph; effects: PNode; s: PSym; t: var TEffects; c: PContext) =
  1271. rawInitEffects(g, effects)
  1272. t.exc = effects[exceptionEffects]
  1273. t.tags = effects[tagEffects]
  1274. t.forbids = effects[forbiddenEffects]
  1275. t.owner = s
  1276. t.ownerModule = s.getModule
  1277. t.init = @[]
  1278. t.guards.s = @[]
  1279. t.guards.g = g
  1280. when defined(drnim):
  1281. t.currOptions = g.config.options + s.options - {optStaticBoundsCheck}
  1282. else:
  1283. t.currOptions = g.config.options + s.options
  1284. t.guards.beSmart = optStaticBoundsCheck in t.currOptions
  1285. t.locked = @[]
  1286. t.graph = g
  1287. t.config = g.config
  1288. t.c = c
  1289. t.currentBlock = 1
  1290. proc hasRealBody(s: PSym): bool =
  1291. ## also handles importc procs with runnableExamples, which requires `=`,
  1292. ## which is not a real implementation, refs #14314
  1293. result = {sfForward, sfImportc} * s.flags == {}
  1294. proc trackProc*(c: PContext; s: PSym, body: PNode) =
  1295. let g = c.graph
  1296. var effects = s.typ.n[0]
  1297. if effects.kind != nkEffectList: return
  1298. # effects already computed?
  1299. if not s.hasRealBody: return
  1300. let emitWarnings = tfEffectSystemWorkaround in s.typ.flags
  1301. if effects.len == effectListLen and not emitWarnings: return
  1302. var inferredEffects = newNodeI(nkEffectList, s.info)
  1303. var t: TEffects
  1304. initEffects(g, inferredEffects, s, t, c)
  1305. rawInitEffects g, effects
  1306. if not isEmptyType(s.typ[0]) and
  1307. s.kind in {skProc, skFunc, skConverter, skMethod}:
  1308. var res = s.ast[resultPos].sym # get result symbol
  1309. t.scopes[res.id] = t.currentBlock
  1310. track(t, body)
  1311. if s.kind != skMacro:
  1312. let params = s.typ.n
  1313. for i in 1..<params.len:
  1314. let param = params[i].sym
  1315. let typ = param.typ
  1316. if isSinkTypeForParam(typ) or
  1317. (t.config.selectedGC in {gcArc, gcOrc} and
  1318. (isClosure(typ.skipTypes(abstractInst)) or param.id in t.escapingParams)):
  1319. createTypeBoundOps(t, typ, param.info)
  1320. if isOutParam(typ) and param.id notin t.init:
  1321. message(g.config, param.info, warnProveInit, param.name.s)
  1322. if not isEmptyType(s.typ[0]) and
  1323. (s.typ[0].requiresInit or s.typ[0].skipTypes(abstractInst).kind == tyVar or
  1324. strictDefs in c.features) and
  1325. s.kind in {skProc, skFunc, skConverter, skMethod} and s.magic == mNone:
  1326. var res = s.ast[resultPos].sym # get result symbol
  1327. if res.id notin t.init:
  1328. message(g.config, body.info, warnProveInit, "result")
  1329. let p = s.ast[pragmasPos]
  1330. let raisesSpec = effectSpec(p, wRaises)
  1331. if not isNil(raisesSpec):
  1332. checkRaisesSpec(g, false, raisesSpec, t.exc, "can raise an unlisted exception: ",
  1333. hints=on, subtypeRelation, hintsArg=s.ast[0])
  1334. # after the check, use the formal spec:
  1335. effects[exceptionEffects] = raisesSpec
  1336. else:
  1337. effects[exceptionEffects] = t.exc
  1338. let tagsSpec = effectSpec(p, wTags)
  1339. if not isNil(tagsSpec):
  1340. checkRaisesSpec(g, false, tagsSpec, t.tags, "can have an unlisted effect: ",
  1341. hints=off, subtypeRelation)
  1342. # after the check, use the formal spec:
  1343. effects[tagEffects] = tagsSpec
  1344. else:
  1345. effects[tagEffects] = t.tags
  1346. let forbidsSpec = effectSpec(p, wForbids)
  1347. if not isNil(forbidsSpec):
  1348. checkRaisesSpec(g, false, forbidsSpec, t.tags, "has an illegal effect: ",
  1349. hints=off, subtypeRelation, isForbids=true)
  1350. # after the check, use the formal spec:
  1351. effects[forbiddenEffects] = forbidsSpec
  1352. else:
  1353. effects[forbiddenEffects] = t.forbids
  1354. let requiresSpec = propSpec(p, wRequires)
  1355. if not isNil(requiresSpec):
  1356. effects[requiresEffects] = requiresSpec
  1357. let ensuresSpec = propSpec(p, wEnsures)
  1358. if not isNil(ensuresSpec):
  1359. patchResult(t, ensuresSpec)
  1360. effects[ensuresEffects] = ensuresSpec
  1361. var mutationInfo = MutationInfo()
  1362. var hasMutationSideEffect = false
  1363. if {strictFuncs, views} * c.features != {}:
  1364. var goals: set[Goal] = {}
  1365. if strictFuncs in c.features: goals.incl constParameters
  1366. if views in c.features: goals.incl borrowChecking
  1367. var partitions = computeGraphPartitions(s, body, g, goals)
  1368. if not t.hasSideEffect and t.hasDangerousAssign:
  1369. t.hasSideEffect = varpartitions.hasSideEffect(partitions, mutationInfo)
  1370. hasMutationSideEffect = t.hasSideEffect
  1371. if views in c.features:
  1372. checkBorrowedLocations(partitions, body, g.config)
  1373. if sfThread in s.flags and t.gcUnsafe:
  1374. if optThreads in g.config.globalOptions and optThreadAnalysis in g.config.globalOptions:
  1375. #localError(s.info, "'$1' is not GC-safe" % s.name.s)
  1376. listGcUnsafety(s, onlyWarning=false, g.config)
  1377. else:
  1378. listGcUnsafety(s, onlyWarning=true, g.config)
  1379. #localError(s.info, warnGcUnsafe2, s.name.s)
  1380. if sfNoSideEffect in s.flags and t.hasSideEffect:
  1381. when false:
  1382. listGcUnsafety(s, onlyWarning=false, g.config)
  1383. else:
  1384. if hasMutationSideEffect:
  1385. localError(g.config, s.info, "'$1' can have side effects$2" % [s.name.s, g.config $ mutationInfo])
  1386. elif c.compilesContextId == 0: # don't render extended diagnostic messages in `system.compiles` context
  1387. var msg = ""
  1388. listSideEffects(msg, s, g.config, t.c)
  1389. message(g.config, s.info, errGenerated, msg)
  1390. else:
  1391. localError(g.config, s.info, "") # simple error for `system.compiles` context
  1392. if not t.gcUnsafe:
  1393. s.typ.flags.incl tfGcSafe
  1394. if not t.hasSideEffect and sfSideEffect notin s.flags:
  1395. s.typ.flags.incl tfNoSideEffect
  1396. when defined(drnim):
  1397. if c.graph.strongSemCheck != nil: c.graph.strongSemCheck(c.graph, s, body)
  1398. when defined(useDfa):
  1399. if s.name.s == "testp":
  1400. dataflowAnalysis(s, body)
  1401. when false: trackWrites(s, body)
  1402. if strictNotNil in c.features and s.kind == skProc:
  1403. checkNil(s, body, g.config, c.idgen)
  1404. proc trackStmt*(c: PContext; module: PSym; n: PNode, isTopLevel: bool) =
  1405. if n.kind in {nkPragma, nkMacroDef, nkTemplateDef, nkProcDef, nkFuncDef,
  1406. nkTypeSection, nkConverterDef, nkMethodDef, nkIteratorDef}:
  1407. return
  1408. let g = c.graph
  1409. var effects = newNodeI(nkEffectList, n.info)
  1410. var t: TEffects
  1411. initEffects(g, effects, module, t, c)
  1412. t.isTopLevel = isTopLevel
  1413. track(t, n)
  1414. when defined(drnim):
  1415. if c.graph.strongSemCheck != nil: c.graph.strongSemCheck(c.graph, module, n)