ttopdowninference.nim 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. block:
  2. var s: seq[string] = (discard; @[])
  3. var x: set[char] =
  4. if true:
  5. try:
  6. case 1
  7. of 1:
  8. if false:
  9. {'4'}
  10. else:
  11. block:
  12. s.add "a"
  13. {}
  14. else: {'3'}
  15. except: {'2'}
  16. else: {'1'}
  17. doAssert x is set[char]
  18. doAssert x == {}
  19. doAssert s == @["a"]
  20. x = {'a', 'b'}
  21. doAssert x == {'a', 'b'}
  22. x = (s.add "b"; {})
  23. doAssert x == {}
  24. doAssert s == @["a", "b"]
  25. let x2: set[byte] = {1}
  26. doAssert x2 == {1u8}
  27. block:
  28. let x3: array[0..2, byte] = [1, 2, 3]
  29. #let x4: openarray[byte] = [1, 2, 3]
  30. #let x5: openarray[byte] = @[1, 2, 3]
  31. let x6: seq[byte] = @[1, 2, 3]
  32. let x7: seq[seq[float32]] = @[@[1, 2, 3], @[4.3, 5, 6]]
  33. type ABC = enum a, b, c
  34. let x8: array[ABC, byte] = [1, 2, 3]
  35. doAssert x8[a] == 1
  36. doAssert x8[a] + x8[b] == x8[c]
  37. const x9: array[-2..2, float] = [0, 1, 2, 3, 4]
  38. let x10: array[ABC, byte] = block:
  39. {.gcsafe.}:
  40. [a: 1, b: 2, c: 3]
  41. proc `@`(x: float): float = x + 1
  42. doAssert @1 == 2
  43. let x11: seq[byte] = system.`@`([1, 2, 3])
  44. block:
  45. type Foo = object
  46. x: BiggestInt
  47. var foo: Foo
  48. foo.x = case true
  49. of true: ord(1)
  50. else: 0
  51. foo.x = if true: ord(1) else: 0
  52. block:
  53. type Foo = object
  54. x: (float, seq[(byte, seq[byte])])
  55. let foo = Foo(x: (1, @{2: @[], 3: @[4, 5]}))
  56. doAssert foo.x == (1.0, @{2u8: @[], 3u8: @[4u8, 5]})
  57. block:
  58. type Foo = object
  59. x: tuple[a: float, b: seq[(byte, seq[byte])]]
  60. let foo = Foo(x: (a: 1, b: @{2: @[3, 4], 5: @[]}))
  61. doAssert foo.x == (1.0, @{2u8: @[3u8, 4], 5u8: @[]})
  62. block:
  63. proc foo(): seq[float] = @[1]
  64. let fooLamb = proc(): seq[float] = @[1]
  65. doAssert foo() == fooLamb()
  66. block:
  67. type Foo[T] = float32
  68. let x: seq[Foo[int32]] = @[1]
  69. block:
  70. type Foo = ref object
  71. type Bar[T] = ptr object
  72. let x1: seq[Foo] = @[nil]
  73. let x2: seq[Bar[int]] = @[nil]
  74. let x3: seq[cstring] = @[nil]
  75. block:
  76. let x: seq[cstring] = @["abc", nil, "def"]
  77. doAssert x.len == 3
  78. doAssert x[0] == cstring"abc"
  79. doAssert x[1].isNil
  80. doAssert x[2] == "def".cstring
  81. block:
  82. type Foo = object
  83. x: tuple[a: float, b: seq[(byte, seq[cstring])]]
  84. let foo = Foo(x: (a: 1, b: @{2: @[nil, "abc"]}))
  85. doAssert foo.x == (1.0, @{2u8: @[cstring nil, cstring "abc"]})
  86. block:
  87. type Foo = object
  88. x: tuple[a: float, b: seq[(byte, seq[ptr int])]]
  89. let foo = Foo(x: (a: 1, b: @{2: @[nil, nil]}))
  90. doAssert foo.x == (1.0, @{2u8: @[(ptr int)(nil), nil]})
  91. when false: # unsupported
  92. block: # type conversion
  93. let x = seq[(cstring, float32)](@{"abc": 1.0, "def": 2.0})
  94. doAssert x[0] == (cstring"abc", 1.0'f32)
  95. doAssert x[1] == (cstring"def", 2.0'f32)
  96. block: # enum
  97. type Foo {.pure.} = enum a
  98. type Bar {.pure.} = enum a, b, c
  99. var s: seq[Bar] = @[a, b, c]
  100. block: # overload selection
  101. proc foo(x, y: int): int = x + y + 1
  102. proc foo(x: int): int = x - 1
  103. var s: seq[proc (x, y: int): int] = @[nil, foo, foo]
  104. var s2: seq[int]
  105. for a in s:
  106. if not a.isNil: s2.add(a(1, 2))
  107. doAssert s2 == @[4, 4]
  108. block: # with generics?
  109. proc foo(x, y: int): int = x + y + 1
  110. proc foo(x: int): int = x - 1
  111. proc bar[T](x, y: T): T = x - y
  112. var s: seq[proc (x, y: int): int] = @[nil, foo, foo, bar]
  113. var s2: seq[int]
  114. for a in s:
  115. if not a.isNil: s2.add(a(1, 2))
  116. doAssert s2 == @[4, 4, -1]
  117. proc foo(x, y: float): float = x + y + 1.0
  118. var s3: seq[proc (x, y: float): float] = @[nil, foo, foo, bar]
  119. var s4: seq[float]
  120. for a in s3:
  121. if not a.isNil: s4.add(a(1, 2))
  122. doAssert s4 == @[4.0, 4, -1]
  123. block: # range types
  124. block:
  125. let x: set[range[1u8..5u8]] = {1, 3}
  126. doAssert x == {range[1u8..5u8](1), 3}
  127. doAssert $x == "{1, 3}"
  128. block:
  129. let x: seq[set[range[1u8..5u8]]] = @[{1, 3}]
  130. doAssert x == @[{range[1u8..5u8](1), 3}]
  131. doAssert $x[0] == "{1, 3}"
  132. block:
  133. let x: seq[range[1u8..5u8]] = @[1, 3]
  134. doAssert x == @[range[1u8..5u8](1), 3]
  135. doAssert $x == "@[1, 3]"
  136. block: # already worked before, make sure it still works
  137. let x: set[range['a'..'e']] = {'a', 'c'}
  138. doAssert x == {range['a'..'e']('a'), 'c'}
  139. doAssert $x == "{'a', 'c'}"
  140. block: # extended
  141. let x: seq[set[range['a'..'e']]] = @[{'a', 'c'}]
  142. doAssert x[0] == {range['a'..'e']('a'), 'c'}
  143. doAssert $x == "@[{'a', 'c'}]"
  144. block:
  145. type Foo = object
  146. x: (range[1u8..5u8], seq[(range[1f32..5f32], seq[range['a'..'e']])])
  147. let foo = Foo(x: (1, @{2: @[], 3: @['c', 'd']}))
  148. doAssert foo.x == (range[1u8..5u8](1u8), @{range[1f32..5f32](2f32): @[], 3f32: @[range['a'..'e']('c'), 'd']})
  149. block:
  150. type Foo = object
  151. x: (range[1u8..5u8], seq[(range[1f32..5f32], seq[set[range['a'..'e']]])])
  152. let foo = Foo(x: (1, @{2: @[], 3: @[{'c', 'd'}]}))
  153. doAssert foo.x == (range[1u8..5u8](1u8), @{range[1f32..5f32](2f32): @[], 3f32: @[{range['a'..'e']('c'), 'd'}]})
  154. block: # templates
  155. template foo: untyped = (1, 2, "abc")
  156. let x: (float, byte, cstring) = foo()
  157. doAssert x[0] == float(1)
  158. doAssert x[1] == byte(2)
  159. doAssert x[2] == cstring("abc")
  160. let (a, b, c) = x
  161. doAssert a == float(1)
  162. doAssert b == byte(2)
  163. doAssert c == cstring("abc")
  164. proc foo(): set[char] = # bug #11259
  165. discard "a"
  166. {}
  167. discard foo()
  168. block: # bug #11085
  169. const ok1: set[char] = {}
  170. var ok1b: set[char] = {}
  171. const ok2: set[char] = block:
  172. {}
  173. const ok3: set[char] = block:
  174. var x: set[char] = {}
  175. x
  176. var ok3b: set[char] = block:
  177. var x: set[char] = {}
  178. x
  179. var bad: set[char] = block:
  180. {}
  181. # bug #6213
  182. block:
  183. block:
  184. type MyEnum = enum a, b
  185. type MyTuple = tuple[x: set[MyEnum]]
  186. var myVar: seq[MyTuple] = @[ (x: {}) ]
  187. doAssert myVar.len == 1
  188. block:
  189. type
  190. Foo = tuple
  191. f: seq[string]
  192. s: string
  193. proc e(): seq[Foo] =
  194. return @[
  195. (@[], "asd")
  196. ]
  197. doAssert e()[0].f == @[]
  198. block: # bug #11777
  199. type S = set[0..5]
  200. var s: S = {1, 2}
  201. doAssert 1 in s
  202. block: # bug #20807
  203. var s: seq[string]
  204. template fail =
  205. s = @[]
  206. template test(body: untyped) =
  207. body
  208. proc test(a: string) = discard
  209. test: fail()
  210. doAssert not (compiles do:
  211. let x: seq[int] = `@`[string]([]))
  212. block: # bug #21377
  213. proc b[T](v: T): seq[int] =
  214. let x = 0
  215. @[]
  216. doAssert b(0) == @[]
  217. block: # bug #21377
  218. proc b[T](v: T): seq[T] =
  219. let x = 0
  220. @[]
  221. doAssert b(0) == @[]
  222. block: # bug #21377
  223. proc b[T](v: T): set[bool] =
  224. let x = 0
  225. {}
  226. doAssert b(0) == {}
  227. block: # bug #21377
  228. proc b[T](v: T): array[0, int] =
  229. let x = 0
  230. []
  231. doAssert b(0) == []
  232. block: # bug #21377
  233. proc b[T](v: T): array[0, (string, string)] =
  234. let x = 0
  235. {:}
  236. doAssert b(0) == {:}
  237. block: # bug #22180
  238. type A = object
  239. proc j() = discard
  240. let x =
  241. if false:
  242. (ref A)(nil)
  243. else:
  244. if false:
  245. quit 1
  246. else:
  247. if true:
  248. j()
  249. nil # compiles with (ref A)(nil) here
  250. else:
  251. (ref A)(nil)
  252. doAssert x.isNil
  253. let y =
  254. case true
  255. of false:
  256. (ref A)(nil)
  257. else:
  258. case true
  259. of false:
  260. quit 1
  261. else:
  262. case true
  263. of true:
  264. j()
  265. nil # compiles with (ref A)(nil) here
  266. else:
  267. (ref A)(nil)
  268. doAssert y.isNil