tcodegenbug1.nim 3.6 KB

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