[libcxx-commits] [libcxx] Fixed Reference copy and move assignment operators (PR #125723)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Feb 20 10:52:01 PST 2025


https://github.com/2LoS updated https://github.com/llvm/llvm-project/pull/125723

>From 92860b2b24832accdb5e1b3b1aeff68a1c54414c Mon Sep 17 00:00:00 2001
From: LoS <aurumpuro at gmail.com>
Date: Thu, 20 Feb 2025 19:58:23 +0100
Subject: [PATCH] [libc++] [test] Further improve proxy Reference class in the
 test against proxy iterator's lifetime bugs.

---
 ...nst_proxy_iterators_lifetime_bugs.pass.cpp | 47 +++++--------------
 1 file changed, 11 insertions(+), 36 deletions(-)

diff --git a/libcxx/test/std/algorithms/robust_against_proxy_iterators_lifetime_bugs.pass.cpp b/libcxx/test/std/algorithms/robust_against_proxy_iterators_lifetime_bugs.pass.cpp
index c89d6f5a229e8..e4b0ebef68c89 100644
--- a/libcxx/test/std/algorithms/robust_against_proxy_iterators_lifetime_bugs.pass.cpp
+++ b/libcxx/test/std/algorithms/robust_against_proxy_iterators_lifetime_bugs.pass.cpp
@@ -143,24 +143,11 @@ class LifetimeIterator {
       lifetime_cache.insert(this);
     }
 
-    Reference& operator=(const Reference& rhs) {
+    Reference operator=(const Reference& rhs) const {
       assert(lifetime_cache.contains(this) && lifetime_cache.contains(&rhs));
+      assert(!moved_from_);
       assert(!rhs.moved_from_);
-
-      *v_ = *rhs.v_;
-      moved_from_ = false;
-
-      return *this;
-    }
-
-    Reference& operator=(Reference&& rhs) noexcept {
-      assert(lifetime_cache.contains(this) && lifetime_cache.contains(&rhs));
-
-      assert(!rhs.moved_from_);
-      rhs.moved_from_ = true;
-
       *v_ = *rhs.v_;
-      moved_from_ = false;
 
       return *this;
     }
@@ -172,12 +159,10 @@ class LifetimeIterator {
       return *v_;
     }
 
-    Reference& operator=(Value v) {
+    Reference operator=(Value v) const {
       assert(lifetime_cache.contains(this));
       assert(!moved_from_);
-
       *v_ = v;
-      moved_from_ = false;
 
       return *this;
     }
@@ -366,26 +351,23 @@ class ConstexprIterator {
     constexpr Reference(int& v) : v_(&v) { }
 
     constexpr Reference(const Reference& rhs) = default;
-    constexpr Reference& operator=(const Reference& rhs) {
+    constexpr Reference operator=(const Reference& rhs) const {
       assert(!rhs.moved_from_);
-      v_ = rhs.v_;
-      moved_from_ = false;
+      assert(!moved_from_);
+      *v_ = *rhs.v_;
 
       return *this;
     }
+    constexpr Reference operator=(int v) const {
+      assert(!moved_from_);
+      *v_ = v;
 
-    constexpr Reference(Reference&& rhs) noexcept : v_(rhs.v_) {
-      assert(!rhs.moved_from_);
-      rhs.moved_from_ = true;
+      return *this;
     }
 
-    constexpr Reference& operator=(Reference&& rhs) noexcept {
+    constexpr Reference(Reference&& rhs) noexcept : v_(rhs.v_) {
       assert(!rhs.moved_from_);
       rhs.moved_from_ = true;
-      moved_from_ = false;
-
-      v_ = rhs.v_;
-      return *this;
     }
 
     constexpr operator int() const {
@@ -393,13 +375,6 @@ class ConstexprIterator {
       return *v_;
     }
 
-    constexpr Reference& operator=(int v) {
-      *v_ = v;
-      moved_from_ = false;
-
-      return *this;
-    }
-
     friend constexpr bool operator<(const Reference& lhs, const Reference& rhs) {
       assert(!lhs.moved_from_ && !rhs.moved_from_);
       return *lhs.v_ < *rhs.v_;



More information about the libcxx-commits mailing list