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

Diagnostic enhancement: "auto" in lambda expression under -std=c++11 #46059

Open
haoxintu opened this issue Jul 14, 2020 · 5 comments
Open

Diagnostic enhancement: "auto" in lambda expression under -std=c++11 #46059

haoxintu opened this issue Jul 14, 2020 · 5 comments
Labels
bugzilla Issues migrated from bugzilla clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer good first issue https://github.com/llvm/llvm-project/contribute

Comments

@haoxintu
Copy link

haoxintu commented Jul 14, 2020

Bugzilla Link 46714
Version trunk
OS Linux
CC @dwblaikie,@zygoloid

Extended Description

Hi, all.

"auto" used in lambda expression is a c++14 extension, but clang doesn't emit the appropriate diagnostic message about it.

For example,

$cat test.cc
int main(){
    auto var = [](auto){};
    return 0;
}
$clang++ -std=c++11 -c test.cc
test.cc:2:19: error: 'auto' not allowed in lambda parameter
    auto var = [](auto){};
                  ^~~~
1 error generated.

I guess clang should give a more specific diagnostic about this like GCC.

$g++ -std=c++11 -c test.cc
test.cc: In function ‘int main()’:
test.cc:2:19: error: use of ‘auto’ in lambda parameter declaration only available with ‘-std=c++14’ or ‘-std=gnu++14’
    2 |     auto var = [](auto){};
      |                   ^~~~

Just a small enhancement I guess, please understand if anything I stated is unsuitable.

Thanks,
Haoxin

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
@cor3ntin cor3ntin added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer good first issue https://github.com/llvm/llvm-project/contribute and removed clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 30, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Aug 30, 2023

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. Assign the issue to you.
  2. Fix the issue locally.
  3. Run the test suite locally.
    3.1) 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.
  4. Create a git commit
  5. Run git clang-format HEAD~1 to format your changes.
  6. Submit the patch to Phabricator.
    6.1) Detailed instructions can be found here

For more instructions on how to submit a patch to LLVM, see our documentation.

If you have any further questions about this issue, don't hesitate to ask via a comment on this Github issue.

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

@cor3ntin
Copy link
Contributor

Looks like a reasonable ask https://godbolt.org/z/d7aT6arnx

@Endilll
Copy link
Contributor

Endilll commented Aug 30, 2023

@cor3ntin can you point out where this diagnostic is defined in sources? I haven't been able to find it.

@cor3ntin
Copy link
Contributor

The code which determines there is an error is here
https://github.com/llvm/llvm-project/blob/main/clang/lib/Sema/SemaType.cpp#L3584-L3594

The error is produced a bit below
https://github.com/llvm/llvm-project/blob/main/clang/lib/Sema/SemaType.cpp#L3737

A solution might be to keep track of when the errors are conditioned on language versions, and modify the existing diagnostic to add "before C++20"

@Endilll
Copy link
Contributor

Endilll commented Aug 30, 2023

def err_auto_not_allowed : Error<
"%select{'auto'|'decltype(auto)'|'__auto_type'|"
"use of "
"%select{class template|function template|variable template|alias template|"
"template template parameter|concept|template}2 %3 requires template "
"arguments; argument deduction}0 not allowed "
"%select{in function prototype"
"|in non-static struct member|in struct member"
"|in non-static union member|in union member"
"|in non-static class member|in interface member"
"|in exception declaration|in template parameter until C++17|in block literal"
"|in template argument|in typedef|in type alias|in function return type"
"|in conversion function type|here|in lambda parameter"
"|in type allocated by 'new'|in K&R-style function parameter"
"|in template parameter|in friend declaration|in function prototype that is "
"not a function declaration|in requires expression parameter}1">;

I've tried to get through this even before writing my previous comment, but I wasn't able to recognize the message, and find it hard still.

CC @AaronBallman

weltschildkroete added a commit to weltschildkroete/llvm-project that referenced this issue Oct 10, 2023
… C++14 (llvm#46059)

Namely, we specify that `auto` in a lambda parameter is a C++14
extension in the error message, which now reads:

`'auto' not allowed in lambda parameter until C++14`

This does not change the behavior for `decltype(auto)` and `__auto_type`
though.

The relevant change to `SemaType.cpp` is the addition of a branch that
sets `Error = 24`, whilst the bulk of the change comes from formatting.
cor3ntin added a commit that referenced this issue May 16, 2024
… C++14 (#46059) (#68540)

Namely, we specify that `auto` in a lambda parameter is a C++14
extension in the error message, which now reads:

`'auto' not allowed in lambda parameter before C++14`

This does not change the behavior for `decltype(auto)` and `__auto_type`
though.

---------

Co-authored-by: cor3ntin <corentinjabot@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer good first issue https://github.com/llvm/llvm-project/contribute
Projects
None yet
Development

No branches or pull requests

4 participants