[PATCH] D74130: [clang] fix consteval call in default arguements

2020-10-15 Thread Ronan Keryell via Phabricator via cfe-commits
keryell added inline comments.



Comment at: clang/lib/Sema/SemaExpr.cpp:16329
 
+  /// If we can't handle immediate invocations yet. add them to the outer 
scope.
+  /// This occurs for default argument of lambdas as we can't know if the 
lambda




Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74130/new/

https://reviews.llvm.org/D74130

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D74130: [clang] fix consteval call in default arguements

2020-10-15 Thread Tyker via Phabricator via cfe-commits
Tyker updated this revision to Diff 298344.
Tyker retitled this revision from "[clang] fix consteval call in default 
arguements " to "[clang] fix consteval call in default arguements".
Tyker added a comment.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

rebased and add more fixes


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74130/new/

https://reviews.llvm.org/D74130

Files:
  clang/include/clang/Parse/Parser.h
  clang/include/clang/Sema/Sema.h
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Sema/Sema.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaLambda.cpp
  clang/test/SemaCXX/cxx2a-consteval.cpp

Index: clang/test/SemaCXX/cxx2a-consteval.cpp
===
--- clang/test/SemaCXX/cxx2a-consteval.cpp
+++ clang/test/SemaCXX/cxx2a-consteval.cpp
@@ -258,6 +258,26 @@
   return f(0);  
 };
 
+consteval int f1() {
+// expected-note@-1+ {{declared here}}
+  return 0;
+}
+consteval auto g() { return f1; }
+consteval int h(int (*p)() = g()) { return p(); }
+int h1(int (*p)() = g()) { return p(); }
+// expected-error@-1 {{is not a constant expression}}
+// expected-note@-2 {{pointer to a consteval}}
+
+constexpr auto e = g();
+// expected-error@-1 {{must be initialized by a constant expression}}
+// expected-note@-2 {{is not a constant expression}}
+
+auto l = [](int (*p)() = g()) { return p(); };
+// expected-error@-1 {{is not a constant expression}}
+// expected-note@-2 {{pointer to a consteval}}
+
+auto l2 = [](int (*p)() = g()) consteval { return p(); };
+
 }
 
 namespace std {
@@ -594,3 +614,22 @@
 }
 
 } // namespace special_ctor
+
+namespace top_level {
+struct S {
+  consteval S() {}
+  int a;
+// expected-note@-1 {{subobject declared here}}
+};
+
+S s; // expected-error {{is not a constant expression}}
+// expected-note@-1 {{is not initialized}}
+
+struct S1 {
+  consteval S1() {}
+  int a = 0;
+};
+
+S1 s1;
+
+}
Index: clang/lib/Sema/SemaLambda.cpp
===
--- clang/lib/Sema/SemaLambda.cpp
+++ clang/lib/Sema/SemaLambda.cpp
@@ -891,6 +891,10 @@
   LambdaScopeInfo *const LSI = getCurLambda();
   assert(LSI && "LambdaScopeInfo should be on stack!");
 
+  if (ParamInfo.getDeclSpec().getConstexprSpecifier() == CSK_consteval)
+ExprEvalContexts.back().IIEndScopeAction =
+ExpressionEvaluationContextRecord::IIESA_Drop;
+
   // Determine if we're within a context where we know that the lambda will
   // be dependent, because there are template parameters in scope.
   bool KnownDependent;
Index: clang/lib/Sema/SemaExpr.cpp
===
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -16097,13 +16097,16 @@
   return TransformToPE(*this).TransformExpr(E);
 }
 
-void
-Sema::PushExpressionEvaluationContext(
+void Sema::PushExpressionEvaluationContext(
 ExpressionEvaluationContext NewContext, Decl *LambdaContextDecl,
-ExpressionEvaluationContextRecord::ExpressionKind ExprContext) {
+ExpressionEvaluationContextRecord::ExpressionKind ExprContext,
+bool IsLambdaParamScope) {
   ExprEvalContexts.emplace_back(NewContext, ExprCleanupObjects.size(), Cleanup,
 LambdaContextDecl, ExprContext);
   Cleanup.reset();
+  if (IsLambdaParamScope)
+ExprEvalContexts.back().IIEndScopeAction =
+Sema::ExpressionEvaluationContextRecord::IIESA_Propagate;
   if (!MaybeODRUseExprs.empty())
 std::swap(MaybeODRUseExprs, ExprEvalContexts.back().SavedMaybeODRUseExprs);
 }
@@ -16183,7 +16186,9 @@
 
 ExprResult Sema::CheckForImmediateInvocation(ExprResult E, FunctionDecl *Decl) {
   if (!E.isUsable() || !Decl || !Decl->isConsteval() || isConstantEvaluated() ||
-  RebuildingImmediateInvocation)
+  RebuildingImmediateInvocation ||
+  ExprEvalContexts.back().IIEndScopeAction ==
+  ExpressionEvaluationContextRecord::IIESA_Drop)
 return E;
 
   /// Opportunistically remove the callee from ReferencesToConsteval if we can.
@@ -16312,14 +16317,31 @@
   It->getPointer()->setSubExpr(Res.get());
 }
 
-static void
-HandleImmediateInvocations(Sema &SemaRef,
-   Sema::ExpressionEvaluationContextRecord &Rec) {
+void Sema::HandleImmediateInvocations(
+Sema::ExpressionEvaluationContextRecord &Rec) {
   if ((Rec.ImmediateInvocationCandidates.size() == 0 &&
Rec.ReferenceToConsteval.size() == 0) ||
-  SemaRef.RebuildingImmediateInvocation)
+  RebuildingImmediateInvocation ||
+  Rec.IIEndScopeAction ==
+  Sema::ExpressionEvaluationContextRecord::IIESA_Drop)
 return;
 
+  /// If we can't handle immediate invocations yet. add them to the outer scope.
+  /// This occurs for default argument of lambdas as we can't know if the lambda
+  /// is consteval until after the parameter context has been poped.
+  if (Rec.IIEndScopeA