tcodegenbug1.nim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. discard """
  2. matrix: "--mm:refc"
  3. output: '''obj = (inner: (kind: Just, id: 7))
  4. obj.inner.id = 7
  5. id = 7
  6. obj = (inner: (kind: Just, id: 7))
  7. 2
  8. (a: "a", b: "b", c: "")
  9. caught
  10. (a: "a", b: "b", c: "")'''
  11. """
  12. # bug #6960
  13. import sugar
  14. type
  15. Kind = enum None, Just, Huge
  16. Inner = object
  17. case kind: Kind
  18. of None: discard
  19. of Just: id: int
  20. of Huge: a,b,c,d,e,f: string
  21. Outer = object
  22. inner: Inner
  23. proc shouldDoNothing(id: int): Inner =
  24. dump id
  25. Inner(kind: Just, id: id)
  26. var obj = Outer(inner: Inner(kind: Just, id: 7))
  27. dump obj
  28. dump obj.inner.id
  29. obj.inner = shouldDoNothing(obj.inner.id)
  30. dump obj
  31. import os
  32. type
  33. TStatusEnum* = enum
  34. sUnknown = -1, sBuildFailure, sBuildInProgress, sBuildSuccess,
  35. sTestFailure, sTestInProgress, sTestSuccess, # ORDER MATTERS!
  36. sDocGenFailure, sDocGenInProgress, sDocGenSuccess,
  37. sCSrcGenFailure, sCSrcGenInProgress, sCSrcGenSuccess
  38. TStatus* = object
  39. status*: TStatusEnum
  40. desc*: string
  41. hash*: string
  42. proc initStatus*(): TStatus =
  43. result.status = sUnknown
  44. result.desc = ""
  45. result.hash = ""
  46. proc isInProgress*(status: TStatusEnum): bool =
  47. return status in {sBuildInProgress, sTestInProgress, sDocGenInProgress,
  48. sCSrcGenInProgress}
  49. proc `$`*(status: TStatusEnum): string =
  50. case status
  51. of sBuildFailure:
  52. return "build failure"
  53. of sBuildInProgress:
  54. return "build in progress"
  55. of sBuildSuccess:
  56. return "build finished"
  57. of sTestFailure:
  58. return "testing failure"
  59. of sTestInProgress:
  60. return "testing in progress"
  61. of sTestSuccess:
  62. return "testing finished"
  63. of sDocGenFailure:
  64. return "documentation generation failed"
  65. of sDocGenInProgress:
  66. return "generating documentation"
  67. of sDocGenSuccess:
  68. return "documentation generation succeeded"
  69. of sCSrcGenFailure:
  70. return "csource generation failed"
  71. of sCSrcGenInProgress:
  72. return "csource generation in progress"
  73. of sCSrcGenSuccess:
  74. return "csource generation succeeded"
  75. of sUnknown:
  76. return "unknown"
  77. proc makeCommitPath*(platform, hash: string): string =
  78. return platform / "nim_" & hash.substr(0, 11) # 11 Chars.
  79. type
  80. TFlag = enum
  81. A, B, C, D
  82. TFlags = set[TFlag]
  83. TObj = object
  84. x: int
  85. flags: TFlags
  86. # have a proc taking TFlags as param and returning object having TFlags field
  87. proc foo(flags: TFlags): TObj = nil
  88. # bug #5137
  89. type
  90. MyInt {.importc: "int".} = object
  91. MyIntDistinct = distinct MyInt
  92. proc bug5137(d: MyIntDistinct) =
  93. discard d.MyInt
  94. #-------------------------------------
  95. # bug #8979
  96. type
  97. MyKind = enum
  98. Fixed, Float
  99. MyObject = object
  100. someInt: int
  101. case kind: MyKind
  102. of Float: index: string
  103. of Fixed: nil
  104. MyResult = object
  105. val: array[0..1, string]
  106. vis: set[0..1]
  107. import macros
  108. func myfunc(obj: MyObject): MyResult {.raises: [].} =
  109. template index: auto =
  110. case obj.kind:
  111. of Float: $obj.index
  112. of Fixed: "Fixed"
  113. macro to_str(a: untyped): string =
  114. result = newStrLitNode(a.repr)
  115. result.val[0] = index
  116. result.val[1] = to_str(obj.kind + Ola)
  117. let x = MyObject(someInt: 10, kind: Fixed)
  118. echo myfunc(x).val.len
  119. # bug #14126
  120. type X = object
  121. a, b, c: string
  122. proc f(): X =
  123. result.a = "a"
  124. result.b = "b"
  125. raise (ref ValueError)()
  126. proc ohmanNoNRVO =
  127. var x: X
  128. x.a = "1"
  129. x.b = "2"
  130. x.c = "3"
  131. try:
  132. x = f()
  133. except:
  134. discard
  135. echo x
  136. # once NVRO is sorted out, x.c == "3"
  137. doAssert x.c == "", "shouldn't modify x if f raises"
  138. ohmanNoNRVO()
  139. proc ohmanNoNRVO2(x: var X) =
  140. x.a = "1"
  141. x.c = "3"
  142. x = f()
  143. var xgg: X
  144. try:
  145. ohmanNoNRVO2(xgg)
  146. except:
  147. echo "caught"
  148. echo xgg
  149. # once NVRO is sorted out, xgg.c == "3"
  150. doAssert xgg.c == "", "this assert will fail"