t14511.nim 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. discard """
  2. output: "true\n(y: XInt, a: 5)\n(y: XString, b: \"abc\")"
  3. """
  4. import macros
  5. block TEST_1:
  6. # https://github.com/nim-lang/Nim/issues/14511
  7. template myPragma() {.pragma.}
  8. type
  9. XType = enum
  10. XInt,
  11. XString,
  12. XUnused
  13. X = object
  14. case y {.myPragma.}: XType
  15. of XInt, XUnused:
  16. a: int
  17. else: # <-- Else case caused the "Error: index 1 not in 0 .. 0" error
  18. b: string
  19. var x: X = X(y: XInt, a: 5)
  20. echo x.y.hasCustomPragma(myPragma)
  21. echo x
  22. echo X(y: XString, b: "abc")
  23. block TEST_2:
  24. template myDevice(val: string) {.pragma.}
  25. template myKey(val: string) {.pragma.}
  26. template myMouse(val: string) {.pragma.}
  27. type
  28. Device {.pure.} = enum Keyboard, Mouse
  29. Key = enum Key1, Key2
  30. Mouse = enum Mouse1, Mouse2
  31. type
  32. Obj = object of RootObj
  33. case device {.myDevice: "MyDevicePragmaStr".}: Device
  34. of Device.Keyboard:
  35. key {.myKey: "MyKeyPragmaStr".}: Key
  36. else: # <-- Else case caused the "Error: index 1 not in 0 .. 0" error
  37. mouse {.myMouse: "MyMousePragmaStr".}: Mouse
  38. var obj: Obj
  39. assert obj.device.hasCustomPragma(myDevice) == true
  40. assert obj.key.hasCustomPragma(myKey) == true
  41. assert obj.mouse.hasCustomPragma(myMouse) == true
  42. assert obj.device.getCustomPragmaVal(myDevice) == "MyDevicePragmaStr"
  43. assert obj.key.getCustomPragmaVal(myKey) == "MyKeyPragmaStr"
  44. assert obj.mouse.getCustomPragmaVal(myMouse) == "MyMousePragmaStr"