tconv.nim 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. discard """
  2. matrix: "--warningAsError:EnumConv --warningAsError:CStringConv"
  3. """
  4. from std/enumutils import items # missing from the example code
  5. from std/sequtils import toSeq
  6. template reject(x) =
  7. static: doAssert(not compiles(x))
  8. template accept(x) =
  9. static: doAssert(compiles(x))
  10. reject:
  11. const x = int8(300)
  12. reject:
  13. const x = int64(NaN)
  14. type
  15. R = range[0..10]
  16. reject:
  17. const x = R(11)
  18. reject:
  19. const x = R(11.0)
  20. reject:
  21. const x = R(NaN)
  22. reject:
  23. const x = R(Inf)
  24. type
  25. FloatRange = range[0'f..10'f]
  26. reject:
  27. const x = FloatRange(-1'f)
  28. reject:
  29. const x = FloatRange(-1)
  30. reject:
  31. const x = FloatRange(NaN)
  32. block:
  33. const x = float32(NaN)
  34. type E = enum a, b, c
  35. reject:
  36. const e = E(4)
  37. block: # issue 3766
  38. type R = range[0..2]
  39. reject:
  40. type
  41. T[n: static[R]] = object
  42. V = T[3.R]
  43. reject:
  44. proc r(x: static[R]) =
  45. echo x
  46. r 3.R
  47. block: # https://github.com/nim-lang/RFCs/issues/294
  48. type Koo = enum k1, k2
  49. type Goo = enum g1, g2
  50. accept: Koo(k2)
  51. accept: k2.Koo
  52. accept: k2.int.Goo
  53. reject: Goo(k2)
  54. reject: k2.Goo
  55. reject: k2.string
  56. {.push warningAsError[EnumConv]:off.}
  57. discard Goo(k2)
  58. accept: Goo(k2)
  59. accept: k2.Goo
  60. reject: k2.string
  61. {.pop.}
  62. reject: Goo(k2)
  63. reject: k2.Goo
  64. type KooRange = range[k2..k2]
  65. accept: KooRange(k2)
  66. accept: k2.KooRange
  67. let k2ranged: KooRange = k2
  68. accept: Koo(k2ranged)
  69. accept: k2ranged.Koo
  70. reject:
  71. # bug #18550
  72. proc f(c: char): cstring =
  73. var x = newString(109*1024*1024)
  74. x[0] = c
  75. x
  76. {.push warning[AnyEnumConv]:on, warningAsError[AnyEnumConv]:on.}
  77. reject:
  78. type
  79. Foo = enum
  80. one
  81. three
  82. var va = 2
  83. var vb = va.Foo
  84. {.pop.}
  85. {.push warningAsError[HoleEnumConv]:on.}
  86. reject:
  87. # bug #12815
  88. type
  89. Hole = enum
  90. one = 1
  91. three = 3
  92. var va = 2
  93. var vb = va.Hole
  94. block: # bug #22844
  95. type
  96. A = enum
  97. a0 = 2
  98. a1 = 4
  99. a2
  100. B[T] = enum
  101. b0 = 2
  102. b1 = 4
  103. doAssert A.toSeq == [a0, a1, a2]
  104. doAssert B[float].toSeq == [B[float].b0, B[float].b1]
  105. {.pop.}