Skip to content

Commit

Permalink
fixes internal error: no generic body fixes #1500 (#22580)
Browse files Browse the repository at this point in the history
* fixes internal error: no generic body fixes #1500

* adds guard

* adds guard

* removes unnecessary test

* refactor: extracts containsGenericInvocationWithForward
  • Loading branch information
jmgomez authored Sep 1, 2023
1 parent f1789cc commit 0c6e138
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/semdata.nim
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ type
sideEffects*: Table[int, seq[(TLineInfo, PSym)]] # symbol.id index
inUncheckedAssignSection*: int
importModuleLookup*: Table[int, seq[int]] # (module.ident.id, [module.id])
skipTypes*: seq[PNode] # used to skip types between passes in type section. So far only used for inheritance and sets.
skipTypes*: seq[PNode] # used to skip types between passes in type section. So far only used for inheritance, sets and generic bodies.
TBorrowState* = enum
bsNone, bsReturnNotMatch, bsNoDistinct, bsGeneric, bsNotSupported, bsMatch

Expand Down
10 changes: 10 additions & 0 deletions compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,14 @@ proc trySemObjectTypeForInheritedGenericInst(c: PContext, n: PNode, t: PType): b
var newf = newNodeI(nkRecList, n.info)
semRecordNodeAux(c, t.n, check, pos, newf, t)

proc containsGenericInvocationWithForward(n: PNode): bool =
if n.kind == nkSym and n.sym.ast != nil and n.sym.ast.len > 1 and n.sym.ast[2].kind == nkObjectTy:
for p in n.sym.ast[2][^1]:
if p.kind == nkIdentDefs and p[1].typ != nil and p[1].typ.kind == tyGenericInvocation and
p[1][0].kind == nkSym and p[1][0].typ.kind == tyForward:
return true
return false

proc semGeneric(c: PContext, n: PNode, s: PSym, prev: PType): PType =
if s.typ == nil:
localError(c.config, n.info, "cannot instantiate the '$1' $2" %
Expand Down Expand Up @@ -1577,6 +1585,8 @@ proc semGeneric(c: PContext, n: PNode, s: PSym, prev: PType): PType =
# XXX: What kind of error is this? is it still relevant?
localError(c.config, n.info, errCannotInstantiateX % s.name.s)
result = newOrPrevType(tyError, prev, c)
elif containsGenericInvocationWithForward(n[0]):
c.skipTypes.add n #fixes 1500
else:
result = instGenericContainer(c, n.info, result,
allowMetaTypes = false)
Expand Down
8 changes: 8 additions & 0 deletions tests/generics/t1500.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#issue 1500

type
TFtpBase*[SockType] = object
job: TFTPJob[SockType]
PFtpBase*[SockType] = ref TFtpBase[SockType]
TFtpClient* = TFtpBase[string]
TFTPJob[T] = object

0 comments on commit 0c6e138

Please sign in to comment.