tconv.nim 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. reject:
  65. # bug #18550
  66. proc f(c: char): cstring =
  67. var x = newString(109*1024*1024)
  68. x[0] = c
  69. x
  70. {.push warning[AnyEnumConv]:on, warningAsError[AnyEnumConv]:on.}
  71. reject:
  72. type
  73. Foo = enum
  74. one
  75. three
  76. var va = 2
  77. var vb = va.Foo
  78. {.pop.}
  79. {.push warningAsError[HoleEnumConv]:on.}
  80. reject:
  81. # bug #12815
  82. type
  83. Hole = enum
  84. one = 1
  85. three = 3
  86. var va = 2
  87. var vb = va.Hole
  88. block: # bug #22844
  89. type
  90. A = enum
  91. a0 = 2
  92. a1 = 4
  93. a2
  94. B[T] = enum
  95. b0 = 2
  96. b1 = 4
  97. doAssert A.toSeq == [a0, a1, a2]
  98. doAssert B[float].toSeq == [B[float].b0, B[float].b1]
  99. {.pop.}