Skip to content

Struct with both bitfields and a slice or class instance ref doesn't compile. #20473

@dlangBugzillaToGithub

Description

@dlangBugzillaToGithub

Jonas Meeuws reported this on 2024-06-22T18:03:34Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=24624

Description

Compiling the following code with -preview=bitfields:
---
struct S
{   
    char[] slice;
    bool flag:1;
}
---
Produces: "Error: cannot take address of bit-field `flag`".

After experimenting it seems that:
  - A struct with bitfields cannot contain:
    - Slices of any type.
    - Class instance references.
    - Structs, enums or references to classes containing any of the above.
  - Bitfields in classes or named unions are not affected.
  - When wrapping the bitfields in a named struct, the issue doesn't happen.
  - With -betterC or ldcs --fno-rtti, the issue doesn't happen.
    The issue might involve TypeInfo_Struct?

More failing examples:
---
struct S1
{
    bool flag:1;
    Object o;
}

struct Wrapper2
{
    void[] wrapped;
}
struct S2
{
    bool flag:1;
    Wrapper2 wrapper;
}

struct Wrapper3
{
    Object wrapped;
}
struct S3
{
    bool flag:1;
    Wrapper3 wrapper;
}

enum Wrapper4 : string
{
    empty = ""
}
struct S4
{
    bool flag:1;
    Wrapper4 wrapper;
}
---

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions