summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJason Beetham <beefers331@gmail.com>2021-09-06 01:43:26 -0600
committerGitHub <noreply@github.com>2021-09-06 09:43:26 +0200
commit7ae52d779184cd8a16b760e384777a75ed54464b (patch)
tree0b21c63dc567dbf1bab97d826fe3628764785c22
parentf373c17ad926b669bb3b5819ae1dff4bde1da88a (diff)
downloadNim-7ae52d779184cd8a16b760e384777a75ed54464b.tar.gz
Fix recursive generic typed defs (#18809)
-rw-r--r--compiler/semtypinst.nim2
-rw-r--r--tests/generics/trecursivegenerics.nim12
2 files changed, 13 insertions, 1 deletions
diff --git a/compiler/semtypinst.nim b/compiler/semtypinst.nim
index 9f9eb9489..01b21498b 100644
--- a/compiler/semtypinst.nim
+++ b/compiler/semtypinst.nim
@@ -378,7 +378,7 @@ proc handleGenericInvocation(cl: var TReplTypeVars, t: PType): PType =
   cl.typeMap = newTypeMapLayer(cl)
 
   for i in 1..<t.len:
-    var x = replaceTypeVarsT(cl, t[i])
+    var x = replaceTypeVarsT(cl, header[i])
     assert x.kind != tyGenericInvocation
     header[i] = x
     propagateToOwner(header, x)
diff --git a/tests/generics/trecursivegenerics.nim b/tests/generics/trecursivegenerics.nim
new file mode 100644
index 000000000..56c074e3f
--- /dev/null
+++ b/tests/generics/trecursivegenerics.nim
@@ -0,0 +1,12 @@
+block: # Replicates #18728
+  type
+    FlipFlop[A, B] = ref object
+      next: FlipFlop[B, A]
+  
+    Trinary[A, B, C] = ref object
+      next: Trinary[B, C, A]
+  
+  assert typeof(FlipFlop[int, string]().next) is FlipFlop[string, int]
+  assert typeof(FlipFlop[string, int]().next) is FlipFlop[int, string]
+  assert typeof(Trinary[int, float, string]().next) is Trinary[float, string, int]
+  assert typeof(Trinary[int, float, string]().next.next) is Trinary[string, int, float]
\ No newline at end of file