summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/semtypes.nim4
-rw-r--r--tests/objects/tobject_default_value.nim39
2 files changed, 41 insertions, 2 deletions
diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim
index aaffa1ea7..26493703d 100644
--- a/compiler/semtypes.nim
+++ b/compiler/semtypes.nim
@@ -222,9 +222,11 @@ proc isRecursiveType(t: PType, cycleDetector: var IntSet): bool =
 
 proc fitDefaultNode(c: PContext, n: PNode): PType =
   let expectedType = if n[^2].kind != nkEmpty: semTypeNode(c, n[^2], nil) else: nil
+  let oldType = n[^1].typ
   n[^1] = semConstExpr(c, n[^1], expectedType = expectedType)
+  n[^1].flags.incl nfSem
   if n[^2].kind != nkEmpty:
-    if expectedType != nil:
+    if expectedType != nil and oldType != expectedType:
       n[^1] = fitNodeConsiderViewType(c, expectedType, n[^1], n[^1].info)
     result = n[^1].typ
   else:
diff --git a/tests/objects/tobject_default_value.nim b/tests/objects/tobject_default_value.nim
index cdc6016e0..150fb0876 100644
--- a/tests/objects/tobject_default_value.nim
+++ b/tests/objects/tobject_default_value.nim
@@ -3,7 +3,7 @@ discard """
   targets: "c cpp js"
 """
 
-import std/[times, macros]
+import std/[times, macros, tables]
 
 type
   Guess = object
@@ -236,6 +236,7 @@ template main {.dirty.} =
     doAssert x.obj.name.scale == 1
 
   when nimvm:
+    # todo
     discard "fixme"
   else:
     when defined(gcArc) or defined(gcOrc):
@@ -523,5 +524,41 @@ template main {.dirty.} =
 
     discard oToEither(O())
 
+  block: # bug #20695
+    type
+      Default = object
+        tabs: Table[string, int] = initTable[string, int]()
+
+    let d = default(Default)
+    doAssert d.tabs.len == 0
+
+  block:
+    type
+      Default = object
+        tabs: Table[string, int] = Table[string, int]()
+
+    let d = default(Default)
+    doAssert d.tabs.len == 0
+
+
+  block:
+    type DjangoDateTime = distinct DateTime
+
+    type Default = object
+      data: DjangoDateTime = DjangoDateTime(DateTime())
+
+    let x = default(Default)
+    doAssert x.data is DjangoDateTime
+
+  block:
+    type DjangoDateTime = distinct DateTime
+
+    type Default = object
+      data = DjangoDateTime(DateTime())
+
+    let x = default(Default)
+    doAssert x.data is DjangoDateTime
+
+
 static: main()
 main()