Skip to content

Commit 644ec10

Browse files
committed
[SemaCXX] Implement CWG2137 (list-initialization from objects of the same type)
Differential Revision: https://reviews.llvm.org/D156032
1 parent 35121ad commit 644ec10

File tree

7 files changed

+103
-29
lines changed

7 files changed

+103
-29
lines changed

clang/docs/ReleaseNotes.rst

+2
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ C++2c Feature Support
229229

230230
Resolutions to C++ Defect Reports
231231
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
232+
- Implemented `CWG2137 <https://wg21.link/CWG2137>`_ which allows
233+
list-initialization from objects of the same type.
232234

233235
- Implemented `CWG2598 <https://wg21.link/CWG2598>`_ and `CWG2096 <https://wg21.link/CWG2096>`_,
234236
making unions (that have either no members or at least one literal member) literal types.

clang/lib/Sema/SemaInit.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -4200,7 +4200,7 @@ static OverloadingResult ResolveConstructorOverload(
42004200
/// \param IsListInit Is this list-initialization?
42014201
/// \param IsInitListCopy Is this non-list-initialization resulting from a
42024202
/// list-initialization from {x} where x is the same
4203-
/// type as the entity?
4203+
/// aggregate type as the entity?
42044204
static void TryConstructorInitialization(Sema &S,
42054205
const InitializedEntity &Entity,
42064206
const InitializationKind &Kind,
@@ -4240,8 +4240,8 @@ static void TryConstructorInitialization(Sema &S,
42404240
// ObjC++: Lambda captured by the block in the lambda to block conversion
42414241
// should avoid copy elision.
42424242
if (S.getLangOpts().CPlusPlus17 && !RequireActualConstructor &&
4243-
UnwrappedArgs.size() == 1 && UnwrappedArgs[0]->isPRValue() &&
4244-
S.Context.hasSameUnqualifiedType(UnwrappedArgs[0]->getType(), DestType)) {
4243+
Args.size() == 1 && Args[0]->isPRValue() &&
4244+
S.Context.hasSameUnqualifiedType(Args[0]->getType(), DestType)) {
42454245
// Convert qualifications if necessary.
42464246
Sequence.AddQualificationConversionStep(DestType, VK_PRValue);
42474247
if (ILE)
@@ -4572,9 +4572,9 @@ static void TryListInitialization(Sema &S,
45724572
return;
45734573
}
45744574

4575-
// C++11 [dcl.init.list]p3, per DR1467:
4576-
// - If T is a class type and the initializer list has a single element of
4577-
// type cv U, where U is T or a class derived from T, the object is
4575+
// C++11 [dcl.init.list]p3, per DR1467 and DR2137:
4576+
// - If T is an aggregate class and the initializer list has a single element
4577+
// of type cv U, where U is T or a class derived from T, the object is
45784578
// initialized from that element (by copy-initialization for
45794579
// copy-list-initialization, or by direct-initialization for
45804580
// direct-list-initialization).
@@ -4585,7 +4585,7 @@ static void TryListInitialization(Sema &S,
45854585
// - Otherwise, if T is an aggregate, [...] (continue below).
45864586
if (S.getLangOpts().CPlusPlus11 && InitList->getNumInits() == 1 &&
45874587
!IsDesignatedInit) {
4588-
if (DestType->isRecordType()) {
4588+
if (DestType->isRecordType() && DestType->isAggregateType()) {
45894589
QualType InitType = InitList->getInit(0)->getType();
45904590
if (S.Context.hasSameUnqualifiedType(InitType, DestType) ||
45914591
S.IsDerivedFrom(InitList->getBeginLoc(), InitType, DestType)) {

clang/lib/Sema/SemaOverload.cpp

+28-10
Original file line numberDiff line numberDiff line change
@@ -1568,19 +1568,37 @@ TryUserDefinedConversion(Sema &S, Expr *From, QualType ToType,
15681568
// called for those cases.
15691569
if (CXXConstructorDecl *Constructor
15701570
= dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) {
1571-
QualType FromCanon
1572-
= S.Context.getCanonicalType(From->getType().getUnqualifiedType());
1571+
QualType FromType;
1572+
SourceLocation FromLoc;
1573+
// C++11 [over.ics.list]p6, per DR2137:
1574+
// C++17 [over.ics.list]p6:
1575+
// If C is not an initializer-list constructor and the initializer list
1576+
// has a single element of type cv U, where U is X or a class derived
1577+
// from X, the implicit conversion sequence has Exact Match rank if U is
1578+
// X, or Conversion rank if U is derived from X.
1579+
if (const auto *InitList = dyn_cast<InitListExpr>(From);
1580+
InitList && InitList->getNumInits() == 1 &&
1581+
!S.isInitListConstructor(Constructor)) {
1582+
const Expr *SingleInit = InitList->getInit(0);
1583+
FromType = SingleInit->getType();
1584+
FromLoc = SingleInit->getBeginLoc();
1585+
} else {
1586+
FromType = From->getType();
1587+
FromLoc = From->getBeginLoc();
1588+
}
1589+
QualType FromCanon =
1590+
S.Context.getCanonicalType(FromType.getUnqualifiedType());
15731591
QualType ToCanon
15741592
= S.Context.getCanonicalType(ToType).getUnqualifiedType();
15751593
if (Constructor->isCopyConstructor() &&
15761594
(FromCanon == ToCanon ||
1577-
S.IsDerivedFrom(From->getBeginLoc(), FromCanon, ToCanon))) {
1595+
S.IsDerivedFrom(FromLoc, FromCanon, ToCanon))) {
15781596
// Turn this into a "standard" conversion sequence, so that it
15791597
// gets ranked with standard conversion sequences.
15801598
DeclAccessPair Found = ICS.UserDefined.FoundConversionFunction;
15811599
ICS.setStandard();
15821600
ICS.Standard.setAsIdentityConversion();
1583-
ICS.Standard.setFromType(From->getType());
1601+
ICS.Standard.setFromType(FromType);
15841602
ICS.Standard.setAllToTypes(ToType);
15851603
ICS.Standard.CopyConstructor = Constructor;
15861604
ICS.Standard.FoundCopyConstructor = Found;
@@ -5306,18 +5324,18 @@ TryListConversion(Sema &S, InitListExpr *From, QualType ToType,
53065324
IsDesignatedInit)
53075325
return Result;
53085326

5309-
// Per DR1467:
5310-
// If the parameter type is a class X and the initializer list has a single
5311-
// element of type cv U, where U is X or a class derived from X, the
5312-
// implicit conversion sequence is the one required to convert the element
5313-
// to the parameter type.
5327+
// Per DR1467 and DR2137:
5328+
// If the parameter type is an aggregate class X and the initializer list
5329+
// has a single element of type cv U, where U is X or a class derived from
5330+
// X, the implicit conversion sequence is the one required to convert the
5331+
// element to the parameter type.
53145332
//
53155333
// Otherwise, if the parameter type is a character array [... ]
53165334
// and the initializer list has a single element that is an
53175335
// appropriately-typed string literal (8.5.2 [dcl.init.string]), the
53185336
// implicit conversion sequence is the identity conversion.
53195337
if (From->getNumInits() == 1 && !IsDesignatedInit) {
5320-
if (ToType->isRecordType()) {
5338+
if (ToType->isRecordType() && ToType->isAggregateType()) {
53215339
QualType InitType = From->getInit(0)->getType();
53225340
if (S.Context.hasSameUnqualifiedType(InitType, ToType) ||
53235341
S.IsDerivedFrom(From->getBeginLoc(), InitType, ToType))

clang/test/CXX/drs/dr14xx.cpp

-10
Original file line numberDiff line numberDiff line change
@@ -488,16 +488,6 @@ namespace dr1467 { // dr1467: 3.7 c++11
488488
}
489489
} // nonaggregate
490490

491-
namespace SelfInitIsNotListInit {
492-
struct S {
493-
S();
494-
explicit S(S &);
495-
S(const S &);
496-
};
497-
S s1;
498-
S s2 = {s1}; // ok, not list-initialization so we pick the non-explicit constructor
499-
}
500-
501491
struct NestedInit { int a, b, c; };
502492
NestedInit ni[1] = {{NestedInit{1, 2, 3}}};
503493

clang/test/CXX/drs/dr21xx.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
// cxx98-error@-1 {{variadic macros are a C99 feature}}
1212
#endif
1313

14+
namespace std {
15+
__extension__ typedef __SIZE_TYPE__ size_t;
16+
17+
template<typename E> struct initializer_list {
18+
const E *p; size_t n;
19+
initializer_list(const E *p, size_t n);
20+
initializer_list();
21+
};
22+
}
23+
1424
namespace dr2100 { // dr2100: 12
1525
template<const int *P, bool = true> struct X {};
1626
template<typename T> struct A {
@@ -132,6 +142,41 @@ namespace dr2126 { // dr2126: 12
132142
#endif
133143
}
134144

145+
namespace dr2137 { // dr2137: 18
146+
#if __cplusplus >= 201103L
147+
struct Q {
148+
Q();
149+
Q(Q&&);
150+
Q(std::initializer_list<Q>) = delete; // #dr2137-Qcons
151+
};
152+
153+
Q x = Q { Q() };
154+
// since-cxx11-error@-1 {{call to deleted constructor of 'Q'}}
155+
// since-cxx11-note@#dr2137-Qcons {{'Q' has been explicitly marked deleted here}}
156+
157+
int f(Q); // #dr2137-f
158+
int y = f({ Q() });
159+
// since-cxx11-error@-1 {{call to deleted constructor of 'Q'}}
160+
// since-cxx11-note@#dr2137-Qcons {{'Q' has been explicitly marked deleted here}}
161+
// since-cxx11-note@#dr2137-f {{passing argument to parameter here}}
162+
163+
struct U {
164+
U();
165+
U(const U&);
166+
};
167+
168+
struct Derived : U {
169+
Derived();
170+
Derived(const Derived&);
171+
} d;
172+
173+
int g(Derived);
174+
int g(U(&&)[1]) = delete;
175+
176+
int z = g({ d });
177+
#endif
178+
}
179+
135180
namespace dr2140 { // dr2140: 9
136181
#if __cplusplus >= 201103L
137182
union U { int a; decltype(nullptr) b; };

clang/www/cxx_dr_status.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -12630,7 +12630,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
1263012630
<td><a href="https://cplusplus.github.io/CWG/issues/2137.html">2137</a></td>
1263112631
<td>CD4</td>
1263212632
<td>List-initialization from object of same type</td>
12633-
<td class="unknown" align="center">Unknown</td>
12633+
<td class="unreleased" align="center">Clang 18</td>
1263412634
</tr>
1263512635
<tr id="2138">
1263612636
<td><a href="https://cplusplus.github.io/CWG/issues/2138.html">2138</a></td>

libcxx/test/std/utilities/utility/pairs/pairs.pair/ctor.pair_U_V_move.pass.cpp

+20-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,26 @@ int main(int, char**)
121121
test_pair_rv<CopyOnly, CopyOnly&>();
122122
test_pair_rv<CopyOnly, CopyOnly&&>();
123123

124-
test_pair_rv<ExplicitTypes::CopyOnly, ExplicitTypes::CopyOnly>();
124+
/* For ExplicitTypes::CopyOnly, two of the viable candidates for initializing from a non-const xvalue are:
125+
* pair(const pair&); // (defaulted copy constructor)
126+
* template<class U1, class U2> explicit pair(const pair<U1, U2>&&); [U1 = ExplicitTypes::CopyOnly, U2 = int]
127+
* This results in diverging behavior for test_convertible which uses copy-list-initialization
128+
* Prior to CWG2137, this would have selected the first (non-explicit) ctor as explicit ctors would not be considered
129+
* Afterwards, it should select the second since it is a better match, and then failed because it is explicit
130+
*
131+
* This may change with future defect reports, and some compilers only have partial support for CWG2137,
132+
* so use std::is_convertible directly to avoid a copy-list-initialization
133+
*/
134+
{
135+
using P1 = std::pair<ExplicitTypes::CopyOnly, int>;
136+
using P2 = std::pair<int, ExplicitTypes::CopyOnly>;
137+
using UP1 = std::pair<ExplicitTypes::CopyOnly, int>&&;
138+
using UP2 = std::pair<int, ExplicitTypes::CopyOnly>&&;
139+
static_assert(std::is_constructible<P1, UP1>::value, "");
140+
static_assert(std::is_convertible<P1, UP1>::value, "");
141+
static_assert(std::is_constructible<P2, UP2>::value, "");
142+
static_assert(std::is_convertible<P2, UP2>::value, "");
143+
}
125144
test_pair_rv<ExplicitTypes::CopyOnly, ExplicitTypes::CopyOnly&, true, false>();
126145
test_pair_rv<ExplicitTypes::CopyOnly, ExplicitTypes::CopyOnly&&, true, false>();
127146

0 commit comments

Comments
 (0)