Skip to content

Commit 696d4f9

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

File tree

7 files changed

+98
-29
lines changed

7 files changed

+98
-29
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ C++2c Feature Support
199199

200200
Resolutions to C++ Defect Reports
201201
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
202+
- Implemented `CWG2137 <https://wg21.link/CWG2137>`_ which allows
203+
list-initialization from objects of the same type.
202204

203205
C Language Changes
204206
------------------

clang/lib/Sema/SemaInit.cpp

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

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

clang/lib/Sema/SemaOverload.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,19 +1562,37 @@ TryUserDefinedConversion(Sema &S, Expr *From, QualType ToType,
15621562
// called for those cases.
15631563
if (CXXConstructorDecl *Constructor
15641564
= dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) {
1565-
QualType FromCanon
1566-
= S.Context.getCanonicalType(From->getType().getUnqualifiedType());
1565+
QualType FromType;
1566+
SourceLocation FromLoc;
1567+
// C++11 [over.ics.list]p6, per DR2137:
1568+
// C++17 [over.ics.list]p6:
1569+
// If C is not an initializer-list constructor and the initializer list
1570+
// has a single element of type cv U, where U is X or a class derived
1571+
// from X, the implicit conversion sequence has Exact Match rank if U is
1572+
// X, or Conversion rank if U is derived from X.
1573+
if (const auto *InitList = dyn_cast<InitListExpr>(From);
1574+
InitList && InitList->getNumInits() == 1 &&
1575+
!S.isInitListConstructor(Constructor)) {
1576+
const Expr *SingleInit = InitList->getInit(0);
1577+
FromType = SingleInit->getType();
1578+
FromLoc = SingleInit->getBeginLoc();
1579+
} else {
1580+
FromType = From->getType();
1581+
FromLoc = From->getBeginLoc();
1582+
}
1583+
QualType FromCanon =
1584+
S.Context.getCanonicalType(FromType.getUnqualifiedType());
15671585
QualType ToCanon
15681586
= S.Context.getCanonicalType(ToType).getUnqualifiedType();
15691587
if (Constructor->isCopyConstructor() &&
15701588
(FromCanon == ToCanon ||
1571-
S.IsDerivedFrom(From->getBeginLoc(), FromCanon, ToCanon))) {
1589+
S.IsDerivedFrom(FromLoc, FromCanon, ToCanon))) {
15721590
// Turn this into a "standard" conversion sequence, so that it
15731591
// gets ranked with standard conversion sequences.
15741592
DeclAccessPair Found = ICS.UserDefined.FoundConversionFunction;
15751593
ICS.setStandard();
15761594
ICS.Standard.setAsIdentityConversion();
1577-
ICS.Standard.setFromType(From->getType());
1595+
ICS.Standard.setFromType(FromType);
15781596
ICS.Standard.setAllToTypes(ToType);
15791597
ICS.Standard.CopyConstructor = Constructor;
15801598
ICS.Standard.FoundCopyConstructor = Found;
@@ -5320,18 +5338,18 @@ TryListConversion(Sema &S, InitListExpr *From, QualType ToType,
53205338
IsDesignatedInit)
53215339
return Result;
53225340

5323-
// Per DR1467:
5324-
// If the parameter type is a class X and the initializer list has a single
5325-
// element of type cv U, where U is X or a class derived from X, the
5326-
// implicit conversion sequence is the one required to convert the element
5327-
// to the parameter type.
5341+
// Per DR1467 and DR2137:
5342+
// If the parameter type is an aggregate class X and the initializer list
5343+
// has a single element of type cv U, where U is X or a class derived from
5344+
// X, the implicit conversion sequence is the one required to convert the
5345+
// element to the parameter type.
53285346
//
53295347
// Otherwise, if the parameter type is a character array [... ]
53305348
// and the initializer list has a single element that is an
53315349
// appropriately-typed string literal (8.5.2 [dcl.init.string]), the
53325350
// implicit conversion sequence is the identity conversion.
53335351
if (From->getNumInits() == 1 && !IsDesignatedInit) {
5334-
if (ToType->isRecordType()) {
5352+
if (ToType->isRecordType() && ToType->isAggregateType()) {
53355353
QualType InitType = From->getInit(0)->getType();
53365354
if (S.Context.hasSameUnqualifiedType(InitType, ToType) ||
53375355
S.IsDerivedFrom(From->getBeginLoc(), InitType, ToType))

clang/test/CXX/drs/dr14xx.cpp

Lines changed: 0 additions & 10 deletions
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

Lines changed: 40 additions & 0 deletions
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,36 @@ 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; // since-cxx11-note 2 {{has been explicitly marked deleted here}}
151+
};
152+
153+
Q x = Q { Q() }; // since-cxx11-error {{call to deleted constructor}}
154+
155+
int f(Q); // since-cxx11-note {{passing argument to parameter here}}
156+
int y = f({ Q() }); // since-cxx11-error {{call to deleted constructor}}
157+
158+
struct U {
159+
U();
160+
U(const U&);
161+
};
162+
163+
struct Derived : U {
164+
Derived();
165+
Derived(const Derived&);
166+
} d;
167+
168+
int g(Derived);
169+
int g(U(&&)[1]) = delete;
170+
171+
int z = g({ d });
172+
#endif
173+
}
174+
135175
namespace dr2140 { // dr2140: 9
136176
#if __cplusplus >= 201103L
137177
union U { int a; decltype(nullptr) b; };

clang/www/cxx_dr_status.html

Lines changed: 1 addition & 1 deletion
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

Lines changed: 20 additions & 1 deletion
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)