fixes #22996; typeAllowedCheck for default fields (#22998)

fixes #22996
This commit is contained in:
ringabout
2023-11-29 17:35:50 +08:00
committed by GitHub
parent 30cf33f04d
commit 795aad4f2a
3 changed files with 15 additions and 2 deletions

View File

@@ -237,6 +237,7 @@ proc isRecursiveType(t: PType, cycleDetector: var IntSet): bool =
return false
proc fitDefaultNode(c: PContext, n: PNode): PType =
inc c.inStaticContext
let expectedType = if n[^2].kind != nkEmpty: semTypeNode(c, n[^2], nil) else: nil
n[^1] = semConstExpr(c, n[^1], expectedType = expectedType)
let oldType = n[^1].typ
@@ -247,6 +248,10 @@ proc fitDefaultNode(c: PContext, n: PNode): PType =
result = n[^1].typ
else:
result = n[^1].typ
# xxx any troubles related to defaults fields, consult `semConst` for a potential answer
if n[^1].kind != nkNilLit:
typeAllowedCheck(c, n.info, result, skConst, {taProcContextIsNotMacro, taIsDefaultField})
dec c.inStaticContext
proc isRecursiveType*(t: PType): bool =
# handle simple recusive types before typeFinalPass

View File

@@ -26,6 +26,7 @@ type
taIsTemplateOrMacro
taProcContextIsNotMacro
taIsCastable
taIsDefaultField
TTypeAllowedFlags* = set[TTypeAllowedFlag]
@@ -172,7 +173,7 @@ proc typeAllowedAux(marker: var IntSet, typ: PType, kind: TSymKind,
elif kind in {skVar, skLet}:
result = t[1]
of tyRef:
if kind == skConst: result = t
if kind == skConst and taIsDefaultField notin flags: result = t
else: result = typeAllowedAux(marker, t.lastSon, kind, c, flags+{taHeap})
of tyPtr:
result = typeAllowedAux(marker, t.lastSon, kind, c, flags+{taHeap})
@@ -182,7 +183,7 @@ proc typeAllowedAux(marker: var IntSet, typ: PType, kind: TSymKind,
if result != nil: break
of tyObject, tyTuple:
if kind in {skProc, skFunc, skConst} and
t.kind == tyObject and t[0] != nil:
t.kind == tyObject and t[0] != nil and taIsDefaultField notin flags:
result = t
else:
let flags = flags+{taField}

7
tests/errmsgs/t22996.nim Normal file
View File

@@ -0,0 +1,7 @@
discard """
errormsg: "invalid type: 'typedesc[string]' for const"
"""
# bug #22996
type MyObject = ref object
_ = string