[PATCH] Fix Bug 23189 in C++14 type inference for lambdas with undeclared identifiers

Tomasz Dudziak me at tdudziak.com
Wed May 20 07:04:12 PDT 2015


Hi dim, yaron.keren, rsmith,

Fixes [[ https://llvm.org/bugs/show_bug.cgi?id=23189 | Bug 23189 ]] by adding additional checks in `DeduceFunctionTypeFromReturnExpr`.

Without these checks, the code attempts to dereference a null pointer when trying to infer the return type of a lambda if there are multiple `return` statements and one of them contains an expression with an undeclared identifier.

http://reviews.llvm.org/D9880

Files:
  lib/Sema/SemaStmt.cpp
  test/SemaCXX/cxx14-auto-lambda-bad-return-1.cpp
  test/SemaCXX/cxx14-auto-lambda-bad-return-2.cpp

Index: lib/Sema/SemaStmt.cpp
===================================================================
--- lib/Sema/SemaStmt.cpp
+++ lib/Sema/SemaStmt.cpp
@@ -3031,9 +3031,10 @@
   //  has multiple return statements, the return type is deduced for each return
   //  statement. [...] if the type deduced is not the same in each deduction,
   //  the program is ill-formed.
-  if (AT->isDeduced() && !FD->isInvalidDecl()) {
+  if (AT->isDeduced() && !AT->isDependentType() && !FD->isInvalidDecl()) {
     AutoType *NewAT = Deduced->getContainedAutoType();
     if (!FD->isDependentContext() &&
+        !NewAT->isDependentType() &&
         !Context.hasSameType(AT->getDeducedType(), NewAT->getDeducedType())) {
       const LambdaScopeInfo *LambdaSI = getCurLambda();
       if (LambdaSI && LambdaSI->HasImplicitReturnType) {
Index: test/SemaCXX/cxx14-auto-lambda-bad-return-1.cpp
===================================================================
--- /dev/null
+++ test/SemaCXX/cxx14-auto-lambda-bad-return-1.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify %s
+
+// First return triggers and error, the type of the second return is known
+// (void). Type inference will try to reconcile these types and can get
+// confused while doing so (bug 23189).
+
+void test() {
+    auto a = [] {
+        return b; // expected-error {{use of undeclared identifier 'b'}}
+        return;
+    };
+}
Index: test/SemaCXX/cxx14-auto-lambda-bad-return-2.cpp
===================================================================
--- /dev/null
+++ test/SemaCXX/cxx14-auto-lambda-bad-return-2.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify %s
+
+// First return is properly typed (void) but the second one triggers an error.
+// Type inference might still try to deduce the type of this lambda and can
+// get confused while doing so (bug 23189).
+
+void test() {
+    auto a = [] {
+        return;
+        return &b; // expected-error {{use of undeclared identifier 'b'}}
+    };
+}

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D9880.26150.patch
Type: text/x-patch
Size: 2017 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150520/b0ea4406/attachment.bin>


More information about the cfe-commits mailing list