llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Clang][C++] Better error message for defining a friend function in a local class defintion

Open dawmd opened this issue 2 years ago • 6 comments

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() {}
      |                     ^~~

dawmd avatar Nov 19 '24 14:11 dawmd

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.

AaronBallman avatar Nov 19 '24 18:11 AaronBallman

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:

  1. 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.
  2. 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.
  3. Fix the issue locally.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. 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.

llvmbot avatar Nov 19 '24 18:11 llvmbot

@llvm/issue-subscribers-good-first-issue

Author: Dawid Mędrek (dawmd)

Reproducer:
void f() {
    struct A {
        friend void foo() {}
    };
}

compiled with --std=gnu++23.


clang-19.1.0:

&lt;source&gt;:3:21: error: no matching function found in local scope
    3 |         friend void foo() {}
      |                     ^~~
1 error generated.

gcc-14-2:

&lt;source&gt;: In function 'void f()':
&lt;source&gt;:3:21: error: cannot define friend function 'foo' in a local class definition
    3 |         friend void foo() {}
      |                     ^~~

llvmbot avatar Nov 19 '24 18:11 llvmbot

I am working on this

tlemy avatar Nov 20 '24 01:11 tlemy

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?"

tlemy avatar Nov 21 '24 12:11 tlemy

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.

AaronBallman avatar Nov 21 '24 17:11 AaronBallman