[PATCH] D29908: Disallow returning a __block variable via a move

Akira Hatanaka via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 14 21:27:16 PST 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL295150: [Sema] Disallow returning a __block variable via a move. (authored by ahatanak).

Changed prior to commit:
  https://reviews.llvm.org/D29908?vs=88430&id=88486#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D29908

Files:
  cfe/trunk/lib/Sema/SemaStmt.cpp
  cfe/trunk/test/SemaObjCXX/blocks.mm


Index: cfe/trunk/test/SemaObjCXX/blocks.mm
===================================================================
--- cfe/trunk/test/SemaObjCXX/blocks.mm
+++ cfe/trunk/test/SemaObjCXX/blocks.mm
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -fblocks -Wno-objc-root-class %s
+// RUN: %clang_cc1 -fsyntax-only -verify -fblocks -Wno-objc-root-class -std=c++11 %s
 @protocol NSObject;
 
 void bar(id(^)(void));
@@ -144,3 +144,17 @@
 
   template void f<X>(X);
 }
+
+namespace MoveBlockVariable {
+struct B0 {
+};
+
+struct B1 { // expected-note 2 {{candidate constructor (the implicit}}
+  B1(B0&&); // expected-note {{candidate constructor not viable}}
+};
+
+B1 test_move() {
+  __block B0 b;
+  return b; // expected-error {{no viable conversion from returned value of type 'MoveBlockVariable::B0' to function return type 'MoveBlockVariable::B1'}}
+}
+}
Index: cfe/trunk/lib/Sema/SemaStmt.cpp
===================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp
+++ cfe/trunk/lib/Sema/SemaStmt.cpp
@@ -2743,15 +2743,17 @@
   // ...automatic...
   if (!VD->hasLocalStorage()) return false;
 
+  // Return false if VD is a __block variable. We don't want to implicitly move
+  // out of a __block variable during a return because we cannot assume the
+  // variable will no longer be used.
+  if (VD->hasAttr<BlocksAttr>()) return false;
+
   if (AllowParamOrMoveConstructible)
     return true;
 
   // ...non-volatile...
   if (VD->getType().isVolatileQualified()) return false;
 
-  // __block variables can't be allocated in a way that permits NRVO.
-  if (VD->hasAttr<BlocksAttr>()) return false;
-
   // Variables with higher required alignment than their type's ABI
   // alignment cannot use NRVO.
   if (!VD->getType()->isDependentType() && VD->hasAttr<AlignedAttr>() &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D29908.88486.patch
Type: text/x-patch
Size: 1820 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170215/0dc4f65d/attachment.bin>


More information about the cfe-commits mailing list