Skip to content

Commit

Permalink
P1413R3 Deprecate aligned_storage & aligned_union (#2583)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephan T. Lavavej <[email protected]>
  • Loading branch information
AlexGuteniev and StephanTLavavej authored May 17, 2022
1 parent 46c0bcb commit cbd964d
Show file tree
Hide file tree
Showing 20 changed files with 79 additions and 28 deletions.
2 changes: 1 addition & 1 deletion stl/inc/algorithm
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct _Optimistic_temporary_buffer { // temporary storage with _alloca-like att

_Ty* _Data; // points to heap memory iff _Capacity > _Optimistic_count
ptrdiff_t _Capacity;
aligned_union_t<0, _Ty> _Stack_space[_Optimistic_count];
_Aligned_storage_t<sizeof(_Ty), alignof(_Ty)> _Stack_space[_Optimistic_count];
};

#ifdef __cpp_lib_concepts
Expand Down
2 changes: 1 addition & 1 deletion stl/inc/condition_variable
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public:
private:
shared_ptr<mutex> _Myptr;

aligned_storage_t<_Cnd_internal_imp_size, _Cnd_internal_imp_alignment> _Cnd_storage;
_Aligned_storage_t<_Cnd_internal_imp_size, _Cnd_internal_imp_alignment> _Cnd_storage;

_NODISCARD _Cnd_t _Mycnd() noexcept { // get pointer to _Cnd_internal_imp_t inside _Cnd_storage
return reinterpret_cast<_Cnd_t>(&_Cnd_storage);
Expand Down
4 changes: 2 additions & 2 deletions stl/inc/mutex
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private:
friend condition_variable;
friend condition_variable_any;

aligned_storage_t<_Mtx_internal_imp_size, _Mtx_internal_imp_alignment> _Mtx_storage;
_Aligned_storage_t<_Mtx_internal_imp_size, _Mtx_internal_imp_alignment> _Mtx_storage;

_Mtx_t _Mymtx() noexcept { // get pointer to _Mtx_internal_imp_t inside _Mtx_storage
return reinterpret_cast<_Mtx_t>(&_Mtx_storage);
Expand Down Expand Up @@ -698,7 +698,7 @@ public:
}

private:
aligned_storage_t<_Cnd_internal_imp_size, _Cnd_internal_imp_alignment> _Cnd_storage;
_Aligned_storage_t<_Cnd_internal_imp_size, _Cnd_internal_imp_alignment> _Cnd_storage;

_Cnd_t _Mycnd() noexcept { // get pointer to _Cnd_internal_imp_t inside _Cnd_storage
return reinterpret_cast<_Cnd_t>(&_Cnd_storage);
Expand Down
22 changes: 17 additions & 5 deletions stl/inc/type_traits
Original file line number Diff line number Diff line change
Expand Up @@ -1055,15 +1055,24 @@ struct _Aligned<_Len, _Align, char, false> {
using type = typename _Aligned<_Len, _Align, _Next, _Fits>::type;
};

// TRANSITION, ABI: Internal non-deprecated version to avoid ABI changes due to deprecation
template <size_t _Len, size_t _Align = alignof(max_align_t)>
struct aligned_storage { // define type with size _Len and alignment _Align
struct _Aligned_storage { // define type with size _Len and alignment _Align
using _Next = char;
static constexpr bool _Fits = _Align <= alignof(_Next);
using type = typename _Aligned<_Len, _Align, _Next, _Fits>::type;
};

template <size_t _Len, size_t _Align = alignof(max_align_t)>
using aligned_storage_t = typename aligned_storage<_Len, _Align>::type;
using _Aligned_storage_t = typename _Aligned_storage<_Len, _Align>::type;

template <size_t _Len, size_t _Align = alignof(max_align_t)>
struct _CXX23_DEPRECATE_ALIGNED_STORAGE aligned_storage { // define type with size _Len and alignment _Align
using type = _Aligned_storage_t<_Len, _Align>;
};

template <size_t _Len, size_t _Align = alignof(max_align_t)>
using aligned_storage_t _CXX23_DEPRECATE_ALIGNED_STORAGE = _Aligned_storage_t<_Len, _Align>;

template <size_t... _Vals>
struct _Maximum;
Expand All @@ -1080,15 +1089,18 @@ struct _Maximum<_First, _Second, _Rest...> : _Maximum<(_First < _Second ? _Secon
};

template <size_t _Len, class... _Types>
struct aligned_union { // define type with size at least _Len, for storing anything in _Types
struct _CXX23_DEPRECATE_ALIGNED_UNION aligned_union {
// define type with size at least _Len, for storing anything in _Types
static constexpr size_t _Max_len = _Maximum<_Len, sizeof(_Types)...>::value; // NOT sizeof...(_Types)
static constexpr size_t alignment_value = _Maximum<alignof(_Types)...>::value;

using type = aligned_storage_t<_Max_len, alignment_value>;
using type = _Aligned_storage_t<_Max_len, alignment_value>;
};

_STL_DISABLE_DEPRECATED_WARNING
template <size_t _Len, class... _Types>
using aligned_union_t = typename aligned_union<_Len, _Types...>::type;
using aligned_union_t _CXX23_DEPRECATE_ALIGNED_UNION = typename aligned_union<_Len, _Types...>::type;
_STL_RESTORE_DEPRECATED_WARNING

template <class _Ty, bool = is_enum_v<_Ty>>
struct _Underlying_type {
Expand Down
3 changes: 2 additions & 1 deletion stl/inc/xnode_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class _Node_handle : public _Base<_Node_handle<_Node, _Alloc, _Base, _Types...>,
using _Nodeptr = typename _Alnode_traits::pointer;

_Nodeptr _Ptr{};
aligned_union_t<0, _Alloc> _Alloc_storage; // Invariant: contains a live _Alloc iff _Ptr != nullptr
_Aligned_storage_t<sizeof(_Alloc), alignof(_Alloc)>
_Alloc_storage; // Invariant: contains a live _Alloc iff _Ptr != nullptr

void _Clear() noexcept { // destroy any contained node and return to the empty state
if (_Ptr != nullptr) {
Expand Down
27 changes: 26 additions & 1 deletion stl/inc/yvals_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@
// P1132R7 out_ptr(), inout_ptr()
// P1147R1 Printing volatile Pointers
// P1272R4 byteswap()
// P1413R3 Deprecate aligned_storage And aligned_union
// P1425R4 Iterator Pair Constructors For stack And queue
// P1659R3 ranges::starts_with, ranges::ends_with
// P1679R3 contains() For basic_string/basic_string_view
Expand Down Expand Up @@ -1144,7 +1145,31 @@
#define _CXX20_DEPRECATE_IS_ALWAYS_EQUAL
#endif // ^^^ warning disabled ^^^

// next warning number: STL4034
#if _HAS_CXX23 && !defined(_SILENCE_CXX23_ALIGNED_STORAGE_DEPRECATION_WARNING) \
&& !defined(_SILENCE_ALL_CXX23_DEPRECATION_WARNINGS)
#define _CXX23_DEPRECATE_ALIGNED_STORAGE \
[[deprecated("warning STL4034: " \
"std::aligned_storage and std::aligned_storage_t are deprecated in C++23. " \
"Prefer alignas(T) std::byte t_buff[sizeof(T)]. " \
"You can define _SILENCE_CXX23_ALIGNED_STORAGE_DEPRECATION_WARNING " \
"or _SILENCE_ALL_CXX23_DEPRECATION_WARNINGS to acknowledge that you have received this warning.")]]
#else // ^^^ warning enabled / warning disabled vvv
#define _CXX23_DEPRECATE_ALIGNED_STORAGE
#endif // ^^^ warning disabled ^^^

#if _HAS_CXX23 && !defined(_SILENCE_CXX23_ALIGNED_UNION_DEPRECATION_WARNING) \
&& !defined(_SILENCE_ALL_CXX23_DEPRECATION_WARNINGS)
#define _CXX23_DEPRECATE_ALIGNED_UNION \
[[deprecated("warning STL4035: " \
"std::aligned_union and std::aligned_union_t are deprecated in C++23. " \
"Prefer alignas(Ts...) std::byte t_buff[std::max({sizeof(Ts)...})]. " \
"You can define _SILENCE_CXX23_ALIGNED_UNION_DEPRECATION_WARNING " \
"or _SILENCE_ALL_CXX23_DEPRECATION_WARNINGS to acknowledge that you have received this warning.")]]
#else // ^^^ warning enabled / warning disabled vvv
#define _CXX23_DEPRECATE_ALIGNED_UNION
#endif // ^^^ warning disabled ^^^

// next warning number: STL4036

// P0619R4 Removing C++17-Deprecated Features
#ifndef _HAS_FEATURES_REMOVED_IN_CXX20
Expand Down
5 changes: 2 additions & 3 deletions stl/src/cond.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
#include "primitives.hpp"

struct _Cnd_internal_imp_t { // condition variable implementation for ConcRT
std::aligned_storage_t<Concurrency::details::stl_condition_variable_max_size,
Concurrency::details::stl_condition_variable_max_alignment>
cv;
typename std::_Aligned_storage<Concurrency::details::stl_condition_variable_max_size,
Concurrency::details::stl_condition_variable_max_alignment>::type cv;

[[nodiscard]] Concurrency::details::stl_condition_variable_interface* _get_cv() noexcept {
// get pointer to implementation
Expand Down
5 changes: 2 additions & 3 deletions stl/src/mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ extern "C" _CRTIMP2 void __cdecl __set_stl_sync_api_mode(__stl_sync_api_modes_en

struct _Mtx_internal_imp_t { // ConcRT mutex
int type;
std::aligned_storage_t<Concurrency::details::stl_critical_section_max_size,
Concurrency::details::stl_critical_section_max_alignment>
cs;
typename std::_Aligned_storage<Concurrency::details::stl_critical_section_max_size,
Concurrency::details::stl_critical_section_max_alignment>::type cs;
long thread_id;
int count;
Concurrency::details::stl_critical_section_interface* _get_cs() { // get pointer to implementation
Expand Down
10 changes: 5 additions & 5 deletions tests/libcxx/expected_results.txt
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,11 @@ std/containers/unord/unord.set/max_size.pass.cpp FAIL
std/utilities/tuple/tuple.tuple/tuple.apply/apply_large_arity.pass.cpp SKIPPED
std/utilities/tuple/tuple.tuple/tuple.cnstr/recursion_depth.pass.cpp SKIPPED

# Deprecation is a mess. We disable all deprecations in msvc_stdlib_force_include.h to allow libc++ tests for
# deprecated features to pass, which breaks when libc++ deprecates the feature and adds two tests that (1) pass
# with deprecation suppressed, and (2) fail without deprecation suppression. We should instead translate libc++
# un-deprecation macros to STL un-deprecation macros in the force-include header, and just skip tests when we
# deprecate before they do.
# Deprecation is a mess. We disable all deprecations in llvm-project/libcxx/test/support/msvc_stdlib_force_include.h
# (external to this repo) to allow libc++ tests for deprecated features to pass, which breaks when libc++ deprecates
# the feature and adds two tests that (1) pass with deprecation suppressed, and (2) fail without deprecation suppression.
# We should instead translate libc++ un-deprecation macros to STL un-deprecation macros in the force-include header,
# and just skip tests when we deprecate before they do.
std/utilities/meta/meta.unary/meta.unary.prop/is_literal_type.deprecated.fail.cpp FAIL


Expand Down
10 changes: 5 additions & 5 deletions tests/libcxx/skipped_tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,11 @@ containers\unord\unord.set\max_size.pass.cpp
utilities\tuple\tuple.tuple\tuple.apply\apply_large_arity.pass.cpp
utilities\tuple\tuple.tuple\tuple.cnstr\recursion_depth.pass.cpp

# Deprecation is a mess. We disable all deprecations in msvc_stdlib_force_include.h to allow libc++ tests for
# deprecated features to pass, which breaks when libc++ deprecates the feature and adds two tests that (1) pass
# with deprecation suppressed, and (2) fail without deprecation suppression. We should instead translate libc++
# un-deprecation macros to STL un-deprecation macros in the force-include header, and just skip tests when we
# deprecate before they do.
# Deprecation is a mess. We disable all deprecations in llvm-project/libcxx/test/support/msvc_stdlib_force_include.h
# (external to this repo) to allow libc++ tests for deprecated features to pass, which breaks when libc++ deprecates
# the feature and adds two tests that (1) pass with deprecation suppressed, and (2) fail without deprecation suppression.
# We should instead translate libc++ un-deprecation macros to STL un-deprecation macros in the force-include header,
# and just skip tests when we deprecate before they do.
utilities\meta\meta.unary\meta.unary.prop\is_literal_type.deprecated.fail.cpp


Expand Down
3 changes: 2 additions & 1 deletion tests/libcxx/usual_matrix.lst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

RUNALL_INCLUDE ..\universal_prefix.lst
RUNALL_CROSSLIST
PM_CL="/EHsc /MTd /std:c++latest /permissive- /FImsvc_stdlib_force_include.h /wd4643 /D_STL_CALL_ABORT_INSTEAD_OF_INVALID_PARAMETER"
# TRANSITION, LLVM-53957: _SILENCE_ALL_CXX23_DEPRECATION_WARNINGS belongs to llvm-project/libcxx/test/support/msvc_stdlib_force_include.h
PM_CL="/EHsc /MTd /std:c++latest /permissive- /FImsvc_stdlib_force_include.h /wd4643 /D_STL_CALL_ABORT_INSTEAD_OF_INVALID_PARAMETER /D_SILENCE_ALL_CXX23_DEPRECATION_WARNINGS"
RUNALL_CROSSLIST
PM_CL="/analyze:autolog- /Zc:preprocessor"
PM_COMPILER="clang-cl" PM_CL="-fno-ms-compatibility -fno-delayed-template-parsing"
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#define _SILENCE_CXX23_ALIGNED_UNION_DEPRECATION_WARNING
#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING

#include <array>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#define _HAS_DEPRECATED_RAW_STORAGE_ITERATOR 1
#define _SILENCE_CXX17_RAW_STORAGE_ITERATOR_DEPRECATION_WARNING
#define _SILENCE_CXX23_ALIGNED_UNION_DEPRECATION_WARNING
#define _SILENCE_EXPERIMENTAL_ERASE_DEPRECATION_WARNING

#include <algorithm>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#define _SILENCE_CXX23_ALIGNED_UNION_DEPRECATION_WARNING

#include <assert.h>
#include <crtdbg.h>
#include <functional>
Expand Down
1 change: 1 addition & 0 deletions tests/std/tests/P0035R4_over_aligned_allocation/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define _ENABLE_EXTENDED_ALIGNED_STORAGE
#define _HAS_DEPRECATED_TEMPORARY_BUFFER 1
#define _SILENCE_CXX17_TEMPORARY_BUFFER_DEPRECATION_WARNING
#define _SILENCE_CXX23_ALIGNED_STORAGE_DEPRECATION_WARNING

#include <array>
#include <assert.h>
Expand Down
1 change: 1 addition & 0 deletions tests/std/tests/P0088R3_variant/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#define _HAS_DEPRECATED_RESULT_OF 1
#define _SILENCE_CXX17_RESULT_OF_DEPRECATION_WARNING
#define _SILENCE_CXX20_VOLATILE_DEPRECATION_WARNING
#define _SILENCE_CXX23_ALIGNED_UNION_DEPRECATION_WARNING
#define _LIBCXX_IN_DEVCRT
#include <msvc_stdlib_force_include.h> // Must precede any other libc++ headers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#define _SILENCE_CXX17_POLYMORPHIC_ALLOCATOR_DESTROY_DEPRECATION_WARNING
#define _SILENCE_CXX23_ALIGNED_UNION_DEPRECATION_WARNING

#include <algorithm>
#include <cmath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define _SILENCE_CXX17_IS_LITERAL_TYPE_DEPRECATION_WARNING
#define _SILENCE_CXX17_RESULT_OF_DEPRECATION_WARNING
#define _SILENCE_CXX20_IS_POD_DEPRECATION_WARNING
#define _SILENCE_CXX23_ALIGNED_UNION_DEPRECATION_WARNING
#define _USE_NAMED_IDL_NAMESPACE 1

#include <array>
Expand Down
3 changes: 3 additions & 0 deletions tests/tr1/tests/type_traits1/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
// test <type_traits> header, part 1
#define TEST_NAME "<type_traits>, part 1"

#define _SILENCE_CXX23_ALIGNED_STORAGE_DEPRECATION_WARNING
#define _SILENCE_CXX23_ALIGNED_UNION_DEPRECATION_WARNING

#include "tdefs.h"
#include "typetr.h"
#include <type_traits>
Expand Down
3 changes: 3 additions & 0 deletions tests/tr1/tests/type_traits5/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#define TEST_NAME "<type_traits>, part 5"

#define _DISABLE_EXTENDED_ALIGNED_STORAGE 1
#define _SILENCE_CXX23_ALIGNED_STORAGE_DEPRECATION_WARNING
#define _SILENCE_CXX23_ALIGNED_UNION_DEPRECATION_WARNING

#include "tdefs.h"
#include "typetr.h"
#include <limits.h>
Expand Down

0 comments on commit cbd964d

Please sign in to comment.