[PATCH] D92211: [clang] Improve diagnostics for auto-return-type function if the return expr contains expr.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 27 00:32:13 PST 2020


hokein created this revision.
hokein added a reviewer: sammccall.
Herald added a project: clang.
hokein requested review of this revision.

Given the following case:

  auto k() {
    return undef();
    return 1;
  }

Prior to the patch, clang emits an `cannot initialize return object of type
'auto' with an rvalue of type 'int'` diagnostic on the second return
(because the return type of the function cannot be deduced from the first contain-errors return).

This patch suppresses this error.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92211

Files:
  clang/lib/Sema/SemaStmt.cpp
  clang/test/SemaCXX/lambda-expressions.cpp


Index: clang/test/SemaCXX/lambda-expressions.cpp
===================================================================
--- clang/test/SemaCXX/lambda-expressions.cpp
+++ clang/test/SemaCXX/lambda-expressions.cpp
@@ -521,6 +521,10 @@
       return undeclared_error; // expected-error {{use of undeclared identifier}}
     return 0;
   };
+  auto bar = []() {
+    return undef(); // expected-error {{use of undeclared identifier}}
+    return 0; // verify no init_conversion_failed diagnostic emitted.
+  };
 }
 }
 
Index: clang/lib/Sema/SemaStmt.cpp
===================================================================
--- clang/lib/Sema/SemaStmt.cpp
+++ clang/lib/Sema/SemaStmt.cpp
@@ -3327,9 +3327,11 @@
   }
 
   if (HasDeducedReturnType) {
+    FunctionDecl *FD = CurLambda->CallOperator;
+    if (FD->isInvalidDecl())
+      return StmtError();
     // In C++1y, the return type may involve 'auto'.
     // FIXME: Blocks might have a return type of 'auto' explicitly specified.
-    FunctionDecl *FD = CurLambda->CallOperator;
     if (CurCap->ReturnType.isNull())
       CurCap->ReturnType = FD->getReturnType();
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92211.307975.patch
Type: text/x-patch
Size: 1117 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201127/c4d007ea/attachment.bin>


More information about the cfe-commits mailing list