https://github.com/akash-manna-sky updated https://github.com/llvm/llvm-project/pull/220830
>From 5f5f9975d841542f03a475a0a47cb7a30e5e791c Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Thu, 3 Sep 2026 12:45:48 +0530 Subject: [PATCH 1/2] [Clang] Fix assertion on delegating initializer when the class is an aggregate An invalid constructor (e.g. one declared with a ref-qualifier) is ignored by CXXRecordDecl::addedMember, so the class stays an aggregate. A delegating initializer such as `a() && : a{} {}` then performs aggregate initialization and BuildDelegatingInitializer casts the resulting InitListExpr (or a CXXParenListInitExpr in C++20) to CXXConstructExpr. Treat a delegation that did not select a constructor as a failed initialization and go through the existing RecoveryExpr path. The constructor has already been diagnosed, so no new diagnostic is needed. Fixes #186650 --- clang/docs/ReleaseNotes.md | 4 ++++ clang/lib/Sema/SemaDeclCXX.cpp | 7 +++++++ clang/test/SemaCXX/GH186650.cpp | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 clang/test/SemaCXX/GH186650.cpp diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index 4c2bf55f6ebdd..492aa0bd37159 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -610,6 +610,10 @@ features cannot lower the translation-unit ABI level; in a token that was lexed and cached before the first occurrence was parsed. (#GH214128) +- Fixed an assertion when an invalid constructor (e.g. one declared with a + ref-qualifier) delegated to its own class and the class had no other + constructors, as in `struct A { A() && : A{} {} };`. (#GH186650) + #### Bug Fixes to AST Handling - Fixed a non-deterministic ordering of unused local typedefs that made diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 4e90c496de342..7c03684d930fd 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -4775,6 +4775,13 @@ Sema::BuildDelegatingInitializer(TypeSourceInfo *TInfo, Expr *Init, InitializationSequence InitSeq(*this, DelegationEntity, Kind, Args); ExprResult DelegationInit = InitSeq.Perform(*this, DelegationEntity, Kind, Args, nullptr); + + // Aggregate initialization here means the class has no valid user-provided + // constructor, so the one being defined has already been diagnosed. + if (DelegationInit.isUsable() && !DelegationInit.get()->containsErrors() && + !isa<CXXConstructExpr>(DelegationInit.get())) + DelegationInit = ExprError(); + if (!DelegationInit.isInvalid()) { assert((DelegationInit.get()->containsErrors() || cast<CXXConstructExpr>(DelegationInit.get())->getConstructor()) && diff --git a/clang/test/SemaCXX/GH186650.cpp b/clang/test/SemaCXX/GH186650.cpp new file mode 100644 index 0000000000000..24f6b8fb83a14 --- /dev/null +++ b/clang/test/SemaCXX/GH186650.cpp @@ -0,0 +1,20 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s + +// The constructors are invalid, so the classes are still aggregates. + +struct A { + A() && : A{} {} // expected-error{{ref-qualifier '&&' is not allowed on a constructor}} +}; + +struct B { + int x; + B() const : B{1} {} // expected-error{{'const' qualifier is not allowed on a constructor}} +}; + +#if __cplusplus >= 202002L +struct C { + int x; + C() & : C(1) {} // expected-error{{ref-qualifier '&' is not allowed on a constructor}} +}; +#endif >From 0be6c444957187fb8dc48f170e1f03d9ec967929 Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Thu, 3 Sep 2026 20:50:38 +0530 Subject: [PATCH 2/2] Keep the aggregate initialization instead of clearing it Now, store the expression Perform built as the delegating initializer and only teach the assertion that an aggregate can show up here. --- clang/lib/Sema/SemaDeclCXX.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 7c03684d930fd..d4dabc9178a94 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -4775,15 +4775,11 @@ Sema::BuildDelegatingInitializer(TypeSourceInfo *TInfo, Expr *Init, InitializationSequence InitSeq(*this, DelegationEntity, Kind, Args); ExprResult DelegationInit = InitSeq.Perform(*this, DelegationEntity, Kind, Args, nullptr); - - // Aggregate initialization here means the class has no valid user-provided - // constructor, so the one being defined has already been diagnosed. - if (DelegationInit.isUsable() && !DelegationInit.get()->containsErrors() && - !isa<CXXConstructExpr>(DelegationInit.get())) - DelegationInit = ExprError(); - if (!DelegationInit.isInvalid()) { + // If all user-provided constructors are invalid the class is still an + // aggregate, and the delegation is an aggregate initialization instead. assert((DelegationInit.get()->containsErrors() || + ClassDecl->isAggregate() || cast<CXXConstructExpr>(DelegationInit.get())->getConstructor()) && "Delegating constructor with no target?"); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
