[libcxx] r304172 - [coroutines] Make coroutine_handle<T>::from_address ill-formed for everything but void*.

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Mon May 29 12:24:26 PDT 2017


Author: ericwf
Date: Mon May 29 14:24:25 2017
New Revision: 304172

URL: http://llvm.org/viewvc/llvm-project?rev=304172&view=rev
Log:
[coroutines] Make coroutine_handle<T>::from_address ill-formed for everything but void*.

from_address requires that the provided pointer refer to the suspended coroutine,
which doesn't have a type, or at least not one knowable by the user. Therefore
every use of `from_address` with a typed pointer is almost certainly a bug.

This behavior is a part of the TS specification, but hopefully it will be
in the future.

Added:
    libcxx/trunk/test/std/experimental/language.support/support.coroutines/coroutine.handle/coroutine.handle.export/from_address.fail.cpp
    libcxx/trunk/test/std/experimental/language.support/support.coroutines/lit.local.cfg
Modified:
    libcxx/trunk/include/experimental/coroutine

Modified: libcxx/trunk/include/experimental/coroutine
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/experimental/coroutine?rev=304172&r1=304171&r2=304172&view=diff
==============================================================================
--- libcxx/trunk/include/experimental/coroutine (original)
+++ libcxx/trunk/include/experimental/coroutine Mon May 29 14:24:25 2017
@@ -145,6 +145,19 @@ public:
         return __tmp;
     }
 
+    // FIXME: Should from_address(nullptr) be allowed?
+    _LIBCPP_ALWAYS_INLINE
+    static coroutine_handle from_address(nullptr_t) _NOEXCEPT {
+      return {};
+    }
+
+    template <class _Tp, bool _CallIsValid = false>
+    static coroutine_handle from_address(_Tp*) {
+      static_assert(_CallIsValid,
+       "coroutine_handle<void>::from_address cannot be called with "
+        "non-void pointers");
+    }
+
 private:
   bool __is_suspended() const _NOEXCEPT  {
     // FIXME actually implement a check for if the coro is suspended.
@@ -221,8 +234,19 @@ public:
       return {};
     }
 
-    // from_address cannot be used with the coroutines promise type.
-    static coroutine_handle from_address(_Promise*) = delete;
+    template <class _Tp, bool _CallIsValid = false>
+    static coroutine_handle from_address(_Tp*) {
+      static_assert(_CallIsValid,
+       "coroutine_handle<promise_type>::from_address cannot be called with "
+        "non-void pointers");
+    }
+
+    template <bool _CallIsValid = false>
+    static coroutine_handle from_address(_Promise*) {
+      static_assert(_CallIsValid,
+       "coroutine_handle<promise_type>::from_address cannot be used with "
+        "pointers to the coroutine's promise type; use 'from_promise' instead");
+    }
 
     _LIBCPP_ALWAYS_INLINE
     static coroutine_handle from_promise(_Promise& __promise) _NOEXCEPT {

Added: libcxx/trunk/test/std/experimental/language.support/support.coroutines/coroutine.handle/coroutine.handle.export/from_address.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/experimental/language.support/support.coroutines/coroutine.handle/coroutine.handle.export/from_address.fail.cpp?rev=304172&view=auto
==============================================================================
--- libcxx/trunk/test/std/experimental/language.support/support.coroutines/coroutine.handle/coroutine.handle.export/from_address.fail.cpp (added)
+++ libcxx/trunk/test/std/experimental/language.support/support.coroutines/coroutine.handle/coroutine.handle.export/from_address.fail.cpp Mon May 29 14:24:25 2017
@@ -0,0 +1,46 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++98, c++03, c++11
+// <experimental/coroutine>
+
+// template <class Promise = void>
+// struct coroutine_handle;
+
+// static coroutine_handle from_address(void*) noexcept
+
+// Test that `from_address` is explicitly ill-formed when called with a typed
+// pointer. The user cannot possibly have a typed pointer to the coroutine.
+// FIXME: This behavior is an extension, and should upstreamed into the TS or
+// the test removed if the TS changes are rejected.
+
+#include <experimental/coroutine>
+#include <type_traits>
+#include <cassert>
+
+namespace coro = std::experimental;
+
+int main()
+{
+  {
+    using H = coro::coroutine_handle<>;
+    // expected-error at experimental/coroutine:* 3 {{coroutine_handle<void>::from_address cannot be called with non-void pointers}}
+    H::from_address((int*)nullptr); // expected-note {{requested here}}
+    H::from_address((const void*)nullptr); // expected-note {{requested here}}
+    H::from_address((const char*)nullptr); // expected-note {{requested here}}
+  }
+  {
+    using H = coro::coroutine_handle<int>;
+    // expected-error at experimental/coroutine:* 1 {{static_assert failed "coroutine_handle<promise_type>::from_address cannot be used with pointers to the coroutine's promise type; use 'from_promise' instead"}}
+    H::from_address((const char*)nullptr); // expected-note {{requested here}}
+    // expected-error at experimental/coroutine:* 1 {{coroutine_handle<promise_type>::from_address cannot be called with non-void pointers}}
+    H::from_address((int*)nullptr); // expected-note {{requested here}}
+  }
+}

Added: libcxx/trunk/test/std/experimental/language.support/support.coroutines/lit.local.cfg
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/experimental/language.support/support.coroutines/lit.local.cfg?rev=304172&view=auto
==============================================================================
--- libcxx/trunk/test/std/experimental/language.support/support.coroutines/lit.local.cfg (added)
+++ libcxx/trunk/test/std/experimental/language.support/support.coroutines/lit.local.cfg Mon May 29 14:24:25 2017
@@ -0,0 +1,9 @@
+# If the compiler doesn't support coroutines mark all of the tests under
+# this directory as unsupported. Otherwise add the required `-fcoroutines-ts`
+# flag.
+if 'fcoroutines-ts' not in config.available_features:
+  config.unsupported = True
+else:
+  import copy
+  config.test_format.cxx = copy.deepcopy(config.test_format.cxx)
+  config.test_format.cxx.compile_flags += ['-fcoroutines-ts']




More information about the cfe-commits mailing list