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

[clang] Inconsistent (stateful) compilation of class template default member initializer from parenthesized aggregate initialization #62266

Closed
ecatmur opened this issue Apr 20, 2023 · 9 comments · Fixed by llvm/llvm-project-release-prs#441
Assignees
Labels
c++20 clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party release:backport release:merged

Comments

@ecatmur
Copy link

ecatmur commented Apr 20, 2023

Since 16.0.0 and on trunk 0acb639:

template<int I> struct S { int x, y, z = I; };
constexpr S<7> s1(1, 2);
using _ = decltype(S<7>{});
constexpr S<7> s2(3, 4);
static_assert(s1.z == s2.z);

fails with:

<source>:6:15: error: static assertion failed due to requirement 's1.z == s2.z'
static_assert(s1.z == s2.z);
              ^~~~~~~~~~~~
<source>:6:20: note: expression evaluates to '0 == 7'
static_assert(s1.z == s2.z);
              ~~~~~^~~~~~~

Condensed to a single evaluation:

template<int I> struct S { int x, y, z = I; };
static_assert(S<7>(1, 2).z == S<S<7>{}.z>(3, 4).z);
<source>:8:15: error: static assertion failed due to requirement 'S<7>((1, 2, /*implicit*/(int)0)).z == S<7>((3, 4, )).z'
static_assert(S<7>(1, 2).z == S<S<7>{}.z>(3, 4).z);
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:8:28: note: expression evaluates to '0 == 7'
static_assert(S<7>(1, 2).z == S<S<7>{}.z>(3, 4).z);
              ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~

My guess would be that the default member initializer is lazy initialized in templates but this is not provoked by parenthesized aggregate initialization.

@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed new issue labels Apr 20, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Apr 20, 2023

@llvm/issue-subscribers-clang-frontend

@shafik
Copy link
Collaborator

shafik commented Apr 20, 2023

Confirmed: https://godbolt.org/z/s63oh9eMM

Modified a bit:

template<int I> struct S { int x, y, z = I; };
constexpr S<7> s1a(1, 2);
constexpr S<7> s1{1, 2};

constexpr S<7> s2a(3, 4);
constexpr S<7> s2{3, 4};


int main() {
    return s1.z + s2.z + s1a.z + s2a.z;
}

It does look related to parens init of aggregates maybe @alanzhao1 might have some ideas here.

@shafik shafik added confirmed Verified by a second party c++20 labels Apr 20, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Apr 20, 2023

@llvm/issue-subscribers-c-20

@alanzhao1
Copy link
Contributor

Candidate patch: https://reviews.llvm.org/D149389

@alanzhao1 alanzhao1 self-assigned this Apr 27, 2023
@alanzhao1
Copy link
Contributor

@shafik any thoughts on backporting this to 16.0.xx?

@alanzhao1
Copy link
Contributor

Closing for now since https://reviews.llvm.org/D149389 has been merged.

Feel free to reopen if you think this should be backported to the 16.0.X release branch.

@alanzhao1
Copy link
Contributor

/branch alanzhao1/llvm-project/backport-62266

@alanzhao1
Copy link
Contributor

Backporting per discussion in https://reviews.llvm.org/D150122

@llvmbot
Copy link
Collaborator

llvmbot commented May 9, 2023

/pull-request llvm/llvm-project-release-prs#441

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++20 clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party release:backport release:merged
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

5 participants