|
7 | 7 | #include <stdint.h> |
8 | 8 |
|
9 | 9 | #include <concepts> |
10 | | -#include <optional> |
11 | 10 |
|
12 | | -#include "base/compiler_specific.h" |
13 | 11 | #include "testing/gtest/include/gtest/gtest.h" |
14 | 12 | #include "third_party/abseil-cpp/absl/functional/function_ref.h" |
15 | 13 |
|
@@ -70,62 +68,6 @@ TEST(FunctionRef, Method) { |
70 | 68 | [&s](FunctionRef<int(const S*)> ref) { EXPECT_EQ(25, ref(&s)); }(&S::Method); |
71 | 69 | } |
72 | 70 |
|
73 | | -// If we construct from another `FunctionRef`, that should work fine, even if |
74 | | -// the input is destroyed before we call the output. In other words, we should |
75 | | -// reference the underlying callable, not the `FunctionRef`. |
76 | | -// |
77 | | -// We construct in a `noinline` function to maximize the chance that ASAN |
78 | | -// notices the use-after-free if we get this wrong. |
79 | | -NOINLINE void ConstructFromLValue(std::optional<FunctionRef<int()>>& ref) { |
80 | | - const auto return_17 = [] { return 17; }; |
81 | | - FunctionRef<int()> other = return_17; |
82 | | - ref.emplace(other); |
83 | | -} |
84 | | -NOINLINE void ConstructFromConstLValue(std::optional<FunctionRef<int()>>& ref) { |
85 | | - const auto return_17 = [] { return 17; }; |
86 | | - const FunctionRef<int()> other = return_17; |
87 | | - ref.emplace(other); |
88 | | -} |
89 | | -NOINLINE void ConstructFromRValue(std::optional<FunctionRef<int()>>& ref) { |
90 | | - const auto return_17 = [] { return 17; }; |
91 | | - using Ref = FunctionRef<int()>; |
92 | | - ref.emplace(Ref(return_17)); |
93 | | -} |
94 | | -NOINLINE void ConstructFromConstRValue(std::optional<FunctionRef<int()>>& ref) { |
95 | | - const auto return_17 = [] { return 17; }; |
96 | | - using Ref = const FunctionRef<int()>; |
97 | | - ref.emplace(Ref(return_17)); |
98 | | -} |
99 | | -TEST(FunctionRef, ConstructionFromOtherFunctionRefObjects) { |
100 | | - using Ref = FunctionRef<int()>; |
101 | | - std::optional<Ref> ref; |
102 | | - |
103 | | - ConstructFromLValue(ref); |
104 | | - EXPECT_EQ(17, (*ref)()); |
105 | | - |
106 | | - ConstructFromConstLValue(ref); |
107 | | - EXPECT_EQ(17, (*ref)()); |
108 | | - |
109 | | - ConstructFromRValue(ref); |
110 | | - EXPECT_EQ(17, (*ref)()); |
111 | | - |
112 | | - ConstructFromConstRValue(ref); |
113 | | - EXPECT_EQ(17, (*ref)()); |
114 | | - |
115 | | - // It shouldn't be possible to construct from `FunctionRef` objects with |
116 | | - // differing signatures, even if they are compatible with `int()`. |
117 | | - static_assert(!std::constructible_from<Ref, FunctionRef<void()>>); |
118 | | - static_assert(!std::constructible_from<Ref, FunctionRef<int(int)>>); |
119 | | - static_assert(!std::constructible_from<Ref, FunctionRef<int64_t()>>); |
120 | | - |
121 | | - // Check again with various qualifiers. |
122 | | - static_assert(!std::constructible_from<Ref, const FunctionRef<void()>>); |
123 | | - static_assert(!std::constructible_from<Ref, FunctionRef<void()>&>); |
124 | | - static_assert(!std::constructible_from<Ref, FunctionRef<void()>&&>); |
125 | | - static_assert(!std::constructible_from<Ref, const FunctionRef<void()>&>); |
126 | | - static_assert(!std::constructible_from<Ref, const FunctionRef<void()>&&>); |
127 | | -} |
128 | | - |
129 | 71 | // `FunctionRef` allows functors with convertible return types to be adapted. |
130 | 72 | TEST(FunctionRef, ConvertibleReturnTypes) { |
131 | 73 | { |
@@ -200,21 +142,4 @@ TEST(FunctionRef, ConstructionFromInexactMatches) { |
200 | 142 | decltype(&functor)>); |
201 | 143 | } |
202 | 144 |
|
203 | | -TEST(FunctionRef, ConstructionFromAbslFunctionRef) { |
204 | | - // It shouldn't be possible to construct a `FunctionRef` from an |
205 | | - // `absl::FunctionRef`, whether the signatures are compatible or not. |
206 | | - using Ref = FunctionRef<int(int)>; |
207 | | - static_assert(!std::is_constructible_v<Ref, absl::FunctionRef<void()>>); |
208 | | - static_assert(!std::is_constructible_v<Ref, absl::FunctionRef<void(int)>>); |
209 | | - static_assert(!std::is_constructible_v<Ref, absl::FunctionRef<int(int)>>); |
210 | | - |
211 | | - // Check again with various qualifiers. |
212 | | - using AbslRef = absl::FunctionRef<int(int)>; |
213 | | - static_assert(!std::is_constructible_v<Ref, const AbslRef>); |
214 | | - static_assert(!std::is_constructible_v<Ref, AbslRef&>); |
215 | | - static_assert(!std::is_constructible_v<Ref, AbslRef&&>); |
216 | | - static_assert(!std::is_constructible_v<Ref, const AbslRef&>); |
217 | | - static_assert(!std::is_constructible_v<Ref, const AbslRef&&>); |
218 | | -} |
219 | | - |
220 | 145 | } // namespace base |
0 commit comments