OSDN Git Service

[libc++] Make sure std::is_scalar returns true for block types
authorLouis Dionne <ldionne@apple.com>
Tue, 21 Jan 2020 20:39:43 +0000 (12:39 -0800)
committerLouis Dionne <ldionne@apple.com>
Wed, 22 Jan 2020 01:15:15 +0000 (17:15 -0800)
Summary:
The compiler already treats them as scalar types, so the library should
too. Furthermore, this allows blocks to be used in more places, for
example in std::optional, which requires an object type.

rdar://problem/57892832

Reviewers: dexonsmith, EricWF, mclow.lists
Differential Revision: https://reviews.llvm.org/D72708

libcxx/include/__config
libcxx/include/type_traits
libcxx/test/libcxx/type_traits/is_scalar.objc.pass.mm [new file with mode: 0644]
libcxx/test/libcxx/utilities/optional/block.objc.pass.mm [new file with mode: 0644]

index 84cd46b..7770009 100644 (file)
@@ -463,6 +463,10 @@ typedef __char32_t char32_t;
 #define _LIBCPP_HAS_OBJC_ARC_WEAK
 #endif
 
+#if __has_extension(blocks)
+#  define _LIBCPP_HAS_EXTENSION_BLOCKS
+#endif
+
 #if !(__has_feature(cxx_relaxed_constexpr))
 #define _LIBCPP_HAS_NO_CXX14_CONSTEXPR
 #endif
index c0c3934..f8ee564 100644 (file)
@@ -1016,11 +1016,17 @@ _LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool is_fundamental_v
 
 // is_scalar
 
+template <class _Tp> struct __is_block : false_type {};
+#if defined(_LIBCPP_HAS_EXTENSION_BLOCKS)
+template <class _Rp, class ..._Args> struct __is_block<_Rp (^)(_Args...)> : true_type {};
+#endif
+
 template <class _Tp> struct _LIBCPP_TEMPLATE_VIS is_scalar
     : public integral_constant<bool, is_arithmetic<_Tp>::value     ||
                                      is_member_pointer<_Tp>::value ||
                                      is_pointer<_Tp>::value        ||
                                      __is_nullptr_t<_Tp>::value    ||
+                                     __is_block<_Tp>::value        ||
                                      is_enum<_Tp>::value           > {};
 
 template <> struct _LIBCPP_TEMPLATE_VIS is_scalar<nullptr_t> : public true_type {};
diff --git a/libcxx/test/libcxx/type_traits/is_scalar.objc.pass.mm b/libcxx/test/libcxx/type_traits/is_scalar.objc.pass.mm
new file mode 100644 (file)
index 0000000..1f7ffec
--- /dev/null
@@ -0,0 +1,38 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// UNSUPPORTED: c++98, c++03
+
+// <type_traits>
+
+// std::is_scalar
+
+// Make sure we report that blocks are scalar types.
+
+#include <type_traits>
+#include <optional>
+
+struct Foo { };
+template <int> struct Arg { };
+
+static_assert(std::is_scalar<void (^)(void)>::value, "");
+static_assert(std::is_scalar<void (^)()>::value, "");
+static_assert(std::is_scalar<void (^)(Arg<0>)>::value, "");
+static_assert(std::is_scalar<void (^)(Arg<0>, Arg<1>)>::value, "");
+static_assert(std::is_scalar<void (^)(Arg<0>, Arg<1>, Arg<2>)>::value, "");
+static_assert(std::is_scalar<Foo (^)(void)>::value, "");
+static_assert(std::is_scalar<Foo (^)()>::value, "");
+static_assert(std::is_scalar<Foo (^)(Arg<0>)>::value, "");
+static_assert(std::is_scalar<Foo (^)(Arg<0>, Arg<1>)>::value, "");
+static_assert(std::is_scalar<Foo (^)(Arg<0>, Arg<1>, Arg<2>)>::value, "");
+
+
+int main(int, char**) {
+    std::optional<Foo (^)(Arg<0>)> opt; (void)opt;
+    return 0;
+}
diff --git a/libcxx/test/libcxx/utilities/optional/block.objc.pass.mm b/libcxx/test/libcxx/utilities/optional/block.objc.pass.mm
new file mode 100644 (file)
index 0000000..8d2722b
--- /dev/null
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++98, c++03, c++11, c++14
+
+// <optional>
+
+// This test makes sure that we can create a `std::optional` containing
+// an Objective-C++ block.
+
+#include <optional>
+#include <cassert>
+
+int main(int, char**)
+{
+    using Block = void (^)(void);
+    std::optional<Block> block;
+    assert(!block);
+
+    return 0;
+}