sempass2.nim 59 KB

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