[libcxx-commits] [libcxx] [libc++][NFC] Simplify some `optional.observe` tests (PR #175682)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jan 19 13:58:57 PST 2026


================
@@ -9,74 +9,117 @@
 // UNSUPPORTED: c++03, c++11, c++14
 // <optional>
 
-// constexpr T* optional<T>::operator->();
+// constexpr T* optional<T>::operator->() noexcept;
+// constexpr const T* optional<T>::operator->() const noexcept;
+// constexpr T* optional<T&>::operator->() const noexcept;
 
 #include <cassert>
 #include <memory>
 #include <optional>
+#include <utility>
 
 #include "test_macros.h"
 
-using std::optional;
-
 struct X {
-  int test() noexcept { return 3; }
-  int test() const noexcept { return 3; }
+  constexpr int test() noexcept { return 3; }
+  constexpr int test() const noexcept { return 4; }
 };
 
-struct Y
-{
-    constexpr int test() {return 3;}
-};
+template <typename T>
+constexpr void test_contract() {
+  std::optional<T> opt;
+  ASSERT_SAME_TYPE(decltype(opt.operator->()), T*);
+  ASSERT_SAME_TYPE(decltype(std::as_const(opt).operator->()), const T*);
 
-constexpr int
-test()
-{
-    optional<Y> opt{Y{}};
-    return opt->test();
+  ASSERT_NOEXCEPT(opt.operator->());
+  ASSERT_NOEXCEPT(std::as_const(opt).operator->());
 }
 
-int main(int, char**)
-{
-    {
-        std::optional<X> opt; ((void)opt);
-        ASSERT_SAME_TYPE(decltype(opt.operator->()), X*);
-        ASSERT_NOEXCEPT(opt.operator->());
-    }
-    {
-        optional<X> opt(X{});
-        assert(opt->test() == 3);
-    }
 #if TEST_STD_VER >= 26
-    {
-      X x{};
-      std::optional<X&> opt(x);
-      ASSERT_SAME_TYPE(decltype(opt.operator->()), X*);
-      ASSERT_NOEXCEPT(opt.operator->());
-      assert(opt.operator->() == std::addressof(x));
-    }
-    {
-      X x{};
-      std::optional<const X&> opt(x);
-      ASSERT_SAME_TYPE(decltype(opt.operator->()), const X*);
-      ASSERT_NOEXCEPT(opt.operator->());
-      assert(opt.operator->() == std::addressof(x));
-    }
-    {
-      X x{};
-      optional<X&> opt{x};
-      assert(opt->test() == 3);
-      assert(opt.operator->() == std::addressof(x));
-    }
-    {
-      X x{};
-      optional<const X&> opt{x};
-      assert(opt->test() == 3);
-    }
+template <typename T>
+constexpr void test_ref_contract() {
+  std::optional<T&> opt;
+
+  ASSERT_SAME_TYPE(decltype(opt.operator->()), T*);
+  ASSERT_SAME_TYPE(decltype(std::as_const(opt).operator->()), T*);
+
+  ASSERT_NOEXCEPT(opt.operator->());
+  ASSERT_NOEXCEPT(std::as_const(opt).operator->());
+}
+
+constexpr void test_ref() {}
----------------
ldionne wrote:

```suggestion
```

https://github.com/llvm/llvm-project/pull/175682


More information about the libcxx-commits mailing list