[PATCH] D140265: [llvm][ADT] Allow returning `std::nullopt` in TypeSwitch

Markus Böck via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 17 13:37:16 PST 2022


zero9178 created this revision.
zero9178 added reviewers: rriddle, MaskRay, dblaikie, lattner.
Herald added a subscriber: StephenFan.
Herald added a project: All.
zero9178 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Returning `std::nullopt` from the case of a `TypeSwitch` yields broken results, by either falling through to another case, or falling of the switch entirely and hitting an assertion. This is simply due to the use of `operator=` of what is now `std::optional`, which has an overload specifically for `std::nullopt`, causing the internal optional, used for the `TypeSwitch` result to be reset, instead of a value being constructed from the `std::nullopt`.

The fix is to simply use the `emplace` method of `std::optional`, causing a value to always be constructed from the value returned by the case function.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140265

Files:
  llvm/include/llvm/ADT/TypeSwitch.h
  llvm/unittests/ADT/TypeSwitchTest.cpp


Index: llvm/unittests/ADT/TypeSwitchTest.cpp
===================================================================
--- llvm/unittests/ADT/TypeSwitchTest.cpp
+++ llvm/unittests/ADT/TypeSwitchTest.cpp
@@ -86,3 +86,31 @@
   EXPECT_EQ(0, translate(DerivedD()));
   EXPECT_EQ(-1, translate(DerivedE()));
 }
+
+TEST(TypeSwitchTest, CaseOptional) {
+  auto translate = [](auto value) {
+    return TypeSwitch<Base *, std::optional<int>>(&value)
+        .Case([](DerivedA *) { return 0; })
+        .Case([](DerivedC *) { return std::nullopt; })
+        .Default([](Base *) { return -1; });
+  };
+  EXPECT_EQ(0, translate(DerivedA()));
+  EXPECT_EQ(std::nullopt, translate(DerivedC()));
+  EXPECT_EQ(-1, translate(DerivedD()));
+  EXPECT_EQ(std::nullopt,
+            (TypeSwitch<Base *, std::optional<int>>(nullptr).Default(
+                [](Base *) { return std::nullopt; })));
+}
+
+TEST(TypeSwitchTest, CasesOptional) {
+  auto translate = [](auto value) {
+    return TypeSwitch<Base *, std::optional<int>>(&value)
+        .Case<DerivedB, DerivedC>([](auto *) { return std::nullopt; })
+        .Case([](DerivedA *) { return 0; })
+        .Default([](Base *) { return -1; });
+  };
+  EXPECT_EQ(0, translate(DerivedA()));
+  EXPECT_EQ(std::nullopt, translate(DerivedB()));
+  EXPECT_EQ(std::nullopt, translate(DerivedC()));
+  EXPECT_EQ(-1, translate(DerivedD()));
+}
Index: llvm/include/llvm/ADT/TypeSwitch.h
===================================================================
--- llvm/include/llvm/ADT/TypeSwitch.h
+++ llvm/include/llvm/ADT/TypeSwitch.h
@@ -119,7 +119,7 @@
 
     // Check to see if CaseT applies to 'value'.
     if (auto caseValue = BaseT::template castValue<CaseT>(this->value))
-      result = caseFn(caseValue);
+      result.emplace(caseFn(caseValue));
     return *this;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140265.483777.patch
Type: text/x-patch
Size: 1807 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221217/d2fd1fe6/attachment.bin>


More information about the llvm-commits mailing list