[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
Thu Dec 8 08:43:20 PST 2022
massberg updated this revision to Diff 481295.
massberg added a comment.
Reuse existing error message.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D139400/new/
https://reviews.llvm.org/D139400
Files:
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 {{default argument references local variable x of enclosing function}}
+ 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
@@ -8,6 +8,7 @@
// This file implements semantic analysis for C++ templates.
//===----------------------------------------------------------------------===//
+#include <iostream> // massberg
#include "TreeTransform.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
@@ -1587,6 +1588,17 @@
// 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->isLocalVarDecl()) {
+ Diag(DRE->getLocation(),
+ diag::err_param_default_argument_references_local)
+ << VD->getNameAsString();
+ }
+ }
+ }
+
// Check for unexpanded parameter packs.
if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
return Param;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139400.481295.patch
Type: text/x-patch
Size: 1737 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221208/081deb0b/attachment.bin>
More information about the cfe-commits
mailing list