diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2020-06-21 19:58:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-21 19:58:37 +0200 |
commit | 3ba0c30758e1044aba11bb908a7d83de7ea215bc (patch) | |
tree | ed9c8151cc4b39de7ed900023ca7bd7cc4e4494e | |
parent | c7dee55b87fe0f35a411b4ec48f833be5537a231 (diff) | |
download | Nim-3ba0c30758e1044aba11bb908a7d83de7ea215bc.tar.gz |
sizeof for empty objects/tuples should be 1; fixes #14690 (#14751)
-rw-r--r-- | compiler/sizealignoffsetimpl.nim | 4 | ||||
-rw-r--r-- | tests/misc/tsizeof.nim | 10 |
2 files changed, 11 insertions, 3 deletions
diff --git a/compiler/sizealignoffsetimpl.nim b/compiler/sizealignoffsetimpl.nim index 9d12297ea..4d73923db 100644 --- a/compiler/sizealignoffsetimpl.nim +++ b/compiler/sizealignoffsetimpl.nim @@ -330,7 +330,7 @@ proc computeSizeAlign(conf: ConfigRef; typ: PType) = sym.offset = accum.offset accum.inc(int(child.size)) typ.paddingAtEnd = int16(accum.finish()) - typ.size = accum.offset + typ.size = if accum.offset == 0: 1 else: accum.offset typ.align = int16(accum.maxAlign) except IllegalTypeRecursionError: typ.paddingAtEnd = szIllegalRecursion @@ -388,7 +388,7 @@ proc computeSizeAlign(conf: ConfigRef; typ: PType) = typ.align = szUnknownSize typ.paddingAtEnd = szUnknownSize else: - typ.size = accum.offset + typ.size = if accum.offset == 0: 1 else: accum.offset typ.align = int16(accum.maxAlign) typ.paddingAtEnd = paddingAtEnd except IllegalTypeRecursionError: diff --git a/tests/misc/tsizeof.nim b/tests/misc/tsizeof.nim index 7b92d3639..579532fcc 100644 --- a/tests/misc/tsizeof.nim +++ b/tests/misc/tsizeof.nim @@ -78,7 +78,7 @@ macro c_offsetof(fieldAccess: typed): int32 = ## Bullet proof implementation that works on actual offsetof operator ## in the c backend. Assuming of course this implementation is ## correct. - let s = if fieldAccess.kind == nnkCheckedFieldExpr: fieldAccess[0] + let s = if fieldAccess.kind == nnkCheckedFieldExpr: fieldAccess[0] else: fieldAccess let a = s[0].getTypeInst let b = s[1] @@ -686,3 +686,11 @@ reject: reject: const off8 = offsetof(MyPackedCaseObject, val5) + + +type + O0 = object + T0 = tuple[] + +doAssert sizeof(O0) == 1 +doAssert sizeof(T0) == 1 |