diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -89,6 +89,8 @@ Resolutions to C++ Defect Reports ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +- Implemented `DR2137 `_ which allows + list-initialization from objects of the same type. C Language Changes ------------------ diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -4234,6 +4234,14 @@ return; } + bool CopyElisionPossible = false; + auto ElideConstructor = [&] { + // Convert qualifications if necessary. + Sequence.AddQualificationConversionStep(DestType, VK_PRValue); + if (ILE) + Sequence.RewrapReferenceInitList(DestType, ILE); + }; + // C++17 [dcl.init]p17: // - If the initializer expression is a prvalue and the cv-unqualified // version of the source type is the same class as the class of the @@ -4250,11 +4258,15 @@ InitializedEntity::EK_LambdaToBlockConversionBlockElement && UnwrappedArgs.size() == 1 && UnwrappedArgs[0]->isPRValue() && S.Context.hasSameUnqualifiedType(UnwrappedArgs[0]->getType(), DestType)) { - // Convert qualifications if necessary. - Sequence.AddQualificationConversionStep(DestType, VK_PRValue); - if (ILE) - Sequence.RewrapReferenceInitList(DestType, ILE); - return; + if (ILE && !DestType->isAggregateType()) { + // CWG2311: T{ prvalue_of_type_T } is not eligible for copy elision + // Make this an elision if this won't call an initializer-list constructor + // (Always on an aggregate type or check constructors first) + CopyElisionPossible = true; + } else { + ElideConstructor(); + return; + } } const RecordType *DestRecordType = DestType->getAs(); @@ -4316,6 +4328,28 @@ IsListInit); } if (Result) { + if (CopyElisionPossible) { + switch (Result) { + case OR_No_Viable_Function: // Possible if you try to initialize from a + // volatile prvalue + case OR_Ambiguous: + // This was never going to be an initializer-list constructor + // so it should be elided + ElideConstructor(); + return; + case OR_Deleted: + // If this deleted constructor was not an initializer-list constructor + // we should elide it + if (!S.isInitListConstructor(Best->Function)) { + ElideConstructor(); + return; + } + break; + default: + llvm_unreachable("Invalid OverloadingResult"); + } + } + Sequence.SetOverloadFailure( IsListInit ? InitializationSequence::FK_ListConstructorOverloadFailed : InitializationSequence::FK_ConstructorOverloadFailed, @@ -4334,6 +4368,8 @@ QualType ConvType = CD->getConversionType(); assert(S.Context.hasSameUnqualifiedType(ConvType, DestType) && "should not have selected this conversion function"); + assert(!CopyElisionPossible && + "conversion function picked converting to the same type?"); Sequence.AddUserConversionStep(CD, Best->FoundDecl, ConvType, HadMultipleCandidates); if (!S.Context.hasSameType(ConvType, DestType)) @@ -4344,6 +4380,10 @@ } CXXConstructorDecl *CtorDecl = cast(Best->Function); + if (CopyElisionPossible && !S.isInitListConstructor(CtorDecl)) { + ElideConstructor(); + return; + } if (Result != OR_Deleted) { // C++11 [dcl.init]p6: // If a program calls for the default initialization of an object @@ -4569,9 +4609,9 @@ return; } - // C++11 [dcl.init.list]p3, per DR1467: - // - If T is a class type and the initializer list has a single element of - // type cv U, where U is T or a class derived from T, the object is + // C++11 [dcl.init.list]p3, per DR1467 and DR2137: + // - If T is an aggregate class and the initializer list has a single element + // of type cv U, where U is T or a class derived from T, the object is // initialized from that element (by copy-initialization for // copy-list-initialization, or by direct-initialization for // direct-list-initialization). @@ -4582,7 +4622,7 @@ // - Otherwise, if T is an aggregate, [...] (continue below). if (S.getLangOpts().CPlusPlus11 && InitList->getNumInits() == 1 && !IsDesignatedInit) { - if (DestType->isRecordType()) { + if (DestType->isRecordType() && DestType->isAggregateType()) { QualType InitType = InitList->getInit(0)->getType(); if (S.Context.hasSameUnqualifiedType(InitType, DestType) || S.IsDerivedFrom(InitList->getBeginLoc(), InitType, DestType)) { diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -1465,19 +1465,37 @@ // called for those cases. if (CXXConstructorDecl *Constructor = dyn_cast(ICS.UserDefined.ConversionFunction)) { - QualType FromCanon - = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); + QualType FromType; + SourceLocation FromLoc; + // C++11 [over.ics.list]p6, per DR2137: + // C++17 [over.ics.list]p6: + // If C is not an initializer-list constructor and the initializer list + // has a single element of type cv U, where U is X or a class derived + // from X, the implicit conversion sequence has Exact Match rank if U is + // X, or Conversion rank if U is derived from X. + if (const auto *InitList = dyn_cast(From); + InitList && InitList->getNumInits() == 1 && + !S.isInitListConstructor(Constructor)) { + const Expr *SingleInit = InitList->getInit(0); + FromType = SingleInit->getType(); + FromLoc = SingleInit->getBeginLoc(); + } else { + FromType = From->getType(); + FromLoc = From->getBeginLoc(); + } + QualType FromCanon = + S.Context.getCanonicalType(FromType.getUnqualifiedType()); QualType ToCanon = S.Context.getCanonicalType(ToType).getUnqualifiedType(); if (Constructor->isCopyConstructor() && (FromCanon == ToCanon || - S.IsDerivedFrom(From->getBeginLoc(), FromCanon, ToCanon))) { + S.IsDerivedFrom(FromLoc, FromCanon, ToCanon))) { // Turn this into a "standard" conversion sequence, so that it // gets ranked with standard conversion sequences. DeclAccessPair Found = ICS.UserDefined.FoundConversionFunction; ICS.setStandard(); ICS.Standard.setAsIdentityConversion(); - ICS.Standard.setFromType(From->getType()); + ICS.Standard.setFromType(FromType); ICS.Standard.setAllToTypes(ToType); ICS.Standard.CopyConstructor = Constructor; ICS.Standard.FoundCopyConstructor = Found; @@ -5188,18 +5206,18 @@ IsDesignatedInit) return Result; - // Per DR1467: - // If the parameter type is a class X and the initializer list has a single - // element of type cv U, where U is X or a class derived from X, the - // implicit conversion sequence is the one required to convert the element - // to the parameter type. + // Per DR1467 and DR2137: + // If the parameter type is an aggregate class X and the initializer list + // has a single element of type cv U, where U is X or a class derived from + // X, the implicit conversion sequence is the one required to convert the + // element to the parameter type. // // Otherwise, if the parameter type is a character array [... ] // and the initializer list has a single element that is an // appropriately-typed string literal (8.5.2 [dcl.init.string]), the // implicit conversion sequence is the identity conversion. if (From->getNumInits() == 1 && !IsDesignatedInit) { - if (ToType->isRecordType()) { + if (ToType->isRecordType() && ToType->isAggregateType()) { QualType InitType = From->getInit(0)->getType(); if (S.Context.hasSameUnqualifiedType(InitType, ToType) || S.IsDerivedFrom(From->getBeginLoc(), InitType, ToType)) diff --git a/clang/test/CXX/drs/dr14xx.cpp b/clang/test/CXX/drs/dr14xx.cpp --- a/clang/test/CXX/drs/dr14xx.cpp +++ b/clang/test/CXX/drs/dr14xx.cpp @@ -423,16 +423,6 @@ } } // nonaggregate - namespace SelfInitIsNotListInit { - struct S { - S(); - explicit S(S &); - S(const S &); - }; - S s1; - S s2 = {s1}; // ok, not list-initialization so we pick the non-explicit constructor - } - struct NestedInit { int a, b, c; }; NestedInit ni[1] = {{NestedInit{1, 2, 3}}}; diff --git a/clang/test/CXX/drs/dr21xx.cpp b/clang/test/CXX/drs/dr21xx.cpp --- a/clang/test/CXX/drs/dr21xx.cpp +++ b/clang/test/CXX/drs/dr21xx.cpp @@ -10,6 +10,16 @@ #define static_assert(...) __extension__ _Static_assert(__VA_ARGS__) #endif +namespace std { + __extension__ typedef __SIZE_TYPE__ size_t; + + template struct initializer_list { + const E *p; size_t n; + initializer_list(const E *p, size_t n); + initializer_list(); + }; +} + namespace dr2100 { // dr2100: 12 template struct X {}; template struct A { @@ -110,6 +120,36 @@ #endif } +namespace dr2137 { // dr2137: 18 +#if __cplusplus >= 201103L + struct Q { + Q(); + Q(Q&&); + Q(std::initializer_list) = delete; // expected-note 2 {{has been explicitly marked deleted here}} + }; + + Q x = Q { Q() }; // expected-error {{call to deleted constructor}} + + int f(Q); // expected-note {{passing argument to parameter here}} + int y = f({ Q() }); // expected-error {{call to deleted constructor}} + + struct U { + U(); + U(const U&); + }; + + struct Derived : U { + Derived(); + Derived(const Derived&); + } d; + + int g(Derived); + int g(U(&&)[1]) = delete; + + int z = g({ d }); +#endif +} + namespace dr2140 { // dr2140: 9 #if __cplusplus >= 201103L union U { int a; decltype(nullptr) b; }; diff --git a/clang/test/CXX/drs/dr23xx.cpp b/clang/test/CXX/drs/dr23xx.cpp --- a/clang/test/CXX/drs/dr23xx.cpp +++ b/clang/test/CXX/drs/dr23xx.cpp @@ -5,6 +5,16 @@ // RUN: %clang_cc1 -std=c++20 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors 2>&1 | FileCheck %s // RUN: %clang_cc1 -std=c++23 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors 2>&1 | FileCheck %s +namespace std { + __extension__ typedef __SIZE_TYPE__ size_t; + + template struct initializer_list { + const E *p; size_t n; + initializer_list(const E *p, size_t n); + initializer_list(); + }; +} + #if __cplusplus >= 201103L namespace dr2303 { // dr2303: 12 template @@ -37,6 +47,67 @@ } //namespace dr2303 #endif +namespace dr2311 { +#if __cplusplus >= 201707L +template +void test() { + // Ensure none of these expressions try to call a move constructor + T a = T{T(0)}; + T b{T(0)}; + auto c{T(0)}; + T d = {T(0)}; + auto e = {T(0)}; +#if __cplusplus >= 202302L + auto f = auto{T(0)}; +#endif + void(*fn)(T); + fn({T(0)}); +} + +struct NonMovable { + NonMovable(int); + NonMovable(NonMovable&&) = delete; +}; +struct NonMovableNonApplicableIList { + NonMovableNonApplicableIList(int); + NonMovableNonApplicableIList(NonMovableNonApplicableIList&&) = delete; + NonMovableNonApplicableIList(std::initializer_list); +}; +struct ExplicitMovable { + ExplicitMovable(int); + explicit ExplicitMovable(ExplicitMovable&&); +}; +struct ExplicitNonMovable { + ExplicitNonMovable(int); + explicit ExplicitNonMovable(ExplicitNonMovable&&) = delete; +}; +struct ExplicitNonMovableNonApplicableIList { + ExplicitNonMovableNonApplicableIList(int); + explicit ExplicitNonMovableNonApplicableIList(ExplicitNonMovableNonApplicableIList&&) = delete; + ExplicitNonMovableNonApplicableIList(std::initializer_list); +}; +struct CopyOnly { + CopyOnly(int); + CopyOnly(const CopyOnly&); + CopyOnly(CopyOnly&&) = delete; +}; +struct ExplicitCopyOnly { + ExplicitCopyOnly(int); + explicit ExplicitCopyOnly(const ExplicitCopyOnly&); + explicit ExplicitCopyOnly(ExplicitCopyOnly&&) = delete; +}; + +template void test(); +template void test(); +template void test(); +template void test(); +template void test(); +template void test(); +template void test(); + +#endif +} + // dr2331: na #if __cplusplus >= 201103L diff --git a/clang/www/cxx_dr_status.html b/clang/www/cxx_dr_status.html --- a/clang/www/cxx_dr_status.html +++ b/clang/www/cxx_dr_status.html @@ -12629,7 +12629,7 @@ 2137 CD4 List-initialization from object of same type - Unknown + Clang 18 2138 diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.pair/ctor.pair_U_V_move.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.pair/ctor.pair_U_V_move.pass.cpp --- a/libcxx/test/std/utilities/utility/pairs/pairs.pair/ctor.pair_U_V_move.pass.cpp +++ b/libcxx/test/std/utilities/utility/pairs/pairs.pair/ctor.pair_U_V_move.pass.cpp @@ -121,7 +121,30 @@ test_pair_rv(); test_pair_rv(); - test_pair_rv(); + /* For ExplicitTypes::CopyOnly, two of the viable candidates for initializing from a non-const xvalue are: + * pair(const pair&); // (defaulted copy constructor) + * template explicit pair(const pair&&); [U1 = ExplicitTypes::CopyOnly, U2 = int] + * This results in diverging behavior for test_convertible which uses copy-list-initialization + * Prior to CWG2137, this would have selected the first (non-explicit) ctor as explicit ctors would not be considered + * Afterwards, it should select the second since it is a better match, and then failed because it is explicit + * + * This may change with future defect reports, and some compilers only have partial support for CWG2137, + * so use std::is_convertible directly to avoid a copy-list-initialization + */ + //test_pair_rv(); + static_assert(std::is_constructible, + std::pair&&>::value, + ""); + static_assert(std::is_convertible&&, + std::pair>::value, + ""); + static_assert(std::is_constructible, + std::pair&&>::value, + ""); + static_assert(std::is_convertible&&, + std::pair>::value, + ""); + test_pair_rv(); test_pair_rv();