[Clang][C++] Better error message for defining a friend function in a local class defintion
Reproducer:
void f() {
struct A {
friend void foo() {}
};
}
compiled with --std=gnu++23.
clang-19.1.0:
<source>:3:21: error: no matching function found in local scope
3 | friend void foo() {}
| ^~~
1 error generated.
gcc-14-2:
<source>: In function 'void f()':
<source>:3:21: error: cannot define friend function 'foo' in a local class definition
3 | friend void foo() {}
| ^~~
This should be relatively easy to do, I think. Rewording the diagnostic and fixing up the tests that start failing to instead expect the new wording should be all that's required.
The diagnostic wording is at: https://github.com/llvm/llvm-project/blob/def22f4e718daa74c2d0c03a32e32d4913a46278/clang/include/clang/Basic/DiagnosticSemaKinds.td#L1730
Be sure to update both err_no_matching_local_friend and err_no_matching_local_friend_suggest at the same time.
Hi!
This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:
- Check that no other contributor has already been assigned to this issue. If you believe that no one is actually working on it despite an assignment, ping the person. After one week without a response, the assignee may be changed.
- In the comments of this issue, request for it to be assigned to you, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
- Fix the issue locally.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.
If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.
@llvm/issue-subscribers-good-first-issue
Author: Dawid Mędrek (dawmd)
void f() {
struct A {
friend void foo() {}
};
}
compiled with --std=gnu++23.
clang-19.1.0:
<source>:3:21: error: no matching function found in local scope
3 | friend void foo() {}
| ^~~
1 error generated.
gcc-14-2:
<source>: In function 'void f()':
<source>:3:21: error: cannot define friend function 'foo' in a local class definition
3 | friend void foo() {}
| ^~~
I am working on this
From what I understand, I assume the messages should be like gcc. So is this ok?
err_no_matching_local_friend: "cannot define friend function in a local class definition" err_no_matching_local_friend_suggest: "cannot define friend function %0 in a local class definition; did you mean %3?"
From what I understand, I assume the messages should be like gcc. So is this ok?
err_no_matching_local_friend: "cannot define friend function in a local class definition" err_no_matching_local_friend_suggest: "cannot define friend function %0 in a local class definition; did you mean %3?"
Yes, those messages look like an improvement to me.