Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #23628; Compiler crash for nested generic type instantiations with static[int] #23630

Open
wants to merge 4 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions compiler/semtypinst.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
14 changes: 14 additions & 0 deletions tests/generics/t23628.nim
Original file line number Diff line number Diff line change
@@ -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
Loading