[PATCH] D159358: [Clang] Realize generic lambda call operators are dependent sooner

2023-09-02 Thread Corentin Jabot via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG890f11d60fef: [Clang] Realize generic lambda call operators 
are dependent sooner (authored by cor3ntin).

Changed prior to commit:
  https://reviews.llvm.org/D159358?vs=555375=62#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D159358

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Sema/Sema.h
  clang/lib/Sema/SemaLambda.cpp
  clang/lib/Sema/TreeTransform.h
  clang/test/SemaCXX/cxx2a-template-lambdas.cpp

Index: clang/test/SemaCXX/cxx2a-template-lambdas.cpp
===
--- clang/test/SemaCXX/cxx2a-template-lambdas.cpp
+++ clang/test/SemaCXX/cxx2a-template-lambdas.cpp
@@ -70,3 +70,20 @@
 }
 
 }
+
+namespace GH64689 {
+void f();
+void foo() {
+  [](int)
+noexcept(requires(int t) { f(); })
+-> decltype(requires(int t) { f(); })
+requires requires(int t) { f(); }
+  {return {};}.operator()(0);
+  [](auto)
+noexcept(requires(int t) { f(); })
+-> decltype(requires(int t) { f(); })
+requires requires(int t) { f(); }
+  {return {};}(1);
+}
+
+}
Index: clang/lib/Sema/TreeTransform.h
===
--- clang/lib/Sema/TreeTransform.h
+++ clang/lib/Sema/TreeTransform.h
@@ -13558,6 +13558,9 @@
   auto TPL = getDerived().TransformTemplateParameterList(
   E->getTemplateParameterList());
   LSI->GLTemplateParameterList = TPL;
+  if (TPL)
+getSema().AddTemplateParametersToLambdaCallOperator(NewCallOperator, Class,
+TPL);
 
   // Transform the type of the original lambda's call operator.
   // The transformation MUST be done in the CurrentInstantiationScope since
Index: clang/lib/Sema/SemaLambda.cpp
===
--- clang/lib/Sema/SemaLambda.cpp
+++ clang/lib/Sema/SemaLambda.cpp
@@ -912,6 +912,17 @@
   return Method;
 }
 
+void Sema::AddTemplateParametersToLambdaCallOperator(
+CXXMethodDecl *CallOperator, CXXRecordDecl *Class,
+TemplateParameterList *TemplateParams) {
+  assert(TemplateParams && "no template parameters");
+  FunctionTemplateDecl *TemplateMethod = FunctionTemplateDecl::Create(
+  Context, Class, CallOperator->getLocation(), CallOperator->getDeclName(),
+  TemplateParams, CallOperator);
+  TemplateMethod->setAccess(AS_public);
+  CallOperator->setDescribedFunctionTemplate(TemplateMethod);
+}
+
 void Sema::CompleteLambdaCallOperator(
 CXXMethodDecl *Method, SourceLocation LambdaLoc,
 SourceLocation CallOperatorLoc, Expr *TrailingRequiresClause,
@@ -930,11 +941,11 @@
   DeclContext *DC = Method->getLexicalDeclContext();
   Method->setLexicalDeclContext(LSI->Lambda);
   if (TemplateParams) {
-FunctionTemplateDecl *TemplateMethod = FunctionTemplateDecl::Create(
-Context, LSI->Lambda, Method->getLocation(), Method->getDeclName(),
-TemplateParams, Method);
-TemplateMethod->setAccess(AS_public);
-Method->setDescribedFunctionTemplate(TemplateMethod);
+FunctionTemplateDecl *TemplateMethod =
+Method->getDescribedFunctionTemplate();
+assert(TemplateMethod &&
+   "AddTemplateParametersToLambdaCallOperator should have been called");
+
 LSI->Lambda->addDecl(TemplateMethod);
 TemplateMethod->setLexicalDeclContext(DC);
   } else {
@@ -1262,6 +1273,17 @@
   PushOnScopeChains(Param, LambdaScope, false);
   }
 
+  // After the parameter list, we may parse a noexcept/requires/trailing return
+  // type which need to know whether the call operator constiture a dependent
+  // context, so we need to setup the FunctionTemplateDecl of generic lambdas
+  // now.
+  TemplateParameterList *TemplateParams =
+  getGenericLambdaTemplateParameterList(LSI, *this);
+  if (TemplateParams) {
+AddTemplateParametersToLambdaCallOperator(LSI->CallOperator, LSI->Lambda,
+  TemplateParams);
+LSI->Lambda->setLambdaIsGeneric(true);
+  }
   LSI->AfterParameterList = true;
 }
 
Index: clang/include/clang/Sema/Sema.h
===
--- clang/include/clang/Sema/Sema.h
+++ clang/include/clang/Sema/Sema.h
@@ -7216,6 +7216,11 @@
 
   CXXMethodDecl *CreateLambdaCallOperator(SourceRange IntroducerRange,
   CXXRecordDecl *Class);
+
+  void AddTemplateParametersToLambdaCallOperator(
+  CXXMethodDecl *CallOperator, CXXRecordDecl *Class,
+  TemplateParameterList *TemplateParams);
+
   void CompleteLambdaCallOperator(
   CXXMethodDecl *Method, SourceLocation LambdaLoc,
   SourceLocation CallOperatorLoc, Expr *TrailingRequiresClause,
Index: clang/docs/ReleaseNotes.rst

[PATCH] D159358: [Clang] Realize generic lambda call operators are dependent sooner

2023-09-01 Thread Corentin Jabot via Phabricator via cfe-commits
cor3ntin updated this revision to Diff 555375.
cor3ntin added a comment.

Fix tests


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D159358

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Sema/Sema.h
  clang/lib/Sema/SemaLambda.cpp
  clang/lib/Sema/TreeTransform.h
  clang/test/SemaCXX/cxx2a-template-lambdas.cpp

Index: clang/test/SemaCXX/cxx2a-template-lambdas.cpp
===
--- clang/test/SemaCXX/cxx2a-template-lambdas.cpp
+++ clang/test/SemaCXX/cxx2a-template-lambdas.cpp
@@ -70,3 +70,20 @@
 }
 
 }
+
+namespace GH64689 {
+void f();
+void foo() {
+  [](int)
+noexcept(requires(int t) { f(); })
+-> decltype(requires(int t) { f(); })
+requires requires(int t) { f(); }
+  {return {};}.operator()(0);
+  [](auto)
+noexcept(requires(int t) { f(); })
+-> decltype(requires(int t) { f(); })
+requires requires(int t) { f(); }
+  {return {};}(1);
+}
+
+}
Index: clang/lib/Sema/TreeTransform.h
===
--- clang/lib/Sema/TreeTransform.h
+++ clang/lib/Sema/TreeTransform.h
@@ -13554,6 +13554,9 @@
   auto TPL = getDerived().TransformTemplateParameterList(
   E->getTemplateParameterList());
   LSI->GLTemplateParameterList = TPL;
+  if (TPL)
+getSema().AddTemplateParametersToLambdaCallOperator(NewCallOperator, Class,
+TPL);
 
   // Transform the type of the original lambda's call operator.
   // The transformation MUST be done in the CurrentInstantiationScope since
Index: clang/lib/Sema/SemaLambda.cpp
===
--- clang/lib/Sema/SemaLambda.cpp
+++ clang/lib/Sema/SemaLambda.cpp
@@ -912,6 +912,17 @@
   return Method;
 }
 
+void Sema::AddTemplateParametersToLambdaCallOperator(
+CXXMethodDecl *CallOperator, CXXRecordDecl *Class,
+TemplateParameterList *TemplateParams) {
+  assert(TemplateParams && "no template parameters");
+  FunctionTemplateDecl *TemplateMethod = FunctionTemplateDecl::Create(
+  Context, Class, CallOperator->getLocation(), CallOperator->getDeclName(),
+  TemplateParams, CallOperator);
+  TemplateMethod->setAccess(AS_public);
+  CallOperator->setDescribedFunctionTemplate(TemplateMethod);
+}
+
 void Sema::CompleteLambdaCallOperator(
 CXXMethodDecl *Method, SourceLocation LambdaLoc,
 SourceLocation CallOperatorLoc, Expr *TrailingRequiresClause,
@@ -930,11 +941,11 @@
   DeclContext *DC = Method->getLexicalDeclContext();
   Method->setLexicalDeclContext(LSI->Lambda);
   if (TemplateParams) {
-FunctionTemplateDecl *TemplateMethod = FunctionTemplateDecl::Create(
-Context, LSI->Lambda, Method->getLocation(), Method->getDeclName(),
-TemplateParams, Method);
-TemplateMethod->setAccess(AS_public);
-Method->setDescribedFunctionTemplate(TemplateMethod);
+FunctionTemplateDecl *TemplateMethod =
+Method->getDescribedFunctionTemplate();
+assert(TemplateMethod &&
+   "AddTemplateParametersToLambdaCallOperator should have been called");
+
 LSI->Lambda->addDecl(TemplateMethod);
 TemplateMethod->setLexicalDeclContext(DC);
   } else {
@@ -1262,6 +1273,17 @@
   PushOnScopeChains(Param, LambdaScope, false);
   }
 
+  // After the parameter list, we may parse a noexcept/requires/trailing return
+  // type which need to know whether the call operator constiture a dependent
+  // context, so we need to setup the FunctionTemplateDecl of generic lambdas
+  // now.
+  TemplateParameterList *TemplateParams =
+  getGenericLambdaTemplateParameterList(LSI, *this);
+  if (TemplateParams) {
+AddTemplateParametersToLambdaCallOperator(LSI->CallOperator, LSI->Lambda,
+  TemplateParams);
+LSI->Lambda->setLambdaIsGeneric(true);
+  }
   LSI->AfterParameterList = true;
 }
 
Index: clang/include/clang/Sema/Sema.h
===
--- clang/include/clang/Sema/Sema.h
+++ clang/include/clang/Sema/Sema.h
@@ -7229,6 +7229,11 @@
 
   CXXMethodDecl *CreateLambdaCallOperator(SourceRange IntroducerRange,
   CXXRecordDecl *Class);
+
+  void AddTemplateParametersToLambdaCallOperator(
+  CXXMethodDecl *CallOperator, CXXRecordDecl *Class,
+  TemplateParameterList *TemplateParams);
+
   void CompleteLambdaCallOperator(
   CXXMethodDecl *Method, SourceLocation LambdaLoc,
   SourceLocation CallOperatorLoc, Expr *TrailingRequiresClause,
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -250,6 +250,9 @@
   (`#64962 `_) and
   (`#28679 `_).
 
+- Fix 

[PATCH] D159358: [Clang] Realize generic lambda call operators are dependent sooner

2023-09-01 Thread Corentin Jabot via Phabricator via cfe-commits
cor3ntin updated this revision to Diff 555374.
cor3ntin added a comment.

Rebase


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D159358

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Sema/Sema.h
  clang/lib/Sema/SemaLambda.cpp
  clang/lib/Sema/TreeTransform.h
  clang/test/SemaCXX/cxx2a-template-lambdas.cpp

Index: clang/test/SemaCXX/cxx2a-template-lambdas.cpp
===
--- clang/test/SemaCXX/cxx2a-template-lambdas.cpp
+++ clang/test/SemaCXX/cxx2a-template-lambdas.cpp
@@ -70,3 +70,22 @@
 }
 
 }
+
+namespace GH64689 {
+void f();
+void foo() {
+  [](int)
+noexcept(requires(int t) { f(); })
+-> decltype(requires(int t) { f(); })
+requires requires(int t) { f(); }
+  {return {};}.operator()(0);
+  [](auto)
+noexcept(requires(int t) { f(); })
+-> decltype(requires(int t) { f(); })
+requires requires(int t) { f(); }
+  {return {};}(1);
+}
+
+}
+
+}
Index: clang/lib/Sema/TreeTransform.h
===
--- clang/lib/Sema/TreeTransform.h
+++ clang/lib/Sema/TreeTransform.h
@@ -13554,6 +13554,9 @@
   auto TPL = getDerived().TransformTemplateParameterList(
   E->getTemplateParameterList());
   LSI->GLTemplateParameterList = TPL;
+  if (TPL)
+getSema().AddTemplateParametersToLambdaCallOperator(NewCallOperator, Class,
+TPL);
 
   // Transform the type of the original lambda's call operator.
   // The transformation MUST be done in the CurrentInstantiationScope since
Index: clang/lib/Sema/SemaLambda.cpp
===
--- clang/lib/Sema/SemaLambda.cpp
+++ clang/lib/Sema/SemaLambda.cpp
@@ -912,6 +912,17 @@
   return Method;
 }
 
+void Sema::AddTemplateParametersToLambdaCallOperator(
+CXXMethodDecl *CallOperator, CXXRecordDecl *Class,
+TemplateParameterList *TemplateParams) {
+  assert(TemplateParams && "no template parameters");
+  FunctionTemplateDecl *TemplateMethod = FunctionTemplateDecl::Create(
+  Context, Class, CallOperator->getLocation(), CallOperator->getDeclName(),
+  TemplateParams, CallOperator);
+  TemplateMethod->setAccess(AS_public);
+  CallOperator->setDescribedFunctionTemplate(TemplateMethod);
+}
+
 void Sema::CompleteLambdaCallOperator(
 CXXMethodDecl *Method, SourceLocation LambdaLoc,
 SourceLocation CallOperatorLoc, Expr *TrailingRequiresClause,
@@ -930,11 +941,11 @@
   DeclContext *DC = Method->getLexicalDeclContext();
   Method->setLexicalDeclContext(LSI->Lambda);
   if (TemplateParams) {
-FunctionTemplateDecl *TemplateMethod = FunctionTemplateDecl::Create(
-Context, LSI->Lambda, Method->getLocation(), Method->getDeclName(),
-TemplateParams, Method);
-TemplateMethod->setAccess(AS_public);
-Method->setDescribedFunctionTemplate(TemplateMethod);
+FunctionTemplateDecl *TemplateMethod =
+Method->getDescribedFunctionTemplate();
+assert(TemplateMethod &&
+   "AddTemplateParametersToLambdaCallOperator should have been called");
+
 LSI->Lambda->addDecl(TemplateMethod);
 TemplateMethod->setLexicalDeclContext(DC);
   } else {
@@ -1262,6 +1273,17 @@
   PushOnScopeChains(Param, LambdaScope, false);
   }
 
+  // After the parameter list, we may parse a noexcept/requires/trailing return
+  // type which need to know whether the call operator constiture a dependent
+  // context, so we need to setup the FunctionTemplateDecl of generic lambdas
+  // now.
+  TemplateParameterList *TemplateParams =
+  getGenericLambdaTemplateParameterList(LSI, *this);
+  if (TemplateParams) {
+AddTemplateParametersToLambdaCallOperator(LSI->CallOperator, LSI->Lambda,
+  TemplateParams);
+LSI->Lambda->setLambdaIsGeneric(true);
+  }
   LSI->AfterParameterList = true;
 }
 
Index: clang/include/clang/Sema/Sema.h
===
--- clang/include/clang/Sema/Sema.h
+++ clang/include/clang/Sema/Sema.h
@@ -7229,6 +7229,11 @@
 
   CXXMethodDecl *CreateLambdaCallOperator(SourceRange IntroducerRange,
   CXXRecordDecl *Class);
+
+  void AddTemplateParametersToLambdaCallOperator(
+  CXXMethodDecl *CallOperator, CXXRecordDecl *Class,
+  TemplateParameterList *TemplateParams);
+
   void CompleteLambdaCallOperator(
   CXXMethodDecl *Method, SourceLocation LambdaLoc,
   SourceLocation CallOperatorLoc, Expr *TrailingRequiresClause,
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -250,6 +250,9 @@
   (`#64962 `_) and
   (`#28679 `_).
 
+- Fix 

[PATCH] D159358: [Clang] Realize generic lambda call operators are dependent sooner

2023-09-01 Thread Erich Keane via Phabricator via cfe-commits
erichkeane accepted this revision.
erichkeane added a comment.
This revision is now accepted and ready to land.

This is great! Probably worth the cherry pick to Clang17 as well!


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D159358

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


[PATCH] D159358: [Clang] Realize generic lambda call operators are dependent sooner

2023-09-01 Thread Corentin Jabot via Phabricator via cfe-commits
cor3ntin created this revision.
Herald added a project: All.
cor3ntin requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

When parsing a trailing return type / noexcept / constraint
of a generic lambda, we need to know that we are in a dependent
context. We currently don't because we only create a TemplateDecl
for the call operator one its fully parsed.

This patch attach a template decl to the call operator as soon
as the parameter declaration clause is parsed - the point at which
we  have collected all template parameters

Fixes #64689


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D159358

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/AST/DeclCXX.h
  clang/include/clang/Sema/Sema.h
  clang/lib/Sema/SemaLambda.cpp
  clang/lib/Sema/TreeTransform.h
  clang/test/SemaCXX/cxx2a-template-lambdas.cpp

Index: clang/test/SemaCXX/cxx2a-template-lambdas.cpp
===
--- clang/test/SemaCXX/cxx2a-template-lambdas.cpp
+++ clang/test/SemaCXX/cxx2a-template-lambdas.cpp
@@ -70,3 +70,22 @@
 }
 
 }
+
+namespace GH64689 {
+void f();
+void foo() {
+  [](int)
+noexcept(requires(int t) { f(); })
+-> decltype(requires(int t) { f(); })
+requires requires(int t) { f(); }
+  {return {};}.operator()(0);
+  [](auto)
+noexcept(requires(int t) { f(); })
+-> decltype(requires(int t) { f(); })
+requires requires(int t) { f(); }
+  {return {};}(1);
+}
+
+}
+
+}
Index: clang/lib/Sema/TreeTransform.h
===
--- clang/lib/Sema/TreeTransform.h
+++ clang/lib/Sema/TreeTransform.h
@@ -13554,6 +13554,9 @@
   auto TPL = getDerived().TransformTemplateParameterList(
   E->getTemplateParameterList());
   LSI->GLTemplateParameterList = TPL;
+  if (TPL)
+getSema().AddTemplateParametersToLambdaCallOperator(NewCallOperator, Class,
+TPL);
 
   // Transform the type of the original lambda's call operator.
   // The transformation MUST be done in the CurrentInstantiationScope since
Index: clang/lib/Sema/SemaLambda.cpp
===
--- clang/lib/Sema/SemaLambda.cpp
+++ clang/lib/Sema/SemaLambda.cpp
@@ -912,6 +912,17 @@
   return Method;
 }
 
+void Sema::AddTemplateParametersToLambdaCallOperator(
+CXXMethodDecl *CallOperator, CXXRecordDecl *Class,
+TemplateParameterList *TemplateParams) {
+  assert(TemplateParams && "no template parameters");
+  FunctionTemplateDecl *TemplateMethod = FunctionTemplateDecl::Create(
+  Context, Class, CallOperator->getLocation(), CallOperator->getDeclName(),
+  TemplateParams, CallOperator);
+  TemplateMethod->setAccess(AS_public);
+  CallOperator->setDescribedFunctionTemplate(TemplateMethod);
+}
+
 void Sema::CompleteLambdaCallOperator(
 CXXMethodDecl *Method, SourceLocation LambdaLoc,
 SourceLocation CallOperatorLoc, Expr *TrailingRequiresClause,
@@ -930,11 +941,11 @@
   DeclContext *DC = Method->getLexicalDeclContext();
   Method->setLexicalDeclContext(LSI->Lambda);
   if (TemplateParams) {
-FunctionTemplateDecl *TemplateMethod = FunctionTemplateDecl::Create(
-Context, LSI->Lambda, Method->getLocation(), Method->getDeclName(),
-TemplateParams, Method);
-TemplateMethod->setAccess(AS_public);
-Method->setDescribedFunctionTemplate(TemplateMethod);
+FunctionTemplateDecl *TemplateMethod =
+Method->getDescribedFunctionTemplate();
+assert(TemplateMethod &&
+   "AddTemplateParametersToLambdaCallOperator should have been called");
+
 LSI->Lambda->addDecl(TemplateMethod);
 TemplateMethod->setLexicalDeclContext(DC);
   } else {
@@ -1262,10 +1273,17 @@
   PushOnScopeChains(Param, LambdaScope, false);
   }
 
-  bool IsGenericLambda = getGenericLambdaTemplateParameterList(LSI, *this);
-  LSI->Lambda->setLambdaIsGeneric(IsGenericLambda);
-
-
+  // After the parameter list, we may parse a noexcept/requires/trailing return
+  // type which need to know whether the call operator constiture a dependent
+  // context, so we need to setup the FunctionTemplateDecl of generic lambdas
+  // now.
+  TemplateParameterList *TemplateParams =
+  getGenericLambdaTemplateParameterList(LSI, *this);
+  if (TemplateParams) {
+AddTemplateParametersToLambdaCallOperator(LSI->CallOperator, LSI->Lambda,
+  TemplateParams);
+LSI->Lambda->setLambdaIsGeneric(true);
+  }
   LSI->AfterParameterList = true;
 }
 
Index: clang/include/clang/Sema/Sema.h
===
--- clang/include/clang/Sema/Sema.h
+++ clang/include/clang/Sema/Sema.h
@@ -7229,6 +7229,11 @@
 
   CXXMethodDecl *CreateLambdaCallOperator(SourceRange IntroducerRange,
   CXXRecordDecl *Class);
+
+  void