MitalAshok created this revision. Herald added a project: All. MitalAshok updated this revision to Diff 543217. MitalAshok added a comment. MitalAshok edited the summary of this revision. MitalAshok added a reviewer: rsmith. MitalAshok published this revision for review. Herald added a project: clang. Herald added a subscriber: cfe-commits.
clang-format ================ Comment at: clang/lib/Sema/SemaInit.cpp:4249-4250 InitializedEntity::EK_LambdaToBlockConversionBlockElement && - UnwrappedArgs.size() == 1 && UnwrappedArgs[0]->isPRValue() && - S.Context.hasSameUnqualifiedType(UnwrappedArgs[0]->getType(), DestType)) { + Args.size() == 1 && Args[0]->isPRValue() && + S.Context.hasSameUnqualifiedType(Args[0]->getType(), DestType)) { // Convert qualifications if necessary. ---------------- This change unfortunately exposes the still-open [[ https://wg21.link/CWG2311 | CWG2311 ]] but allows `T{ object_of_type_T }` to consider user declared constructors. I am working on a separate fix for CWG2311 (Consider constructors as below, but then if the chosen constructor is not an initializer-list constructor, elide it). ================ Comment at: clang/lib/Sema/SemaOverload.cpp:1470-1480 + // C++ [over.ics.list]p6, per DR2137: + // 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<InitListExpr>(From); + InitList && InitList->getNumInits() == 1 && ---------------- This allows with `struct T { T(const T&); } t; void f(T);` for `f({ t })` is an exact match standard conversion instead of a user-defined conversion CWG2137 <https://wg21.link/CWG2137> summary: A non-aggregate class being initialized with an initializer list with a single element (previously changed by CWG1467 <https://wg21.link/CWG1467>) should prefer initializer-list constructors over copy constructors, like everywhere else. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D156032 Files: clang/lib/Sema/SemaInit.cpp clang/lib/Sema/SemaOverload.cpp clang/test/CXX/drs/dr14xx.cpp clang/test/CXX/drs/dr21xx.cpp clang/www/cxx_dr_status.html
Index: clang/www/cxx_dr_status.html =================================================================== --- clang/www/cxx_dr_status.html +++ clang/www/cxx_dr_status.html @@ -12629,7 +12629,7 @@ <td><a href="https://cplusplus.github.io/CWG/issues/2137.html">2137</a></td> <td>CD4</td> <td>List-initialization from object of same type</td> - <td class="none" align="center">Unknown</td> + <td class="unreleased" align="center">Clang 17</td> </tr> <tr id="2138"> <td><a href="https://cplusplus.github.io/CWG/issues/2138.html">2138</a></td> Index: clang/test/CXX/drs/dr21xx.cpp =================================================================== --- clang/test/CXX/drs/dr21xx.cpp +++ 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<typename E> struct initializer_list { + const E *p; size_t n; + initializer_list(const E *p, size_t n); + initializer_list(); + }; +} + namespace dr2100 { // dr2100: 12 template<const int *P, bool = true> struct X {}; template<typename T> struct A { @@ -110,6 +120,36 @@ #endif } +namespace dr2137 { // dr2137: 17 +#if __cplusplus >= 201103L + struct Q { + Q(); + Q(Q&&); + Q(std::initializer_list<Q>) = 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; }; Index: clang/test/CXX/drs/dr14xx.cpp =================================================================== --- clang/test/CXX/drs/dr14xx.cpp +++ clang/test/CXX/drs/dr14xx.cpp @@ -423,6 +423,8 @@ } } // nonaggregate +#if 0 // Pending CWG2311 + namespace SelfInitIsNotListInit { struct S { S(); @@ -433,6 +435,8 @@ S s2 = {s1}; // ok, not list-initialization so we pick the non-explicit constructor } +#endif + struct NestedInit { int a, b, c; }; NestedInit ni[1] = {{NestedInit{1, 2, 3}}}; Index: clang/lib/Sema/SemaOverload.cpp =================================================================== --- clang/lib/Sema/SemaOverload.cpp +++ clang/lib/Sema/SemaOverload.cpp @@ -1465,19 +1465,36 @@ // called for those cases. if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { - QualType FromCanon - = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); + QualType FromType; + SourceLocation FromLoc; + // C++ [over.ics.list]p6, per DR2137: + // 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<InitListExpr>(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; @@ -5168,18 +5185,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)) Index: clang/lib/Sema/SemaInit.cpp =================================================================== --- clang/lib/Sema/SemaInit.cpp +++ clang/lib/Sema/SemaInit.cpp @@ -4246,8 +4246,8 @@ Entity.getKind() != InitializedEntity::EK_Delegating && Entity.getKind() != InitializedEntity::EK_LambdaToBlockConversionBlockElement && - UnwrappedArgs.size() == 1 && UnwrappedArgs[0]->isPRValue() && - S.Context.hasSameUnqualifiedType(UnwrappedArgs[0]->getType(), DestType)) { + Args.size() == 1 && Args[0]->isPRValue() && + S.Context.hasSameUnqualifiedType(Args[0]->getType(), DestType)) { // Convert qualifications if necessary. Sequence.AddQualificationConversionStep(DestType, VK_PRValue); if (ILE) @@ -4567,9 +4567,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). @@ -4580,7 +4580,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)) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits