aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/SemaLambda.cpp
diff options
context:
space:
mode:
authorMariya Podchishchaeva <mariya.podchishchaeva@intel.com>2023-04-28 06:26:35 -0400
committerMariya Podchishchaeva <mariya.podchishchaeva@intel.com>2023-04-28 07:26:30 -0400
commit0fb84bc7fdec1c20c2461e54d5c994939fe5b47f (patch)
tree3c7cfac9ea7bc5fb01054f9a7556f344927020ba /clang/lib/Sema/SemaLambda.cpp
parentcf59f649ffa14818487713fd2636992b62a69a76 (diff)
downloadllvm-0fb84bc7fdec1c20c2461e54d5c994939fe5b47f.zip
llvm-0fb84bc7fdec1c20c2461e54d5c994939fe5b47f.tar.gz
llvm-0fb84bc7fdec1c20c2461e54d5c994939fe5b47f.tar.bz2
[clang] Diagnose shadowing of lambda's template parameter by a capture
expr.prim.lambda.capture p5 says: If an identifier in a capture appears as the declarator-id of a parameter of the lambda-declarator's parameter-declaration-clause or as the name of a template parameter of the lambda-expression's template-parameter-list, the program is ill-formed. and also has the following example: ``` auto h = [y = 0]<typename y>(y) { return 0; }; ``` which now results in ``` error: declaration of 'y' shadows template parameter auto l1 = [y = 0]<typename y>(y) { return 0; }; ^ note: template parameter is declared here auto l1 = [y = 0]<typename y>(y) { return 0; }; ^ ``` Fixes https://github.com/llvm/llvm-project/issues/61105 Reviewed By: shafik, cor3ntin Differential Revision: https://reviews.llvm.org/D148712
Diffstat (limited to 'clang/lib/Sema/SemaLambda.cpp')
-rw-r--r--clang/lib/Sema/SemaLambda.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp
index 7f9bec2..730f70e 100644
--- a/clang/lib/Sema/SemaLambda.cpp
+++ b/clang/lib/Sema/SemaLambda.cpp
@@ -1365,6 +1365,26 @@ void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro,
PushOnScopeChains(P, CurScope);
}
+ // C++23 [expr.prim.lambda.capture]p5:
+ // If an identifier in a capture appears as the declarator-id of a parameter
+ // of the lambda-declarator's parameter-declaration-clause or as the name of a
+ // template parameter of the lambda-expression's template-parameter-list, the
+ // program is ill-formed.
+ TemplateParameterList *TemplateParams =
+ getGenericLambdaTemplateParameterList(LSI, *this);
+ if (TemplateParams) {
+ for (const auto *TP : TemplateParams->asArray()) {
+ if (!TP->getIdentifier())
+ continue;
+ for (const auto &Capture : Intro.Captures) {
+ if (Capture.Id == TP->getIdentifier()) {
+ Diag(Capture.Loc, diag::err_template_param_shadow) << Capture.Id;
+ Diag(TP->getLocation(), diag::note_template_param_here);
+ }
+ }
+ }
+ }
+
// C++20: dcl.decl.general p4:
// The optional requires-clause ([temp.pre]) in an init-declarator or
// member-declarator shall be present only if the declarator declares a