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

Gergely Futo via libcxx-commits libcxx-commits at lists.llvm.org
Fri Aug 28 07:48:55 PDT 2026


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

>From 96028da865902c7b2e2e6ab0128e7c234f7d1c5e 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/valarray                                   | 2 +-
 .../mask.array.assign/mask_array.pass.cpp                 | 8 ++++++++
 2 files changed, 9 insertions(+), 1 deletion(-)

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..1fe617ff0fa6a 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