[PATCH] D93510: Fix llvm::Optional build breaks in MSVC using std::is_trivially_copyable

James Player via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 18 11:32:58 PST 2020


jplayer-nv updated this revision to Diff 312862.
jplayer-nv edited the summary of this revision.
jplayer-nv added a comment.

1. Use `std::is_trivially_copyable` instead of `llvm::is_trivially_copyable` in `Optional` storage template specialization logic.
2. Added test cases to OptionalTest.cpp which will fail compilation (verified on MSVC 16.8.3) if new conditions are absent from `Optional` storage template specialization logic.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D93510/new/

https://reviews.llvm.org/D93510

Files:
  llvm/include/llvm/ADT/Optional.h
  llvm/unittests/ADT/OptionalTest.cpp


Index: llvm/unittests/ADT/OptionalTest.cpp
===================================================================
--- llvm/unittests/ADT/OptionalTest.cpp
+++ llvm/unittests/ADT/OptionalTest.cpp
@@ -390,6 +390,77 @@
   EXPECT_EQ(0u, Immovable::Destructions);
 }
 
+// Craft a class which is_trivially_copyable, but not
+// is_trivially_copy_constructible.
+struct NonTCopy {
+  NonTCopy() = default;
+
+  // Delete the volatile copy constructor.
+  NonTCopy(volatile NonTCopy const &) = delete;
+
+  // Leave the default copy constructor unspecified (deleted)
+
+  // This template can serve as the copy constructor, but isn't chosen
+  // by =default in a class with a 'NonTCopy' member.
+  template <typename Self = NonTCopy>
+  NonTCopy(Self const &Other) : Val(Other.Val) {}
+
+  NonTCopy &operator=(NonTCopy const &) = default;
+
+  int Val{0};
+};
+
+TEST(OptionalTest, DeletedCopyConstructor) {
+
+  // Expect compile to fail if 'trivial' version of
+  // optional_detail::OptionalStorage is chosen.
+  using NonTCopyOptT = Optional<NonTCopy>;
+  NonTCopyOptT NonTCopy1;
+
+  // Check that the Optional can be copy constructed.
+  NonTCopyOptT NonTCopy2{NonTCopy1};
+
+  // Check that the Optional can be copy assigned.
+  NonTCopy1 = NonTCopy2;
+}
+
+// Craft a class which is_trivially_copyable, but not
+// is_trivially_copy_assignable.
+class NonTAssign {
+public:
+  NonTAssign() = default;
+  NonTAssign(NonTAssign const &) = default;
+
+  // Delete the volatile copy assignment.
+  NonTAssign &operator=(volatile NonTAssign const &) = delete;
+
+  // Leave the default copy assignment unspecified (deleted).
+
+  // This template can serve as the copy assignment, but isn't chosen
+  // by =default in a class with a 'NonTAssign' member.
+  template <typename Self = NonTAssign>
+  NonTAssign &operator=(Self const &Other) {
+    A = Other.A;
+    return *this;
+  }
+
+  int A{0};
+};
+
+TEST(OptionalTest, DeletedCopyAssignment) {
+
+  // Expect compile to fail if 'trivial' version of
+  // optional_detail::OptionalStorage is chosen.
+  using NonTAssignOptT = Optional<NonTAssign>;
+  NonTAssignOptT NonTAssign1;
+
+  // Check that the Optional can be copy constructed.
+  NonTAssignOptT NonTAssign2{NonTAssign1};
+
+  // Check that the Optional can be copy assigned.
+  NonTAssign1 = NonTAssign2;
+}
+
 #if LLVM_HAS_RVALUE_REFERENCE_THIS
 
 TEST(OptionalTest, MoveGetValueOr) {
Index: llvm/include/llvm/ADT/Optional.h
===================================================================
--- llvm/include/llvm/ADT/Optional.h
+++ llvm/include/llvm/ADT/Optional.h
@@ -33,7 +33,9 @@
 struct in_place_t {};
 
 /// Storage for any type.
-template <typename T, bool = is_trivially_copyable<T>::value>
+template <typename T, bool = std::is_trivially_copyable<T>::value
+                          &&std::is_trivially_copy_constructible<T>::value
+                              &&std::is_trivially_copy_assignable<T>::value>
 class OptionalStorage {
   union {
     char empty;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93510.312862.patch
Type: text/x-patch
Size: 2982 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201218/ce24f955/attachment.bin>


More information about the llvm-commits mailing list