From 6cb8bf8045b72180d66c4e6c26f47435406a0252 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Mon, 30 Oct 2017 09:18:47 +0100 Subject: [PATCH] fixes bug reported in PR #5637 --- compiler/ast.nim | 8 ++++++++ compiler/semtypes.nim | 5 ++++- tests/types/tillegaltyperecursion2.nim | 8 ++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/types/tillegaltyperecursion2.nim diff --git a/compiler/ast.nim b/compiler/ast.nim index e6d967dde1..176c2edca2 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -1387,6 +1387,14 @@ proc skipTypes*(t: PType, kinds: TTypeKinds): PType = result = t while result.kind in kinds: result = lastSon(result) +proc skipTypes*(t: PType, kinds: TTypeKinds; maxIters: int): PType = + result = t + var i = maxIters + while result.kind in kinds: + result = lastSon(result) + dec i + if i == 0: return nil + proc skipTypesOrNil*(t: PType, kinds: TTypeKinds): PType = ## same as skipTypes but handles 'nil' result = t diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index 18ea873411..8c4ad81ad4 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -1167,7 +1167,10 @@ proc semGeneric(c: PContext, n: PNode, s: PSym, prev: PType): PType = # special check for generic object with # generic/partial specialized parent - let tx = result.skipTypes(abstractPtrs) + let tx = result.skipTypes(abstractPtrs, 50) + if tx.isNil: + localError(n.info, "invalid recursion in type '$1'" % typeToString(result[0])) + return errorType(c) if tx != result and tx.kind == tyObject and tx.sons[0] != nil: semObjectTypeForInheritedGenericInst(c, n, tx) diff --git a/tests/types/tillegaltyperecursion2.nim b/tests/types/tillegaltyperecursion2.nim new file mode 100644 index 0000000000..b5ffdda72b --- /dev/null +++ b/tests/types/tillegaltyperecursion2.nim @@ -0,0 +1,8 @@ +discard """ + errormsg: "invalid recursion in type 'Executor'" + line: 8 +""" +# bug reported by PR #5637 +type + Executor[N] = Executor[N] +var e: Executor[int]