[PATCH] D87728: [flang][msvc] Work around if constexpr (false) evaluation. NFC.

Michael Kruse via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 15 16:22:01 PDT 2020


Meinersbur created this revision.
Meinersbur added reviewers: isuruf, DavidTruby, sscalpone, tskeith, klausler.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Meinersbur requested review of this revision.

MSVC tries to expand templates that are in the false-branch of a `if constexpr` construct. In this case, the condition checks whether a tuple has at least one element and then is trying to access it using `std::get<0>`, which fails when the tuple has 0 elements.

The workaround is to extract that case into a separate method.

This patch is part of the series to make flang compilable with MS Visual Studio http://lists.llvm.org/pipermail/flang-dev/2020-July/000448.html.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D87728

Files:
  flang/lib/Parser/basic-parsers.h


Index: flang/lib/Parser/basic-parsers.h
===================================================================
--- flang/lib/Parser/basic-parsers.h
+++ flang/lib/Parser/basic-parsers.h
@@ -729,13 +729,7 @@
       return RESULT{};
     } else {
       if constexpr (sizeof...(PARSER) == 1) {
-        if constexpr (std::is_same_v<Success, typename PARSER::resultType...>) {
-          if (std::get<0>(parsers_).Parse(state)) {
-            return RESULT{};
-          }
-        } else if (auto arg{std::get<0>(parsers_).Parse(state)}) {
-          return RESULT{std::move(*arg)};
-        }
+        return ParseOne(state);
       } else {
         ApplyArgs<PARSER...> results;
         using Sequence = std::index_sequence_for<PARSER...>;
@@ -749,6 +743,17 @@
   }
 
 private:
+  std::optional<resultType> ParseOne(ParseState &state) const {
+    if constexpr (std::is_same_v<Success, typename PARSER::resultType...>) {
+      if (std::get<0>(parsers_).Parse(state)) {
+        return RESULT{};
+      }
+    } else if (auto arg{std::get<0>(parsers_).Parse(state)}) {
+      return RESULT{std::move(*arg)};
+    }
+    return std::nullopt;
+  }
+
   const std::tuple<PARSER...> parsers_;
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87728.292049.patch
Type: text/x-patch
Size: 1193 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200915/e2fc7515/attachment.bin>


More information about the llvm-commits mailing list