File: tgenericwhen.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 (58 lines) | stat: -rw-r--r-- 1,335 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
discard """
  targets: "c js"
"""

block: # issue #24041
  type ArrayBuf[N: static int, T = byte] = object
    when sizeof(int) > sizeof(uint8):
      when N <= int(uint8.high):
        n: uint8
      else:
        when sizeof(int) > sizeof(uint16):
          when N <= int(uint16.high):
            n: uint16
          else:
            when sizeof(int) > sizeof(uint32):
              when N <= int(uint32.high):
                n: uint32
              else:
                n: int
            else:
              n: int
        else:
          n: int
    else:
      n: int

  var x: ArrayBuf[8]
  doAssert x.n is uint8
  when sizeof(int) > sizeof(uint32):
    var y: ArrayBuf[int(uint32.high) * 8]
    doAssert y.n is int

block: # constant condition after dynamic one
  type Foo[T] = object
    when T is int:
      a: int
    elif true:
      a: string
    else:
      a: bool
  var x: Foo[string]
  doAssert x.a is string
  var y: Foo[int]
  doAssert y.a is int
  var z: Foo[float]
  doAssert z.a is string

block: # issue #4774, but not with threads
  const hasThreadSupport = not defined(js)
  when hasThreadSupport:
    type Channel[T] = object
      value: T
  type
    SomeObj[T] = object
      when hasThreadSupport:
        channel: ptr Channel[T]
  var x: SomeObj[int]
  doAssert compiles(x.channel) == hasThreadSupport