r365406 - [cxx2a] P0624R2 fix: only lambdas with no lambda-capture are default-constructible and assignable.
David Blaikie via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 8 16:24:41 PDT 2019
Author: dblaikie
Date: Mon Jul 8 16:24:41 2019
New Revision: 365406
URL: http://llvm.org/viewvc/llvm-project?rev=365406&view=rev
Log:
[cxx2a] P0624R2 fix: only lambdas with no lambda-capture are default-constructible and assignable.
This is a fix for rG864949 which only disabled default construction and
assignment for lambdas with capture-defaults, where the C++2a draft
disables them for lambdas with any lambda-capture at all.
Patch by Logan Smith!
Differential Revision: https://reviews.llvm.org/D64058
Modified:
cfe/trunk/lib/AST/DeclCXX.cpp
cfe/trunk/lib/Sema/SemaDeclCXX.cpp
cfe/trunk/test/SemaCXX/cxx2a-lambda-default-ctor-assign.cpp
Modified: cfe/trunk/lib/AST/DeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclCXX.cpp?rev=365406&r1=365405&r2=365406&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclCXX.cpp (original)
+++ cfe/trunk/lib/AST/DeclCXX.cpp Mon Jul 8 16:24:41 2019
@@ -645,7 +645,8 @@ bool CXXRecordDecl::lambdaIsDefaultConst
// C++17 [expr.prim.lambda]p21:
// The closure type associated with a lambda-expression has no default
// constructor and a deleted copy assignment operator.
- if (getLambdaCaptureDefault() != LCD_None)
+ if (getLambdaCaptureDefault() != LCD_None ||
+ getLambdaData().NumCaptures != 0)
return false;
return getASTContext().getLangOpts().CPlusPlus2a;
}
Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=365406&r1=365405&r2=365406&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Mon Jul 8 16:24:41 2019
@@ -7238,7 +7238,7 @@ bool Sema::ShouldDeleteSpecialMember(CXX
// The closure type associated with a lambda-expression has a
// deleted (8.4.3) default constructor and a deleted copy
// assignment operator.
- // C++2a adds back these operators if the lambda has no capture-default.
+ // C++2a adds back these operators if the lambda has no lambda-capture.
if (RD->isLambda() && !RD->lambdaIsDefaultConstructibleAndAssignable() &&
(CSM == CXXDefaultConstructor || CSM == CXXCopyAssignment)) {
if (Diagnose)
Modified: cfe/trunk/test/SemaCXX/cxx2a-lambda-default-ctor-assign.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/cxx2a-lambda-default-ctor-assign.cpp?rev=365406&r1=365405&r2=365406&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/cxx2a-lambda-default-ctor-assign.cpp (original)
+++ cfe/trunk/test/SemaCXX/cxx2a-lambda-default-ctor-assign.cpp Mon Jul 8 16:24:41 2019
@@ -1,37 +1,22 @@
// RUN: %clang_cc1 -std=c++2a -verify %s
-auto a = []{};
-decltype(a) a2;
-void f(decltype(a) x, decltype(a) y) {
- x = y;
- x = static_cast<decltype(a)&&>(y);
+void no_capture() {
+ auto x = [] {};
+ decltype(x) y;
+ x = x;
+ x = static_cast<decltype(x)&&>(x);
}
-template<typename T>
-struct X {
- constexpr X() { T::error; } // expected-error {{'::'}}
- X(const X&);
- constexpr X &operator=(const X&) { T::error; } // expected-error {{'::'}}
- constexpr X &operator=(X&&) { T::error; } // expected-error {{'::'}}
-};
-extern X<int> x;
-auto b = [x = x]{}; // expected-note 3{{in instantiation of}}
-decltype(b) b2; // expected-note {{in implicit default constructor}}
-void f(decltype(b) x, decltype(b) y) {
- x = y; // expected-note {{in implicit copy assignment}}
- x = static_cast<decltype(b)&&>(y); // expected-note {{in implicit move assignment}}
+void capture_default(int i) {
+ auto x = [=] {}; // expected-note 2{{candidate constructor}} expected-note 2{{lambda expression begins here}}
+ decltype(x) y; // expected-error {{no matching constructor}}
+ x = x; // expected-error {{cannot be assigned}}
+ x = static_cast<decltype(x)&&>(x); // expected-error {{cannot be assigned}}
}
-struct Y {
- Y() = delete; // expected-note {{deleted}}
- Y(const Y&);
- Y &operator=(const Y&) = delete; // expected-note 2{{deleted}}
- Y &operator=(Y&&) = delete;
-};
-extern Y y;
-auto c = [y = y]{}; // expected-note 3{{deleted because}}
-decltype(c) c2; // expected-error {{deleted}}
-void f(decltype(c) x, decltype(c) y) {
- x = y; // expected-error {{deleted}}
- x = static_cast<decltype(c)&&>(y); // expected-error {{deleted}}
+void explicit_capture(int i) {
+ auto x = [i] {}; // expected-note 2{{candidate constructor}} expected-note 2{{lambda expression begins here}}
+ decltype(x) y; // expected-error {{no matching constructor}}
+ x = x; // expected-error {{cannot be assigned}}
+ x = static_cast<decltype(x)&&>(x); // expected-error {{cannot be assigned}}
}
More information about the cfe-commits
mailing list