Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overlapping field initialization in CTFE-generated union literal #20675

Open
ibuclaw opened this issue Jan 9, 2025 · 0 comments
Open

Overlapping field initialization in CTFE-generated union literal #20675

ibuclaw opened this issue Jan 9, 2025 · 0 comments
Labels
Feature:CTFE Compile Time Function Evaluation

Comments

@ibuclaw
Copy link
Member

ibuclaw commented Jan 9, 2025

Reduced from dlang/phobos#9064

union U
{
    struct S
    {
        long l;
    }

    int i;
    S s;
}

enum ctfeInit = {
    U u;
    u.s.l = 1;
    return u;
}();

void main()
{
    static a = ctfeInit;
    auto b = ctfeInit;
    assert(a is b);
}

Assert fails because what CTFE generates is:

enum U ctfeInit = U(0, S(1L));

And dmd backend codegen for static initializers ignores overlapping fields, resulting in the value becoming U(0), while the runtime initializer sets each field individually as b.i = 0; b.s.l = 1;.


Workaround is to declare variable as being void initialized.

enum ctfeInit = {
    U u = void;
    u.s.l = 1;
    return u;
}();

This results in the correct value being generated.

enum U ctfeInit = U(, S(1L));
ibuclaw added a commit to ibuclaw/phobos that referenced this issue Jan 10, 2025
ibuclaw added a commit to ibuclaw/phobos that referenced this issue Jan 10, 2025
thewilsonator pushed a commit to dlang/phobos that referenced this issue Jan 10, 2025
@thewilsonator thewilsonator added the Feature:CTFE Compile Time Function Evaluation label Jan 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:CTFE Compile Time Function Evaluation
Projects
None yet
Development

No branches or pull requests

2 participants