summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/semtypes.nim5
-rw-r--r--compiler/typeallowed.nim5
-rw-r--r--tests/errmsgs/t22996.nim7
3 files changed, 15 insertions, 2 deletions
diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim
index db7ce9d73..a2e7bb639 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
@@ -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
diff --git a/compiler/typeallowed.nim b/compiler/typeallowed.nim
index d0df66f18..483e55bc9 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}
diff --git a/tests/errmsgs/t22996.nim b/tests/errmsgs/t22996.nim
new file mode 100644
index 000000000..3a51fd8b0
--- /dev/null
+++ b/tests/errmsgs/t22996.nim
@@ -0,0 +1,7 @@
+discard """
+  errormsg: "invalid type: 'typedesc[string]' for const"
+"""
+
+# bug #22996
+type MyObject = ref object
+  _ = string