tcontrolflow.nim 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. discard """
  2. output: '''begin A
  3. elif
  4. end A
  5. destroyed
  6. begin false
  7. if
  8. end false
  9. destroyed
  10. begin true
  11. if
  12. end true
  13. 7
  14. ##index 2 not in 0 .. 1##
  15. true
  16. '''
  17. cmd: "nim c --gc:arc -d:danger $file"
  18. """
  19. # we use the -d:danger switch to detect uninitialized stack
  20. # slots more reliably (there shouldn't be any, of course).
  21. type
  22. Foo = object
  23. id: int
  24. proc `=destroy`(x: var Foo) =
  25. if x.id != 0:
  26. echo "destroyed"
  27. x.id = 0
  28. proc construct(): Foo = Foo(id: 3)
  29. proc elifIsEasy(cond: bool) =
  30. echo "begin A"
  31. if cond:
  32. echo "if"
  33. elif construct().id == 3:
  34. echo "elif"
  35. else:
  36. echo "else"
  37. echo "end A"
  38. elifIsEasy(false)
  39. proc orIsHard(cond: bool) =
  40. echo "begin ", cond
  41. if cond or construct().id == 3:
  42. echo "if"
  43. else:
  44. echo "else"
  45. echo "end ", cond
  46. orIsHard(false)
  47. orIsHard(true)
  48. type
  49. Control = ref object
  50. x: int
  51. MouseEvent = ref object
  52. control: Control
  53. button: int
  54. proc run(data: Control) =
  55. var evt = MouseEvent(button: 1)
  56. evt.control = data
  57. if evt.button == 1:
  58. discard
  59. else:
  60. return
  61. echo data.x
  62. var c = Control(x: 7)
  63. run(c)
  64. proc sysFatal(exceptn: typedesc, message: string) {.inline, noreturn.} =
  65. var buf = newStringOfCap(200)
  66. add(buf, "##")
  67. add(buf, message)
  68. add(buf, "##")
  69. echo buf
  70. proc ifexpr(i, a, b: int) {.compilerproc, noinline.} =
  71. sysFatal(IndexDefect,
  72. if b < a: "index out of bounds, the container is empty"
  73. else: "index " & $i & " not in " & $a & " .. " & $b)
  74. ifexpr(2, 0, 1)
  75. # bug #14899
  76. template toSeq(): untyped =
  77. block:
  78. var result = @[1]
  79. result
  80. proc clItems(s: seq[int]) =
  81. assert s.len == 1
  82. proc escapeCheck =
  83. clItems(toSeq())
  84. escapeCheck()
  85. # bug #14900
  86. proc seqsEqual(a, b: string): bool =
  87. if false:
  88. false
  89. else:
  90. (var result1 = a; result1) == (var result2 = b; result2)
  91. # can be const or var too
  92. let expected = "hello"
  93. echo seqsEqual(expected, expected)