[PATCH] D37667: [C++14][Sema] Disallow decltype(auto) deduction for lambdas

Blitz Rakete via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 10 03:09:36 PDT 2017


Rakete1111 created this revision.

`decltype(auto) lambda = []{};` is currently allowed by clang, gcc and MSVC, but it is actually not valid, because `decltype([]{})` (which is how the type of `lambda` is deduced) is ill-formed, I think. I honestly could argue both ways (and I did once), but the fact that `decltype(auto) list = {0,1};` is already rejected by all three, it seems like the most sensible way is to disallow it for lambdas too.


https://reviews.llvm.org/D37667

Files:
  include/clang/Basic/DiagnosticSemaKinds.td
  lib/Sema/SemaTemplateDeduction.cpp
  test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p7-1y.cpp


Index: test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p7-1y.cpp
===================================================================
--- test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p7-1y.cpp
+++ test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p7-1y.cpp
@@ -96,3 +96,10 @@
 
 auto init_list_1() { return { 1, 2, 3 }; } // expected-error {{cannot deduce return type from initializer list}}
 decltype(auto) init_list_2() { return { 1, 2, 3 }; } // expected-error {{cannot deduce return type from initializer list}}
+
+auto auto_lambda() { return []{}; }
+decltype(auto) decltype_lambda() { return []{}; } // expected-error {{cannot deduce 'decltype(auto)' from lambda}}
+
+auto AutoLambda = []{};
+decltype(auto) DecltypeLambda = []{}; // expected-error {{cannot deduce 'decltype(auto)' from lambda}}
+
Index: lib/Sema/SemaTemplateDeduction.cpp
===================================================================
--- lib/Sema/SemaTemplateDeduction.cpp
+++ lib/Sema/SemaTemplateDeduction.cpp
@@ -4314,6 +4314,9 @@
       if (isa<InitListExpr>(Init)) {
         Diag(Init->getLocStart(), diag::err_decltype_auto_initializer_list);
         return DAR_FailedAlreadyDiagnosed;
+      } else if (isa<LambdaExpr>(Init)) {
+        Diag(Init->getLocStart(), diag::err_decltype_auto_lambda);
+        return DAR_FailedAlreadyDiagnosed;
       }
 
       QualType Deduced = BuildDecltypeType(Init, Init->getLocStart(), false);
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td
+++ include/clang/Basic/DiagnosticSemaKinds.td
@@ -2021,6 +2021,8 @@
   "cannot form %select{pointer to|reference to|array of}0 'decltype(auto)'">;
 def err_decltype_auto_initializer_list : Error<
   "cannot deduce 'decltype(auto)' from initializer list">;
+def err_decltype_auto_lambda : Error<
+  "cannot deduce 'decltype(auto)' from lambda">;
 
 // C++17 deduced class template specialization types
 def err_deduced_class_template_compound_type : Error<


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37667.114511.patch
Type: text/x-patch
Size: 2044 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170910/2c395046/attachment-0001.bin>


More information about the cfe-commits mailing list