r224620 - DR1048: drop top-level cv-qualifiers when deducing the return type of a

Richard Smith richard-llvm at metafoo.co.uk
Fri Dec 19 14:10:51 PST 2014


Author: rsmith
Date: Fri Dec 19 16:10:51 2014
New Revision: 224620

URL: http://llvm.org/viewvc/llvm-project?rev=224620&view=rev
Log:
DR1048: drop top-level cv-qualifiers when deducing the return type of a
lambda-expression in C++11, to match the C++14 rules.

Modified:
    cfe/trunk/lib/Sema/SemaLambda.cpp
    cfe/trunk/lib/Sema/SemaStmt.cpp
    cfe/trunk/test/CXX/drs/dr10xx.cpp
    cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p4.cpp
    cfe/trunk/test/SemaCXX/lambda-expressions.cpp
    cfe/trunk/www/cxx_dr_status.html

Modified: cfe/trunk/lib/Sema/SemaLambda.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaLambda.cpp?rev=224620&r1=224619&r2=224620&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaLambda.cpp (original)
+++ cfe/trunk/lib/Sema/SemaLambda.cpp Fri Dec 19 16:10:51 2014
@@ -617,7 +617,7 @@ void Sema::deduceClosureReturnType(Captu
   // If it was ever a placeholder, it had to been deduced to DependentTy.
   assert(CSI.ReturnType.isNull() || !CSI.ReturnType->isUndeducedType()); 
 
-  // C++ Core Issue #975, proposed resolution:
+  // C++ core issue 975:
   //   If a lambda-expression does not include a trailing-return-type,
   //   it is as if the trailing-return-type denotes the following type:
   //     - if there are no return statements in the compound-statement,
@@ -631,6 +631,10 @@ void Sema::deduceClosureReturnType(Captu
   //       same, that common type;
   //     - otherwise, the program is ill-formed.
   //
+  // C++ core issue 1048 additionally removes top-level cv-qualifiers
+  // from the types of returned expressions to match the C++14 auto
+  // deduction rules.
+  //
   // In addition, in blocks in non-C++ modes, if all of the return
   // statements are enumerator-like expressions of some type T, where
   // T has a name for linkage, then we infer the return type of the
@@ -679,7 +683,8 @@ void Sema::deduceClosureReturnType(Captu
     const ReturnStmt *RS = *I;
     const Expr *RetE = RS->getRetValue();
 
-    QualType ReturnType = (RetE ? RetE->getType() : Context.VoidTy);
+    QualType ReturnType =
+        (RetE ? RetE->getType() : Context.VoidTy).getUnqualifiedType();
     if (Context.hasSameType(ReturnType, CSI.ReturnType))
       continue;
 

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=224620&r1=224619&r2=224620&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Fri Dec 19 16:10:51 2014
@@ -2652,8 +2652,12 @@ Sema::ActOnCapScopeReturnStmt(SourceLoca
         return StmtError();
       RetValExp = Result.get();
 
+      // DR1048: even prior to C++14, we should use the 'auto' deduction rules
+      // when deducing a return type for a lambda-expression (or by extension
+      // for a block). These rules differ from the stated C++11 rules only in
+      // that they remove top-level cv-qualifiers.
       if (!CurContext->isDependentContext())
-        FnRetType = RetValExp->getType();
+        FnRetType = RetValExp->getType().getUnqualifiedType();
       else
         FnRetType = CurCap->ReturnType = Context.DependentTy;
     } else {

Modified: cfe/trunk/test/CXX/drs/dr10xx.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/drs/dr10xx.cpp?rev=224620&r1=224619&r2=224620&view=diff
==============================================================================
--- cfe/trunk/test/CXX/drs/dr10xx.cpp (original)
+++ cfe/trunk/test/CXX/drs/dr10xx.cpp Fri Dec 19 16:10:51 2014
@@ -14,6 +14,24 @@ namespace std {
   };
 }
 
+namespace dr1048 { // dr1048: 3.6
+  struct A {};
+  const A f();
+  A g();
+  typedef const A CA;
+#if __cplusplus >= 201103L
+  // ok: we deduce non-const A in each case.
+  A &&a = [] (int n) {
+    while (1) switch (n) {
+      case 0: return f();
+      case 1: return g();
+      case 2: return A();
+      case 3: return CA();
+    }
+  } (0);
+#endif
+}
+
 namespace dr1070 { // dr1070: 3.5
 #if __cplusplus >= 201103L
   struct A {

Modified: cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p4.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p4.cpp?rev=224620&r1=224619&r2=224620&view=diff
==============================================================================
--- cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p4.cpp (original)
+++ cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p4.cpp Fri Dec 19 16:10:51 2014
@@ -34,15 +34,12 @@ X infer_X_return_type(X x) {
   }(5);
 }
 
-X infer_X_return_type_fail(X x) {
+X infer_X_return_type_2(X x) {
   return [x](int y) {
     if (y > 0)
       return X();
     else
-      return x;
-#if __cplusplus <= 201103L
-    // expected-error at -2 {{return type 'const X' must match previous return type 'X' when lambda expression has unspecified explicit return type}}
-#endif
+      return x; // ok even in c++11, per dr1048.
   }(5);
 }
 

Modified: cfe/trunk/test/SemaCXX/lambda-expressions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/lambda-expressions.cpp?rev=224620&r1=224619&r2=224620&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/lambda-expressions.cpp (original)
+++ cfe/trunk/test/SemaCXX/lambda-expressions.cpp Fri Dec 19 16:10:51 2014
@@ -258,9 +258,7 @@ namespace TypeDeduction {
   void f() {
     const S s {};
     S &&t = [&] { return s; } ();
-#if __cplusplus <= 201103L
-    // expected-error at -2 {{drops qualifiers}}
-#else
+#if __cplusplus > 201103L
     S &&u = [&] () -> auto { return s; } ();
 #endif
   }

Modified: cfe/trunk/www/cxx_dr_status.html
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/www/cxx_dr_status.html?rev=224620&r1=224619&r2=224620&view=diff
==============================================================================
--- cfe/trunk/www/cxx_dr_status.html (original)
+++ cfe/trunk/www/cxx_dr_status.html Fri Dec 19 16:10:51 2014
@@ -6103,7 +6103,7 @@ and <I>POD class</I></td>
     <td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1048">1048</a></td>
     <td>CD3</td>
     <td><TT>auto</TT> deduction and lambda return type deduction.</td>
-    <td class="none" align="center">Unknown</td>
+    <td class="svn" align="center">SVN</td>
   </tr>
   <tr class="open" id="1049">
     <td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1049">1049</a></td>





More information about the cfe-commits mailing list