]> granicus.if.org Git - clang/commitdiff
Lambdas have a deleted default constructor and a deleted copy
authorDouglas Gregor <dgregor@apple.com>
Sun, 12 Feb 2012 17:34:23 +0000 (17:34 +0000)
committerDouglas Gregor <dgregor@apple.com>
Sun, 12 Feb 2012 17:34:23 +0000 (17:34 +0000)
assignment operator, per C++ [expr.prim.lambda]p19. Make it so.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@150345 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/Sema.cpp
lib/Sema/SemaDeclCXX.cpp
lib/Sema/SemaLambda.cpp
test/CXX/expr/expr.prim/expr.prim.lambda/p19.cpp [new file with mode: 0644]
test/CXX/expr/expr.prim/expr.prim.lambda/p20.cpp [new file with mode: 0644]
test/CXX/expr/expr.prim/expr.prim.lambda/p5.cpp
test/CXX/expr/expr.prim/expr.prim.lambda/p7.cpp

index 22d2a20d457271a0e6272c9f634b00bab139c1aa..42b102f55a09204efb78febd978f4cdbac118509 100644 (file)
@@ -635,6 +635,7 @@ DeclContext *Sema::getFunctionLevelDeclContext() {
     if (isa<BlockDecl>(DC) || isa<EnumDecl>(DC)) {
       DC = DC->getParent();
     } else if (isa<CXXMethodDecl>(DC) &&
+               cast<CXXMethodDecl>(DC)->getOverloadedOperator() == OO_Call &&
                cast<CXXRecordDecl>(DC->getParent())->isLambda()) {
       DC = DC->getParent()->getParent();
     }
index 467cf43797ed8093e49cb2dbf8f6880b4a1a0870..b8ea85bea9614fe70429f0680bb7706ffd628aca 100644 (file)
@@ -4340,6 +4340,13 @@ bool Sema::ShouldDeleteSpecialMember(CXXMethodDecl *MD, CXXSpecialMember CSM) {
   switch (CSM) {
   case CXXDefaultConstructor:
     IsConstructor = true;
+
+    // C++11 [expr.lambda.prim]p19:
+    //   The closure type associated with a lambda-expression has a
+    //   deleted (8.4.3) default constructor.
+    if (RD->isLambda())
+      return true;
+
     break;
   case CXXCopyConstructor:
     IsConstructor = true;
@@ -4621,6 +4628,12 @@ bool Sema::ShouldDeleteCopyAssignmentOperator(CXXMethodDecl *MD) {
   if (!LangOpts.CPlusPlus0x || RD->isInvalidDecl())
     return false;
 
+  // C++11 [expr.lambda.prim]p19:
+  //   The closure type associated with a lambda-expression has a
+  //   [...] deleted copy assignment operator.
+  if (RD->isLambda())
+    return true;
+
   SourceLocation Loc = MD->getLocation();
 
   // Do access control from the constructor
index e82654ef06d7c99c16801e14c789f6749cd79053..2bd69edb07cf490ce3bb3e2edd97bca0675fac15 100644 (file)
@@ -406,6 +406,13 @@ ExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc,
       CallOperator->setType(FunctionTy);
     }
 
+    // C++ [expr.prim.lambda]p7:
+    //   The lambda-expression's compound-statement yields the
+    //   function-body (8.4) of the function call operator [...].
+    ActOnFinishFunctionBody(CallOperator, Body, /*IsInstantation=*/false);
+    CallOperator->setLexicalDeclContext(Class);
+    Class->addDecl(CallOperator);
+
     // C++11 [expr.prim.lambda]p6:
     //   The closure type for a lambda-expression with no lambda-capture
     //   has a public non-virtual non-explicit const conversion function
@@ -450,15 +457,8 @@ ExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc,
       Class->addDecl(Conversion);
     }
 
-    // C++ [expr.prim.lambda]p7:
-    //   The lambda-expression's compound-statement yields the
-    //   function-body (8.4) of the function call operator [...].
-    ActOnFinishFunctionBody(CallOperator, Body, /*IsInstantation=*/false);
-
     // Finalize the lambda class.
     SmallVector<Decl*, 4> Fields(Class->field_begin(), Class->field_end());
-    CallOperator->setLexicalDeclContext(Class);
-    Class->addDecl(CallOperator);
     ActOnFields(0, Class->getLocation(), Class, Fields, 
                 SourceLocation(), SourceLocation(), 0);
     CheckCompletedCXXClass(Class);
diff --git a/test/CXX/expr/expr.prim/expr.prim.lambda/p19.cpp b/test/CXX/expr/expr.prim/expr.prim.lambda/p19.cpp
new file mode 100644 (file)
index 0000000..b5a445c
--- /dev/null
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 -std=c++11 %s -Wunused -verify
+
+struct MoveOnly {
+  MoveOnly(MoveOnly&&);
+  MoveOnly(const MoveOnly&);
+};
+
+template<typename T> T &&move(T&);
+void test_special_member_functions(MoveOnly mo, int i) {
+  // FIXME: terrible note
+  auto lambda1 = [i]() { }; // expected-note{{function has been explicitly marked deleted here}} \
+  // expected-note{{the implicit copy assignment operator}} \
+  // expected-note{{the implicit move assignment operator}} \
+
+  // Default constructor
+  decltype(lambda1) lambda2; // expected-error{{call to deleted constructor}}
+
+  // Copy assignment operator
+  lambda1 = lambda1; // expected-error{{overload resolution selected deleted operator '='}}
+
+  // Move assignment operator
+  lambda1 = move(lambda1);
+
+  // Copy constructor
+  decltype(lambda1) lambda3 = lambda1;
+  decltype(lambda1) lambda4(lambda1);
+
+  // Move constructor
+  decltype(lambda1) lambda5 = move(lambda1);
+  decltype(lambda1) lambda6(move(lambda1));
+}
diff --git a/test/CXX/expr/expr.prim/expr.prim.lambda/p20.cpp b/test/CXX/expr/expr.prim/expr.prim.lambda/p20.cpp
new file mode 100644 (file)
index 0000000..4487cfc
--- /dev/null
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -std=c++11 %s -Wunused -verify
+
+template<typename T>
+void destroy(T* ptr) {
+  ptr->~T();
+  (*ptr).~T();
+}
+
+void destructor() {
+  auto lambda = []{};
+  destroy(&lambda);
+}
index 74003431c683f94381971c741f1a68a62b38a10f..581dbca309f8fc922bbd9da516017b8520e52814 100644 (file)
@@ -8,6 +8,7 @@ void test_attributes() {
 
 template<typename T>
 struct bogus_override_if_virtual : public T {
+  bogus_override_if_virtual() : T(*(T*)0) { }
   int operator()() const;
 };
 
index 8fa3837214d3f2d58e00d8f6453f1909d6f6c71f..627071343e0f1312710a3e23a0e1e6133de91384 100644 (file)
@@ -45,9 +45,11 @@ void test_capture_constness(int i, const int ic) {
 
 struct S1 {
   int x, y;
+  S1 &operator=(int*);
   int operator()(int);
   void f() {
     [&]()->int {
+      S1 &s1 = operator=(&this->x);
       return operator()(this->x + y);
     }(); 
   }