-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Improve error message for invalid lambda captures #94865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang Author: CedricSWA (CedricSwa) Changesclose issue #94764 Full diff: https://github.com/llvm/llvm-project/pull/94865.diff 2 Files Affected:
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 9f0b6f5a36389..fdf4409125c00 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -8173,6 +8173,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 e9476a0c93c5d..79c10c3bad6d7 100644
--- a/clang/lib/Sema/SemaLambda.cpp
+++ b/clang/lib/Sema/SemaLambda.cpp
@@ -1246,7 +1246,12 @@ void Sema::ActOnLambdaExpressionAfterIntroducer(LambdaIntroducer &Intro,
if (auto *BD = R.getAsSingle<BindingDecl>())
Var = BD;
- else
+ else if (auto *FD = R.getAsSingle<FieldDecl>()) {
+ Var = R.getAsSingle<VarDecl>();
+ 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;
|
Needs a test. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks reasonable, but as was already pointed out, it needs a few tests, and also a release note (in clang/docs/ReleaseNotes.rst
, probably in the ‘Improvements to Clang’s Diagnostics’ section or whatever it was called).
not sure if lambda-expression.cpp is the best file for the test. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM except that the release note could be a bit more descriptive.
clang/docs/ReleaseNotes.rst
Outdated
@@ -622,6 +622,8 @@ 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 when trying to capture a variable for lambda function expressions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be more descriptive, i.e. I’d mention that this is specifically about the case of trying to capture a class member.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Release note could still be a bit less wordy and should also include the issue this fixes.
(Also, just so you know, don’t worry too much about merge conflicts in the release notes; usually, whoever merges the pr can handle those if they’re not too complicated) |
Co-authored-by: Sirraide <[email protected]>
@CedricSwa Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
…ember (llvm#94865) This introduces are more helpful error message when trying to explicitly capture a class member in a lambda. Fixes llvm#94764.
…ember (llvm#94865) This introduces are more helpful error message when trying to explicitly capture a class member in a lambda. Fixes llvm#94764.
close issue #94764