Skip to content

pass typeid(T) when extending struct arrays so they finalize as structs#5161

Open
badnikhil wants to merge 1 commit into
ldc-developers:masterfrom
badnikhil:fix-issue-5150
Open

pass typeid(T) when extending struct arrays so they finalize as structs#5161
badnikhil wants to merge 1 commit into
ldc-developers:masterfrom
badnikhil:fix-issue-5150

Conversation

@badnikhil

@badnikhil badnikhil commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Fixes #5150. Extending an array of structs that have a destructor (e.g. arr.length = n) lowers to d_arraysetlengthT in core/internal/array/capacity.d, which sets BlkAttr.FINALIZE on the allocated block but never passes typeid(T) to GC.malloc. Without the TypeInfo, the conservative GC's adjustAttrs cannot promote the block to BlkAttr.STRUCTFINAL, so when the block is later collected the GC finalizes it as a class , reading a vtable/ClassInfo pointer out of what is really struct data — and segfaults in rt_finalize2 during the sweep typically reached from gc_term at shutdown).

This passes typeid(T) to both GC.malloc calls in d_arraysetlengthT (the initial allocation and the reallocation path), guarded by version (D_TypeInfo) to match the existing pattern already used in construction.d and utils.d. With the TypeInfo present, adjustAttrs sets STRUCTFINAL and the block is finalized correctly as a struct. This is a direct port of the upstream DMD fix dlang/dmd#22990 (original report dlang/dmd#22884); LDC 1.42 ships the pre-fix druntime, which is why the regression surfaced there.

Verified with the module from the issue (ddn.adam, built with ldc2 -unittest -main): it segfaults in rt_finalize2 on every run before this change and exits cleanly after, using an otherwise identical toolchain that differs only by this patch.

_d_arraysetlengthT_ set FINALIZE without passing the TypeInfo, so the GC never promoted the block to STRUCTFINAL and finalized struct data as a class on sweep -> segfault in rt_finalize2. mirrors dmd #22990.
@badnikhil badnikhil changed the title Fix #5150: pass typeid(T) when extending struct arrays pass typeid(T) when extending struct arrays so they finalize as structs Jun 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LDC2 1.42.0 seems to have the same issue that was fixed by the DMD PR 22990

1 participant