[clang] [Clang] Fix the order of addInstantiatedParameters in LambdaScopeForCallOperatorInstantiationRAII (PR #97215)
Yupei Liu via cfe-commits
cfe-commits at lists.llvm.org
Sat Jul 6 11:28:53 PDT 2024
https://github.com/LYP951018 updated https://github.com/llvm/llvm-project/pull/97215
>From a997ae70f86230200b1082c9bdc0bdf56e30b7c4 Mon Sep 17 00:00:00 2001
From: letrec <liuyupei951018 at hotmail.com>
Date: Sun, 30 Jun 2024 21:03:23 +0800
Subject: [PATCH 1/2] Fix the order of addInstantiatedParameters in
LambdaScopeForCallOperatorInstantiationRAII.
---
clang/lib/Sema/SemaLambda.cpp | 37 +++++++++++++--------
clang/test/SemaTemplate/concepts-lambda.cpp | 14 ++++++++
2 files changed, 37 insertions(+), 14 deletions(-)
diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp
index ca9c7cb9faadf..b6344f53861f4 100644
--- a/clang/lib/Sema/SemaLambda.cpp
+++ b/clang/lib/Sema/SemaLambda.cpp
@@ -2379,23 +2379,32 @@ Sema::LambdaScopeForCallOperatorInstantiationRAII::
SemaRef.RebuildLambdaScopeInfo(cast<CXXMethodDecl>(FD));
- FunctionDecl *Pattern = getPatternFunctionDecl(FD);
- if (Pattern) {
- SemaRef.addInstantiatedCapturesToScope(FD, Pattern, Scope, MLTAL);
+ FunctionDecl *FDPattern = getPatternFunctionDecl(FD);
+ if (!FDPattern)
+ return;
- FunctionDecl *ParentFD = FD;
- while (ShouldAddDeclsFromParentScope) {
+ SemaRef.addInstantiatedCapturesToScope(FD, FDPattern, Scope, MLTAL);
- ParentFD =
- dyn_cast<FunctionDecl>(getLambdaAwareParentOfDeclContext(ParentFD));
- Pattern =
- dyn_cast<FunctionDecl>(getLambdaAwareParentOfDeclContext(Pattern));
+ if (!ShouldAddDeclsFromParentScope)
+ return;
- if (!ParentFD || !Pattern)
- break;
+ llvm::SmallVector<std::pair<FunctionDecl *, FunctionDecl *>, 4>
+ ParentInstantiations;
+ std::pair<FunctionDecl *, FunctionDecl *> Current = {FDPattern, FD};
+ while (true) {
+ Current.first = dyn_cast<FunctionDecl>(
+ getLambdaAwareParentOfDeclContext(Current.first));
+ Current.second = dyn_cast<FunctionDecl>(
+ getLambdaAwareParentOfDeclContext(Current.second));
- SemaRef.addInstantiatedParametersToScope(ParentFD, Pattern, Scope, MLTAL);
- SemaRef.addInstantiatedLocalVarsToScope(ParentFD, Pattern, Scope);
- }
+ if (!Current.first || !Current.second)
+ break;
+
+ ParentInstantiations.push_back(Current);
+ }
+
+ for (const auto &[Pattern, Inst] : llvm::reverse(ParentInstantiations)) {
+ SemaRef.addInstantiatedParametersToScope(Inst, Pattern, Scope, MLTAL);
+ SemaRef.addInstantiatedLocalVarsToScope(Inst, Pattern, Scope);
}
}
diff --git a/clang/test/SemaTemplate/concepts-lambda.cpp b/clang/test/SemaTemplate/concepts-lambda.cpp
index 280be71284f97..252ef08549a48 100644
--- a/clang/test/SemaTemplate/concepts-lambda.cpp
+++ b/clang/test/SemaTemplate/concepts-lambda.cpp
@@ -237,3 +237,17 @@ concept D = []<C T = int>() { return true; }();
D auto x = 0;
} // namespace GH93821
+
+namespace dependent_param_concept {
+template <typename... Ts> void sink(Ts...) {}
+void dependent_param() {
+ auto L = [](auto... x) {
+ return [](decltype(x)... y) {
+ return [](int z)
+ requires requires { sink(y..., z); }
+ {};
+ };
+ };
+ L(0, 1)(1, 2)(1);
+}
+} // namespace dependent_param_concept
>From 85730ed7829d0a327e9b9ba9efa95e0b6dce9761 Mon Sep 17 00:00:00 2001
From: letrec <liuyupei951018 at hotmail.com>
Date: Sun, 7 Jul 2024 02:27:53 +0800
Subject: [PATCH 2/2] CR comments
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaLambda.cpp | 25 +++++++++++++++----------
2 files changed, 16 insertions(+), 10 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c720e47dbe35b..8f6c8b98b1322 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -929,6 +929,7 @@ Bug Fixes to C++ Support
- Fix a crash caused by improper use of ``__array_extent``. (#GH80474)
- Fixed several bugs in capturing variables within unevaluated contexts. (#GH63845), (#GH67260), (#GH69307),
(#GH88081), (#GH89496), (#GH90669) and (#GH91633).
+- Fixed a crash in constraint instantiation under nested lambdas with dependent parameters.
- Fixed handling of brace ellison when building deduction guides. (#GH64625), (#GH83368).
- Clang now instantiates local constexpr functions eagerly for constant evaluators. (#GH35052), (#GH94849)
- Fixed a failed assertion when attempting to convert an integer representing the difference
diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp
index b6344f53861f4..e461827be1e9d 100644
--- a/clang/lib/Sema/SemaLambda.cpp
+++ b/clang/lib/Sema/SemaLambda.cpp
@@ -2390,21 +2390,26 @@ Sema::LambdaScopeForCallOperatorInstantiationRAII::
llvm::SmallVector<std::pair<FunctionDecl *, FunctionDecl *>, 4>
ParentInstantiations;
- std::pair<FunctionDecl *, FunctionDecl *> Current = {FDPattern, FD};
while (true) {
- Current.first = dyn_cast<FunctionDecl>(
- getLambdaAwareParentOfDeclContext(Current.first));
- Current.second = dyn_cast<FunctionDecl>(
- getLambdaAwareParentOfDeclContext(Current.second));
+ FDPattern =
+ dyn_cast<FunctionDecl>(getLambdaAwareParentOfDeclContext(FDPattern));
+ FD = dyn_cast<FunctionDecl>(getLambdaAwareParentOfDeclContext(FD));
- if (!Current.first || !Current.second)
+ if (!FDPattern || !FD)
break;
- ParentInstantiations.push_back(Current);
+ ParentInstantiations.emplace_back(FDPattern, FD);
}
- for (const auto &[Pattern, Inst] : llvm::reverse(ParentInstantiations)) {
- SemaRef.addInstantiatedParametersToScope(Inst, Pattern, Scope, MLTAL);
- SemaRef.addInstantiatedLocalVarsToScope(Inst, Pattern, Scope);
+ // Add instantiated parameters and local vars to scopes, starting from the
+ // outermost lambda to the innermost lambda. This ordering ensures that
+ // parameters in inner lambdas can correctly depend on those defined
+ // in outer lambdas, e.g. auto L = [](auto... x) {
+ // return [](decltype(x)... y) { }; // `y` depends on `x`
+ // };
+
+ for (const auto &[FDPattern, FD] : llvm::reverse(ParentInstantiations)) {
+ SemaRef.addInstantiatedParametersToScope(FD, FDPattern, Scope, MLTAL);
+ SemaRef.addInstantiatedLocalVarsToScope(FD, FDPattern, Scope);
}
}
More information about the cfe-commits
mailing list