Skip to content

whitelist prev types to reuse in newOrPrevType #24899

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

Merged
merged 2 commits into from
Apr 22, 2025
Merged
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
18 changes: 12 additions & 6 deletions compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,27 @@ const
errNoGenericParamsAllowedForX = "no generic parameters allowed for $1"
errInOutFlagNotExtern = "the '$1' modifier can be used only with imported types"

proc reusePrev(prev: PType): bool {.inline.} =
# only overwrite `prev` if it is a forward type, partial object or magic type
result = prev != nil and (prev.kind == tyForward or (prev.sym != nil and
# partial object marks sym as `sfForward`
(sfForward in prev.sym.flags or prev.sym.magic != mNone)))

proc newOrPrevType(kind: TTypeKind, prev: PType, c: PContext, son: sink PType): PType =
if prev == nil or prev.kind == tyGenericBody:
result = newTypeS(kind, c, son)
else:
if reusePrev(prev):
result = prev
result.setSon(son)
if result.kind == tyForward: result.kind = kind
else:
result = newTypeS(kind, c, son)
#if kind == tyError: result.flags.incl tfCheckedForDestructor

proc newOrPrevType(kind: TTypeKind, prev: PType, c: PContext): PType =
if prev == nil or prev.kind == tyGenericBody:
result = newTypeS(kind, c)
else:
if reusePrev(prev):
result = prev
if result.kind == tyForward: result.kind = kind
else:
result = newTypeS(kind, c)

proc newConstraint(c: PContext, k: TTypeKind): PType =
result = newTypeS(tyBuiltInTypeClass, c)
Expand Down
16 changes: 16 additions & 0 deletions tests/types/tresemtypesection.nim
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ foo:
discard Bar[int](x: 123)
discard Bar[string](x: "abc")

type
Generic1[T] = object
Generic2[T] = ref int
Generic3[T] = ref Generic1[T]
Generic4[T] = Generic2[T]
GenericInst1 = Generic1[int]
GenericInst2 = Generic2[int]
GenericInst3 = Generic3[int]
GenericInst4 = Generic4[int]

# regression test:
template templ(): untyped =
proc injected() {.inject.} = discard
Expand All @@ -49,7 +59,13 @@ foo:
type TestInject = templ()
var x1: TestInject
injected() # normally works

echo $NONE
echo a
var x2: TestInject
injected()

block: # issue #24898
type V[W] = object
template g(d: int) = discard d
g((; type J = V[int]; 0))