-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Clang] correct error message when assigning to const reference captured in lambda #105647
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 notified. 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 permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. 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: None (nfrmtk) ChangesFixes:#98772 Full diff: https://github.com/llvm/llvm-project/pull/105647.diff 2 Files Affected:
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index c67183df335dd5..9c256fd1ed1a41 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -13140,6 +13140,8 @@ static NonConstCaptureKind isReferenceToNonConstCapture(Sema &S, Expr *E) {
VarDecl *var = dyn_cast<VarDecl>(DRE->getDecl());
if (!var) return NCCK_None;
if (var->getType().isConstQualified()) return NCCK_None;
+ if (var->getType()->isReferenceType())
+ return NCCK_None;
assert(var->hasLocalStorage() && "capture added 'const' to non-local?");
// Decide whether the first capture was for a block or a lambda.
diff --git a/clang/test/SemaCXX/lambda-expressions.cpp b/clang/test/SemaCXX/lambda-expressions.cpp
index acf8d014a9896b..612c6db950f273 100644
--- a/clang/test/SemaCXX/lambda-expressions.cpp
+++ b/clang/test/SemaCXX/lambda-expressions.cpp
@@ -189,6 +189,11 @@ namespace ModifyingCapture {
[=] {
n = 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
};
+ const int cn = 0;
+ // cxx03-cxx11-warning@+1 {{initialized lambda captures are a C++14 extension}}
+ [&cnr = cn]{ // expected-note {{variable 'cnr' declared const here}}
+ cnr = 1; // expected-error {{cannot assign to variable 'cnr' with const-qualified type 'const int &'}}
+ };
}
}
|
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.
I think this PR moves things in the right direction, but it would be nice to make notes more helpful.
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.
Note emitted for this line is technically correct, because according to https://eel.is/c++draft/expr.prim.lambda#capture-6, this is where we deduce type of cnr
to be const int&
. But I believe it would be significantly more helpful if we point out to a const
in the declaration of cn
, because that's the real reason we can't perform the assignment.
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.
Hello.
I've thought of this too.
IMO the change shouldn't be limited to lambdas, but to diagnosing attempts to mutate objects behind const references in general.
I think, the most precise (if this word makes any sense in the context) solution is to point to the first place where user added const to type. For example
int a = 5;
const auto& ar = a;
auto& arr = ar;
auto& arrr = arr;
arrr = 2;
Should make note pointing to second line(currently points to fifth).
But I'm not sure if this is common case and will be helpful to the final user.
So, I'm confused and need some help.
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.
I'm not sure we want to go this far and look through explicit declarations with deduced type.
CC @cor3ntin
ping @cor3ntin |
1 similar comment
ping @cor3ntin |
@nfrmtk I'm so sorry this fell off my radar. Feel free pinging every week |
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. sorry for the delay
can you fix the merge conflict and ping me so that I can merge? thanks a lot! |
@cor3ntin ping |
@nfrmtk 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 by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. 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. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
[=] { | ||
n = 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}} | ||
}; | ||
const int cn = 0; |
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.
I think we should have also tested a constexpr
variable case and maybe a auto &
assigned from cn
as well just to cover a more complete test set.
Fixes:#98772