]> granicus.if.org Git - clang/commitdiff
Implement C++11 [expr.lambda.prim]p13, which prohibits lambdas in
authorDouglas Gregor <dgregor@apple.com>
Fri, 10 Feb 2012 23:30:22 +0000 (23:30 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 10 Feb 2012 23:30:22 +0000 (23:30 +0000)
default arguments if in fact those lambdas capture any entity.

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

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDeclCXX.cpp
test/CXX/expr/expr.prim/expr.prim.lambda/p13.cpp [new file with mode: 0644]

index 0caf192fad369f4a83e0bf61447a7f0202590f86..c35968cdb0d15006f30f44c5263c32021c4aba26 100644 (file)
@@ -4121,6 +4121,8 @@ def ext_lambda_implies_void_return : ExtWarn<
   InGroup<DiagGroup<"lambda-return">>;
 def err_lambda_return_init_list : Error<
   "cannot deduce lambda return type from initializer list">;
+def err_lambda_capture_default_arg : Error<
+  "lambda expression in default argument cannot capture any entity">;
 
 def err_operator_arrow_circular : Error<
   "circular pointer delegation detected">;
index ec92470ae1f6d29c8e5cf211e0f624f2aefdcb35..a6d7d63b3a164ece983891d312ac22c811e54df9 100644 (file)
@@ -62,6 +62,7 @@ namespace {
     bool VisitExpr(Expr *Node);
     bool VisitDeclRefExpr(DeclRefExpr *DRE);
     bool VisitCXXThisExpr(CXXThisExpr *ThisE);
+    bool VisitLambdaExpr(LambdaExpr *Lambda);
   };
 
   /// VisitExpr - Visit all of the children of this expression.
@@ -111,6 +112,17 @@ namespace {
                    diag::err_param_default_argument_references_this)
                << ThisE->getSourceRange();
   }
+
+  bool CheckDefaultArgumentVisitor::VisitLambdaExpr(LambdaExpr *Lambda) {
+    // C++11 [expr.lambda.prim]p13:
+    //   A lambda-expression appearing in a default argument shall not
+    //   implicitly or explicitly capture any entity.
+    if (Lambda->capture_begin() == Lambda->capture_end())
+      return false;
+
+    return S->Diag(Lambda->getLocStart(), 
+                   diag::err_lambda_capture_default_arg);
+  }
 }
 
 void Sema::ImplicitExceptionSpecification::CalledDecl(CXXMethodDecl *Method) {
diff --git a/test/CXX/expr/expr.prim/expr.prim.lambda/p13.cpp b/test/CXX/expr/expr.prim/expr.prim.lambda/p13.cpp
new file mode 100644 (file)
index 0000000..53f458a
--- /dev/null
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -std=c++11 %s -Wunused -verify
+
+void f2() {
+  int i = 1;
+  void g1(int = ([i]{ return i; })()); // expected-error{{lambda expression in default argument cannot capture any entity}}
+  void g2(int = ([i]{ return 0; })()); // expected-error{{lambda expression in default argument cannot capture any entity}}
+  void g3(int = ([=]{ return i; })()); // expected-error{{lambda expression in default argument cannot capture any entity}}
+  void g4(int = ([=]{ return 0; })());
+  void g5(int = ([]{ return sizeof i; })());
+}