summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorLemonBoy <LemonBoy@users.noreply.github.com>2018-07-07 20:49:06 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-07-07 20:49:06 +0200
commit88714e77d82e65b68da79bbff1b491e56f874a08 (patch)
tree65e9c35ab1c83a13bb2a3186a5da580d05726d29
parent73f9ce022108491994540272136bb6d454419f2a (diff)
downloadNim-88714e77d82e65b68da79bbff1b491e56f874a08.tar.gz
Fix comparison of tyGenericBody in typerel (#8045)
As shown in #7734 and #7733 the logic in typerel fails to determine that
`type Foo` and `type Foo` are indeed equal.

Fixes #7734
-rw-r--r--compiler/sigmatch.nim2
-rw-r--r--tests/typerel/t7734.nim19
2 files changed, 20 insertions, 1 deletions
diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim
index 523783b36..f9d1edc89 100644
--- a/compiler/sigmatch.nim
+++ b/compiler/sigmatch.nim
@@ -1425,7 +1425,7 @@ proc typeRelImpl(c: var TCandidate, f, aOrig: PType,
 
   of tyGenericBody:
     considerPreviousT:
-      if a.kind == tyGenericInst and a.sons[0] == f:
+      if a == f or a.kind == tyGenericInst and a.sons[0] == f:
         bindingRet isGeneric
       let ff = lastSon(f)
       if ff != nil:
diff --git a/tests/typerel/t7734.nim b/tests/typerel/t7734.nim
new file mode 100644
index 000000000..1e8df2cf1
--- /dev/null
+++ b/tests/typerel/t7734.nim
@@ -0,0 +1,19 @@
+type
+  Foo[T: SomeFloat] = object
+    learning_rate: T
+
+  Bar[T: SomeFloat] = object
+    learning_rate: T
+    momentum: T
+
+  Model = object
+    weight: int
+
+  FooClass = Foo or Bar
+
+
+proc optimizer[M; T: SomeFloat](model: M, _: typedesc[Foo], learning_rate: T): Foo[T] =
+  result.learning_rate = learning_rate
+
+let a = Model(weight: 1)
+let opt = a.optimizer(Foo, 10.0)