summary refs log tree commit diff stats
path: root/tests/stdlib/tsystem.nim
blob: 21dbdb59d684eeb1d041e0c71d0d7d1cc9a4067b (plain) (blame)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
discard """
  matrix: "--mm:refc; --mm:orc"
  targets: "c cpp js"
"""

import stdtest/testutils
import std/assertions

# TODO: in future work move existing `system` tests here, where they belong


template main =
  block: # closure
    proc outer() =
      var a = 0
      proc inner1 = a.inc
      proc inner2 = discard
      doAssert inner1 is "closure"
      doAssert inner2 isnot "closure"
      doAssert inner1 is (proc)
      doAssert inner2 is (proc)
      let inner1b = inner1
      doAssert inner1b is "closure"
      doAssert inner1b == inner1
    outer()

  block: # rawProc, rawProc, bug #17911
    proc outer() =
      var a = 0
      var b = 0
      proc inner1() = a.inc
      proc inner2() = a += 2
      proc inner3() = b.inc
      let inner1b = inner1
      doAssert inner2 != inner1
      doAssert inner3 != inner1
      whenVMorJs: discard
      do:
        doAssert rawProc(inner1b) == rawProc(inner1)
        doAssert rawProc(inner2) != rawProc(inner1)
        doAssert rawProc(inner3) != rawProc(inner1)

        doAssert rawEnv(inner1b) == rawEnv(inner1)
        doAssert rawEnv(inner2) == rawEnv(inner1) # because both use `a`
        # doAssert rawEnv(inner3) != rawEnv(inner1) # because `a` vs `b` # this doesn't hold
    outer()

  block: # system.delete
    block:
      var s = @[1]
      s.delete(0)
      doAssert s == @[]

    block:
      var s = @["foo", "bar"]
      s.delete(1)
      doAssert s == @["foo"]

    when false:
      var s: seq[string]
      doAssertRaises(IndexDefect):
        s.delete(0)

    block:
      doAssert not compiles(@["foo"].delete(-1))

    block: # bug #6710
      var s = @["foo"]
      s.delete(0)
      doAssert s == @[]

    when false: # bug #16544: deleting out of bounds index should raise
      var s = @["foo"]
      doAssertRaises(IndexDefect):
        s.delete(1)

static: main()
main()

# bug #19967
block:
  type
    X = object
      a: string
      b: set[char]
      c: int
      d: float
      e: int64


  var x = X(b: {'a'}, e: 10)

  var y = move x

  doAssert x.a == ""
  doAssert x.b == {}
  doAssert x.c == 0
  doAssert x.d == 0.0
  doAssert x.e == 0

  reset(y)

  doAssert y.a == ""
  doAssert y.b == {}
  doAssert y.c == 0
  doAssert y.d == 0.0
  doAssert y.e == 0

block:
  var x = 2
  var y = move x
  doAssert y == 2
  doAssert x == 0
  reset y
  doAssert y == 0

block:
  type
    X = object
      a: string
      b: float

  var y = X(b: 1314.521)

  reset(y)

  doAssert y.b == 0.0

block:
  type
    X = object
      a: string
      b: string

  var y = X(b: "1314")

  reset(y)

  doAssert y.b == ""

block:
  type
    X = object
      a: string
      b: seq[int]

  var y = X(b: @[1, 3])

  reset(y)

  doAssert y.b == @[]

block:
  type
    X = object
      a: string
      b: tuple[a: int, b: string]

  var y = X(b: (1, "cc"))

  reset(y)

  doAssert y.b == (0, "")

block:
  type
    Color = enum
      Red, Blue, Yellow
    X = object
      a: string
      b: set[Color]

  var y = X(b: {Red, Blue})

  reset(y)
  doAssert y.b == {}

block: # bug #20516
  type Foo = object
    x {.bitsize:4.}: uint
    y {.bitsize:4.}: uint

  when not defined(js):
    let a = create(Foo)
yped = var id = newNodeX(nkIdentDefs) id.add n # name id.add mapTypeToAst(t, info) # type id.add newNodeI(nkEmpty, info) # no assigned value id template newIdentDefs(s): untyped = newIdentDefs(s, s.typ) if inst: if t.sym != nil: # if this node has a symbol if not allowRecursion: # getTypeInst behavior: return symbol return atomicType(t.sym) #else: # getTypeImpl behavior: turn off recursion # allowRecursion = false case t.kind of tyNone: result = atomicType("none", mNone) of tyBool: result = atomicType("bool", mBool) of tyChar: result = atomicType("char", mChar) of tyNil: result = atomicType("nil", mNil) of tyExpr: result = atomicType("expr", mExpr) of tyStmt: result = atomicType("stmt", mStmt) of tyVoid: result = atomicType("void", mVoid) of tyEmpty: result = atomicType("empty", mNone) of tyArray: result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t) result.add atomicType("array", mArray) if inst and t.sons[0].kind == tyRange: var rng = newNodeX(nkInfix) rng.add newIdentNode(getIdent(".."), info) rng.add t.sons[0].n.sons[0].copyTree rng.add t.sons[0].n.sons[1].copyTree result.add rng else: result.add mapTypeToAst(t.sons[0], info) result.add mapTypeToAst(t.sons[1], info) of tyTypeDesc: if t.base != nil: result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t) result.add atomicType("typeDesc", mTypeDesc) result.add mapTypeToAst(t.base, info) else: result = atomicType("typeDesc", mTypeDesc) of tyGenericInvocation: result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t) for i in 0 ..< t.len: result.add mapTypeToAst(t.sons[i], info) of tyGenericInst: if inst: if allowRecursion: result = mapTypeToAstR(t.lastSon, info) else: result = newNodeX(nkBracketExpr) #result.add mapTypeToAst(t.lastSon, info) result.add mapTypeToAst(t[0], info) for i in 1 ..< t.len-1: result.add mapTypeToAst(t.sons[i], info) else: result = mapTypeToAstX(t.lastSon, info, inst, allowRecursion) of tyGenericBody: if inst: result = mapTypeToAstR(t.lastSon, info) else: result = mapTypeToAst(t.lastSon, info) of tyAlias: result = mapTypeToAstX(t.lastSon, info, inst, allowRecursion) of tyOrdinal: result = mapTypeToAst(t.lastSon, info) of tyDistinct: if inst: result = newNodeX(nkDistinctTy) result.add mapTypeToAst(t.sons[0], info) else: if allowRecursion or t.sym == nil: result = mapTypeToBracket("distinct", mDistinct, t, info) else: result = atomicType(t.sym) of tyGenericParam, tyForward: result = atomicType(t.sym) of tyObject: if inst: result = newNodeX(nkObjectTy) result.add newNodeI(nkEmpty, info) # pragmas not reconstructed yet if t.sons[0] == nil: result.add newNodeI(nkEmpty, info) # handle parent object else: var nn = newNodeX(nkOfInherit) nn.add mapTypeToAst(t.sons[0], info) result.add nn if t.n.len > 0: result.add objectNode(t.n) else: result.add newNodeI(nkEmpty, info) else: if allowRecursion or t.sym == nil: result = newNodeIT(nkObjectTy, if t.n.isNil: info else: t.n.info, t) result.add newNodeI(nkEmpty, info) if t.sons[0] == nil: result.add newNodeI(nkEmpty, info) else: result.add mapTypeToAst(t.sons[0], info) result.add copyTree(t.n) else: result = atomicType(t.sym) of tyEnum: result = newNodeIT(nkEnumTy, if t.n.isNil: info else: t.n.info, t) result.add newNodeI(nkEmpty, info) # pragma node, currently always empty for enum for c in t.n.sons: result.add copyTree(c) of tyTuple: if inst: # only named tuples have a node, unnamed tuples don't if t.n.isNil: result = newNodeX(nkTupleConstr) for subType in t.sons: result.add mapTypeToAst(subType, info) else: result = newNodeX(nkTupleTy) for s in t.n.sons: result.add newIdentDefs(s) else: result = mapTypeToBracket("tuple", mTuple, t, info) of tySet: result = mapTypeToBracket("set", mSet, t, info) of tyPtr: if inst: result = newNodeX(nkPtrTy) result.add mapTypeToAst(t.sons[0], info) else: result = mapTypeToBracket("ptr", mPtr, t, info) of tyRef: if inst: result = newNodeX(nkRefTy) result.add mapTypeToAst(t.sons[0], info) else: result = mapTypeToBracket("ref", mRef, t, info) of tyVar: if inst: result = newNodeX(nkVarTy) result.add mapTypeToAst(t.sons[0], info) else: result = mapTypeToBracket("var", mVar, t, info) of tyLent: result = mapTypeToBracket("lent", mBuiltinType, t, info) of tySink: result = mapTypeToBracket("sink", mBuiltinType, t, info) of tySequence: result = mapTypeToBracket("seq", mSeq, t, info) of tyOpt: result = mapTypeToBracket("opt", mOpt, t, info) of tyProc: if inst: result = newNodeX(nkProcTy) var fp = newNodeX(nkFormalParams) if t.sons[0] == nil: fp.add newNodeI(nkEmpty, info) else: fp.add mapTypeToAst(t.sons[0], t.n[0].info) for i in 1..<t.sons.len: fp.add newIdentDefs(t.n[i], t.sons[i]) result.add fp result.add newNodeI(nkEmpty, info) # pragmas aren't reconstructed yet else: result = mapTypeToBracket("proc", mNone, t, info) of tyOpenArray: result = mapTypeToBracket("openArray", mOpenArray, t, info) of tyRange: result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t) result.add atomicType("range", mRange) result.add t.n.sons[0].copyTree result.add t.n.sons[1].copyTree of tyPointer: result = atomicType("pointer", mPointer) of tyString: result = atomicType("string", mString) of tyCString: result = atomicType("cstring", mCString) of tyInt: result = atomicType("int", mInt) of tyInt8: result = atomicType("int8", mInt8) of tyInt16: result = atomicType("int16", mInt16) of tyInt32: result = atomicType("int32", mInt32) of tyInt64: result = atomicType("int64", mInt64) of tyFloat: result = atomicType("float", mFloat) of tyFloat32: result = atomicType("float32", mFloat32) of tyFloat64: result = atomicType("float64", mFloat64) of tyFloat128: result = atomicType("float128", mFloat128) of tyUInt: result = atomicType("uint", mUint) of tyUInt8: result = atomicType("uint8", mUint8) of tyUInt16: result = atomicType("uint16", mUint16) of tyUInt32: result = atomicType("uint32", mUint32) of tyUInt64: result = atomicType("uint64", mUint64) of tyVarargs: result = mapTypeToBracket("varargs", mVarargs, t, info) of tyProxy: result = atomicType("error", mNone) of tyBuiltInTypeClass: result = mapTypeToBracket("builtinTypeClass", mNone, t, info) of tyUserTypeClass, tyUserTypeClassInst: if t.isResolvedUserTypeClass: result = mapTypeToAst(t.lastSon, info) else: result = mapTypeToBracket("concept", mNone, t, info) result.add t.n.copyTree of tyCompositeTypeClass: result = mapTypeToBracket("compositeTypeClass", mNone, t, info) of tyAnd: result = mapTypeToBracket("and", mAnd, t, info) of tyOr: result = mapTypeToBracket("or", mOr, t, info) of tyNot: result = mapTypeToBracket("not", mNot, t, info) of tyAnything: result = atomicType("anything", mNone) of tyInferred: assert false of tyStatic, tyFromExpr: if inst: if t.n != nil: result = t.n.copyTree else: result = atomicType("void", mVoid) else: result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t) result.add atomicType("static", mNone) if t.n != nil: result.add t.n.copyTree of tyUnused, tyOptAsRef: assert(false, "mapTypeToAstX") proc opMapTypeToAst*(t: PType; info: TLineInfo): PNode = result = mapTypeToAstX(t, info, false, true) # the "Inst" version includes generic parameters in the resulting type tree # and also tries to look like the corresponding Nim type declaration proc opMapTypeInstToAst*(t: PType; info: TLineInfo): PNode = result = mapTypeToAstX(t, info, true, false) # the "Impl" version includes generic parameters in the resulting type tree # and also tries to look like the corresponding Nim type implementation proc opMapTypeImplToAst*(t: PType; info: TLineInfo): PNode = result = mapTypeToAstX(t, info, true, true)