[ADT] Work around `enumerate` compilation error with modules enabled
authorJakub Kuderski <kubak@google.com>
Mon, 20 Mar 2023 18:27:50 +0000 (14:27 -0400)
committerJakub Kuderski <kubak@google.com>
Mon, 20 Mar 2023 18:27:51 +0000 (14:27 -0400)
This manifests on Apple clang 14 with `-DLLVM_ENABLE_MODULES=1` and
`-DLLVM_ENABLE_ASSERTIONS=1` and seems like a host compiler bug.

Sample compilation failure:
https://green.lab.llvm.org/green/view/LLDB/job/lldb-cmake/52513/consoleFull#-458239162a1ca8a51-895e-46c6-af87-ce24fa4cd561.

Reviewed By: aprantl

Differential Revision: https://reviews.llvm.org/D146340

llvm/include/llvm/ADT/STLExtras.h

index bf33d79..8d73910 100644 (file)
@@ -2385,10 +2385,16 @@ struct index_stream {
 ///
 template <typename FirstRange, typename... RestRanges>
 auto enumerate(FirstRange &&First, RestRanges &&...Rest) {
-  assert((sizeof...(Rest) == 0 ||
-          all_equal({std::distance(adl_begin(First), adl_end(First)),
-                     std::distance(adl_begin(Rest), adl_end(Rest))...})) &&
-         "Ranges have different length");
+  if constexpr (sizeof...(Rest) != 0) {
+#ifndef NDEBUG
+    // Note: Create an array instead of an initializer list to work around an
+    // Apple clang 14 compiler bug.
+    size_t sizes[] = {
+        static_cast<size_t>(std::distance(adl_begin(First), adl_end(First))),
+        static_cast<size_t>(std::distance(adl_begin(Rest), adl_end(Rest)))...};
+    assert(all_equal(sizes) && "Ranges have different length");
+#endif
+  }
   using enumerator = detail::zippy<detail::zip_enumerator, detail::index_stream,
                                    FirstRange, RestRanges...>;
   return enumerator(detail::index_stream{}, std::forward<FirstRange>(First),