[PATCH] D139400: [clang] Show error when a local variables is passed as default template parameter
Jens Massberg via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 6 00:55:22 PST 2022
massberg created this revision.
massberg added a reviewer: ilya-biryukov.
Herald added a project: All.
massberg requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
I haven't found an existing error message for this in
clang/include/clang/Basic/DiagnosticSemaKinds.td so I have added a new
one. I'm not sure if it in the right place and the formulation might be
improved.
Fixes #48755
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D139400
Files:
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaTemplate.cpp
clang/test/SemaTemplate/default-template-arguments.cpp
Index: clang/test/SemaTemplate/default-template-arguments.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaTemplate/default-template-arguments.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c++20 -verify %s
+
+namespace GH48755 {
+constexpr auto z = 2;
+
+auto f() {
+ const auto x = 1;
+
+ auto lambda1 = [] <auto y = x> {}; // expected-error {{local variables may not be used as default parameters}}
+ auto lambda2 = [] <auto y = 42> {};
+ auto lambda3 = [] <auto y = z> {};
+ auto lambda4 = [] <auto y> {};
+}
+}
Index: clang/lib/Sema/SemaTemplate.cpp
===================================================================
--- clang/lib/Sema/SemaTemplate.cpp
+++ clang/lib/Sema/SemaTemplate.cpp
@@ -1587,6 +1587,15 @@
// Check the well-formedness of the default template argument, if provided.
if (Default) {
+ // Local variables may not be used as default template arguments.
+ if (auto *DRE = dyn_cast<DeclRefExpr>(Default)) {
+ if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
+ if(VD->hasLocalStorage()) {
+ Diag(DRE->getLocation(), diag::err_local_variable_as_default_in_template) << VD->getNameAsString();
+ }
+ }
+ }
+
// Check for unexpanded parameter packs.
if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
return Param;
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -7896,6 +7896,9 @@
def warn_cxx17_compat_lambda_def_ctor_assign : Warning<
"%select{default construction|assignment}0 of lambda is incompatible with "
"C++ standards before C++20">, InGroup<CXXPre20Compat>, DefaultIgnore;
+
+ def err_local_variable_as_default_in_template : Error<
+ "local variable '%0' may not be used as default parameter">;
}
def err_return_in_captured_stmt : Error<
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139400.480366.patch
Type: text/x-patch
Size: 2035 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221206/9df82b18/attachment.bin>
More information about the cfe-commits
mailing list