[llvm-branch-commits] [cfe-branch] r196469 - Merging r196423:

Bill Wendling isanbard at gmail.com
Wed Dec 4 21:24:30 PST 2013


Author: void
Date: Wed Dec  4 23:24:30 2013
New Revision: 196469

URL: http://llvm.org/viewvc/llvm-project?rev=196469&view=rev
Log:
Merging r196423:
------------------------------------------------------------------------
r196423 | faisalv | 2013-12-04 14:43:08 -0800 (Wed, 04 Dec 2013) | 17 lines

Fix for PR18052 - Lambdas within NSDMI's and default arguments in Nested classes.

Clang currently croaks on the following:
  struct X1 {
    struct X2 {
      int L = ([] (int i) { return i; })(2);
    };
  };

asserting that the containing lexical context of the lambda is not Sema's cur context, when pushing the lambda's decl context on.

This occurs because (prior to this patch) getContainingDC always returns the non-nested class for functions at class scope (even for inline member functions of nested classes (to account for delayed parsing of their bodies)).  The patch addresses this by having getContainingDC always return the lexical DC for a lambda's call operator.

Link to the bug: http://llvm.org/bugs/show_bug.cgi?id=18052
Link to Richard Smith's feedback on phabricator: http://llvm-reviews.chandlerc.com/D2331

Thanks!
------------------------------------------------------------------------

Modified:
    cfe/branches/release_34/   (props changed)
    cfe/branches/release_34/lib/Sema/SemaDecl.cpp
    cfe/branches/release_34/test/SemaCXX/cxx1y-generic-lambdas.cpp

Propchange: cfe/branches/release_34/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Dec  4 23:24:30 2013
@@ -1,4 +1,4 @@
 /cfe/branches/type-system-rewrite:134693-134817
-/cfe/trunk:195126,195128,195135-195136,195146,195149,195154,195158,195163,195168,195174,195249,195268,195283,195303,195326,195329,195367,195384,195409,195420,195422,195501,195547,195556,195558,195587,195620,195635,195669,195687,195693,195710,195713,195716,195756,195760,195768,195777,195789,195792,195804,195827,195843-195844,195877,195887-195888,195897,195903,195905-195906,195932,195936-195943,195970,195983,196045,196048,196050,196058,196114-196115,196153,196206,196215,196370
+/cfe/trunk:195126,195128,195135-195136,195146,195149,195154,195158,195163,195168,195174,195249,195268,195283,195303,195326,195329,195367,195384,195409,195420,195422,195501,195547,195556,195558,195587,195620,195635,195669,195687,195693,195710,195713,195716,195756,195760,195768,195777,195789,195792,195804,195827,195843-195844,195877,195887-195888,195897,195903,195905-195906,195932,195936-195943,195970,195983,196045,196048,196050,196058,196114-196115,196153,196206,196215,196370,196423
 /cfe/trunk/test:170344
 /cfe/trunk/test/SemaTemplate:126920

Modified: cfe/branches/release_34/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_34/lib/Sema/SemaDecl.cpp?rev=196469&r1=196468&r2=196469&view=diff
==============================================================================
--- cfe/branches/release_34/lib/Sema/SemaDecl.cpp (original)
+++ cfe/branches/release_34/lib/Sema/SemaDecl.cpp Wed Dec  4 23:24:30 2013
@@ -870,7 +870,16 @@ DeclContext *Sema::getContainingDC(DeclC
   // Functions defined inline within classes aren't parsed until we've
   // finished parsing the top-level class, so the top-level class is
   // the context we'll need to return to.
-  if (isa<FunctionDecl>(DC)) {
+  // A Lambda call operator whose parent is a class must not be treated 
+  // as an inline member function.  A Lambda can be used legally
+  // either as an in-class member initializer or a default argument.  These
+  // are parsed once the class has been marked complete and so the containing
+  // context would be the nested class (when the lambda is defined in one);
+  // If the class is not complete, then the lambda is being used in an 
+  // ill-formed fashion (such as to specify the width of a bit-field, or
+  // in an array-bound) - in which case we still want to return the 
+  // lexically containing DC (which could be a nested class). 
+  if (isa<FunctionDecl>(DC) && !isLambdaCallOperator(DC)) {
     DC = DC->getLexicalParent();
 
     // A function not defined within a class will always return to its

Modified: cfe/branches/release_34/test/SemaCXX/cxx1y-generic-lambdas.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_34/test/SemaCXX/cxx1y-generic-lambdas.cpp?rev=196469&r1=196468&r2=196469&view=diff
==============================================================================
--- cfe/branches/release_34/test/SemaCXX/cxx1y-generic-lambdas.cpp (original)
+++ cfe/branches/release_34/test/SemaCXX/cxx1y-generic-lambdas.cpp Wed Dec  4 23:24:30 2013
@@ -844,5 +844,65 @@ void Do() {
   [=] { v.size(); };
 }
 
+}
+
+namespace inclass_lambdas_within_nested_classes {
+namespace ns1 {
+
+struct X1 {  
+  struct X2 {
+    enum { E = [](auto i) { return i; }(3) }; //expected-error{{inside of a constant expression}}\
+                                          //expected-error{{not an integral constant}}
+    int L = ([] (int i) { return i; })(2);
+    void foo(int i = ([] (int i) { return i; })(2)) { }
+    int B : ([](int i) { return i; })(3); //expected-error{{inside of a constant expression}}\
+                                          //expected-error{{not an integral constant}}
+    int arr[([](int i) { return i; })(3)]; //expected-error{{inside of a constant expression}}\
+                                           //expected-error{{must have a constant size}}
+    int (*fp)(int) = [](int i) { return i; };
+    void fooptr(int (*fp)(char) = [](char c) { return 0; }) { }
+    int L2 = ([](auto i) { return i; })(2);
+    void fooG(int i = ([] (auto i) { return i; })(2)) { }
+    int BG : ([](auto i) { return i; })(3); //expected-error{{inside of a constant expression}}  \
+                                             //expected-error{{not an integral constant}}
+    int arrG[([](auto i) { return i; })(3)]; //expected-error{{inside of a constant expression}}\
+                                           //expected-error{{must have a constant size}}
+    int (*fpG)(int) = [](auto i) { return i; };
+    void fooptrG(int (*fp)(char) = [](auto c) { return 0; }) { }
+  };
+};
+} //end ns
+
+namespace ns2 {
+struct X1 {  
+  template<class T>
+  struct X2 {
+    int L = ([] (T i) { return i; })(2);
+    void foo(int i = ([] (int i) { return i; })(2)) { }
+    int B : ([](T i) { return i; })(3); //expected-error{{inside of a constant expression}}\
+                                          //expected-error{{not an integral constant}}
+    int arr[([](T i) { return i; })(3)]; //expected-error{{inside of a constant expression}}\
+                                           //expected-error{{must have a constant size}}
+    int (*fp)(T) = [](T i) { return i; };
+    void fooptr(T (*fp)(char) = [](char c) { return 0; }) { }
+    int L2 = ([](auto i) { return i; })(2);
+    void fooG(T i = ([] (auto i) { return i; })(2)) { }
+    int BG : ([](auto i) { return i; })(3); //expected-error{{not an integral constant}}
+    int arrG[([](auto i) { return i; })(3)]; //expected-error{{must have a constant size}}
+    int (*fpG)(T) = [](auto i) { return i; };
+    void fooptrG(T (*fp)(char) = [](auto c) { return 0; }) { }
+    template<class U = char> int fooG2(T (*fp)(U) = [](auto a) { return 0; }) { return 0; }
+    template<class U = char> int fooG3(T (*fp)(U) = [](auto a) { return 0; });
+  };
+};
+template<class T> 
+template<class U>
+int X1::X2<T>::fooG3(T (*fp)(U)) { return 0; } 
+X1::X2<int> x2; //expected-note 3{{in instantiation of}}
+int run1 = x2.fooG2();
+int run2 = x2.fooG3();
+} // end ns
+
+
 
-}
\ No newline at end of file
+} //end ns inclass_lambdas_within_nested_classes
\ No newline at end of file





More information about the llvm-branch-commits mailing list