summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorcooldome <ariabushenko@gmail.com>2020-10-08 08:12:03 +0100
committerGitHub <noreply@github.com>2020-10-08 09:12:03 +0200
commitfe187719abf5fa87e12e4092f87c270234fad25c (patch)
tree3c76ba739c0eb9500e5d148a21220ae79087f4ed
parent3b901d1e361f49d48fb64d115e42c04a4a37100c (diff)
downloadNim-fe187719abf5fa87e12e4092f87c270234fad25c.tar.gz
fix #15516 (#15518)
-rw-r--r--compiler/sizealignoffsetimpl.nim10
-rw-r--r--tests/misc/tsizeof.nim18
2 files changed, 23 insertions, 5 deletions
diff --git a/compiler/sizealignoffsetimpl.nim b/compiler/sizealignoffsetimpl.nim
index baa2c40f9..be35247f7 100644
--- a/compiler/sizealignoffsetimpl.nim
+++ b/compiler/sizealignoffsetimpl.nim
@@ -138,12 +138,13 @@ proc computeObjectOffsetsFoldFunction(conf: ConfigRef; n: PNode, packed: bool, a
       accum.offset  = szUnknownSize
       accum.maxAlign = szUnknownSize
     else:
-      # the union neds to be aligned first, before the offsets can be assigned
+      # the union needs to be aligned first, before the offsets can be assigned
       accum.align(maxChildAlign)
       let accumRoot = accum # copy, because each branch should start af the same offset
       for i in 1..<n.len:
-        var branchAccum = accumRoot
+        var branchAccum = OffsetAccum(offset: accumRoot.offset, maxAlign: 1)
         computeObjectOffsetsFoldFunction(conf, n[i].lastSon, packed, branchAccum)
+        discard finish(branchAccum)
         accum.mergeBranch(branchAccum)
   of nkRecList:
     for i, child in n.sons:
@@ -173,9 +174,10 @@ proc computeUnionObjectOffsetsFoldFunction(conf: ConfigRef; n: PNode; packed: bo
     localError(conf, n.info, "Illegal use of ``case`` in union type.")
   of nkRecList:
     let accumRoot = accum # copy, because each branch should start af the same offset
-    for i, child in n.sons:
-      var branchAccum = accumRoot
+    for child in n.sons:
+      var branchAccum = OffsetAccum(offset: accumRoot.offset, maxAlign: 1)
       computeUnionObjectOffsetsFoldFunction(conf, child, packed, branchAccum)
+      discard finish(branchAccum)
       accum.mergeBranch(branchAccum)
   of nkSym:
     var size = szUnknownSize
diff --git a/tests/misc/tsizeof.nim b/tests/misc/tsizeof.nim
index c1d9df611..a09265667 100644
--- a/tests/misc/tsizeof.nim
+++ b/tests/misc/tsizeof.nim
@@ -346,6 +346,21 @@ testinstance:
       c: char
       d: int32  # unaligned
 
+    Kind = enum
+      K1, K2
+  
+    AnotherEnum = enum
+      X1, X2, X3
+
+    MyObject = object
+      s: string
+      case k: Kind
+      of K1: nil
+      of K2:
+          x: float
+          y: int32
+      z: AnotherEnum
+
   const trivialSize = sizeof(TrivialType) # needs to be able to evaluate at compile time
 
   proc main(): void =
@@ -361,6 +376,7 @@ testinstance:
     var go : GenericObject[int64]
     var po : PaddingOfSetEnum33
     var capo: MyCustomAlignPackedObject
+    var issue15516: MyObject
 
     var
       e1: Enum1
@@ -379,7 +395,7 @@ testinstance:
     else:
       doAssert sizeof(SimpleAlignment) > 10
 
-    testSizeAlignOf(t,a,b,c,d,e,f,g,ro,go,po, e1, e2, e4, e8, eoa, eob, capo)
+    testSizeAlignOf(t,a,b,c,d,e,f,g,ro,go,po, e1, e2, e4, e8, eoa, eob, capo, issue15516)
 
     type
       WithBitsize {.objectconfig.} = object