[PATCH] D146893: [ADT] Work around MSVC bug affecting `get(enumerator_result)`

Jakub Kuderski via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 25 18:25:44 PDT 2023


kuhar created this revision.
kuhar added reviewers: dstuttard, dblaikie, zero9178, kazu.
Herald added a subscriber: hanchung.
Herald added a project: All.
kuhar requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D146893

Files:
  llvm/include/llvm/ADT/STLExtras.h


Index: llvm/include/llvm/ADT/STLExtras.h
===================================================================
--- llvm/include/llvm/ADT/STLExtras.h
+++ llvm/include/llvm/ADT/STLExtras.h
@@ -2311,14 +2311,22 @@
       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) {
+  /// Returns the value at index `I`. This case covers 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 17.1) return type deduction bug.
     static_assert(I < NumValues, "Index out of bounds");
-    if constexpr (I == 0)
-      return Result.Idx;
-    else
-      return std::get<I - 1>(Result.Storage);
+    return std::get<I - 1>(Result.Storage);
   }
 
   template <typename... Ts>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146893.508360.patch
Type: text/x-patch
Size: 1330 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230326/f4328973/attachment.bin>


More information about the llvm-commits mailing list