0

[base] Fail CHECK when dereferencing an empty optional

This change promotes the DCHECKs in base::Optional::value() to CHECKs
in order to match std::optional more closely. Furthermore, it also
CHECKs when dereferencing a base::Optional via operator* or operator->.

Bug: 817982
Change-Id: Ib2bfdd3a863e8bade21fa1b6b1c10c7c4d2ca135
Reviewed-on: https://chromium-review.googlesource.com/997335
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#550197}
This commit is contained in:
jdoerrie
2018-04-12 15:01:30 +00:00
committed by Commit Bot
parent 723fb47254
commit c85bfab56b

@ -574,39 +574,57 @@ class OPTIONAL_DECLSPEC_EMPTY_BASES Optional
return *this;
}
constexpr const T* operator->() const { return &value(); }
constexpr const T* operator->() const {
CHECK(storage_.is_populated_);
return &storage_.value_;
}
constexpr T* operator->() { return &value(); }
constexpr T* operator->() {
CHECK(storage_.is_populated_);
return &storage_.value_;
}
constexpr const T& operator*() const& { return value(); }
constexpr const T& operator*() const & {
CHECK(storage_.is_populated_);
return storage_.value_;
}
constexpr T& operator*() & { return value(); }
constexpr T& operator*() & {
CHECK(storage_.is_populated_);
return storage_.value_;
}
constexpr const T&& operator*() const&& { return std::move(value()); }
constexpr const T&& operator*() const && {
CHECK(storage_.is_populated_);
return std::move(storage_.value_);
}
constexpr T&& operator*() && { return std::move(value()); }
constexpr T&& operator*() && {
CHECK(storage_.is_populated_);
return std::move(storage_.value_);
}
constexpr explicit operator bool() const { return storage_.is_populated_; }
constexpr bool has_value() const { return storage_.is_populated_; }
constexpr T& value() & {
DCHECK(storage_.is_populated_);
CHECK(storage_.is_populated_);
return storage_.value_;
}
constexpr const T& value() const & {
DCHECK(storage_.is_populated_);
CHECK(storage_.is_populated_);
return storage_.value_;
}
constexpr T&& value() && {
DCHECK(storage_.is_populated_);
CHECK(storage_.is_populated_);
return std::move(storage_.value_);
}
constexpr const T&& value() const && {
DCHECK(storage_.is_populated_);
CHECK(storage_.is_populated_);
return std::move(storage_.value_);
}