From eaf35fd9e5db635a4c7ab88913305cf08c8c0afc Mon Sep 17 00:00:00 2001 From: metagn Date: Thu, 5 Dec 2024 06:57:58 +0300 Subject: [PATCH 1/4] make getType node for generic inst have skipped type fixes #24503, refs #22655 --- compiler/vmdeps.nim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/vmdeps.nim b/compiler/vmdeps.nim index 2a088ac9645b4..1803d4d6217fe 100644 --- a/compiler/vmdeps.nim +++ b/compiler/vmdeps.nim @@ -135,8 +135,8 @@ proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo; if inst: if allowRecursion: result = mapTypeToAstR(t.skipModifier, info) - # keep original type info for getType calls on the output node: - result.typ() = t + # result.typ can be tyGenericBody, give it a proper type: + result.typ() = t.skipModifier else: result = newNodeX(nkBracketExpr) #result.add mapTypeToAst(t.last, info) @@ -145,8 +145,8 @@ proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo; result.add mapTypeToAst(a, info) else: result = mapTypeToAstX(cache, t.skipModifier, info, idgen, inst, allowRecursion) - # keep original type info for getType calls on the output node: - result.typ() = t + # result.typ can be tyGenericBody, give it a proper type: + result.typ() = t.skipModifier of tyGenericBody: if inst: result = mapTypeToAstR(t.typeBodyImpl, info) From 71a60de5885a54fee6d7be01acb99d5d2ef46659 Mon Sep 17 00:00:00 2001 From: metagn Date: Thu, 5 Dec 2024 07:02:55 +0300 Subject: [PATCH 2/4] fix getType on object/distinct types giving bare generic sym fixes #24503, refs #22655, refs #24509 --- compiler/vmdeps.nim | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/compiler/vmdeps.nim b/compiler/vmdeps.nim index 1803d4d6217fe..ba5cde8fc1535 100644 --- a/compiler/vmdeps.nim +++ b/compiler/vmdeps.nim @@ -135,8 +135,6 @@ proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo; if inst: if allowRecursion: result = mapTypeToAstR(t.skipModifier, info) - # result.typ can be tyGenericBody, give it a proper type: - result.typ() = t.skipModifier else: result = newNodeX(nkBracketExpr) #result.add mapTypeToAst(t.last, info) @@ -145,8 +143,6 @@ proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo; result.add mapTypeToAst(a, info) else: result = mapTypeToAstX(cache, t.skipModifier, info, idgen, inst, allowRecursion) - # result.typ can be tyGenericBody, give it a proper type: - result.typ() = t.skipModifier of tyGenericBody: if inst: result = mapTypeToAstR(t.typeBodyImpl, info) @@ -161,7 +157,7 @@ proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo; result = newNodeX(nkDistinctTy) result.add mapTypeToAst(t.skipModifier, info) else: - if allowRecursion or t.sym == nil: + if allowRecursion or t.sym == nil or tfFromGeneric in t.flags: result = mapTypeToBracket("distinct", mDistinct, t, info) else: result = atomicType(t.sym) @@ -185,7 +181,7 @@ proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo; else: result.add newNodeI(nkEmpty, info) else: - if allowRecursion or t.sym == nil: + if allowRecursion or t.sym == nil or tfFromGeneric in t.flags: result = newNodeIT(nkObjectTy, if t.n.isNil: info else: t.n.info, t) result.add newNodeI(nkEmpty, info) if t.baseClass == nil: From c59d95cbd2dbb7f0390e3f210763d7d6d553be6c Mon Sep 17 00:00:00 2001 From: metagn Date: Thu, 5 Dec 2024 07:20:11 +0300 Subject: [PATCH 3/4] still produce sym node, but give it the correct type --- compiler/vmdeps.nim | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/vmdeps.nim b/compiler/vmdeps.nim index ba5cde8fc1535..b49e57d38e120 100644 --- a/compiler/vmdeps.nim +++ b/compiler/vmdeps.nim @@ -157,10 +157,12 @@ proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo; result = newNodeX(nkDistinctTy) result.add mapTypeToAst(t.skipModifier, info) else: - if allowRecursion or t.sym == nil or tfFromGeneric in t.flags: + if allowRecursion or t.sym == nil: result = mapTypeToBracket("distinct", mDistinct, t, info) else: result = atomicType(t.sym) + # keep original type, t.sym can have type tyGenericBody: + result.typ() = t of tyGenericParam, tyForward: result = atomicType(t.sym) of tyObject: @@ -181,7 +183,7 @@ proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo; else: result.add newNodeI(nkEmpty, info) else: - if allowRecursion or t.sym == nil or tfFromGeneric in t.flags: + if allowRecursion or t.sym == nil: result = newNodeIT(nkObjectTy, if t.n.isNil: info else: t.n.info, t) result.add newNodeI(nkEmpty, info) if t.baseClass == nil: @@ -191,6 +193,8 @@ proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo; result.add copyTree(t.n) else: result = atomicType(t.sym) + # keep original type, t.sym can have type tyGenericBody: + result.typ() = t of tyEnum: result = newNodeIT(nkEnumTy, if t.n.isNil: info else: t.n.info, t) result.add newNodeI(nkEmpty, info) # pragma node, currently always empty for enum From 690e2a174c7f145288e42334bc944e8b44362665 Mon Sep 17 00:00:00 2001 From: metagn Date: Fri, 6 Dec 2024 17:09:14 +0300 Subject: [PATCH 4/4] test restricting to invalid cases --- compiler/vmdeps.nim | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/compiler/vmdeps.nim b/compiler/vmdeps.nim index b49e57d38e120..0670b004005ed 100644 --- a/compiler/vmdeps.nim +++ b/compiler/vmdeps.nim @@ -161,8 +161,9 @@ proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo; result = mapTypeToBracket("distinct", mDistinct, t, info) else: result = atomicType(t.sym) - # keep original type, t.sym can have type tyGenericBody: - result.typ() = t + if tfFromGeneric in t.flags and t.sym.typ.kind == tyGenericBody: + # keep original type, t.sym can have type tyGenericBody: + result.typ() = t of tyGenericParam, tyForward: result = atomicType(t.sym) of tyObject: @@ -193,8 +194,9 @@ proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo; result.add copyTree(t.n) else: result = atomicType(t.sym) - # keep original type, t.sym can have type tyGenericBody: - result.typ() = t + if tfFromGeneric in t.flags and t.sym.typ.kind == tyGenericBody: + # keep original type, t.sym can have type tyGenericBody: + result.typ() = t of tyEnum: result = newNodeIT(nkEnumTy, if t.n.isNil: info else: t.n.info, t) result.add newNodeI(nkEmpty, info) # pragma node, currently always empty for enum