-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc++] P2641R4: Checking if a union
alternative is active (std::is_within_lifetime
)
#107450
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
Open
MitalAshok
wants to merge
2
commits into
llvm:main
Choose a base branch
from
MitalAshok:libcxx-is_within_lifetime
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef _LIBCPP___TYPE_TRAITS_IS_WITHIN_LIFETIME_H | ||
#define _LIBCPP___TYPE_TRAITS_IS_WITHIN_LIFETIME_H | ||
|
||
#include <__config> | ||
#include <__type_traits/is_function.h> | ||
|
||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
# pragma GCC system_header | ||
#endif | ||
|
||
_LIBCPP_BEGIN_NAMESPACE_STD | ||
|
||
#if _LIBCPP_STD_VER >= 26 && __has_builtin(__builtin_is_within_lifetime) | ||
template <class _Tp> | ||
_LIBCPP_HIDE_FROM_ABI consteval bool is_within_lifetime(const _Tp* __p) noexcept { | ||
if constexpr (is_function_v<_Tp>) { | ||
// Avoid multiple diagnostics | ||
static_assert(!is_function_v<_Tp>, "std::is_within_lifetime<T> cannot explicitly specify T as a function type"); | ||
return false; | ||
} else { | ||
return __builtin_is_within_lifetime(__p); | ||
} | ||
} | ||
#endif | ||
|
||
_LIBCPP_END_NAMESPACE_STD | ||
|
||
#endif // _LIBCPP___TYPE_TRAITS_IS_WITHIN_LIFETIME_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2008,6 +2008,7 @@ module std_private_type_traits_is_void [system | |
export std_private_type_traits_integral_constant | ||
} | ||
module std_private_type_traits_is_volatile [system] { header "__type_traits/is_volatile.h" } | ||
module std_private_type_traits_is_within_lifetime [system] { header "__type_traits/is_within_lifetime.h" } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When you rebase onto |
||
module std_private_type_traits_lazy [system] { header "__type_traits/lazy.h" } | ||
module std_private_type_traits_make_32_64_or_128_bit [system] { header "__type_traits/make_32_64_or_128_bit.h" } | ||
module std_private_type_traits_make_const_lvalue_ref [system] { header "__type_traits/make_const_lvalue_ref.h" } | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
libcxx/test/libcxx/utilities/meta/is_within_lifetime.verify.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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, c++11, c++14, c++17, c++20, c++23 | ||
// UNSUPPORTED: clang-18, clang-19, gcc-14, apple-clang-16 | ||
|
||
// <type_traits> | ||
|
||
// LWG4138 <https://cplusplus.github.io/LWG/issue4138> | ||
// std::is_within_lifetime shouldn't work when a function type is | ||
// explicitly specified, even if it isn't evaluated | ||
|
||
#include <type_traits> | ||
|
||
template <class T> | ||
consteval bool checked_is_within_lifetime(T* p) { | ||
return p ? std::is_within_lifetime<T>(p) : false; | ||
} | ||
static_assert(!checked_is_within_lifetime<int>(nullptr)); | ||
static_assert(!checked_is_within_lifetime<void()>(nullptr)); | ||
// expected-error@*:* {{static assertion failed due to requirement '!is_function_v<void ()>': std::is_within_lifetime<T> cannot explicitly specify T as a function type}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
libcxx/test/std/utilities/meta/meta.const.eval/is_within_lifetime.compile.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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, c++11, c++14, c++17, c++20, c++23 | ||
// UNSUPPORTED: clang-18, clang-19, gcc-14, apple-clang-16 | ||
|
||
// <type_traits> | ||
|
||
// template <class T> | ||
// consteval bool is_within_lifetime(const T*) noexcept; // C++26 | ||
|
||
#include <type_traits> | ||
#include <cassert> | ||
|
||
#include "test_macros.h" | ||
|
||
ASSERT_SAME_TYPE(decltype(std::is_within_lifetime(std::declval<int*>())), bool); | ||
ASSERT_SAME_TYPE(decltype(std::is_within_lifetime(std::declval<const int*>())), bool); | ||
ASSERT_SAME_TYPE(decltype(std::is_within_lifetime(std::declval<void*>())), bool); | ||
ASSERT_SAME_TYPE(decltype(std::is_within_lifetime(std::declval<const void*>())), bool); | ||
|
||
ASSERT_NOEXCEPT(std::is_within_lifetime(std::declval<int*>())); | ||
ASSERT_NOEXCEPT(std::is_within_lifetime(std::declval<const int*>())); | ||
ASSERT_NOEXCEPT(std::is_within_lifetime(std::declval<void*>())); | ||
ASSERT_NOEXCEPT(std::is_within_lifetime(std::declval<const void*>())); | ||
|
||
template <class T> | ||
concept is_within_lifetime_exists = requires(T t) { std::is_within_lifetime(t); }; | ||
|
||
struct S {}; | ||
|
||
static_assert(is_within_lifetime_exists<int*>); | ||
static_assert(is_within_lifetime_exists<const int*>); | ||
static_assert(is_within_lifetime_exists<void*>); | ||
static_assert(is_within_lifetime_exists<const void*>); | ||
static_assert(!is_within_lifetime_exists<int>); // Not a pointer | ||
static_assert(!is_within_lifetime_exists<decltype(nullptr)>); // Not a pointer | ||
static_assert(!is_within_lifetime_exists<void() const>); // Not a pointer | ||
static_assert(!is_within_lifetime_exists<int S::*>); // Doesn't accept pointer-to-data-member | ||
static_assert(!is_within_lifetime_exists<void (S::*)()>); // Doesn't accept pointer-to-member-function | ||
static_assert(!is_within_lifetime_exists<void (*)()>); // Doesn't match `const T*` | ||
|
||
consteval bool f() { | ||
// Test that it works with global variables whose lifetime is in a | ||
// different constant expression | ||
{ | ||
static constexpr int i = 0; | ||
static_assert(std::is_within_lifetime(&i)); | ||
// (Even when cast to a different type) | ||
static_assert(std::is_within_lifetime(const_cast<int*>(&i))); | ||
static_assert(std::is_within_lifetime(static_cast<const void*>(&i))); | ||
static_assert(std::is_within_lifetime(static_cast<void*>(const_cast<int*>(&i)))); | ||
static_assert(std::is_within_lifetime<const int>(&i)); | ||
static_assert(std::is_within_lifetime<int>(const_cast<int*>(&i))); | ||
static_assert(std::is_within_lifetime<const void>(static_cast<const void*>(&i))); | ||
static_assert(std::is_within_lifetime<void>(static_cast<void*>(const_cast<int*>(&i)))); | ||
} | ||
|
||
{ | ||
static constexpr union { | ||
int member1; | ||
int member2; | ||
} u{.member2 = 1}; | ||
static_assert(!std::is_within_lifetime(&u.member1) && std::is_within_lifetime(&u.member2)); | ||
} | ||
|
||
// Test that it works for varibles inside the same constant expression | ||
{ | ||
int i = 0; | ||
assert(std::is_within_lifetime(&i)); | ||
// (Even when cast to a different type) | ||
assert(std::is_within_lifetime(const_cast<int*>(&i))); | ||
assert(std::is_within_lifetime(static_cast<const void*>(&i))); | ||
assert(std::is_within_lifetime(static_cast<void*>(const_cast<int*>(&i)))); | ||
assert(std::is_within_lifetime<const int>(&i)); | ||
assert(std::is_within_lifetime<int>(const_cast<int*>(&i))); | ||
assert(std::is_within_lifetime<const void>(static_cast<const void*>(&i))); | ||
assert(std::is_within_lifetime<void>(static_cast<void*>(const_cast<int*>(&i)))); | ||
} | ||
// Anonymous union | ||
{ | ||
union { | ||
int member1; | ||
int member2; | ||
}; | ||
assert(!std::is_within_lifetime(&member1) && !std::is_within_lifetime(&member2)); | ||
member1 = 1; | ||
assert(std::is_within_lifetime(&member1) && !std::is_within_lifetime(&member2)); | ||
member2 = 1; | ||
assert(!std::is_within_lifetime(&member1) && std::is_within_lifetime(&member2)); | ||
} | ||
// Variant members | ||
{ | ||
struct X { | ||
union { | ||
int member1; | ||
int member2; | ||
}; | ||
} x; | ||
assert(!std::is_within_lifetime(&x.member1) && !std::is_within_lifetime(&x.member2)); | ||
x.member1 = 1; | ||
assert(std::is_within_lifetime(&x.member1) && !std::is_within_lifetime(&x.member2)); | ||
x.member2 = 1; | ||
assert(!std::is_within_lifetime(&x.member1) && std::is_within_lifetime(&x.member2)); | ||
} | ||
// Unions | ||
{ | ||
union X { | ||
int member1; | ||
int member2; | ||
} x; | ||
assert(!std::is_within_lifetime(&x.member1) && !std::is_within_lifetime(&x.member2)); | ||
x.member1 = 1; | ||
assert(std::is_within_lifetime(&x.member1) && !std::is_within_lifetime(&x.member2)); | ||
x.member2 = 1; | ||
assert(!std::is_within_lifetime(&x.member1) && std::is_within_lifetime(&x.member2)); | ||
} | ||
{ | ||
S s; // uninitialised | ||
assert(std::is_within_lifetime(&s)); | ||
} | ||
|
||
return true; | ||
} | ||
static_assert(f()); | ||
|
||
// Check that it is a consteval (and consteval-propagating) function | ||
// (i.e., taking the address of below will fail because it will be an immediate function) | ||
template <typename T> | ||
constexpr void does_escalate(T p) { | ||
std::is_within_lifetime(p); | ||
} | ||
template <typename T, void (*)(T) = &does_escalate<T>> | ||
constexpr bool check_escalated(int) { | ||
return false; | ||
} | ||
template <typename T> | ||
constexpr bool check_escalated(long) { | ||
return true; | ||
} | ||
static_assert(check_escalated<int*>(0), ""); | ||
static_assert(check_escalated<void*>(0), ""); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is clever, but I think I'd rather use the naive way we consistently do this, and just have
static_assert
at the start of the function. If we think the duplicate diagnostics are bad, that's something we can (and should) fix in Clang (e.g. when it hits astatic_assert
it should probably stop).There have been past discussions about this and I'm not really ready to start working around the issue in the library, I don't think that's the right place to do this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the builtin already diagnoses this, why do we add the static_assert at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be possible for the builtin on other implementations not to produce a clear diagnostic, I guess. I'm mostly neutral on this FWIW.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that's a problem we can still add a static assert and file a bug against GCC though. FWIW I've filed a bug for
__builtin_launder
against GCC some time ago and they agreed it's a bug and fixed it fairly quickly.