summary refs log tree commit diff stats
path: root/tests/generics
diff options
context:
space:
mode:
Diffstat (limited to 'tests/generics')
-rw-r--r--tests/generics/t5570.nim27
-rw-r--r--tests/generics/t5602_inheritence.nim18
-rw-r--r--tests/generics/t5643.nim30
-rw-r--r--tests/generics/t5683.nim31
-rw-r--r--tests/generics/tbindoncevsbindmany.nim68
-rw-r--r--tests/generics/tmapping_generic_alias.nim28
-rw-r--r--tests/generics/tparam_binding.nim28
-rw-r--r--tests/generics/tptrinheritance.nim20
8 files changed, 250 insertions, 0 deletions
diff --git a/tests/generics/t5570.nim b/tests/generics/t5570.nim
new file mode 100644
index 000000000..e3f9ff415
--- /dev/null
+++ b/tests/generics/t5570.nim
@@ -0,0 +1,27 @@
+discard """
+  nimout: "type uint32\ntype uint32"
+  output: "(weight: 17.0, color: 100)"
+"""
+
+import macros
+
+type
+  BaseFruit[T] = object of RootObj
+    color: T
+
+  Banana[T] = object of BaseFruit[uint32]
+    weight: T
+
+macro printTypeName(typ: typed): untyped =
+  echo "type ", getType(typ).repr
+
+proc setColor[K](self: var BaseFruit[K], c: int) =
+  printTypeName(self.color)
+  self.color = uint32(c)
+
+var x: Banana[float64]
+x.weight = 17
+printTypeName(x.color)
+x.setColor(100)
+echo x
+
diff --git a/tests/generics/t5602_inheritence.nim b/tests/generics/t5602_inheritence.nim
new file mode 100644
index 000000000..6d48c796e
--- /dev/null
+++ b/tests/generics/t5602_inheritence.nim
@@ -0,0 +1,18 @@
+discard """
+  output: "seq[float]\n0"
+"""
+
+# https://github.com/nim-lang/Nim/issues/5602
+
+import typetraits
+
+type
+  Foo[T] = object of RootObj
+  Bar[T] = object of Foo[seq[T]]
+
+proc p[T](f: Foo[T]): T =
+  echo T.name
+
+var s: Bar[float]
+echo p(s).len # the bug was: p(s) should return seq[float], but returns float instead
+
diff --git a/tests/generics/t5643.nim b/tests/generics/t5643.nim
new file mode 100644
index 000000000..962d5cef5
--- /dev/null
+++ b/tests/generics/t5643.nim
@@ -0,0 +1,30 @@
+type
+  Matrix*[M, N: static[int], T: SomeReal] = object
+    data: ref array[N * M, T]
+
+  Matrix64*[M, N: static[int]] = Matrix[M, N, float64]
+
+proc zeros64(M,N: static[int]): Matrix64[M,N] =
+  new result.data
+  for i in 0 .. < (M * N):
+    result.data[i] = 0'f64
+
+proc bar*[M,N: static[int], T](a: Matrix[M,N,T], b: Matrix[M,N,T]) =
+  discard
+
+let a = zeros64(2,2)
+bar(a,a)
+  # https://github.com/nim-lang/Nim/issues/5643
+  #
+  # The test case was failing here, because the compiler failed to
+  # detect the two matrix instantiations as the same type.
+  #
+  # The root cause was that the `T` type variable is a different
+  # type after the first Matrix type has been matched.
+  #
+  # Sigmatch was failing to match the second version of `T`, but
+  # due to some complex interplay between tyOr, tyTypeDesc and
+  # tyGenericParam this was allowed to went through. The generic
+  # instantiation of the second matrix was incomplete and the
+  # generic cache lookup failed, producing two separate types.
+
diff --git a/tests/generics/t5683.nim b/tests/generics/t5683.nim
new file mode 100644
index 000000000..38da52ec2
--- /dev/null
+++ b/tests/generics/t5683.nim
@@ -0,0 +1,31 @@
+discard """
+output: "perm: 22 det: 22"
+"""
+
+type Matrix[M,N: static[int]] = array[M, array[N, float]]
+
+proc det[M,N](a: Matrix[M,N]): int = N*10 + M
+proc perm[M,N](a: Matrix[M,N]): int = M*10 + N
+
+const
+  a = [ [1.0, 2.0]
+      , [3.0, 4.0]
+      ]
+
+echo "perm: ", a.perm, " det: ", a.det
+
+# This tests multiple instantiations of a generic
+# proc involving static params:
+type
+  Vector64*[N: static[int]] = ref array[N, float64]
+  Array64[N: static[int]] = array[N, float64]
+
+proc vector*[N: static[int]](xs: Array64[N]): Vector64[N] =
+  new result
+  for i in 0 .. < N:
+    result[i] = xs[i]
+
+let v1 = vector([1.0, 2.0, 3.0, 4.0, 5.0])
+let v2 = vector([1.0, 2.0, 3.0, 4.0, 5.0])
+let v3 = vector([1.0, 2.0, 3.0, 4.0])
+
diff --git a/tests/generics/tbindoncevsbindmany.nim b/tests/generics/tbindoncevsbindmany.nim
new file mode 100644
index 000000000..01e801f0e
--- /dev/null
+++ b/tests/generics/tbindoncevsbindmany.nim
@@ -0,0 +1,68 @@
+template accept(x) =
+  static: assert(compiles(x))
+
+template reject(x) =
+  static: assert(not compiles(x))
+
+type
+  ObjectWithNumber = concept obj
+    obj.number is int
+
+  Foo[T] = object
+    x: T
+
+type A = object
+  anumber: int
+
+type B = object
+  bnumber: int
+
+proc number(a: A): int = a.anumber
+proc number(b: B): int = b.bnumber
+
+proc notDistincConcept1(a: ObjectWithNumber, b: ObjectWithNumber) = discard
+proc notDistincConcept2(a, b: ObjectWithNumber) = discard
+proc distinctConcept1(a, b: distinct ObjectWithNumber) = discard
+proc distinctConcept2(a: ObjectWithNumber, b: distinct ObjectWithNumber) = discard
+proc distinctConcept3(a: distinct ObjectWithNumber, b: ObjectWithNumber) = discard
+proc distinctConcept4(a: distinct ObjectWithNumber, b: distinct ObjectWithNumber) = discard
+
+var a = A(anumber: 5)
+var b = B(bnumber: 6)
+
+accept notDistincConcept1(a, a)
+accept notDistincConcept1(b, b)
+reject notDistincConcept2(a, b)
+
+accept notDistincConcept2(a, a)
+accept notDistincConcept2(b, b)
+reject notDistincConcept2(a, b)
+
+accept distinctConcept1(a, b)
+accept distinctConcept2(a, b)
+accept distinctConcept3(a, b)
+accept distinctConcept4(a, b)
+
+proc nonDistincGeneric1(a: Foo, b: Foo) = discard
+proc nonDistincGeneric2(a, b: Foo) = discard
+proc distinctGeneric1(a, b: distinct Foo) = discard
+proc distinctGeneric2(a: distinct Foo, b: Foo) = discard
+proc distinctGeneric3(a: Foo, b: distinct Foo) = discard
+proc distinctGeneric4(a: distinct Foo, b: distinct Foo) = discard
+
+var f1 = Foo[int](x: 10)
+var f2 = Foo[string](x: "x")
+
+accept nonDistincGeneric1(f1, f1)
+accept nonDistincGeneric1(f2, f2)
+reject nonDistincGeneric1(f1, f2)
+
+accept nonDistincGeneric2(f1, f1)
+accept nonDistincGeneric2(f2, f2)
+reject nonDistincGeneric2(f1, f2)
+
+accept distinctGeneric1(f1, f1)
+accept distinctGeneric2(f1, f1)
+accept distinctGeneric3(f1, f1)
+accept distinctGeneric4(f1, f1)
+
diff --git a/tests/generics/tmapping_generic_alias.nim b/tests/generics/tmapping_generic_alias.nim
new file mode 100644
index 000000000..efdf32ead
--- /dev/null
+++ b/tests/generics/tmapping_generic_alias.nim
@@ -0,0 +1,28 @@
+discard """
+output: '''type(c) = GenAlias[system.int]
+T = int
+seq[int]
+'''
+"""
+
+import typetraits
+
+type
+  Gen[T] = object
+    x: T
+
+  GenAlias[T] = Gen[seq[T]]
+
+proc f1[T](x: Gen[T]) =
+  echo T.name
+
+proc f2[T](x: GenAlias[T]) =
+  echo "type(c) = ", type(x).name
+  echo "T = ", T.name
+  f1 x
+
+let
+  y = Gen[seq[int]](x: @[10])
+
+f2 y
+
diff --git a/tests/generics/tparam_binding.nim b/tests/generics/tparam_binding.nim
new file mode 100644
index 000000000..643e9b226
--- /dev/null
+++ b/tests/generics/tparam_binding.nim
@@ -0,0 +1,28 @@
+discard """
+  errormsg: "got (ref Matrix[2, 2, system.float], ref Matrix[2, 1, system.float])"
+  line: 27
+"""
+
+type
+  Matrix[M,N: static[int]; T: SomeReal] = distinct array[0..(M*N - 1), T]
+
+let a = new Matrix[2,2,float]
+let b = new Matrix[2,1,float]
+
+proc foo[M,N: static[int],T](a: ref Matrix[M, N, T], b: ref Matrix[M, N, T])=
+  discard
+
+foo(a, a)
+
+proc bar[M,N: static[int],T](a: ref Matrix[M, M, T], b: ref Matrix[M, N, T])=
+  discard
+
+bar(a, b)
+bar(a, a)
+
+proc baz[M,N: static[int],T](a: ref Matrix[N, N, T], b: ref Matrix[M, N, T])=
+  discard
+
+baz(a, a)
+baz(a, b)
+
diff --git a/tests/generics/tptrinheritance.nim b/tests/generics/tptrinheritance.nim
new file mode 100644
index 000000000..1e1115fa5
--- /dev/null
+++ b/tests/generics/tptrinheritance.nim
@@ -0,0 +1,20 @@
+type NSPasteboardItem* = ptr object
+type NSPasteboard* = ptr object
+type NSArrayAbstract = ptr object {.inheritable.}
+type NSMutableArrayAbstract = ptr object of NSArrayAbstract
+type NSArray*[T] = ptr object of NSArrayAbstract
+type NSMutableArray*[T] = ptr object of NSArray[T]
+
+proc newMutableArrayAbstract*(): NSMutableArrayAbstract = discard
+
+template newMutableArray*(T: typedesc): NSMutableArray[T] =
+  cast[NSMutableArray[T]](newMutableArrayAbstract())
+
+proc writeObjects*(p: NSPasteboard, o: ptr NSArray[NSPasteboardItem]) = discard
+
+let a = newMutableArray NSPasteboardItem
+var x: NSMutableArray[NSPasteboardItem]
+var y: NSArray[NSPasteboardItem] = x
+
+writeObjects(nil, a)
+