topt_wasmoved_destroy_pairs.nim 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. discard """
  2. output: ''''''
  3. cmd: '''nim c --gc:arc --expandArc:main --expandArc:tfor --hint:Performance:off $file'''
  4. nimout: '''
  5. --expandArc: main
  6. var
  7. a
  8. b
  9. x
  10. x = f()
  11. if cond:
  12. add(a):
  13. let blitTmp = x
  14. blitTmp
  15. else:
  16. add(b):
  17. let blitTmp_1 = x
  18. blitTmp_1
  19. `=destroy`(b)
  20. `=destroy`(a)
  21. -- end of expandArc ------------------------
  22. --expandArc: tfor
  23. var
  24. a
  25. b
  26. x
  27. try:
  28. x = f()
  29. block :tmp:
  30. var i_cursor
  31. mixin inc
  32. var i_1 = 0
  33. block :tmp_1:
  34. while i_1 < 4:
  35. var :tmpD
  36. i_cursor = i_1
  37. if i_cursor == 2:
  38. return
  39. add(a):
  40. `=wasMoved`(:tmpD)
  41. `=copy`(:tmpD, x)
  42. :tmpD
  43. inc i_1, 1
  44. if cond:
  45. add(a):
  46. let blitTmp = x
  47. `=wasMoved`(x)
  48. blitTmp
  49. else:
  50. add(b):
  51. let blitTmp_1 = x
  52. `=wasMoved`(x)
  53. blitTmp_1
  54. finally:
  55. `=destroy`(x)
  56. `=destroy_1`(b)
  57. `=destroy_1`(a)
  58. -- end of expandArc ------------------------
  59. '''
  60. """
  61. proc f(): seq[int] =
  62. @[1, 2, 3]
  63. proc main(cond: bool) =
  64. var a, b: seq[seq[int]]
  65. var x = f()
  66. if cond:
  67. a.add x
  68. else:
  69. b.add x
  70. # all paths move 'x' so no wasMoved(x); destroy(x) pair should be left in the
  71. # AST.
  72. main(false)
  73. proc tfor(cond: bool) =
  74. var a, b: seq[seq[int]]
  75. var x = f()
  76. for i in 0 ..< 4:
  77. if i == 2: return
  78. a.add x
  79. if cond:
  80. a.add x
  81. else:
  82. b.add x
  83. tfor(false)