tderefblock.nim 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. discard """
  2. matrix: "--mm:refc -d:release -d:danger;--mm:orc -d:useMalloc -d:release -d:danger"
  3. output: "42"
  4. """
  5. # bug #20107
  6. type Foo = object
  7. a, b, c, d: uint64
  8. proc c(i: uint64): Foo =
  9. Foo(a: i, b: i, c: i, d: i)
  10. func x(f: Foo): lent Foo {.inline.} =
  11. f
  12. proc m() =
  13. let f = block:
  14. let i = c(42)
  15. x(i)
  16. echo $f.a
  17. m()
  18. block: # bug #21540
  19. type
  20. Option = object
  21. val: string
  22. has: bool
  23. proc some(val: string): Option =
  24. result.has = true
  25. result.val = val
  26. # Remove lent and it works
  27. proc get(self: Option): lent string =
  28. result = self.val
  29. type
  30. StringStream = ref object
  31. data: string
  32. pos: int
  33. proc readAll(s: StringStream): string =
  34. result = newString(s.data.len)
  35. copyMem(addr(result[0]), addr(s.data[0]), s.data.len)
  36. proc newStringStream(s: string = ""): StringStream =
  37. new(result)
  38. result.data = s
  39. proc parseJson(s: string): string =
  40. let stream = newStringStream(s)
  41. result = stream.readAll()
  42. proc main =
  43. let initialFEN = block:
  44. let initialFEN = some parseJson("startpos")
  45. initialFEN.get
  46. doAssert initialFEN == "startpos"
  47. main()
  48. import std/[
  49. json,
  50. options
  51. ]
  52. block: # bug #21540
  53. let cheek = block:
  54. let initialFEN = some("""{"initialFen": "startpos"}""".parseJson{"initialFen"}.getStr)
  55. initialFEN.get
  56. doAssert cheek == "startpos"