From b4d9be0f8ae6c97d581e3d32f502a3c323f18475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20M=20G=C3=B3mez?= Date: Fri, 16 Jun 2023 07:23:25 +0100 Subject: [PATCH] fixes ilegal recursion (#22105) --- compiler/semobjconstr.nim | 4 +++- tests/objects/tillegal_recursion2.nim | 6 ++++++ tests/objects/tillegal_recursion3.nim | 6 ++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tests/objects/tillegal_recursion2.nim create mode 100644 tests/objects/tillegal_recursion3.nim diff --git a/compiler/semobjconstr.nim b/compiler/semobjconstr.nim index 28471f016a..d4eba2112c 100644 --- a/compiler/semobjconstr.nim +++ b/compiler/semobjconstr.nim @@ -376,7 +376,9 @@ proc semConstructTypeAux(c: PContext, if status in {initPartial, initNone, initUnknown}: discard collectMissingFields(c, t.n, constrCtx, result.defaults) let base = t[0] - if base == nil: break + if base == nil or base.id == t.id or + base.kind in { tyRef, tyPtr } and base[0].id == t.id: + break t = skipTypes(base, skipPtrs) if t.kind != tyObject: # XXX: This is not supposed to happen, but apparently diff --git a/tests/objects/tillegal_recursion2.nim b/tests/objects/tillegal_recursion2.nim new file mode 100644 index 0000000000..ce2279f04a --- /dev/null +++ b/tests/objects/tillegal_recursion2.nim @@ -0,0 +1,6 @@ +discard """ + errormsg: "Cannot inherit from: 'Foo'" + line: 6 +""" +type + Foo = object of Foo diff --git a/tests/objects/tillegal_recursion3.nim b/tests/objects/tillegal_recursion3.nim new file mode 100644 index 0000000000..c80f29e281 --- /dev/null +++ b/tests/objects/tillegal_recursion3.nim @@ -0,0 +1,6 @@ +discard """ + errormsg: "Cannot inherit from: 'Foo'" + line: 6 +""" +type + Foo = object of ref Foo