Skip to content
Open
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
12 changes: 12 additions & 0 deletions compiler/semtypinst.nim
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,17 @@ proc hasValuelessStatics(n: PNode): bool =
if hasValuelessStatics(x):
return true

proc resetTypeTestPath(n: PNode): bool =
# `mIs` is resolved during semantic checking. Reset cached types along the
# path so nested type tests are checked again after generic substitution.
result = n.kind in nkCallKinds and n[0].kind == nkSym and
n[0].sym.magic == mIs
for i in ord(n.kind in nkCallKinds)..<n.safeLen:
result = resetTypeTestPath(n[i]) or result
if result:
n.typ = nil
n.flags.excl nfSem

proc replaceTypeVarsN(cl: var TReplTypeVars, n: PNode; start=0; expectedType: PType = nil): PNode =
if n == nil: return
result = copyNode(n)
Expand Down Expand Up @@ -324,6 +335,7 @@ proc replaceTypeVarsN(cl: var TReplTypeVars, n: PNode; start=0; expectedType: PT
checkSonsLen(it, 2, cl.c.config)
var cond = prepareNode(cl, it[0])
if not cond.hasValuelessStatics:
discard resetTypeTestPath(cond)
var e = cl.c.semConstExpr(cl.c, cond)
if e.kind != nkIntLit:
internalError(cl.c.config, e.info, "ReplaceTypeVarsN: when condition not a bool")
Expand Down
16 changes: 16 additions & 0 deletions tests/generics/t26069.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
block: # bug #26069
type
ConditionalField[T] = object
when not (T is object):
value: int
else:
value: string

Obj = object
value: int

var objectField: ConditionalField[Obj]
var nonObjectField: ConditionalField[int]

doAssert typeof(objectField.value) is string
doAssert typeof(nonObjectField.value) is int
Loading