diff options
Diffstat (limited to 'tests/converter')
-rw-r--r-- | tests/converter/m18986.nim | 3 | ||||
-rw-r--r-- | tests/converter/mdontleak.nim | 3 | ||||
-rw-r--r-- | tests/converter/t18986.nim | 10 | ||||
-rw-r--r-- | tests/converter/t21531.nim | 10 | ||||
-rw-r--r-- | tests/converter/t7097.nim | 38 | ||||
-rw-r--r-- | tests/converter/t7098.nim | 4 | ||||
-rw-r--r-- | tests/converter/t9165.nim | 11 | ||||
-rw-r--r-- | tests/converter/tconverter.nim | 11 | ||||
-rw-r--r-- | tests/converter/tconverter_unique_ptr.nim | 15 | ||||
-rw-r--r-- | tests/converter/tconverter_with_varargs.nim | 2 | ||||
-rw-r--r-- | tests/converter/tdontleak.nim | 10 | ||||
-rw-r--r-- | tests/converter/texplicit_conversion.nim | 6 | ||||
-rw-r--r-- | tests/converter/tgenericconverter.nim | 24 |
13 files changed, 135 insertions, 12 deletions
diff --git a/tests/converter/m18986.nim b/tests/converter/m18986.nim new file mode 100644 index 000000000..0ebf343ae --- /dev/null +++ b/tests/converter/m18986.nim @@ -0,0 +1,3 @@ +import std/macros + +converter Lit*(x: uint): NimNode = newLit(x) diff --git a/tests/converter/mdontleak.nim b/tests/converter/mdontleak.nim new file mode 100644 index 000000000..e55c3f87c --- /dev/null +++ b/tests/converter/mdontleak.nim @@ -0,0 +1,3 @@ + +converter toBool(x: uint32): bool = x != 0 +# Note: This convertes is not exported! diff --git a/tests/converter/t18986.nim b/tests/converter/t18986.nim new file mode 100644 index 000000000..ef300fa49 --- /dev/null +++ b/tests/converter/t18986.nim @@ -0,0 +1,10 @@ +discard """ + output: "Found a 0" +""" + +import m18986 except Lit +import std/macros + +# bug #18986 +var x = 0.uint +echo "Found a ", x diff --git a/tests/converter/t21531.nim b/tests/converter/t21531.nim new file mode 100644 index 000000000..b0198684d --- /dev/null +++ b/tests/converter/t21531.nim @@ -0,0 +1,10 @@ +import std/typetraits + +type Foo* = distinct string + +converter toBase*(headers: var Foo): var string = + headers.distinctBase + +proc bar*(headers: var Foo) = + for x in headers: discard + diff --git a/tests/converter/t7097.nim b/tests/converter/t7097.nim new file mode 100644 index 000000000..d8e953080 --- /dev/null +++ b/tests/converter/t7097.nim @@ -0,0 +1,38 @@ +type + Byte* = uint8 + Bytes* = seq[Byte] + + BytesRange* = object + bytes: Bytes + ibegin, iend: int + +proc initBytesRange*(s: var Bytes, ibegin = 0, iend = -1): BytesRange = + let e = if iend < 0: s.len + iend + 1 + else: iend + assert ibegin > 0 and e <= s.len + when defined(gcRefc): + shallow(s) + result.bytes = s + result.ibegin = ibegin + result.iend = e + +template `[]=`*(r: var BytesRange, i: int, v: Byte) = + r.bytes[r.ibegin + i] = v + +converter fromSeq*(s: Bytes): BytesRange = + var seqCopy = s + return initBytesRange(seqCopy) + +type + Reader* = object + data: BytesRange + position: int + +proc readerFromHex*(input: string): Reader = + let totalBytes = input.len div 2 + var backingStore = newSeq[Byte](totalBytes) + result.data = initBytesRange(backingStore) + + for i in 0 ..< totalBytes: + var nextByte = 0 + result.data[i] = Byte(nextByte) # <-------- instantiated from here diff --git a/tests/converter/t7098.nim b/tests/converter/t7098.nim index 8e7634882..30c9c1e25 100644 --- a/tests/converter/t7098.nim +++ b/tests/converter/t7098.nim @@ -14,8 +14,8 @@ proc initBytesRange*(s: var Bytes, ibegin = 0, iend = -1): BytesRange = let e = if iend < 0: s.len + iend + 1 else: iend assert ibegin >= 0 and e <= s.len - - shallow(s) + when defined(gcRefc): + shallow(s) result.bytes = s result.ibegin = ibegin result.iend = e diff --git a/tests/converter/t9165.nim b/tests/converter/t9165.nim new file mode 100644 index 000000000..d0225ffbc --- /dev/null +++ b/tests/converter/t9165.nim @@ -0,0 +1,11 @@ +type ustring = distinct string + +converter toUString(s: string): ustring = ustring(s) +converter toString(s: ustring): string = string(s) + +proc `[]=`*(s: var ustring, slice: Slice[int], replacement: ustring) {.inline.} = + s = replacement + +var s = ustring("123") +s[1..2] = "3" +doAssert s == "3" \ No newline at end of file diff --git a/tests/converter/tconverter.nim b/tests/converter/tconverter.nim new file mode 100644 index 000000000..0bf067c55 --- /dev/null +++ b/tests/converter/tconverter.nim @@ -0,0 +1,11 @@ +discard """ + output: '''fooo fooo''' +""" + +converter intToString[T](i: T): string = "fooo" + +let + foo: string = 1 + bar: string = intToString(2) + +echo foo, " ", bar \ No newline at end of file diff --git a/tests/converter/tconverter_unique_ptr.nim b/tests/converter/tconverter_unique_ptr.nim index 23c1a3d96..6902f9e9e 100644 --- a/tests/converter/tconverter_unique_ptr.nim +++ b/tests/converter/tconverter_unique_ptr.nim @@ -22,12 +22,11 @@ proc `$`(x: MyLen): string {.borrow.} proc `==`(x1, x2: MyLen): bool {.borrow.} -proc `=destroy`*(m: var MySeq) {.inline.} = +proc `=destroy`*(m: MySeq) {.inline.} = if m.data != nil: deallocShared(m.data) - m.data = nil -proc `=`*(m: var MySeq, m2: MySeq) = +proc `=copy`*(m: var MySeq, m2: MySeq) = if m.data == m2.data: return if m.data != nil: `=destroy`(m) @@ -77,13 +76,12 @@ converter literalToLen*(x: int{lit}): MyLen = # Unique pointer implementation #------------------------------------------------------------- -proc `=destroy`*[T](p: var UniquePtr[T]) = +proc `=destroy`*[T](p: UniquePtr[T]) = if p.val != nil: `=destroy`(p.val[]) dealloc(p.val) - p.val = nil -proc `=`*[T](dest: var UniquePtr[T], src: UniquePtr[T]) {.error.} +proc `=copy`*[T](dest: var UniquePtr[T], src: UniquePtr[T]) {.error.} proc `=sink`*[T](dest: var UniquePtr[T], src: UniquePtr[T]) {.inline.} = if dest.val != nil and dest.val != src.val: @@ -118,13 +116,12 @@ type ## as it returns only `lent T` val: ptr T -proc `=destroy`*[T](p: var ConstPtr[T]) = +proc `=destroy`*[T](p: ConstPtr[T]) = if p.val != nil: `=destroy`(p.val[]) dealloc(p.val) - p.val = nil -proc `=`*[T](dest: var ConstPtr[T], src: ConstPtr[T]) {.error.} +proc `=copy`*[T](dest: var ConstPtr[T], src: ConstPtr[T]) {.error.} proc `=sink`*[T](dest: var ConstPtr[T], src: ConstPtr[T]) {.inline.} = if dest.val != nil and dest.val != src.val: diff --git a/tests/converter/tconverter_with_varargs.nim b/tests/converter/tconverter_with_varargs.nim index 6d7e31e85..fae83221b 100644 --- a/tests/converter/tconverter_with_varargs.nim +++ b/tests/converter/tconverter_with_varargs.nim @@ -8,7 +8,7 @@ type converter to_py*(i: int) : PPyRef = nil when false: - proc to_tuple*(vals: openarray[PPyRef]): PPyRef = + proc to_tuple*(vals: openArray[PPyRef]): PPyRef = discard proc abc(args: varargs[PPyRef]) = diff --git a/tests/converter/tdontleak.nim b/tests/converter/tdontleak.nim new file mode 100644 index 000000000..4965fa90a --- /dev/null +++ b/tests/converter/tdontleak.nim @@ -0,0 +1,10 @@ +discard """ + output: '''5''' +joinable: false +""" + +import mdontleak +# bug #19213 + +let a = 5'u32 +echo a diff --git a/tests/converter/texplicit_conversion.nim b/tests/converter/texplicit_conversion.nim index 6b2e96faf..e36d78ad5 100644 --- a/tests/converter/texplicit_conversion.nim +++ b/tests/converter/texplicit_conversion.nim @@ -11,3 +11,9 @@ converter toInt(s: string): int = let x = (int)"234" echo x + +block: # Test for nkconv + proc foo(o: var int) = + assert o == 0 + var a = 0 + foo(int(a)) \ No newline at end of file diff --git a/tests/converter/tgenericconverter.nim b/tests/converter/tgenericconverter.nim index e1c9f7c4c..cbbd2e1b0 100644 --- a/tests/converter/tgenericconverter.nim +++ b/tests/converter/tgenericconverter.nim @@ -28,3 +28,27 @@ aa.x = 666 p aa q aa + + +#------------------------------------------------------------- +# issue #16651 +type + PointTup = tuple + x: float32 + y: float32 + +converter tupleToPoint[T1, T2: SomeFloat](self: tuple[x: T1, y: T2]): PointTup = + result = (self.x.float32, self.y.float32) + +proc tupleToPointX(self: tuple[x: SomeFloat, y: SomeFloat]): PointTup = + result = (self.x.float32, self.y.float32) + +proc tupleToPointX2(self: tuple[x: SomeFloat, y: distinct SomeFloat]): PointTup = + result = (self.x.float32, self.y.float32) + +var t1: PointTup = tupleToPointX((1.0, 0.0)) +var t2: PointTup = tupleToPointX2((1.0, 0.0)) +var t3: PointTup = tupleToPointX2((1.0'f32, 0.0)) +var t4: PointTup = tupleToPointX2((1.0, 0.0'f32)) + +var x2: PointTup = (1.0, 0.0) \ No newline at end of file |