From 4ae564735564d8afcac260915d77f14e78da7904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20S=C3=A1nchez?= Date: Mon, 25 Aug 2025 18:17:18 +0000 Subject: [PATCH] Fix direct index aliased assignment. --- Eigen/src/Core/IndexedView.h | 6 ++++++ test/indexed_view.cpp | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/Eigen/src/Core/IndexedView.h b/Eigen/src/Core/IndexedView.h index 358239ca8..10562c194 100644 --- a/Eigen/src/Core/IndexedView.h +++ b/Eigen/src/Core/IndexedView.h @@ -308,6 +308,12 @@ struct unary_evaluator, IndexBased> const XprType& m_xpr; }; +// Catch assignments to an IndexedView. +template +struct evaluator_assume_aliasing> { + static const bool value = true; +}; + } // end namespace internal } // end namespace Eigen diff --git a/test/indexed_view.cpp b/test/indexed_view.cpp index 064cc4ac7..9f0df851d 100644 --- a/test/indexed_view.cpp +++ b/test/indexed_view.cpp @@ -840,11 +840,21 @@ void check_tutorial_examples() { } } +void check_aliasing() { + Eigen::Vector z = {0.0f, 1.1f, 2.2f, 3.3f, 4.4f}; + std::vector left_indices = {0, 1, 3, 4}; + std::vector right_indices = {1, 3, 4, 0}; + z(left_indices) = z(right_indices); + Eigen::Vector expected = {1.1f, 3.3f, 2.2f, 4.4f, 0.0f}; + VERIFY_IS_EQUAL(z, expected); +} + EIGEN_DECLARE_TEST(indexed_view) { for (int i = 0; i < g_repeat; i++) { CALL_SUBTEST_1(check_indexed_view()); } CALL_SUBTEST_1(check_tutorial_examples()); + CALL_SUBTEST_1(check_aliasing()); // static checks of some internals: STATIC_CHECK((internal::is_valid_index_type::value));