aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorOwen Avery <powerboat9.gamer@gmail.com>2024-09-18 16:05:31 -0400
committerCohenArthur <arthur.cohen@embecosm.com>2024-09-24 08:06:05 +0000
commit1a9f70b2ab7ab593091847b200e5fd1e02f5a21b (patch)
treee28fbf6fce55b6167af37983cdd791def33d68cf /gcc
parentb282387d5a653a4000ddfe429eb2ab596c52bae8 (diff)
downloadgcc-1a9f70b2ab7ab593091847b200e5fd1e02f5a21b.zip
gcc-1a9f70b2ab7ab593091847b200e5fd1e02f5a21b.tar.gz
gcc-1a9f70b2ab7ab593091847b200e5fd1e02f5a21b.tar.bz2
Add extra assertions to tl::optional
gcc/rust/ChangeLog: * util/optional.h (optional): Add assertions to dereference operator overloads when C++14 is available. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/util/optional.h86
1 files changed, 79 insertions, 7 deletions
diff --git a/gcc/rust/util/optional.h b/gcc/rust/util/optional.h
index 4518b42..a8ef545 100644
--- a/gcc/rust/util/optional.h
+++ b/gcc/rust/util/optional.h
@@ -1249,19 +1249,56 @@ public:
/// Returns a pointer to the stored value
constexpr const T *operator->() const {
+ // constexpr function must only contain a return statement in C++11
+#ifdef TL_OPTIONAL_CXX14
+ // undefined behavior if we don't have a value
+ rust_assert(has_value ());
+#endif
+
return std::addressof(this->m_value);
}
TL_OPTIONAL_11_CONSTEXPR T *operator->() {
+ // constexpr function must only contain a return statement in C++11
+#ifdef TL_OPTIONAL_CXX14
+ // undefined behavior if we don't have a value
+ rust_assert(has_value ());
+#endif
+
return std::addressof(this->m_value);
}
/// Returns the stored value
- TL_OPTIONAL_11_CONSTEXPR T &operator*() & { return this->m_value; }
+ TL_OPTIONAL_11_CONSTEXPR T &operator*() &
+ {
+ // constexpr function must only contain a return statement in C++11
+#ifdef TL_OPTIONAL_CXX14
+ // undefined behavior if we don't have a value
+ rust_assert(has_value ());
+#endif
- constexpr const T &operator*() const & { return this->m_value; }
+ return this->m_value;
+ }
+
+ constexpr const T &operator*() const &
+ {
+ // constexpr function must only contain a return statement in C++11
+#ifdef TL_OPTIONAL_CXX14
+ // undefined behavior if we don't have a value
+ rust_assert(has_value ());
+#endif
+
+ return this->m_value;
+ }
+
+ TL_OPTIONAL_11_CONSTEXPR T &&operator*() &&
+ {
+ // constexpr function must only contain a return statement in C++11
+#ifdef TL_OPTIONAL_CXX14
+ // undefined behavior if we don't have a value
+ rust_assert(has_value ());
+#endif
- TL_OPTIONAL_11_CONSTEXPR T &&operator*() && {
return std::move(this->m_value);
}
@@ -1988,14 +2025,49 @@ public:
void swap(optional &rhs) noexcept { std::swap(m_value, rhs.m_value); }
/// Returns a pointer to the stored value
- constexpr const T *operator->() const noexcept { return m_value; }
+ constexpr const T *operator->() const noexcept
+ {
+ // constexpr function must only contain a return statement in C++11
+#ifdef TL_OPTIONAL_CXX14
+ // undefined behavior if we don't have a value
+ rust_assert(has_value ());
+#endif
+
+ return m_value;
+ }
- TL_OPTIONAL_11_CONSTEXPR T *operator->() noexcept { return m_value; }
+ TL_OPTIONAL_11_CONSTEXPR T *operator->() noexcept
+ {
+ // constexpr function must only contain a return statement in C++11
+#ifdef TL_OPTIONAL_CXX14
+ // undefined behavior if we don't have a value
+ rust_assert(has_value ());
+#endif
+
+ return m_value;
+ }
/// Returns the stored value
- TL_OPTIONAL_11_CONSTEXPR T &operator*() noexcept { return *m_value; }
+ TL_OPTIONAL_11_CONSTEXPR T &operator*() noexcept {
+ // constexpr function must only contain a return statement in C++11
+#ifdef TL_OPTIONAL_CXX14
+ // undefined behavior if we don't have a value
+ rust_assert(has_value ());
+#endif
+
+ return *m_value;
+ }
- constexpr const T &operator*() const noexcept { return *m_value; }
+ constexpr const T &operator*() const noexcept
+ {
+ // constexpr function must only contain a return statement in C++11
+#ifdef TL_OPTIONAL_CXX14
+ // undefined behavior if we don't have a value
+ rust_assert(has_value ());
+#endif
+
+ return *m_value;
+ }
constexpr bool has_value() const noexcept { return m_value != nullptr; }