fixes #22553; regression of offsetof(T, anFieldOfUncheckedArray) (#24014)

fixes #22553

follow up https://github.com/nim-lang/Nim/pull/21979 which introduced a
char dummy member prepended to objs only containing an UncheckedArray
This commit is contained in:
ringabout
2024-08-27 15:45:30 +08:00
committed by GitHub
parent 4c250d69a8
commit 7e88091de3
3 changed files with 17 additions and 3 deletions

View File

@@ -385,6 +385,13 @@ proc computeSizeAlign(conf: ConfigRef; typ: PType) =
accum.maxAlign = 1
computeObjectOffsetsFoldFunction(conf, typ.n, true, accum)
else:
if typ.baseClass == nil and lacksMTypeField(typ) and typ.n.len == 1 and
typ.n[0].kind == nkSym and
typ.n[0].sym.typ.skipTypes(abstractInst).kind == tyUncheckedArray:
# a dummy field is generated for an object with a single field
# with an UncheckedArray type
assert accum.offset == 0
accum.offset = 1
computeObjectOffsetsFoldFunction(conf, typ.n, false, accum)
let paddingAtEnd = int16(accum.finish())
if typ.sym != nil and

View File

@@ -1406,6 +1406,8 @@ proc commonSuperclass*(a, b: PType): PType =
return t
y = y.baseClass
proc lacksMTypeField*(typ: PType): bool {.inline.} =
(typ.sym != nil and sfPure in typ.sym.flags) or tfFinal in typ.flags
include sizealignoffsetimpl
@@ -1854,6 +1856,3 @@ proc isCharArrayPtr*(t: PType; allowPointerToChar: bool): bool =
result = false
else:
result = false
proc lacksMTypeField*(typ: PType): bool {.inline.} =
(typ.sym != nil and sfPure in typ.sym.flags) or tfFinal in typ.flags

View File

@@ -732,3 +732,11 @@ type
doAssert sizeof(PackedUnion) == 11
doAssert alignof(PackedUnion) == 1
# bug #22553
type
ChunkObj = object
data: UncheckedArray[byte]
doAssert sizeof(ChunkObj) == 1
doAssert offsetOf(ChunkObj, data) == 1