aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/SemaInit.cpp
diff options
context:
space:
mode:
authorMital Ashok <mital@mitalashok.co.uk>2024-01-19 20:10:51 +0000
committerGitHub <noreply@github.com>2024-01-19 21:10:51 +0100
commit924701311aa79180e86ad8ce43d253f27d25ec7d (patch)
tree9c676a8f98c7b62bf0e4a28a60b095031ffd3fd5 /clang/lib/Sema/SemaInit.cpp
parent2b31a673de51061d0407b79127054a5083659efc (diff)
downloadllvm-924701311aa79180e86ad8ce43d253f27d25ec7d.zip
llvm-924701311aa79180e86ad8ce43d253f27d25ec7d.tar.gz
llvm-924701311aa79180e86ad8ce43d253f27d25ec7d.tar.bz2
[SemaCXX] Implement CWG2137 (list-initialization from objects of the same type) (#77768)
Closes #77638, #24186 Rebased from <https://reviews.llvm.org/D156032>, see there for more information. Implements wording change in [CWG2137](https://wg21.link/CWG2137) in the first commit. This also implements an approach to [CWG2311](https://wg21.link/CWG2311) in the second commit, because too much code that relies on `T{ T_prvalue}` being an elision would break. Because that issue is still open and the CWG issue doesn't provide wording to fix the issue, there may be different behaviours on other compilers.
Diffstat (limited to 'clang/lib/Sema/SemaInit.cpp')
-rw-r--r--clang/lib/Sema/SemaInit.cpp40
1 files changed, 30 insertions, 10 deletions
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 18440a6..3170f41 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -4200,7 +4200,7 @@ static OverloadingResult ResolveConstructorOverload(
/// \param IsListInit Is this list-initialization?
/// \param IsInitListCopy Is this non-list-initialization resulting from a
/// list-initialization from {x} where x is the same
-/// type as the entity?
+/// aggregate type as the entity?
static void TryConstructorInitialization(Sema &S,
const InitializedEntity &Entity,
const InitializationKind &Kind,
@@ -4230,6 +4230,14 @@ static void TryConstructorInitialization(Sema &S,
Entity.getKind() !=
InitializedEntity::EK_LambdaToBlockConversionBlockElement);
+ 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
@@ -4242,11 +4250,17 @@ static void TryConstructorInitialization(Sema &S,
if (S.getLangOpts().CPlusPlus17 && !RequireActualConstructor &&
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.)
+ assert(!IsInitListCopy &&
+ "IsInitListCopy only possible with aggregate types");
+ CopyElisionPossible = true;
+ } else {
+ ElideConstructor();
+ return;
+ }
}
const RecordType *DestRecordType = DestType->getAs<RecordType>();
@@ -4291,6 +4305,12 @@ static void TryConstructorInitialization(Sema &S,
S, Kind.getLocation(), Args, CandidateSet, DestType, Ctors, Best,
CopyInitialization, AllowExplicit,
/*OnlyListConstructors=*/true, IsListInit, RequireActualConstructor);
+
+ if (CopyElisionPossible && Result == OR_No_Viable_Function) {
+ // No initializer list candidate
+ ElideConstructor();
+ return;
+ }
}
// C++11 [over.match.list]p1:
@@ -4572,9 +4592,9 @@ static void TryListInitialization(Sema &S,
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).
@@ -4585,7 +4605,7 @@ static void TryListInitialization(Sema &S,
// - 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)) {