[clang] [clang] Improved diagnostics for explicit specialization/instantiation of closure type members (PR #192843)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Apr 19 04:23:18 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: StefanPaulet
<details>
<summary>Changes</summary>
Follow-up from #<!-- -->191419:
- Added separate diagnostics for explicit instantiation and explicit specialization of a closure type member.
- Diagnostic for explicit instantiation is now issued at the point of instantiation and not at the lambda declaration.
- Added note with `defined here` pointing to lambda declaration for both diagnostics.
---
Full diff: https://github.com/llvm/llvm-project/pull/192843.diff
5 Files Affected:
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+3-1)
- (modified) clang/lib/Sema/SemaDecl.cpp (+5-3)
- (modified) clang/lib/Sema/SemaTemplate.cpp (+11-1)
- (modified) clang/test/CXX/drs/cwg17xx.cpp (+19-9)
- (modified) clang/test/SemaCXX/lambda-expressions.cpp (+7-4)
``````````diff
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 193758221c0a9..a94a3552cde88 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -8949,7 +8949,9 @@ let CategoryName = "Lambda Issue" in {
"generic lambdas are incompatible with C++11">,
InGroup<CXXPre14Compat>, DefaultIgnore;
def err_lambda_explicit_spec : Error<
- "lambda call operator should not be explicitly specialized or instantiated">;
+ "a member of a lambda should not be explicitly specialized">;
+ def err_lambda_explicit_inst : Error<
+ "a member of a lambda should not be explicitly instantiated">;
// C++17 '*this' captures.
def warn_cxx14_compat_star_this_lambda_capture : Warning<
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 6afdd73328eba..033c7fddc47d6 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -16331,10 +16331,12 @@ Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D,
// have the LSI properly restored.
if (isGenericLambdaCallOperatorSpecialization(FD)) {
// C++2c 7.5.5.2p17 A member of a closure type shall not be explicitly
- // instantiated, explicitly specialized.
- if (FD->getTemplateSpecializationInfo()
- ->isExplicitInstantiationOrSpecialization()) {
+ // specialized.
+ if (FD->getTemplateSpecializationInfo()->isExplicitSpecialization()) {
Diag(FD->getLocation(), diag::err_lambda_explicit_spec);
+ CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(FD->getParent());
+ Diag(RD->getLocation(), diag::note_defined_here) << RD;
+
FD->setInvalidDecl();
PushFunctionScope();
} else {
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 16b4815be2010..d150619fb290f 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -11052,8 +11052,18 @@ DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
// Let the ASTConsumer know that this function has been explicitly
// instantiated now, and its linkage might have changed.
Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization));
- } else if (TSK == TSK_ExplicitInstantiationDefinition)
+ } else if (TSK == TSK_ExplicitInstantiationDefinition) {
+ // C++2c [expr.prim.lambda#closure-19] A member of a closure type shall not
+ // be explicitly instantiated.
+ if (CXXRecordDecl *RD =
+ dyn_cast<CXXRecordDecl>(Specialization->getParent());
+ RD && RD->isLambda()) {
+ Diag(D.getBeginLoc(), diag::err_lambda_explicit_inst);
+ Diag(RD->getLocation(), diag::note_defined_here) << RD;
+ return (Decl *)nullptr;
+ }
InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
+ }
// C++0x [temp.explicit]p2:
// If the explicit instantiation is for a member function, a member class
diff --git a/clang/test/CXX/drs/cwg17xx.cpp b/clang/test/CXX/drs/cwg17xx.cpp
index d2ea5b6f2ae52..0d0ccbcf0bc6a 100644
--- a/clang/test/CXX/drs/cwg17xx.cpp
+++ b/clang/test/CXX/drs/cwg17xx.cpp
@@ -226,28 +226,38 @@ using Bind = Instantiate<Internal<Template>::template Bind, Argument>;
namespace cwg1780 { // cwg1780: 23
#if __cplusplus >= 201103L
+#if __cplusplus >= 201703L
+#define CONSTEXPR constexpr
+#elif __cplusplus >= 201103L
+#define CONSTEXPR
+#endif
+
auto l = []() -> int { return 5; };
using L = decltype(l);
class A {
-#if __cplusplus >= 201703L
- friend constexpr auto L::operator()() const -> int; // expected-error{{a member of a lambda should not be the target of a friend declaration}}
-#else
- friend auto L::operator()() const -> int; // expected-error{{a member of a lambda should not be the target of a friend declaration}}
-#endif
+ friend CONSTEXPR auto L::operator()() const -> int;
+ // since-cxx11-error at -1{{a member of a lambda should not be the target of a friend declaration}}
};
+#undef CONSTEXPR
+
#if __cplusplus >= 201402L
-auto gl = [](auto a) { return 5; };
+auto gl = [](auto a) { return 5; }; // #cwg1780-spec
using GL = decltype(gl);
template <>
-auto GL::operator()(int a) const { // expected-error{{lambda call operator should not be explicitly specialized or instantiated}}
+auto GL::operator()(int a) const {
+// since-cxx11-error at -1{{a member of a lambda should not be explicitly specialized}}
+// since-cxx11-note-re@#cwg1780-spec{{{{'\(lambda at .+\)'}} defined here}}
return 6;
}
-auto gll = [](auto a) { return 5; }; // expected-error{{lambda call operator should not be explicitly specialized or instantiated}}
+auto gll = [](auto a) -> int { return 5; }; // #cwg1780-inst
+
using GLL = decltype(gll);
-template auto GLL::operator()<int>(int a) const; // expected-note{{in instantiation of function template specialization 'cwg1780::(lambda)::operator()<int>' requested here}}
+template auto GLL::operator()<int>(int a) const -> int;
+// since-cxx11-error at -1{{a member of a lambda should not be explicitly instantiated}}
+// since-cxx11-note-re@#cwg1780-inst{{{{'\(lambda at .+\)'}} defined here}}
#endif
#endif
diff --git a/clang/test/SemaCXX/lambda-expressions.cpp b/clang/test/SemaCXX/lambda-expressions.cpp
index 6ae7ac9888b41..25cc2e14c1897 100644
--- a/clang/test/SemaCXX/lambda-expressions.cpp
+++ b/clang/test/SemaCXX/lambda-expressions.cpp
@@ -788,18 +788,21 @@ void GH67492() {
// FIXME: This currently causes clang to crash in C++11 mode.
#if __cplusplus >= 201402L
namespace GH83267 {
-auto l = [](auto a) { return 1; };
+auto l = [](auto a) { return 1; }; // expected-note {{defined here}}
using type = decltype(l);
template<>
-auto type::operator()(int a) const { // expected-error{{lambda call operator should not be explicitly specialized or instantiated}}
+auto type::operator()(int a) const { // expected-error {{a member of a lambda should not be explicitly specialized}}
return c; // expected-error {{use of undeclared identifier 'c'}}
}
-auto ll = [](auto a) { return 1; }; // expected-error{{lambda call operator should not be explicitly specialized or instantiated}}
+auto ll = [](auto a) -> int { return 1; }; // expected-note 2 {{defined here}}
using t = decltype(ll);
-template auto t::operator()<int>(int a) const; // expected-note {{in instantiation}}
+template auto t::operator()<int>(int a) const -> int; // expected-error {{a member of a lambda should not be explicitly instantiated}}
+template <typename T>
+using cll = int(*)(T);
+template t::operator cll<int>() const; // expected-error {{a member of a lambda should not be explicitly instantiated}}
}
#endif
``````````
</details>
https://github.com/llvm/llvm-project/pull/192843
More information about the cfe-commits
mailing list