tdecls.nim 992 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. discard """
  2. targets: "c cpp js"
  3. """
  4. import std/assertions
  5. import std/decls
  6. template fun() =
  7. var s = @[10,11,12]
  8. var a {.byaddr.} = s[0]
  9. a+=100
  10. doAssert s == @[110,11,12]
  11. doAssert a is int
  12. var b {.byaddr.}: int = s[0]
  13. doAssert a.addr == b.addr
  14. when false:
  15. # template specific redeclaration issue
  16. # see https://github.com/nim-lang/Nim/issues/8275
  17. doAssert not compiles(block:
  18. # redeclaration not allowed
  19. var foo = 0
  20. var foo {.byaddr.} = s[0])
  21. doAssert not compiles(block:
  22. # ditto
  23. var foo {.byaddr.} = s[0]
  24. var foo {.byaddr.} = s[0])
  25. block:
  26. var b {.byaddr.} = s[1] # redeclaration ok in sub scope
  27. b = 123
  28. doAssert s == @[110,123,12]
  29. b = b * 10
  30. doAssert s == @[1100,123,12]
  31. doAssert not compiles(block:
  32. var b2 {.byaddr.}: float = s[2])
  33. doAssert compiles(block:
  34. var b2 {.byaddr.}: int = s[2])
  35. proc fun2() = fun()
  36. fun()
  37. fun2()
  38. static: fun2()
  39. when false: # pending bug #13887
  40. static: fun()