Skip to content

Commit 476f7f6

Browse files
[clang][Sema] Emit more specific diagnostic for auto in lambda before 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 <[email protected]>
1 parent a383b3c commit 476f7f6

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,9 @@ Improvements to Clang's diagnostics
494494
Improvements to Clang's time-trace
495495
----------------------------------
496496

497+
- Clang now specifies that using ``auto`` in a lambda parameter is a C++14 extension when
498+
appropriate. (`#46059: <https://github.com/llvm/llvm-project/issues/46059>`_).
499+
497500
Bug Fixes in This Version
498501
-------------------------
499502
- Clang's ``-Wundefined-func-template`` no longer warns on pure virtual

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2433,7 +2433,9 @@ def err_auto_not_allowed : Error<
24332433
"|in template parameter|in friend declaration|in function prototype that is "
24342434
"not a function declaration|in requires expression parameter"
24352435
"|in array declaration"
2436-
"|in declaration of conversion function template}1">;
2436+
"|in declaration of conversion function template"
2437+
"|in lambda parameter before C++14}1">;
2438+
24372439
def err_dependent_deduced_tst : Error<
24382440
"typename specifier refers to "
24392441
"%select{class template|function template|variable template|alias template|"

clang/lib/Sema/SemaType.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3257,9 +3257,13 @@ static QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state,
32573257
Info = &SemaRef.InventedParameterInfos.back();
32583258
} else {
32593259
// In C++14, generic lambdas allow 'auto' in their parameters.
3260-
if (!SemaRef.getLangOpts().CPlusPlus14 || !Auto ||
3261-
Auto->getKeyword() != AutoTypeKeyword::Auto) {
3262-
Error = 16;
3260+
if (!SemaRef.getLangOpts().CPlusPlus14 && Auto &&
3261+
Auto->getKeyword() == AutoTypeKeyword::Auto) {
3262+
Error = 25; // auto not allowed in lambda parameter (before C++14)
3263+
break;
3264+
} else if (!Auto || Auto->getKeyword() != AutoTypeKeyword::Auto) {
3265+
Error = 16; // __auto_type or decltype(auto) not allowed in lambda
3266+
// parameter
32633267
break;
32643268
}
32653269
Info = SemaRef.getCurLambda();

clang/test/SemaCXX/auto-cxx0x.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ thread_local auto x; // expected-error {{requires an initializer}}
1212
void g() {
1313
[](auto){}(0);
1414
#if __cplusplus == 201103L
15-
// expected-error@-2 {{'auto' not allowed in lambda parameter}}
15+
// expected-error@-2 {{'auto' not allowed in lambda parameter before C++14}}
1616
#endif
1717
}
1818

1919
void rdar47689465() {
2020
int x = 0;
2121
[](auto __attribute__((noderef)) *){}(&x);
2222
#if __cplusplus == 201103L
23-
// expected-error@-2 {{'auto' not allowed in lambda parameter}}
23+
// expected-error@-2 {{'auto' not allowed in lambda parameter before C++14}}
2424
#endif
2525
}

0 commit comments

Comments
 (0)