Skip to content

Worse diagnostic for init-captured lambda appearing within a brace initializer #163498

@zyn0217

Description

@zyn0217
struct S {
  template <class T>
  S(T) {}
};

void foo() {
  S s([a(42), &] {}); // Clang diagnoses 'capture default must be first' which is very clear
}

However, if we initialize S with a brace initializer,

struct S {
  template <class T>
  S(T) {}
};

void foo() {
  S s{[a(42), &] {}};
}

Clang diagnoses

<source>:7:8: error: use of undeclared identifier 'a'
    7 |   S s{[a(42), &] {}};
      |        ^
<source>:7:13: error: expected ']'
    7 |   S s{[a(42), &] {}};
      |             ^
<source>:7:7: note: to match this '['
    7 |   S s{[a(42), &] {}};
      |       ^
<source>:7:7: warning: array designators are a C99 extension [-Wc99-designator]
    7 |   S s{[a(42), &] {}};
      |       ^~~~~~~~~~

for which clang does seem to parse it as an array designator and incurs confusing diagnostics.

See it on godbolt:

https://godbolt.org/z/xq6GfEbcx

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerclang:frontendLanguage frontend issues, e.g. anything involving "Sema"lambdaC++11 lambda expressions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions