Skip to content

Improve error message for invalid lambda captures #94764

@ilya-biryukov

Description

@ilya-biryukov

For the following code (https://gcc.godbolt.org/z/GGoWq45hs) Clang produces an error message that some people find confusing:

struct X {
    int x;
    void foo() {
        [x](){};
    }
};
// <source>:4:10: error: 'x' in capture list does not name a variable

It seems that the error message could indeed be improved, a few ideas that come to mind:

  • mentioning what kind of symbol we are actually referring to: 'class member 'x' cannot appear in capture list as it is not a variable'
  • we could suggest fixes in some situations did you mean to capture 'this'? or did you mean to use a capture with initializer?
  • variable is technically correct and what Clang is using, but maybe it would be better to saylocal variable or parameter? I might be missing some cases, but it sounds more user-friendly than the similar error about "automatic storage duration` that we have today:
<source>:4:10: error: 'x' cannot be captured because it does not have automatic storage duration

Metadata

Metadata

Assignees

Labels

clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzergood first issuehttps://github.com/llvm/llvm-project/contribute

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions