|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23 |
| 10 | +// UNSUPPORTED: clang-18, clang-19, gcc-14, apple-clang-16 |
| 11 | + |
| 12 | +// <type_traits> |
| 13 | + |
| 14 | +// template <class T> |
| 15 | +// consteval bool is_within_lifetime(const T*) noexcept; // C++26 |
| 16 | + |
| 17 | +#include <type_traits> |
| 18 | +#include <cassert> |
| 19 | + |
| 20 | +#include "test_macros.h" |
| 21 | + |
| 22 | +ASSERT_SAME_TYPE(decltype(std::is_within_lifetime(std::declval<int*>())), bool); |
| 23 | +ASSERT_SAME_TYPE(decltype(std::is_within_lifetime(std::declval<const int*>())), bool); |
| 24 | +ASSERT_SAME_TYPE(decltype(std::is_within_lifetime(std::declval<void*>())), bool); |
| 25 | +ASSERT_SAME_TYPE(decltype(std::is_within_lifetime(std::declval<const void*>())), bool); |
| 26 | + |
| 27 | +ASSERT_NOEXCEPT(std::is_within_lifetime(std::declval<int*>())); |
| 28 | +ASSERT_NOEXCEPT(std::is_within_lifetime(std::declval<const int*>())); |
| 29 | +ASSERT_NOEXCEPT(std::is_within_lifetime(std::declval<void*>())); |
| 30 | +ASSERT_NOEXCEPT(std::is_within_lifetime(std::declval<const void*>())); |
| 31 | + |
| 32 | +template <class T> |
| 33 | +concept is_within_lifetime_exists = requires(T t) { std::is_within_lifetime(t); }; |
| 34 | + |
| 35 | +struct S {}; |
| 36 | + |
| 37 | +static_assert(is_within_lifetime_exists<int*>); |
| 38 | +static_assert(is_within_lifetime_exists<const int*>); |
| 39 | +static_assert(is_within_lifetime_exists<void*>); |
| 40 | +static_assert(is_within_lifetime_exists<const void*>); |
| 41 | +static_assert(!is_within_lifetime_exists<int>); // Not a pointer |
| 42 | +static_assert(!is_within_lifetime_exists<decltype(nullptr)>); // Not a pointer |
| 43 | +static_assert(!is_within_lifetime_exists<void() const>); // Not a pointer |
| 44 | +static_assert(!is_within_lifetime_exists<int S::*>); // Doesn't accept pointer-to-data-member |
| 45 | +static_assert(!is_within_lifetime_exists<void (S::*)()>); // Doesn't accept pointer-to-member-function |
| 46 | +static_assert(!is_within_lifetime_exists<void (*)()>); // Doesn't match `const T*` |
| 47 | + |
| 48 | +consteval bool f() { |
| 49 | + // Test that it works with global variables whose lifetime is in a |
| 50 | + // different constant expression |
| 51 | + { |
| 52 | + static constexpr int i = 0; |
| 53 | + static_assert(std::is_within_lifetime(&i)); |
| 54 | + // (Even when cast to a different type) |
| 55 | + static_assert(std::is_within_lifetime(const_cast<int*>(&i))); |
| 56 | + static_assert(std::is_within_lifetime(static_cast<const void*>(&i))); |
| 57 | + static_assert(std::is_within_lifetime(static_cast<void*>(const_cast<int*>(&i)))); |
| 58 | + static_assert(std::is_within_lifetime<const int>(&i)); |
| 59 | + static_assert(std::is_within_lifetime<int>(const_cast<int*>(&i))); |
| 60 | + static_assert(std::is_within_lifetime<const void>(static_cast<const void*>(&i))); |
| 61 | + static_assert(std::is_within_lifetime<void>(static_cast<void*>(const_cast<int*>(&i)))); |
| 62 | + } |
| 63 | + |
| 64 | + { |
| 65 | + static constexpr union { |
| 66 | + int member1; |
| 67 | + int member2; |
| 68 | + } u{.member2 = 1}; |
| 69 | + static_assert(!std::is_within_lifetime(&u.member1) && std::is_within_lifetime(&u.member2)); |
| 70 | + } |
| 71 | + |
| 72 | + // Test that it works for varibles inside the same constant expression |
| 73 | + { |
| 74 | + int i = 0; |
| 75 | + assert(std::is_within_lifetime(&i)); |
| 76 | + // (Even when cast to a different type) |
| 77 | + assert(std::is_within_lifetime(const_cast<int*>(&i))); |
| 78 | + assert(std::is_within_lifetime(static_cast<const void*>(&i))); |
| 79 | + assert(std::is_within_lifetime(static_cast<void*>(const_cast<int*>(&i)))); |
| 80 | + assert(std::is_within_lifetime<const int>(&i)); |
| 81 | + assert(std::is_within_lifetime<int>(const_cast<int*>(&i))); |
| 82 | + assert(std::is_within_lifetime<const void>(static_cast<const void*>(&i))); |
| 83 | + assert(std::is_within_lifetime<void>(static_cast<void*>(const_cast<int*>(&i)))); |
| 84 | + } |
| 85 | + // Anonymous union |
| 86 | + { |
| 87 | + union { |
| 88 | + int member1; |
| 89 | + int member2; |
| 90 | + }; |
| 91 | + assert(!std::is_within_lifetime(&member1) && !std::is_within_lifetime(&member2)); |
| 92 | + member1 = 1; |
| 93 | + assert(std::is_within_lifetime(&member1) && !std::is_within_lifetime(&member2)); |
| 94 | + member2 = 1; |
| 95 | + assert(!std::is_within_lifetime(&member1) && std::is_within_lifetime(&member2)); |
| 96 | + } |
| 97 | + // Variant members |
| 98 | + { |
| 99 | + struct X { |
| 100 | + union { |
| 101 | + int member1; |
| 102 | + int member2; |
| 103 | + }; |
| 104 | + } x; |
| 105 | + assert(!std::is_within_lifetime(&x.member1) && !std::is_within_lifetime(&x.member2)); |
| 106 | + x.member1 = 1; |
| 107 | + assert(std::is_within_lifetime(&x.member1) && !std::is_within_lifetime(&x.member2)); |
| 108 | + x.member2 = 1; |
| 109 | + assert(!std::is_within_lifetime(&x.member1) && std::is_within_lifetime(&x.member2)); |
| 110 | + } |
| 111 | + // Unions |
| 112 | + { |
| 113 | + union X { |
| 114 | + int member1; |
| 115 | + int member2; |
| 116 | + } x; |
| 117 | + assert(!std::is_within_lifetime(&x.member1) && !std::is_within_lifetime(&x.member2)); |
| 118 | + x.member1 = 1; |
| 119 | + assert(std::is_within_lifetime(&x.member1) && !std::is_within_lifetime(&x.member2)); |
| 120 | + x.member2 = 1; |
| 121 | + assert(!std::is_within_lifetime(&x.member1) && std::is_within_lifetime(&x.member2)); |
| 122 | + } |
| 123 | + { |
| 124 | + S s; // uninitialised |
| 125 | + assert(std::is_within_lifetime(&s)); |
| 126 | + } |
| 127 | + |
| 128 | + return true; |
| 129 | +} |
| 130 | +static_assert(f()); |
| 131 | + |
| 132 | +// Check that it is a consteval (and consteval-propagating) function |
| 133 | +// (i.e., taking the address of below will fail because it will be an immediate function) |
| 134 | +template <typename T> |
| 135 | +constexpr void does_escalate(T p) { |
| 136 | + std::is_within_lifetime(p); |
| 137 | +} |
| 138 | +template <typename T, void (*)(T) = &does_escalate<T>> |
| 139 | +constexpr bool check_escalated(int) { |
| 140 | + return false; |
| 141 | +} |
| 142 | +template <typename T> |
| 143 | +constexpr bool check_escalated(long) { |
| 144 | + return true; |
| 145 | +} |
| 146 | +static_assert(check_escalated<int*>(0), ""); |
| 147 | +static_assert(check_escalated<void*>(0), ""); |
0 commit comments