]> granicus.if.org Git - clang/commitdiff
Sema: Do not allow lambda expressions to appear inside of constant expressions
authorDavid Majnemer <david.majnemer@gmail.com>
Fri, 25 Oct 2013 09:12:52 +0000 (09:12 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Fri, 25 Oct 2013 09:12:52 +0000 (09:12 +0000)
We would previously not diagnose this which would lead to crashes (on
very strange code).

This fixes PR17675.

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

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaExpr.cpp
lib/Sema/SemaLambda.cpp
test/CXX/expr/expr.const/p2-0x.cpp
test/SemaCXX/new-delete-0x.cpp

index 21aad683eacb5a35538f79d228013c5da09ac44e..441ad4ccace64d6eafd18acf2a151e9c8ac09ccc 100644 (file)
@@ -5067,6 +5067,8 @@ let CategoryName = "Lambda Issue" in {
   def note_lambda_decl : Note<"lambda expression begins here">;
   def err_lambda_unevaluated_operand : Error<
     "lambda expression in an unevaluated operand">;
+  def err_lambda_in_constant_expression : Error<
+    "a lambda expression may not appear inside of a constant expression">;
   def err_lambda_return_init_list : Error<
     "cannot deduce lambda return type from initializer list">;
   def err_lambda_capture_default_arg : Error<
index c842ed035d8884e7bb9ef349b0bc96c6fac2f797..69f8f2e739a0a05b462ada62263213755abdff86 100644 (file)
@@ -10962,13 +10962,22 @@ void Sema::PopExpressionEvaluationContext() {
   ExpressionEvaluationContextRecord& Rec = ExprEvalContexts.back();
 
   if (!Rec.Lambdas.empty()) {
-    if (Rec.isUnevaluated()) {
-      // C++11 [expr.prim.lambda]p2:
-      //   A lambda-expression shall not appear in an unevaluated operand
-      //   (Clause 5).
+    if (Rec.isUnevaluated() || Rec.Context == ConstantEvaluated) {
+      unsigned D;
+      if (Rec.isUnevaluated()) {
+        // C++11 [expr.prim.lambda]p2:
+        //   A lambda-expression shall not appear in an unevaluated operand
+        //   (Clause 5).
+        D = diag::err_lambda_unevaluated_operand;
+      } else {
+        // C++1y [expr.const]p2:
+        //   A conditional-expression e is a core constant expression unless the
+        //   evaluation of e, following the rules of the abstract machine, would
+        //   evaluate [...] a lambda-expression.
+        D = diag::err_lambda_in_constant_expression;
+      }
       for (unsigned I = 0, N = Rec.Lambdas.size(); I != N; ++I)
-        Diag(Rec.Lambdas[I]->getLocStart(), 
-             diag::err_lambda_unevaluated_operand);
+        Diag(Rec.Lambdas[I]->getLocStart(), D);
     } else {
       // Mark the capture expressions odr-used. This was deferred
       // during lambda expression creation.
index c843da74cd228b27b343a9f87e4ee85ce16b4ff0..2e1cd105a98fa098e48f517afe950225eb3b80d2 100644 (file)
@@ -1229,20 +1229,25 @@ ExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc, Stmt *Body,
                                           CaptureInits, ArrayIndexVars, 
                                           ArrayIndexStarts, Body->getLocEnd(),
                                           ContainsUnexpandedParameterPack);
-  // C++11 [expr.prim.lambda]p2:
-  //   A lambda-expression shall not appear in an unevaluated operand
-  //   (Clause 5).
+
   if (!CurContext->isDependentContext()) {
     switch (ExprEvalContexts.back().Context) {
+    // C++11 [expr.prim.lambda]p2:
+    //   A lambda-expression shall not appear in an unevaluated operand
+    //   (Clause 5).
     case Unevaluated:
     case UnevaluatedAbstract:
+    // C++1y [expr.const]p2:
+    //   A conditional-expression e is a core constant expression unless the
+    //   evaluation of e, following the rules of the abstract machine, would
+    //   evaluate [...] a lambda-expression.
+    case ConstantEvaluated:
       // We don't actually diagnose this case immediately, because we
       // could be within a context where we might find out later that
       // the expression is potentially evaluated (e.g., for typeid).
       ExprEvalContexts.back().Lambdas.push_back(Lambda);
       break;
 
-    case ConstantEvaluated:
     case PotentiallyEvaluated:
     case PotentiallyEvaluatedIfUsed:
       break;
index d4afeb8a0318403d9bc6113aa69eed81814931c4..f0b53c7d3b5ccf6c71a1a5d65d99a6e5bf3d5127 100644 (file)
@@ -275,9 +275,7 @@ namespace UndefinedBehavior {
 
 // - a lambda-expression (5.1.2);
 struct Lambda {
-  // FIXME: clang crashes when trying to parse this! Revisit this check once
-  // lambdas are fully implemented.
-  //int n : []{ return 1; }();
+  int n : []{ return 1; }(); // expected-error {{constant expression}} expected-error {{integral constant expression}}
 };
 
 // - an lvalue-to-rvalue conversion (4.1) unless it is applied to
index 9e3b4928b14154e2685d37ed0e7d69bea0f2906a..a11392d5896ca1d826446d3c9eb903e665da1808 100644 (file)
@@ -21,7 +21,9 @@ void bad_news(int *ip)
   auto s = new int*[[]{return 1;}()][2]; // expected-error {{expected ']'}}
   // ... but not here:
   auto t = new (int(*)[[]]); // expected-error {{an attribute list cannot appear here}}
-  auto u = new (int(*)[[]{return 1;}()][2]); // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} expected-error {{variably modified type}}
+  auto u = new (int(*)[[]{return 1;}()][2]); // expected-error {{C++11 only allows consecutive left square brackets when introducing an attribute}} \
+                                                expected-error {{variably modified type}} \
+                                                expected-error {{a lambda expression may not appear inside of a constant expression}}
 }
 
 void good_deletes()