[ADT] Work around MSVC bug affecting `get(enumerator_result)`
authorJakub Kuderski <kubak@google.com>
Mon, 27 Mar 2023 14:28:36 +0000 (10:28 -0400)
committerJakub Kuderski <kubak@google.com>
Mon, 27 Mar 2023 14:35:09 +0000 (10:35 -0400)
This happened on a small number of MSVC releases (19.31.31xxx, Visual Studio 2022 17.1.x), and worked fine on everything else.

The issue seemed to be related to return type deduction on a function with and `if constexpr`; the compiler got confused and deduced different function return type from the type of the return statement.

The workaround is to split `get` into two functions using `enable_if`.

Reviewed By: dstuttard

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

llvm/include/llvm/ADT/STLExtras.h

index d19e2f9..155d5c3 100644 (file)
@@ -2311,14 +2311,21 @@ template <typename... Refs> struct enumerator_result<std::size_t, Refs...> {
       return Storage;
   }
 
-  /// Returns the value at index `I`. This includes the index.
-  template <std::size_t I>
-  friend decltype(auto) get(const enumerator_result &Result) {
-    static_assert(I < NumValues, "Index out of bounds");
-    if constexpr (I == 0)
-      return Result.Idx;
-    else
-      return std::get<I - 1>(Result.Storage);
+  /// Returns the value at index `I`. This case covers the index.
+  template <std::size_t I, typename = std::enable_if_t<I == 0>>
+  friend std::size_t get(const enumerator_result &Result) {
+    return Result.Idx;
+  }
+
+  /// Returns the value at index `I`. This case covers references to the
+  /// iteratees.
+  template <std::size_t I, typename = std::enable_if_t<I != 0>>
+  friend std::tuple_element_t<I, value_reference_tuple>
+  get(const enumerator_result &Result) {
+    // Note: This is a separate function from the other `get`, instead of an
+    // `if constexpr` case, to work around an MSVC 19.31.31XXX compiler
+    // (Visual Studio 2022 17.1) return type deduction bug.
+    return std::get<I - 1>(Result.Storage);
   }
 
   template <typename... Ts>