diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index 0b92b2a40f2b1..a2e7bb639a623 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -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 @@ -249,7 +250,8 @@ proc fitDefaultNode(c: PContext, n: PNode): PType = 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}) + typeAllowedCheck(c, n.info, result, skConst, {taProcContextIsNotMacro, taIsDefaultField}) + dec c.inStaticContext proc isRecursiveType*(t: PType): bool = # handle simple recusive types before typeFinalPass diff --git a/compiler/typeallowed.nim b/compiler/typeallowed.nim index d0df66f1851f5..483e55bc9dbd8 100644 --- a/compiler/typeallowed.nim +++ b/compiler/typeallowed.nim @@ -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}