Skip to content
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

Add extra assertions to tl:optional #3169

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 79 additions & 7 deletions gcc/rust/util/optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -1249,19 +1249,56 @@ class optional : private detail::optional_move_assign_base<T>,

/// 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);
}

Expand Down Expand Up @@ -1988,14 +2025,49 @@ template <class T> class optional<T &> {
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; }

Expand Down
Loading