Skip to content

Wrong struct layout (alignment related), followup #4719 #4734

Closed
@JohanEngelen

Description

@JohanEngelen

This is a followup from #4719.

The layout of struct Table (or Item) is wrong since LDC 1.31.

align(1) struct Item {
        KV v;
        uint i;
}
/+
// Also fails:
struct Item {
    align(1):
        KV v;
        uint i;
}+/

struct KV {
        align(1) S* s;
        uint k;
}

struct S {
        Table table;
}

struct Table {
        char a;
        Item v;
}

pragma(msg, Table.sizeof); // "17LU"
pragma(msg, Table.v.offsetof); // "1LU"

void foo(ref Table table, Item a) {
    // should write at offset 1, but LDC >= 1.31 writes at offset 4
    table.v = a;
}

In LLVM IR:

; LDC 1.30
%example.S = type { %example.Table }
%example.Table = type { i8, %example.Item }
%example.Item = type <{ %example.KV, i32 }>
%example.KV = type <{ %example.S*, i32 }>

; LDC >= 1.31
%example.S = type { %example.Table }
%example.Table = type { i8, %example.Item }
%example.Item = type { %example.KV, i32 }  ; < > missing here
%example.KV = type <{ %example.S*, i32 }>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions