File: tobjcov.nim

package info (click to toggle)
nim 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,911,644 kB
  • sloc: sh: 24,603; ansic: 1,761; python: 1,492; makefile: 1,013; sql: 298; asm: 141; xml: 13
file content (24 lines) | stat: -rw-r--r-- 640 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
discard """
action: compile
targets: "c"
"""

# Covariance is not type safe:
# Note: `nim cpp` makes it a compile error (after codegen), even with:
# `var f = cast[proc (x: var TA) {.nimcall.}](cast[pointer](bp))`, which
# currently removes all the `cast` in cgen'd code, hence the compile error.

type
  TA = object of RootObj
    a: int
  TB = object of TA
    b: array[0..5000_000, int]

proc ap(x: var TA) = x.a = -1
proc bp(x: var TB) = x.b[high(x.b)] = -1

# in Nim proc (x: TB) is compatible to proc (x: TA),
# but this is not type safe:
var f = cast[proc (x: var TA) {.nimcall.}](bp)
var a: TA
f(a) # bp expects a TB, but gets a TA