Skip to content
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

[[maybe_unused]] attribute used inside a template argument specifying a type of a function causes a compilation error #108095

Open
jarzec opened this issue Sep 10, 2024 · 1 comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer confirmed Verified by a second party diverges-from:edg Does the clang frontend diverge from edg compiler diverges-from:gcc Does the clang frontend diverge from gcc on this issue diverges-from:msvc Does the clang frontend diverge from msvc on this issue

Comments

@jarzec
Copy link

jarzec commented Sep 10, 2024

If a specification of a type of a function in a template argument uses the [[maybe_unused]] attribute an error is raised - the initial [ is interpreted as a beginning of a lambda capture.
This description is quite a mouthful, so let me give an example. The following code:

//...
std::function<int([[maybe_unused]] int)> f = fun;
//...

Causes the following error:

<source>:9:24: error: expected variable name or 'this' in lambda capture list
    9 |     std::function<int([[maybe_unused]] int)> f = fun;
      |                        ^
<source>:11:2: error: expected '>'
   11 | }
      |  ^
<source>:9:18: note: to match this '<'
    9 |     std::function<int([[maybe_unused]] int)> f = fun;
      |                  ^
<source>:11:2: error: expected unqualified-id
   11 | }
      |  ^
<source>:11:2: error: expected '}'
<source>:8:1: note: to match this '{'
    8 | {
      | ^
4 errors generated.
Compiler returned: 1

The quoted error is for x86-64 clang 18.1.0 from godbolt, but it is almost identical for all versions (at least) since 12.0.0.

I agree the [[maybe_unused]] attribute makes little sense in the presented context. However, at least to my understanding, the mentioned example is valid C++ syntax. I also believe a valid use case where it should be accepted is generated code. This, in fact, is how the issue was identified.
Other compilers (GCC and MSVC) correctly compile the code.

@HerrCai0907 HerrCai0907 added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer and removed new issue labels Sep 14, 2024
@shafik
Copy link
Collaborator

shafik commented Sep 17, 2024

Looks like only clang rejects this: https://godbolt.org/z/Kbh44KxKn

CC @cor3ntin @erichkeane

@shafik shafik added diverges-from:gcc Does the clang frontend diverge from gcc on this issue diverges-from:msvc Does the clang frontend diverge from msvc on this issue diverges-from:edg Does the clang frontend diverge from edg compiler labels Sep 17, 2024
@AaronBallman AaronBallman added the confirmed Verified by a second party label Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer confirmed Verified by a second party diverges-from:edg Does the clang frontend diverge from edg compiler diverges-from:gcc Does the clang frontend diverge from gcc on this issue diverges-from:msvc Does the clang frontend diverge from msvc on this issue
Projects
None yet
Development

No branches or pull requests

4 participants