tobjcov.nim 639 B

12345678910111213141516171819202122232425
  1. discard """
  2. action: compile
  3. target: "c"
  4. """
  5. # Covariance is not type safe:
  6. # Note: `nim cpp` makes it a compile error (after codegen), even with:
  7. # `var f = cast[proc (x: var TA) {.nimcall.}](cast[pointer](bp))`, which
  8. # currently removes all the `cast` in cgen'd code, hence the compile error.
  9. type
  10. TA = object of RootObj
  11. a: int
  12. TB = object of TA
  13. b: array[0..5000_000, int]
  14. proc ap(x: var TA) = x.a = -1
  15. proc bp(x: var TB) = x.b[high(x.b)] = -1
  16. # in Nim proc (x: TB) is compatible to proc (x: TA),
  17. # but this is not type safe:
  18. var f = cast[proc (x: var TA) {.nimcall.}](bp)
  19. var a: TA
  20. f(a) # bp expects a TB, but gets a TA