Skip to content

Commit

Permalink
fully disable static paramTypesMatch for tyFromExpr in generics (#24049)
Browse files Browse the repository at this point in the history
fixes #24044

When matching a `tyFromExpr` against a `static` generic parameter,
`paramTypesMatch` tries to evaluate it as a constant expression, which
causes a segfault in the case of #24044. In #24005 a consequence of the
same behavior was an issue where `nkStaticExpr` was created for
`tyFromExpr` which made it not instantiate, so only the generation of
`nkStaticExpr` was disabled. Instead we now just completely ignore
`tyFromExpr` matching a `static` generic parameter in generic contexts
and keep it untouched.
  • Loading branch information
metagn authored Sep 3, 2024
1 parent d27061f commit 1ebdcb3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
9 changes: 4 additions & 5 deletions compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2293,6 +2293,9 @@ proc paramTypesMatchAux(m: var TCandidate, f, a: PType,
a.n == nil and
tfGenericTypeParam notin a.flags:
return newNodeIT(nkType, argOrig.info, makeTypeFromExpr(c, arg))
elif a.kind == tyFromExpr and c.inGenericContext > 0:
# don't try to evaluate
discard
elif arg.kind != nkEmpty:
var evaluated = c.semTryConstExpr(c, arg)
if evaluated != nil:
Expand All @@ -2305,11 +2308,7 @@ proc paramTypesMatchAux(m: var TCandidate, f, a: PType,
a = typ
else:
if m.callee.kind == tyGenericBody:
# we can't use `makeStaticExpr` if `arg` has a generic type
# because it generates `tyStatic`, which semtypinst doesn't touch
# not sure if checking for `tyFromExpr` is enough
if f.kind == tyStatic and typeRel(m, f.base, a) != isNone and
a.kind != tyFromExpr:
if f.kind == tyStatic and typeRel(m, f.base, a) != isNone:
result = makeStaticExpr(m.c, arg)
result.typ.flags.incl tfUnresolved
result.typ.n = arg
Expand Down
8 changes: 8 additions & 0 deletions tests/generics/tuninstantiatedgenericcalls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,14 @@ block: # issue #12714
MyChannel[T: Enqueueable] = object
dummy: type(default(T)[])

block: # issue #24044
type ArrayBuf[N: static int, T = byte] = object
buf: array[N, T]
template maxLen(T: type): int =
sizeof(T) * 2
type MyBuf[I] = ArrayBuf[maxLen(I)]
var v: MyBuf[int]

when false: # issue #22342, type section version of #22607
type GenAlias[isInt: static bool] = (
when isInt:
Expand Down

0 comments on commit 1ebdcb3

Please sign in to comment.