[libcxx-commits] [libcxx] [libc++] Correct mask_array assignment (PR #219490)

Gergely Futo via libcxx-commits libcxx-commits at lists.llvm.org
Wed Sep 2 03:58:27 PDT 2026


https://github.com/futog updated https://github.com/llvm/llvm-project/pull/219490

>From 716c1f8e3e897e597cd899bbba47407a2a45ef73 Mon Sep 17 00:00:00 2001
From: Gergely Futo <gergely.futo at hightec-rt.com>
Date: Fri, 28 Aug 2026 16:29:07 +0200
Subject: [PATCH] [libc++] Correct mask_array assignment

The index array from `__ma` should be used for indexing, instead of the
index array from `this`.
---
 libcxx/include/__cxx03/valarray                           | 2 +-
 libcxx/include/valarray                                   | 2 +-
 .../mask.array.assign/mask_array.pass.cpp                 | 8 ++++++++
 3 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/__cxx03/valarray b/libcxx/include/__cxx03/valarray
index 869bc306abb3a..7103f09a65ba9 100644
--- a/libcxx/include/__cxx03/valarray
+++ b/libcxx/include/__cxx03/valarray
@@ -1543,7 +1543,7 @@ template <class _Tp>
 inline const mask_array<_Tp>& mask_array<_Tp>::operator=(const mask_array& __ma) const {
   size_t __n = __1d_.size();
   for (size_t __i = 0; __i < __n; ++__i)
-    __vp_[__1d_[__i]] = __ma.__vp_[__1d_[__i]];
+    __vp_[__1d_[__i]] = __ma.__vp_[__ma.__1d_[__i]];
   return *this;
 }
 
diff --git a/libcxx/include/valarray b/libcxx/include/valarray
index d0997ebf2caae..0d80b997c1295 100644
--- a/libcxx/include/valarray
+++ b/libcxx/include/valarray
@@ -1614,7 +1614,7 @@ template <class _Tp>
 inline const mask_array<_Tp>& mask_array<_Tp>::operator=(const mask_array& __ma) const {
   size_t __n = __1d_.size();
   for (size_t __i = 0; __i < __n; ++__i)
-    __vp_[__1d_[__i]] = __ma.__vp_[__1d_[__i]];
+    __vp_[__1d_[__i]] = __ma.__vp_[__ma.__1d_[__i]];
   return *this;
 }
 
diff --git a/libcxx/test/std/numerics/numarray/template.mask.array/mask.array.assign/mask_array.pass.cpp b/libcxx/test/std/numerics/numarray/template.mask.array/mask.array.assign/mask_array.pass.cpp
index 500341aceed37..a42618f2fe370 100644
--- a/libcxx/test/std/numerics/numarray/template.mask.array/mask.array.assign/mask_array.pass.cpp
+++ b/libcxx/test/std/numerics/numarray/template.mask.array/mask.array.assign/mask_array.pass.cpp
@@ -51,6 +51,14 @@ int main(int, char**)
     assert(v1[14] == 14);
     assert(v1[15] == 15);
     }
+    {
+      std::valarray<int> v = {1, 2, 3, 4};
+      v[(v % 2) == 0]      = v[(v % 2) == 1];
+      assert(v[0] == 1);
+      assert(v[1] == 1);
+      assert(v[2] == 3);
+      assert(v[3] == 3);
+    }
     // Test return value of assignment
     {
     int a1[] = {0, 1, 2};



More information about the libcxx-commits mailing list