massberg updated this revision to Diff 482140. massberg added a comment. Additionally check for ODR use and remove inlcude added by accident.
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,21 @@ +// RUN: %clang_cc1 -fsyntax-only -std=c++20 -verify %s + +namespace GH48755 { +constexpr int z = 2; + +auto f() { + constexpr int x_constexpr = 1; + const int x_const = 1; + static int x_static = 1; + + auto lambda1 = [] <auto y = x_constexpr> {}; // expected-error {{default argument references local variable x_constexpr of enclosing function}} + auto lambda2 = [] <auto y = x_static> {}; // expected-error {{default argument references local variable x_static of enclosing function}} + auto lambda3 = [] <auto y = x_const> {}; // expected-error {{default argument references local variable x_const of enclosing function}} + auto lambda4 = [] <auto y = 42> {}; + auto lambda5 = [] <auto y = z> {}; + auto lambda6 = [] <auto y> {}; + + const int x_int_const = 1; + auto lambda7 = [] <int y = x_int_const> {}; // expected-error {{default argument references local variable x_int_const of enclosing function}} +} +} Index: clang/lib/Sema/SemaTemplate.cpp =================================================================== --- clang/lib/Sema/SemaTemplate.cpp +++ clang/lib/Sema/SemaTemplate.cpp @@ -1587,6 +1587,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() && !DRE->isNonOdrUse()) { + Diag(DRE->getLocation(), + diag::err_param_default_argument_references_local) + << VD->getNameAsString(); + } + } + } + // Check for unexpanded parameter packs. if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) return Param;
Index: clang/test/SemaTemplate/default-template-arguments.cpp =================================================================== --- /dev/null +++ clang/test/SemaTemplate/default-template-arguments.cpp @@ -0,0 +1,21 @@ +// RUN: %clang_cc1 -fsyntax-only -std=c++20 -verify %s + +namespace GH48755 { +constexpr int z = 2; + +auto f() { + constexpr int x_constexpr = 1; + const int x_const = 1; + static int x_static = 1; + + auto lambda1 = [] <auto y = x_constexpr> {}; // expected-error {{default argument references local variable x_constexpr of enclosing function}} + auto lambda2 = [] <auto y = x_static> {}; // expected-error {{default argument references local variable x_static of enclosing function}} + auto lambda3 = [] <auto y = x_const> {}; // expected-error {{default argument references local variable x_const of enclosing function}} + auto lambda4 = [] <auto y = 42> {}; + auto lambda5 = [] <auto y = z> {}; + auto lambda6 = [] <auto y> {}; + + const int x_int_const = 1; + auto lambda7 = [] <int y = x_int_const> {}; // expected-error {{default argument references local variable x_int_const of enclosing function}} +} +} Index: clang/lib/Sema/SemaTemplate.cpp =================================================================== --- clang/lib/Sema/SemaTemplate.cpp +++ clang/lib/Sema/SemaTemplate.cpp @@ -1587,6 +1587,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() && !DRE->isNonOdrUse()) { + Diag(DRE->getLocation(), + diag::err_param_default_argument_references_local) + << VD->getNameAsString(); + } + } + } + // Check for unexpanded parameter packs. if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) return Param;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits