Author: CedricSWA Date: 2024-07-01T15:38:45+02:00 New Revision: 51d87aa4380046588124c2e474924cd8f57189db
URL: https://github.com/llvm/llvm-project/commit/51d87aa4380046588124c2e474924cd8f57189db DIFF: https://github.com/llvm/llvm-project/commit/51d87aa4380046588124c2e474924cd8f57189db.diff LOG: [Clang] Improve error message for lambda captures that name a class member (#94865) This introduces are more helpful error message when trying to explicitly capture a class member in a lambda. Fixes #94764. Added: Modified: clang/docs/ReleaseNotes.rst clang/include/clang/Basic/DiagnosticSemaKinds.td clang/lib/Sema/SemaLambda.cpp clang/test/SemaCXX/lambda-expressions.cpp Removed: ################################################################################ diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index b7a2d97f00087..9489493482170 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -639,6 +639,9 @@ Improvements to Clang's diagnostics - Clang no longer emits a "declared here" note for a builtin function that has no declaration in source. Fixes #GH93369. +- Clang now has an improved error message for captures that refer to a class member. + Fixes #GH94764. + - Clang now diagnoses unsupported class declarations for ``std::initializer_list<E>`` when they are used rather than when they are needed for constant evaluation or when code is generated for them. The check is now stricter to prevent crashes for some unsupported declarations (Fixes #GH95495). diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 5dc36c594bcb7..2725d482c12ad 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -8204,6 +8204,8 @@ let CategoryName = "Lambda Issue" in { "'&' must precede a capture when the capture default is '='">; def err_capture_does_not_name_variable : Error< "%0 in capture list does not name a variable">; + def err_capture_class_member_does_not_name_variable : Error< + "class member %0 cannot appear in capture list as it is not a variable">; def err_capture_non_automatic_variable : Error< "%0 cannot be captured because it does not have automatic storage " "duration">; diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp index ca9c7cb9faadf..0795daa8403d1 100644 --- a/clang/lib/Sema/SemaLambda.cpp +++ b/clang/lib/Sema/SemaLambda.cpp @@ -1246,7 +1246,11 @@ void Sema::ActOnLambdaExpressionAfterIntroducer(LambdaIntroducer &Intro, if (auto *BD = R.getAsSingle<BindingDecl>()) Var = BD; - else + else if (auto *FD = R.getAsSingle<FieldDecl>()) { + Diag(C->Loc, diag::err_capture_class_member_does_not_name_variable) + << C->Id; + continue; + } else Var = R.getAsSingle<VarDecl>(); if (Var && DiagnoseUseOfDecl(Var, C->Loc)) continue; diff --git a/clang/test/SemaCXX/lambda-expressions.cpp b/clang/test/SemaCXX/lambda-expressions.cpp index 151d74f21d64d..acf8d014a9896 100644 --- a/clang/test/SemaCXX/lambda-expressions.cpp +++ b/clang/test/SemaCXX/lambda-expressions.cpp @@ -609,6 +609,15 @@ namespace PR25627_dont_odr_use_local_consts { } } +namespace PR94764 { + struct X { + int x; + void foo() { + [x](){}; // expected-error{{class member 'x' cannot appear in capture list as it is not a variable}} + } + }; +} + namespace ConversionOperatorDoesNotHaveDeducedReturnType { auto x = [](int){}; auto y = [](auto &v) -> void { v.n = 0; }; // cxx03-cxx11-error {{'auto' not allowed in lambda parameter}} cxx03-cxx11-note {{candidate function not viable}} cxx03-cxx11-note {{conversion candidate}} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits