r216936 - Don't allow lambdas to capture invalid decls during template instantiations.

Richard Trieu rtrieu at google.com
Tue Sep 2 12:32:45 PDT 2014


Author: rtrieu
Date: Tue Sep  2 14:32:44 2014
New Revision: 216936

URL: http://llvm.org/viewvc/llvm-project?rev=216936&view=rev
Log:
Don't allow lambdas to capture invalid decls during template instantiations.

Fixes PR20731.

Modified:
    cfe/trunk/lib/Sema/TreeTransform.h
    cfe/trunk/test/SemaCXX/lambda-expressions.cpp

Modified: cfe/trunk/lib/Sema/TreeTransform.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/TreeTransform.h?rev=216936&r1=216935&r2=216936&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/TreeTransform.h (original)
+++ cfe/trunk/lib/Sema/TreeTransform.h Tue Sep  2 14:32:44 2014
@@ -9085,7 +9085,7 @@ TreeTransform<Derived>::TransformLambdaS
     VarDecl *CapturedVar
       = cast_or_null<VarDecl>(getDerived().TransformDecl(C->getLocation(),
                                                          C->getCapturedVar()));
-    if (!CapturedVar) {
+    if (!CapturedVar || CapturedVar->isInvalidDecl()) {
       Invalid = true;
       continue;
     }

Modified: cfe/trunk/test/SemaCXX/lambda-expressions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/lambda-expressions.cpp?rev=216936&r1=216935&r2=216936&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/lambda-expressions.cpp (original)
+++ cfe/trunk/test/SemaCXX/lambda-expressions.cpp Tue Sep  2 14:32:44 2014
@@ -363,3 +363,53 @@ namespace PR18473 {
 void PR19249() {
   auto x = [&x]{}; // expected-error {{cannot appear in its own init}}
 }
+
+namespace PR20731 {
+template <class L, int X = sizeof(L)>
+void Job(L l);
+
+template <typename... Args>
+void Logger(Args &&... args) {
+  auto len = Invalid_Function((args)...);
+  // expected-error at -1 {{use of undeclared identifier 'Invalid_Function'}}
+  Job([len]() {});
+}
+
+void GetMethod() {
+  Logger();
+  // expected-note at -1 {{in instantiation of function template specialization 'PR20731::Logger<>' requested here}}
+}
+
+template <typename T>
+struct A {
+  T t;
+  // expected-error at -1 {{field has incomplete type 'void'}}
+};
+
+template <typename F>
+void g(F f) {
+  auto a = A<decltype(f())>{};
+  // expected-note at -1 {{in instantiation of template class 'PR20731::A<void>' requested here}}
+  auto xf = [a, f]() {};
+  int x = sizeof(xf);
+};
+void f() {
+  g([] {});
+  // expected-note-re at -1 {{in instantiation of function template specialization 'PR20731::g<(lambda at {{.*}}>' requested here}}
+}
+
+template <class _Rp> struct function {
+  template <class _Fp>
+  function(_Fp) {
+    static_assert(sizeof(_Fp) > 0, "Type must be complete.");
+  }
+};
+
+template <typename T> void p(T t) {
+  auto l = some_undefined_function(t);
+  // expected-error at -1 {{use of undeclared identifier 'some_undefined_function'}}
+  function<void()>(([l]() {}));
+}
+void q() { p(0); }
+// expected-note at -1 {{in instantiation of function template specialization 'PR20731::p<int>' requested here}}
+}





More information about the cfe-commits mailing list