Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc++] Fix constant initialization of unique_ptr in C++17 and prior #108956

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

philnik777
Copy link
Contributor

@philnik777 philnik777 commented Sep 17, 2024

This is already tested in std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.ctor/default.pass.cpp except that TEST_CONSTINIT doesn't do anything before C++20 without this patch.

@philnik777 philnik777 requested a review from a team as a code owner September 17, 2024 10:50
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Sep 17, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 17, 2024

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/108956.diff

3 Files Affected:

  • (modified) libcxx/include/__memory/compressed_pair.h (+1-1)
  • (added) libcxx/test/std/utilities/smartptr/unique.ptr/constinit.compile.pass.cpp (+19)
  • (modified) libcxx/test/support/test_macros.h (+4-2)
diff --git a/libcxx/include/__memory/compressed_pair.h b/libcxx/include/__memory/compressed_pair.h
index 629e3ad8848ffa..afa78f99612693 100644
--- a/libcxx/include/__memory/compressed_pair.h
+++ b/libcxx/include/__memory/compressed_pair.h
@@ -56,7 +56,7 @@ template <class _ToPad>
 class __compressed_pair_padding {
   char __padding_[((is_empty<_ToPad>::value && !__libcpp_is_final<_ToPad>::value) || is_reference<_ToPad>::value)
                       ? 0
-                      : sizeof(_ToPad) - __datasizeof_v<_ToPad>];
+                      : sizeof(_ToPad) - __datasizeof_v<_ToPad>] = {};
 };
 
 #  define _LIBCPP_COMPRESSED_PAIR(T1, Initializer1, T2, Initializer2)                                                  \
diff --git a/libcxx/test/std/utilities/smartptr/unique.ptr/constinit.compile.pass.cpp b/libcxx/test/std/utilities/smartptr/unique.ptr/constinit.compile.pass.cpp
new file mode 100644
index 00000000000000..a52089385211fb
--- /dev/null
+++ b/libcxx/test/std/utilities/smartptr/unique.ptr/constinit.compile.pass.cpp
@@ -0,0 +1,19 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03
+
+// <memory>
+
+// Check that uniuqe_ptr<T> is constant initialized
+
+#include <memory>
+
+#include "test_macros.h"
+
+TEST_CONSTINIT std::unique_ptr<int> ptr;
diff --git a/libcxx/test/support/test_macros.h b/libcxx/test/support/test_macros.h
index 3aa818af1d2695..ae171b3f223ab3 100644
--- a/libcxx/test/support/test_macros.h
+++ b/libcxx/test/support/test_macros.h
@@ -221,9 +221,11 @@
 #endif
 
 #if TEST_STD_VER > 17
-#define TEST_CONSTINIT constinit
+#  define TEST_CONSTINIT constinit
+#elif __has_cpp_attribute(clang::require_constant_initialization)
+#  define TEST_CONSTINIT [[clang::require_constant_initialization]]
 #else
-#define TEST_CONSTINIT
+#  define TEST_CONSTINIT
 #endif
 
 #if TEST_STD_VER < 11

Copy link
Member

@mstorsjo mstorsjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! This does indeed seem to fix my build issues.

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would these tests fail if we refactored to run them in a constexpr context, or is this required to use constinit? I'm worried that this is a roundabout way of testing this and that we could lose the coverage in the future via some refactoring.

@philnik777
Copy link
Contributor Author

Would these tests fail if we refactored to run them in a constexpr context, or is this required to use constinit? I'm worried that this is a roundabout way of testing this and that we could lose the coverage in the future via some refactoring.

It requires constant initialization, since unique_ptr isn't fully constexpr before C++23(or 26?).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants