From: Richard Smith Date: Mon, 15 Aug 2016 02:34:23 +0000 (+0000) Subject: Disable lambda-capture of decomposition declaration bindings for now, until CWG X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=47b2f02130c48642d579f56c3678f67a565af249;p=clang Disable lambda-capture of decomposition declaration bindings for now, until CWG agrees on how they're supposed to work. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@278648 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 7fa2eb0e5a..8a9f8c0e6c 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -7046,14 +7046,9 @@ def ext_ms_anonymous_record : ExtWarn< InGroup; // C++ local classes -def err_reference_to_local_var_in_enclosing_function : Error< - "reference to local variable %0 declared in enclosing function %1">; -def err_reference_to_local_var_in_enclosing_block : Error< - "reference to local variable %0 declared in enclosing block literal">; -def err_reference_to_local_var_in_enclosing_lambda : Error< - "reference to local variable %0 declared in enclosing lambda expression">; -def err_reference_to_local_var_in_enclosing_context : Error< - "reference to local variable %0 declared in enclosing context">; +def err_reference_to_local_in_enclosing_context : Error< + "reference to local %select{variable|binding}1 %0 declared in enclosing " + "%select{%3|block literal|lambda expression|context}2">; def err_static_data_member_not_allowed_in_local_class : Error< "static data member %0 not allowed in local class %1">; diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 2e53e3bd64..201248b3b3 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -2838,6 +2838,10 @@ ExprResult Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, return ULE; } +static void +diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, + ValueDecl *var, DeclContext *DC); + /// \brief Complete semantic analysis for a reference to the given declaration. ExprResult Sema::BuildDeclarationNameExpr( const CXXScopeSpec &SS, const DeclarationNameInfo &NameInfo, NamedDecl *D, @@ -2981,7 +2985,12 @@ ExprResult Sema::BuildDeclarationNameExpr( // These are always lvalues. valueKind = VK_LValue; type = type.getNonReferenceType(); - // FIXME: Adjust cv-qualifiers for capture. + // FIXME: Support lambda-capture of BindingDecls, once CWG actually + // decides how that's supposed to work. + auto *BD = cast(VD); + if (BD->getDeclContext()->isFunctionOrMethod() && + BD->getDeclContext() != CurContext) + diagnoseUncapturableValueReference(*this, Loc, BD, CurContext); break; } @@ -13214,7 +13223,7 @@ void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func, static void diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, - VarDecl *var, DeclContext *DC) { + ValueDecl *var, DeclContext *DC) { DeclContext *VarDC = var->getDeclContext(); // If the parameter still belongs to the translation unit, then @@ -13234,25 +13243,21 @@ diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, if (!S.getLangOpts().CPlusPlus && !S.CurContext->isFunctionOrMethod()) return; + unsigned ValueKind = isa(var) ? 1 : 0; + unsigned ContextKind = 3; // unknown if (isa(VarDC) && cast(VarDC->getParent())->isLambda()) { - S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_lambda) - << var->getIdentifier(); - } else if (FunctionDecl *fn = dyn_cast(VarDC)) { - S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_function) - << var->getIdentifier() << fn->getDeclName(); + ContextKind = 2; + } else if (isa(VarDC)) { + ContextKind = 0; } else if (isa(VarDC)) { - S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_block) - << var->getIdentifier(); - } else { - // FIXME: Is there any other context where a local variable can be - // declared? - S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_context) - << var->getIdentifier(); + ContextKind = 1; } + S.Diag(loc, diag::err_reference_to_local_in_enclosing_context) + << var << ValueKind << ContextKind << VarDC; S.Diag(var->getLocation(), diag::note_entity_declared_at) - << var->getIdentifier(); + << var; // FIXME: Add additional diagnostic info about class etc. which prevents // capture. diff --git a/test/SemaCXX/cxx1z-decomposition.cpp b/test/SemaCXX/cxx1z-decomposition.cpp index 0ddd9ea304..12c863c8c3 100644 --- a/test/SemaCXX/cxx1z-decomposition.cpp +++ b/test/SemaCXX/cxx1z-decomposition.cpp @@ -37,4 +37,14 @@ constexpr bool g(S &&s) { } static_assert(g({1, 2})); +void enclosing() { + struct S { int a; }; + auto [n] = S(); // expected-note 2{{'n' declared here}} + + struct Q { int f() { return n; } }; // expected-error {{reference to local binding 'n' declared in enclosing function}} + // FIXME: This is probably supposed to be valid, but we do not have clear rules on how it's supposed to work. + (void) [&] { return n; }; // expected-error {{reference to local binding 'n' declared in enclosing function}} + (void) [n] {}; // expected-error {{'n' in capture list does not name a variable}} +} + // FIXME: by-value array copies