tref.nim 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. static:
  2. var
  3. a: ref string
  4. b: ref string
  5. new a
  6. a[] = "Hello world"
  7. b = a
  8. b[5] = 'c'
  9. doAssert a[] == "Hellocworld"
  10. doAssert b[] == "Hellocworld"
  11. proc notGlobal() =
  12. var
  13. a: ref string
  14. b: ref string
  15. new a
  16. a[] = "Hello world"
  17. b = a
  18. b[5] = 'c'
  19. doAssert a[] == "Hellocworld"
  20. doAssert b[] == "Hellocworld"
  21. notGlobal()
  22. static: # bug 6081
  23. block:
  24. type Obj = object
  25. field: ref int
  26. var i: ref int
  27. new(i)
  28. var r = Obj(field: i)
  29. var rr = r
  30. r.field = nil
  31. doAssert rr.field != nil
  32. proc foo() = # Proc to avoid special global logic
  33. var s: seq[ref int]
  34. var i: ref int
  35. new(i)
  36. s.add(i)
  37. var head = s[0]
  38. s[0] = nil
  39. doAssert head != nil
  40. foo()
  41. static:
  42. block: # global alias
  43. var s: ref int
  44. new(s)
  45. var ss = s
  46. s[] = 1
  47. doAssert ss[] == 1
  48. static: # bug #8402
  49. type R = ref object
  50. var empty: R
  51. let otherEmpty = empty
  52. block:
  53. # fix https://github.com/timotheecour/Nim/issues/88
  54. template fun() =
  55. var s = @[10,11,12]
  56. var a = s[0].addr
  57. a[] += 100 # was giving SIGSEGV
  58. doAssert a[] == 110
  59. static: fun()