diff --git a/compiler/semtypinst.nim b/compiler/semtypinst.nim index 2aca2c7a4baf..6b2e8a679277 100644 --- a/compiler/semtypinst.nim +++ b/compiler/semtypinst.nim @@ -375,6 +375,10 @@ proc lookupTypeVar(cl: var TReplTypeVars, t: PType): PType = result = cl.typeMap.lookup(t) if result == nil: if cl.allowMetaTypes or tfRetType in t.flags: return + + if t.kind == tyStatic: + return nil + localError(cl.c.config, t.sym.info, "cannot instantiate: '" & typeToString(t) & "'") result = errorType(cl.c) # In order to prevent endless recursions, we must remember @@ -431,6 +435,16 @@ proc handleGenericInvocation(cl: var TReplTypeVars, t: PType): PType = if header == t: header = instCopyType(cl, t) header[i] = x propagateToOwner(header, x) + elif x.kind in {tyStatic}: + let y = lookupTypeVar(cl, x) + # In templates the lookup can lead to concrete types like tyInt instead of tyStatic, + # which can't be handled like this. + if y != nil and y.kind == tyStatic: + if header == t: header = instCopyType(cl, t) + header[i] = y + propagateToOwner(header, y) + else: + propagateToOwner(header, x) else: propagateToOwner(header, x) diff --git a/tests/generics/t23628.nim b/tests/generics/t23628.nim new file mode 100644 index 000000000000..3e2750a56f40 --- /dev/null +++ b/tests/generics/t23628.nim @@ -0,0 +1,14 @@ +discard """ +output: '''5 +''' +""" + +type + Bar[T; C: static int] = object + arr: array[C, ptr T] + + Foo[T; C: static int] = object + bar: Bar[Foo[T, C], C] + +var f: Foo[int, 5] +echo f.bar.arr.len