tobject_default_value.nim 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. discard """
  2. matrix: "-d:nimPreviewRangeDefault --mm:refc; -d:nimPreviewRangeDefault --warningAsError:ProveInit --mm:orc"
  3. targets: "c cpp js"
  4. """
  5. import std/[times, macros, tables]
  6. type
  7. Guess = object
  8. poi: DateTime
  9. GuessDistinct = distinct Guess
  10. block:
  11. var x: Guess
  12. discard Guess()
  13. var y: GuessDistinct
  14. discard y
  15. discard GuessDistinct(x)
  16. import mobject_default_value
  17. block:
  18. let x = Default()
  19. doAssert x.se == 0'i32
  20. block:
  21. let x = default(Default)
  22. doAssert x.se == 0'i32
  23. # echo Default(poi: 12)
  24. # echo Default(poi: 17)
  25. # echo NonDefault(poi: 77)
  26. block:
  27. var x: Default
  28. doAssert x.se == 0'i32
  29. type
  30. ObjectBase = object of RootObj
  31. value = 12
  32. ObjectBaseDistinct = distinct ObjectBase
  33. DinstinctInObject = object
  34. data: ObjectBaseDistinct
  35. Object = object of ObjectBase
  36. time: float = 1.2
  37. date: int
  38. scale: range[1..10]
  39. Object2 = object
  40. name: Object
  41. Object3 = object
  42. obj: Object2
  43. ObjectTuple = tuple
  44. base: ObjectBase
  45. typ: int
  46. obj: Object
  47. TupleInObject = object
  48. size = 777
  49. data: ObjectTuple
  50. Ref = ref object of ObjectBase
  51. RefInt = ref object of Ref
  52. data = 73
  53. Ref2 = ref object of ObjectBase
  54. RefInt2 = ref object of Ref
  55. data = 73
  56. var t {.threadvar.}: Default
  57. # var m1, m2 {.threadvar.}: Default
  58. block:
  59. doAssert t.se == 0'i32
  60. block: # ARC/ORC cannot bind destructors twice, so it cannot
  61. # be moved into main
  62. block:
  63. var x: Ref
  64. new(x)
  65. doAssert x.value == 12, "Ref.value = " & $x.value
  66. var y: RefInt
  67. new(y)
  68. doAssert y.value == 12
  69. doAssert y.data == 73
  70. block:
  71. var x: Ref2
  72. new(x, proc (x: Ref2) {.nimcall.} = discard "call Ref")
  73. doAssert x.value == 12, "Ref.value = " & $x.value
  74. proc call(x: RefInt2) =
  75. discard "call RefInt"
  76. var y: RefInt2
  77. new(y, call)
  78. doAssert y.value == 12
  79. doAssert y.data == 73
  80. template main {.dirty.} =
  81. block: # bug #16744
  82. type
  83. R = range[1..10]
  84. Obj = object
  85. r: R
  86. var
  87. rVal: R = default(R) # Works fine
  88. objVal = default(Obj)
  89. doAssert rVal == 1
  90. doAssert objVal.r == 1
  91. block: # bug #16744
  92. type
  93. R = range[1..10]
  94. Obj = object
  95. r: R
  96. var
  97. rVal: R = default(R) # Works fine
  98. objVal = Obj()
  99. doAssert rVal == 1 # it should be 1
  100. doAssert objVal.r == 1
  101. block: # bug #3608
  102. type
  103. abc = ref object
  104. w: range[2..100]
  105. proc createABC(): abc =
  106. new(result)
  107. result.w = 20
  108. doAssert createABC().w == 20
  109. block:
  110. var x = new ObjectBase
  111. doAssert x.value == 12
  112. proc hello(): ref ObjectBase =
  113. new result
  114. let z = hello()
  115. doAssert z.value == 12
  116. block:
  117. var base = ObjectBase()
  118. var x: ObjectBaseDistinct = ObjectBaseDistinct(base)
  119. doAssert ObjectBase(x).value == 12
  120. let y = ObjectBaseDistinct(default(ObjectBase))
  121. doAssert ObjectBase(y).value == 12
  122. let m = ObjectBaseDistinct(ObjectBase())
  123. doAssert ObjectBase(m).value == 12
  124. proc hello(): ObjectBaseDistinct =
  125. result = ObjectBaseDistinct(default(ObjectBase))
  126. let z = hello()
  127. doAssert ObjectBase(z).value == 12
  128. block:
  129. var x: DinstinctInObject
  130. x.data = ObjectBaseDistinct(default(ObjectBase))
  131. doAssert ObjectBase(x.data).value == 12
  132. block:
  133. var x = Object()
  134. doAssert x.value == 12
  135. doAssert x.time == 1.2
  136. doAssert x.scale == 1
  137. let y = default(Object)
  138. doAssert y.value == 12
  139. doAssert y.time == 1.2
  140. doAssert y.scale == 1
  141. var x1, x2, x3 = default(Object)
  142. doAssert x1.value == 12
  143. doAssert x1.time == 1.2
  144. doAssert x1.scale == 1
  145. doAssert x2.value == 12
  146. doAssert x2.time == 1.2
  147. doAssert x2.scale == 1
  148. doAssert x3.value == 12
  149. doAssert x3.time == 1.2
  150. doAssert x3.scale == 1
  151. block:
  152. var x = new Object
  153. doAssert x[] == default(Object)
  154. block:
  155. var x = default(Object2)
  156. doAssert x.name.value == 12
  157. doAssert x.name.time == 1.2
  158. doAssert x.name.scale == 1
  159. block:
  160. let x = Object2()
  161. doAssert x.name.value == 12
  162. doAssert x.name.time == 1.2
  163. doAssert x.name.scale == 1
  164. block:
  165. var x: ref Object2
  166. new x
  167. doAssert x[] == default(Object2)
  168. block:
  169. var x = default(Object3)
  170. doAssert x.obj.name.value == 12
  171. doAssert x.obj.name.time == 1.2
  172. doAssert x.obj.name.scale == 1
  173. block:
  174. var x = Object3()
  175. doAssert x.obj.name.value == 12
  176. doAssert x.obj.name.time == 1.2
  177. doAssert x.obj.name.scale == 1
  178. when nimvm:
  179. # todo
  180. discard "fixme"
  181. else:
  182. when defined(gcArc) or defined(gcOrc):
  183. block: #seq
  184. var x = newSeq[Object](10)
  185. let y = x[0]
  186. doAssert y.value == 12
  187. doAssert y.time == 1.2
  188. doAssert y.scale == 1
  189. block:
  190. var x: seq[Object]
  191. setLen(x, 5)
  192. let y = x[^1]
  193. doAssert y.value == 12
  194. doAssert y.time == 1.2
  195. doAssert y.scale == 1
  196. block:
  197. var my = @[1, 2, 3, 4, 5]
  198. my.setLen(0)
  199. my.setLen(5)
  200. doAssert my == @[0, 0, 0, 0, 0]
  201. block:
  202. var my = "hello"
  203. my.setLen(0)
  204. my.setLen(5)
  205. doAssert $(@my) == """@['\x00', '\x00', '\x00', '\x00', '\x00']"""
  206. block: # array
  207. var x: array[10, Object] = default(array[10, Object])
  208. let y = x[0]
  209. doAssert y.value == 12
  210. doAssert y.time == 1.2
  211. doAssert y.scale == 1
  212. block: # array
  213. var x {.noinit.}: array[10, Object]
  214. discard x
  215. block: # tuple
  216. var x = default(ObjectTuple)
  217. doAssert x.base.value == 12
  218. doAssert x.typ == 0
  219. doAssert x.obj.time == 1.2
  220. doAssert x.obj.date == 0
  221. doAssert x.obj.scale == 1
  222. doAssert x.obj.value == 12
  223. block: # tuple in object
  224. var x = default(TupleInObject)
  225. doAssert x.data.base.value == 12
  226. doAssert x.data.typ == 0
  227. doAssert x.data.obj.time == 1.2
  228. doAssert x.data.obj.date == 0
  229. doAssert x.data.obj.scale == 1
  230. doAssert x.data.obj.value == 12
  231. doAssert x.size == 777
  232. type
  233. ObjectArray = object
  234. data: array[10, Object]
  235. block:
  236. var x = default(ObjectArray)
  237. let y = x.data[0]
  238. doAssert y.value == 12
  239. doAssert y.time == 1.2
  240. doAssert y.scale == 1
  241. block:
  242. var x: PrellDeque[int]
  243. doAssert x.pendingTasks == 0
  244. type
  245. Color = enum
  246. Red, Blue, Yellow
  247. ObjectVarint = object
  248. case kind: Color
  249. of Red:
  250. data: int = 10
  251. of Blue:
  252. fill = "123"
  253. of Yellow:
  254. time = 1.8'f32
  255. ObjectVarint1 = object
  256. case kind: Color = Blue
  257. of Red:
  258. data1: int = 10
  259. of Blue:
  260. fill2 = "123"
  261. cry: float
  262. of Yellow:
  263. time3 = 1.8'f32
  264. him: int
  265. block:
  266. var x = ObjectVarint(kind: Red)
  267. doAssert x.kind == Red
  268. doAssert x.data == 10
  269. block:
  270. var x = ObjectVarint(kind: Blue)
  271. doAssert x.kind == Blue
  272. doAssert x.fill == "123"
  273. block:
  274. var x = ObjectVarint(kind: Yellow)
  275. doAssert x.kind == Yellow
  276. doAssert typeof(x.time) is float32
  277. block:
  278. var x = default(ObjectVarint1)
  279. doAssert x.kind == Blue
  280. doAssert x.fill2 == "123"
  281. x.cry = 326
  282. type
  283. ObjectVarint2 = object
  284. case kind: Color
  285. of Red:
  286. data: int = 10
  287. of Blue:
  288. fill = "123"
  289. of Yellow:
  290. time = 1.8'f32
  291. block:
  292. var x = ObjectVarint2(kind: Blue)
  293. doAssert x.fill == "123"
  294. block:
  295. type
  296. Color = enum
  297. Red, Blue, Yellow
  298. type
  299. ObjectVarint3 = object
  300. case kind: Color = Blue
  301. of Red:
  302. data1: int = 10
  303. of Blue:
  304. case name: Color = Blue
  305. of Blue:
  306. go = 12
  307. else:
  308. temp = 66
  309. fill2 = "123"
  310. cry: float
  311. of Yellow:
  312. time3 = 1.8'f32
  313. him: int
  314. block:
  315. var x = default(ObjectVarint3)
  316. doAssert x.kind == Blue
  317. doAssert x.name == Blue
  318. doAssert x.go == 12
  319. block:
  320. var x = ObjectVarint3(kind: Blue, name: Red, temp: 99)
  321. doAssert x.kind == Blue
  322. doAssert x.name == Red
  323. doAssert x.temp == 99
  324. block:
  325. type
  326. Default = tuple
  327. id: int = 1
  328. obj: ObjectBase
  329. name: string
  330. Class = object
  331. def: Default
  332. Member = object
  333. def: Default = (id: 777, obj: ObjectBase(), name: "fine")
  334. block:
  335. var x = default(Default)
  336. doAssert x.id == 1
  337. doAssert x.obj == default(ObjectBase)
  338. doAssert x.name == ""
  339. block:
  340. var x = default(Class)
  341. doAssert x.def == default(Default)
  342. doAssert x.def.id == 1
  343. doAssert x.def.obj == default(ObjectBase)
  344. doAssert x.def.name == ""
  345. block:
  346. var x = default(Member)
  347. doAssert x.def.id == 777
  348. doAssert x.def.obj == default(ObjectBase)
  349. doAssert x.def.name == "fine"
  350. block:
  351. var x {.noinit.} = 12
  352. doAssert x == 12
  353. type
  354. Pure = object
  355. id: int = 12
  356. var y {.noinit.}: Pure
  357. doAssert y.id == 0
  358. var z {.noinit.}: Pure = Pure(id: 77)
  359. doAssert z.id == 77
  360. block: # bug #20681
  361. type A = object
  362. d: DateTime = DateTime()
  363. let x = default(A)
  364. doAssert $x == "(d: Uninitialized DateTime)"
  365. block: # bug #20715
  366. block:
  367. type
  368. Foo = enum
  369. A
  370. B
  371. Bar = object
  372. case foo: Foo
  373. of A:
  374. t: range[-1..2]
  375. else: discard
  376. var d = default(Bar)
  377. doAssert d.t == -1
  378. block:
  379. type
  380. Foo = enum
  381. A
  382. B
  383. Bar = object
  384. case foo: Foo
  385. of A:
  386. t: range[0..2]
  387. else: discard
  388. var d = default(Bar)
  389. doAssert d.t == 0
  390. block: # bug #20740
  391. block:
  392. proc foo(x: static DateTime = Datetime()) =
  393. discard
  394. foo()
  395. block:
  396. macro foo(x: static DateTime) =
  397. discard x
  398. macro foo2: untyped =
  399. var x = DateTime()
  400. result = quote do:
  401. foo(`x`)
  402. foo2()
  403. block: # issue #20699
  404. type
  405. Either[A,B] = object
  406. case kind:bool
  407. of false:
  408. b: B
  409. of true:
  410. a: A
  411. O = object of RootRef
  412. proc oToEither(o:O):Either[O,void] =
  413. Either[O,void](kind:true,a: o)
  414. discard oToEither(O())
  415. block: # bug #20695
  416. type
  417. Default = object
  418. tabs: Table[string, int] = initTable[string, int]()
  419. let d = default(Default)
  420. doAssert d.tabs.len == 0
  421. block:
  422. type
  423. Default = object
  424. tabs: Table[string, int] = Table[string, int]()
  425. let d = default(Default)
  426. doAssert d.tabs.len == 0
  427. block:
  428. type DjangoDateTime = distinct DateTime
  429. type Default = object
  430. data: DjangoDateTime = DjangoDateTime(DateTime())
  431. let x = default(Default)
  432. doAssert x.data is DjangoDateTime
  433. block:
  434. type DjangoDateTime = distinct DateTime
  435. type Default = object
  436. data = DjangoDateTime(DateTime())
  437. let x = default(Default)
  438. doAssert x.data is DjangoDateTime
  439. block:
  440. type
  441. Result2 = object
  442. case o: bool
  443. of false:
  444. e: float
  445. of true:
  446. v {.requiresInit.} : int = 1
  447. proc startSessionSync(): Result2 =
  448. return Result2(o: true)
  449. proc mainSync =
  450. let ff = startSessionSync()
  451. doAssert ff.v == 1
  452. mainSync()
  453. block:
  454. type
  455. Result2 = object
  456. v {.requiresInit.} : int = 1
  457. proc startSessionSync(): Result2 =
  458. return Result2()
  459. proc mainSync =
  460. let ff = startSessionSync()
  461. doAssert ff.v == 1
  462. mainSync()
  463. block: # bug #21801
  464. func evaluate(i: int): float =
  465. 0.0
  466. func evaluate(): float =
  467. 0.0
  468. type SearchOptions = object
  469. evaluation: proc(): float = evaluate
  470. block:
  471. func evaluate(): float =
  472. 0.0
  473. type SearchOptions = object
  474. evaluation: proc(): float = evaluate
  475. block:
  476. func evaluate(i: int): float =
  477. 0.0
  478. type SearchOptions = object
  479. evaluation = evaluate
  480. block:
  481. type
  482. Result[T, E] = object
  483. when T is void:
  484. when E is void:
  485. oResultPrivate: bool
  486. else:
  487. case oResultPrivate: bool
  488. of false:
  489. eResultPrivate: E
  490. of true:
  491. discard
  492. else:
  493. when E is void:
  494. case oResultPrivate: bool
  495. of false:
  496. discard
  497. of true:
  498. vResultPrivate: T
  499. else:
  500. case oResultPrivate: bool
  501. of false:
  502. eResultPrivate: E
  503. of true:
  504. vResultPrivate: T
  505. template `?`[T, E](self: Result[T, E]): auto =
  506. let v = (self)
  507. if not v.oResultPrivate:
  508. when compiles(`assignResult?`(default(typeof(result)))):
  509. when typeof(result) is typeof(v):
  510. `assignResult?`(v)
  511. elif E is void:
  512. `assignResult?`(err(typeof(result)))
  513. else:
  514. `assignResult?`(err(typeof(result), v.eResultPrivate))
  515. return
  516. else:
  517. return
  518. when typeof(result) is typeof(v):
  519. v
  520. elif E is void:
  521. err(typeof(result))
  522. else:
  523. err(typeof(result), v.eResultPrivate)
  524. when not(T is void):
  525. v.vResultPrivate
  526. type R = Result[int, string]
  527. proc testAssignResult() =
  528. var assigned: bool
  529. template `assignResult?`(v: Result) =
  530. assigned = true
  531. result = v
  532. proc failed(): Result[int, string] =
  533. result = default(Result[int, string])
  534. proc calling(): Result[int, string] =
  535. let _ = ? failed()
  536. raiseAssert "false"
  537. let r = calling()
  538. doAssert assigned
  539. when nimvm:
  540. when not defined(js):
  541. testAssignResult()
  542. else:
  543. testAssignResult()
  544. block: # bug #22123
  545. type Thing = object
  546. x: float32 = 1
  547. type ThingWithArray = object
  548. arr: array[256, float32]
  549. n: float32 = 1
  550. type Container = ref object
  551. thing: array[5, Thing]
  552. thing_with_array: array[5, ThingWithArray]
  553. var foo = new Container
  554. doAssert int(foo.thing[0].x) == 1
  555. block: # bug #22613
  556. type
  557. K = enum
  558. A = "a"
  559. B = "b"
  560. T = object
  561. case kind: K = B
  562. of A:
  563. a: int
  564. of B:
  565. b: float
  566. doAssert T().kind == B
  567. block: # bug #22926
  568. type
  569. Direction = enum
  570. North
  571. South
  572. East
  573. West
  574. ArrayObj1 = object
  575. list: array[Direction, int]
  576. ArrayObj2 = object
  577. list: array[Direction, int] = [1, 2, 3, 4]
  578. block:
  579. var a: ArrayObj1
  580. doAssert a.list[West] == 0
  581. var b = default ArrayObj1
  582. doAssert b.list[North] == 0
  583. block:
  584. var a: ArrayObj2
  585. doAssert a.list[West] == 0
  586. var b = default ArrayObj2
  587. doAssert b.list[North] == 1
  588. block:
  589. type limited_float = range[1.2..20.0]
  590. doAssert default(limited_float) == 1.2
  591. block:
  592. type
  593. range1 = range[1..10]
  594. range2 = range[-1..10]
  595. proc foo =
  596. doAssert default(range1) == 1
  597. doAssert default(range2) == -1
  598. let s = default(array[5, range1])
  599. doAssert s == [range1 1, 1, 1, 1, 1]
  600. foo()
  601. block:
  602. type
  603. Object = object
  604. id: range[1.2..29.3]
  605. var s = default(Object)
  606. doAssert s.id == 1.2
  607. block: # bug #23943
  608. type limited_int = range[1..20]
  609. var d: limited_int;
  610. doAssert d == 1
  611. block: # bug #23545
  612. proc evaluate(params: int) =
  613. discard
  614. proc evaluate() =
  615. discard
  616. type SearchInfo = object
  617. evaluation: proc() = evaluate
  618. var a = SearchInfo()
  619. a.evaluation()
  620. block: # bug #23770
  621. type
  622. Enum = enum A, B
  623. Object = object
  624. case a: Enum
  625. of A:
  626. integer: int = 200
  627. of B:
  628. time: string
  629. Simple = object
  630. v = -1
  631. Another = object
  632. o = Object(a: A)
  633. v: Simple
  634. let s1 = Object(a: A)
  635. let s2 = Another()
  636. doAssert s1.integer == 200 and s2.o.integer == 200
  637. static: main()
  638. main()