From 9193c85b614530dfb0c34fe7afc11d70e8faa1d1 Mon Sep 17 00:00:00 2001 From: cooldome Date: Thu, 8 Oct 2020 08:12:03 +0100 Subject: [PATCH] fix #15516 (#15518) (cherry picked from commit fe187719abf5fa87e12e4092f87c270234fad25c) --- compiler/sizealignoffsetimpl.nim | 10 ++++++---- tests/misc/tsizeof.nim | 19 +++++++++++++++++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/compiler/sizealignoffsetimpl.nim b/compiler/sizealignoffsetimpl.nim index 1692aaf64c..5cd2f8c3fb 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 ..< len(n): - var branchAccum = accumRoot + var branchAccum = OffsetAccum(offset: accumRoot.offset, maxAlign: 1) computeObjectOffsetsFoldFunction(conf, n.sons[i].lastSon, packed, branchAccum) + discard finish(branchAccum) accum.mergeBranch(branchAccum) of nkRecList: for i, child in n.sons: @@ -171,9 +172,10 @@ proc computeUnionObjectOffsetsFoldFunction(conf: ConfigRef; n: PNode; accum: var 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, 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 a4633021b2..89dd669027 100644 --- a/tests/misc/tsizeof.nim +++ b/tests/misc/tsizeof.nim @@ -336,6 +336,21 @@ testinstance: a: int32 b: T + 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 = @@ -350,6 +365,7 @@ testinstance: var ro : RootObj var go : GenericObject[int64] var po : PaddingOfSetEnum33 + var issue15516: MyObject var e1: Enum1 @@ -368,8 +384,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) - + testSizeAlignOf(t,a,b,c,d,e,f,g,ro,go,po, e1, e2, e4, e8, eoa, eob, issue15516) type WithBitsize {.objectconfig.} = object