isolation_check.nim 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2020 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Implementation of the check that `recover` needs, see
  10. ## https://github.com/nim-lang/RFCs/issues/244 for more details.
  11. import
  12. ast, types, renderer, intsets
  13. when defined(nimPreviewSlimSystem):
  14. import std/assertions
  15. proc canAlias(arg, ret: PType; marker: var IntSet): bool
  16. proc canAliasN(arg: PType; n: PNode; marker: var IntSet): bool =
  17. case n.kind
  18. of nkRecList:
  19. for i in 0..<n.len:
  20. result = canAliasN(arg, n[i], marker)
  21. if result: return
  22. of nkRecCase:
  23. assert(n[0].kind == nkSym)
  24. result = canAliasN(arg, n[0], marker)
  25. if result: return
  26. for i in 1..<n.len:
  27. case n[i].kind
  28. of nkOfBranch, nkElse:
  29. result = canAliasN(arg, lastSon(n[i]), marker)
  30. if result: return
  31. else: discard
  32. of nkSym:
  33. result = canAlias(arg, n.sym.typ, marker)
  34. else: discard
  35. proc canAlias(arg, ret: PType; marker: var IntSet): bool =
  36. if containsOrIncl(marker, ret.id):
  37. return false
  38. if ret.kind in {tyPtr, tyPointer}:
  39. # unsafe so we don't care:
  40. return false
  41. if compareTypes(arg, ret, dcEqIgnoreDistinct):
  42. return true
  43. case ret.kind
  44. of tyObject:
  45. if isFinal(ret):
  46. result = canAliasN(arg, ret.n, marker)
  47. if not result and ret.len > 0 and ret[0] != nil:
  48. result = canAlias(arg, ret[0], marker)
  49. else:
  50. result = true
  51. of tyTuple:
  52. for i in 0..<ret.len:
  53. result = canAlias(arg, ret[i], marker)
  54. if result: break
  55. of tyArray, tySequence, tyDistinct, tyGenericInst,
  56. tyAlias, tyInferred, tySink, tyLent, tyOwned, tyRef:
  57. result = canAlias(arg, ret.lastSon, marker)
  58. of tyProc:
  59. result = ret.callConv == ccClosure
  60. else:
  61. result = false
  62. proc isValueOnlyType(t: PType): bool =
  63. # t doesn't contain pointers and references
  64. proc wrap(t: PType): bool {.nimcall.} = t.kind in {tyRef, tyPtr, tyVar, tyLent}
  65. result = not types.searchTypeFor(t, wrap)
  66. type
  67. SearchResult = enum
  68. NotFound, Abort, Found
  69. proc containsDangerousRefAux(t: PType; marker: var IntSet): SearchResult
  70. proc containsDangerousRefAux(n: PNode; marker: var IntSet): SearchResult =
  71. result = NotFound
  72. case n.kind
  73. of nkRecList:
  74. for i in 0..<n.len:
  75. result = containsDangerousRefAux(n[i], marker)
  76. if result == Found: return result
  77. of nkRecCase:
  78. assert(n[0].kind == nkSym)
  79. result = containsDangerousRefAux(n[0], marker)
  80. if result == Found: return result
  81. for i in 1..<n.len:
  82. case n[i].kind
  83. of nkOfBranch, nkElse:
  84. result = containsDangerousRefAux(lastSon(n[i]), marker)
  85. if result == Found: return result
  86. else: discard
  87. of nkSym:
  88. result = containsDangerousRefAux(n.sym.typ, marker)
  89. else: discard
  90. proc containsDangerousRefAux(t: PType; marker: var IntSet): SearchResult =
  91. result = NotFound
  92. if t == nil: return result
  93. if containsOrIncl(marker, t.id): return result
  94. if t.kind == tyRef or (t.kind == tyProc and t.callConv == ccClosure):
  95. result = Found
  96. elif tfSendable in t.flags:
  97. result = Abort
  98. else:
  99. # continue the type traversal:
  100. result = NotFound
  101. if result != NotFound: return result
  102. case t.kind
  103. of tyObject:
  104. if t[0] != nil:
  105. result = containsDangerousRefAux(t[0].skipTypes(skipPtrs), marker)
  106. if result == NotFound: result = containsDangerousRefAux(t.n, marker)
  107. of tyGenericInst, tyDistinct, tyAlias, tySink:
  108. result = containsDangerousRefAux(lastSon(t), marker)
  109. of tyArray, tySet, tyTuple, tySequence:
  110. for i in 0..<t.len:
  111. result = containsDangerousRefAux(t[i], marker)
  112. if result == Found: return result
  113. else:
  114. discard
  115. proc containsDangerousRef(t: PType): bool =
  116. # a `ref` type is "dangerous" if it occurs not within a type that is like `Isolated[T]`.
  117. # For example:
  118. # `ref int` # dangerous
  119. # `Isolated[ref int]` # not dangerous
  120. var marker = initIntSet()
  121. result = containsDangerousRefAux(t, marker) == Found
  122. proc canAlias*(arg, ret: PType): bool =
  123. if isValueOnlyType(arg):
  124. # can alias only with addr(arg.x) and we don't care if it is not safe
  125. result = false
  126. else:
  127. var marker = initIntSet()
  128. result = canAlias(arg, ret, marker)
  129. const
  130. SomeVar = {skForVar, skParam, skVar, skLet, skConst, skResult, skTemp}
  131. proc containsVariable(n: PNode): bool =
  132. case n.kind
  133. of nodesToIgnoreSet:
  134. result = false
  135. of nkSym:
  136. result = n.sym.kind in SomeVar
  137. else:
  138. for ch in n:
  139. if containsVariable(ch): return true
  140. result = false
  141. proc checkIsolate*(n: PNode): bool =
  142. if types.containsTyRef(n.typ):
  143. # XXX Maybe require that 'n.typ' is acyclic. This is not much
  144. # worse than the already exisiting inheritance and closure restrictions.
  145. case n.kind
  146. of nkCharLit..nkNilLit:
  147. result = true
  148. of nkCallKinds:
  149. # XXX: as long as we don't update the analysis while examining arguments
  150. # we can do an early check of the return type, otherwise this is a
  151. # bug and needs to be moved below
  152. if tfNoSideEffect notin n[0].typ.flags:
  153. return false
  154. for i in 1..<n.len:
  155. if checkIsolate(n[i]):
  156. discard "fine, it is isolated already"
  157. else:
  158. let argType = n[i].typ
  159. if argType != nil and not isCompileTimeOnly(argType) and containsDangerousRef(argType):
  160. if argType.canAlias(n.typ) or containsVariable(n[i]):
  161. # bug #19013: Alias information is not enough, we need to check for potential
  162. # "overlaps". I claim the problem can only happen by reading again from a location
  163. # that materialized which is only possible if a variable that contains a `ref`
  164. # is involved.
  165. return false
  166. result = true
  167. of nkIfStmt, nkIfExpr:
  168. for it in n:
  169. result = checkIsolate(it.lastSon)
  170. if not result: break
  171. of nkCaseStmt:
  172. for i in 1..<n.len:
  173. result = checkIsolate(n[i].lastSon)
  174. if not result: break
  175. of nkObjConstr:
  176. result = true
  177. for i in 1..<n.len:
  178. result = checkIsolate(n[i].lastSon)
  179. if not result: break
  180. of nkBracket, nkTupleConstr, nkPar:
  181. for it in n:
  182. result = checkIsolate(it)
  183. if not result: break
  184. of nkHiddenStdConv, nkHiddenSubConv, nkCast, nkConv:
  185. result = checkIsolate(n[1])
  186. of nkObjUpConv, nkObjDownConv, nkDotExpr:
  187. result = checkIsolate(n[0])
  188. of nkStmtList, nkStmtListExpr:
  189. if n.len > 0:
  190. result = checkIsolate(n[^1])
  191. else:
  192. result = false
  193. of nkSym:
  194. result = true
  195. if n.sym.kind in SomeVar:
  196. let argType = n.typ
  197. if argType != nil and not isCompileTimeOnly(argType) and containsDangerousRef(argType):
  198. result = false
  199. else:
  200. # unanalysable expression:
  201. result = false
  202. else:
  203. # no ref, no cry:
  204. result = true