From 386c17d55f68155b711672f87d803eb934372a70 Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Fri, 26 Jan 2024 18:03:37 +0800 Subject: [PATCH 1/2] [Concepts] Traverse the instantiation chain for parameter injection inside a constraint scope We preserve the trailing requires-expression during the lambda expression transformation. In order to get those referenced parameters inside a requires-expression properly resolved to the instantiated decls, we intended to inject these 'original' ParmVarDecls to the current instantiaion scope, at `Sema::SetupConstraintScope`. The previous approach seems to overlook nested instantiation chains, leading to the crash within a nested lambda followed by a requires clause. This fixes https://github.com/llvm/llvm-project/issues/73418. --- clang/docs/ReleaseNotes.rst | 3 +++ clang/lib/Sema/SemaConcept.cpp | 8 ++++++-- clang/test/SemaTemplate/concepts-lambda.cpp | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 392f694065a24..72416a8b5ba59 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -1025,6 +1025,9 @@ Bug Fixes to AST Handling - Fixed a bug where Template Instantiation failed to handle Lambda Expressions with certain types of Attributes. (`#76521 `_) +- Fixed a bug where variables referenced by requires-clauses inside + nested generic lambdas were not properly injected into the constraint scope. + (`#73418 `_) Miscellaneous Bug Fixes ^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp index acfc00f412540..88fc846c89e42 100644 --- a/clang/lib/Sema/SemaConcept.cpp +++ b/clang/lib/Sema/SemaConcept.cpp @@ -612,8 +612,12 @@ bool Sema::SetupConstraintScope( // If this is a member function, make sure we get the parameters that // reference the original primary template. - if (const auto *FromMemTempl = - PrimaryTemplate->getInstantiatedFromMemberTemplate()) { + // We walk up the instantiated template chain so that nested lambdas get + // handled properly. + for (FunctionTemplateDecl *FromMemTempl = + PrimaryTemplate->getInstantiatedFromMemberTemplate(); + FromMemTempl; + FromMemTempl = FromMemTempl->getInstantiatedFromMemberTemplate()) { if (addInstantiatedParametersToScope(FD, FromMemTempl->getTemplatedDecl(), Scope, MLTAL)) return true; diff --git a/clang/test/SemaTemplate/concepts-lambda.cpp b/clang/test/SemaTemplate/concepts-lambda.cpp index 7e431529427df..694d048422a40 100644 --- a/clang/test/SemaTemplate/concepts-lambda.cpp +++ b/clang/test/SemaTemplate/concepts-lambda.cpp @@ -149,3 +149,21 @@ void foo() { auto caller = make_caller.operator()<&S1::f1>(); } } // namespace ReturnTypeRequirementInLambda + +namespace GH73418 { +void foo() { + int x; + [&x](auto) { + return [](auto y) { + return [](auto obj, auto... params) + requires requires { + sizeof...(params); + [](auto... pack) { + return sizeof...(pack); + }(params); + } + { return false; }(y); + }(x); + }(x); +} +} // namespace GH73418 From 561876370d8a052dd0308c9c842c4cb69679eb0e Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Sat, 27 Jan 2024 00:48:40 +0800 Subject: [PATCH 2/2] fixup && format --- clang/docs/ReleaseNotes.rst | 2 +- clang/test/SemaTemplate/concepts-lambda.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 72416a8b5ba59..63b04982629b0 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -1025,7 +1025,7 @@ Bug Fixes to AST Handling - Fixed a bug where Template Instantiation failed to handle Lambda Expressions with certain types of Attributes. (`#76521 `_) -- Fixed a bug where variables referenced by requires-clauses inside +- Fixed a bug where variables referenced by requires-clauses inside nested generic lambdas were not properly injected into the constraint scope. (`#73418 `_) diff --git a/clang/test/SemaTemplate/concepts-lambda.cpp b/clang/test/SemaTemplate/concepts-lambda.cpp index 694d048422a40..0b7580f91043c 100644 --- a/clang/test/SemaTemplate/concepts-lambda.cpp +++ b/clang/test/SemaTemplate/concepts-lambda.cpp @@ -160,7 +160,7 @@ void foo() { sizeof...(params); [](auto... pack) { return sizeof...(pack); - }(params); + }(params...); } { return false; }(y); }(x);