Fail on implicit convert from BOOL on ObjC in folly::dynamic
authorMarc Horowitz <mhorowitz@fb.com>
Tue, 18 Apr 2017 22:21:35 +0000 (15:21 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 18 Apr 2017 22:34:33 +0000 (15:34 -0700)
Summary: On some platforms (iPhone 5C at least), ObjC/Objc++ BOOL is really signed char.  There is code which expects this to be a boolean when converted to dynamic (and then to JSON and into JS), but the old code treated it as a number.  This makes such code (like [mobileConfig getBool:]) fail to compile, so the developer needs to resolve the ambiguity one way or the other.

Reviewed By: yfeldblum

Differential Revision: D4648133

fbshipit-source-id: 76ece7803a1e966dca08bdb857af7990035544a0

folly/Portability.h
folly/dynamic-inl.h

index 1792abf5966c4172b2415132b7b26ec1d68a6037..51032fdacd43addc99388e9f765cca2e246e77a4 100644 (file)
@@ -331,6 +331,12 @@ using namespace FOLLY_GFLAGS_NAMESPACE;
 
 namespace folly {
 
+#if __OBJC__
+constexpr auto kIsObjC = true;
+#else
+constexpr auto kIsObjC = false;
+#endif
+
 #if defined(__linux__) && !FOLLY_MOBILE
 constexpr auto kIsLinux = true;
 #else
index dbe36b5d9347314dfe479e9d9415ff5335156213..3c85ee4b27c351d383d232cf5adca1da6e86cce5 100644 (file)
@@ -310,6 +310,9 @@ inline dynamic::~dynamic() noexcept { destroy(); }
 template <class T>
 struct dynamic::NumericTypeHelper<
     T, typename std::enable_if<std::is_integral<T>::value>::type> {
+  static_assert(
+      !kIsObjC || sizeof(T) > sizeof(char),
+      "char-sized types are ambiguous in objc; cast to bool or wider type");
   using type = int64_t;
 };
 template <>