Skip to content

[clang++] Error when inspect the common initial sequence of standard-layout structs of the union #146879

Closed
@safocl

Description

@safocl

Code

consteval void foo(){
    union { struct {int i1; int i2; int i3; int i4;} s;
            struct {int i1; int i2; int i3; int i4;} s2;
            int i[4];
    } u{};
                
    auto sum = u.s2.i1+u.s2.i1+u.s2.i1+u.s2.i1;
}

int main(){
    foo();
}

produces error with

note: read of member 's2' of union with active member 's' is not allowed in a constant expression
    7 |     auto sum = u.s2.i1+u.s2.i1+u.s2.i1+u.s2.i1;
      |                ^

https://godbolt.org/z/dcPWW8ncx
(this tested from clang-x86_64 10.0.0 to clang-x86_64 20.1.0 and trunk version on godbolt)

but:
https://eel.is/c++draft/class.union#general-note-1
https://eel.is/c++draft/class.mem#general-27
https://eel.is/c++draft/class.prop#10
https://eel.is/c++draft/class.mem#def:layout-compatible,class
https://eel.is/c++draft/class.mem#general-29

the s and s2 union members are two standard-layout struct types and have common initial sequence (and what's more: they are layout-compatible).

in my opinion the error behavior here is a bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"constevalC++20 constevalquestionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions