tnon_concrete_cast.nim 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. discard """
  2. errormsg: "cannot cast to a non concrete type: 'ptr SomeNumber'"
  3. line: 36
  4. """
  5. # https://github.com/nim-lang/Nim/issues/5428
  6. type
  7. MemFile = object
  8. mem: pointer
  9. proc memfileopen(filename: string, newFileSize: int): MemFile =
  10. # just a memfile mock
  11. return
  12. type
  13. MyData = object
  14. member1: seq[int]
  15. member2: int
  16. type
  17. MyReadWrite = object
  18. memfile: MemFile
  19. offset: int
  20. # Here, SomeNumber is bound to a concrete type, and that's OK
  21. proc write(rw: var MyReadWrite; value: SomeNumber): void =
  22. (cast[ptr SomeNumber](cast[uint](rw.memfile.mem) + rw.offset.uint))[] = value
  23. rw.offset += sizeof(SomeNumber)
  24. # Here, we try to use SomeNumber without binding it to a type. This should
  25. # produce an error message for now. It's also possible to relax the rules
  26. # and allow for type-class based type inference in such situations.
  27. proc write[T](rw: var MyReadWrite; value: seq[T]): void =
  28. rw.write value.len
  29. let dst = cast[ptr SomeNumber](cast[uint](rw.memfile.mem) + uint(rw.offset))
  30. let src = cast[pointer](value[0].addr)
  31. let size = sizeof(T) * value.len
  32. copyMem(dst, src, size)
  33. rw.offset += size
  34. proc saveBinFile(arg: var MyData, filename: string): void =
  35. var rw: MyReadWrite
  36. rw.memfile = memfileOpen(filename, newFileSize = rw.offset)
  37. rw.offset = 0
  38. rw.write arg.member1