Core bug fixes: expressions and views

libeigen/eigen!3344

Co-authored-by: Rasmus Munk Larsen <rmlarsen@gmail.com>
diff --git a/Eigen/src/Core/CwiseUnaryOp.h b/Eigen/src/Core/CwiseUnaryOp.h
index bc095c8..1d4c960 100644
--- a/Eigen/src/Core/CwiseUnaryOp.h
+++ b/Eigen/src/Core/CwiseUnaryOp.h
@@ -76,7 +76,7 @@
   }
 
   /** \returns the nested expression */
-  EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE internal::remove_all_t<XprTypeNested>& nestedExpression() {
+  EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE std::remove_reference_t<XprTypeNested>& nestedExpression() {
     return m_xpr;
   }
 
diff --git a/Eigen/src/Core/IndexedView.h b/Eigen/src/Core/IndexedView.h
index dab89b7..8225f9b 100644
--- a/Eigen/src/Core/IndexedView.h
+++ b/Eigen/src/Core/IndexedView.h
@@ -180,6 +180,17 @@
     return nestedExpression().coeffRef(m_rowIndices[rowId], m_colIndices[colId]);
   }
 
+  // The two-index overloads above hide the base's linear coeffRef(), so vector views need their own.
+  constexpr Scalar& coeffRef(Index index) {
+    EIGEN_STATIC_ASSERT(Base::IsVectorAtCompileTime, YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX)
+    return coeffRef(Base::RowsAtCompileTime == 1 ? 0 : index, Base::RowsAtCompileTime == 1 ? index : 0);
+  }
+
+  constexpr const Scalar& coeffRef(Index index) const {
+    EIGEN_STATIC_ASSERT(Base::IsVectorAtCompileTime, YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX)
+    return coeffRef(Base::RowsAtCompileTime == 1 ? 0 : index, Base::RowsAtCompileTime == 1 ? index : 0);
+  }
+
  protected:
   MatrixTypeNested m_xpr;
   RowIndices m_rowIndices;
diff --git a/Eigen/src/Core/MatrixNorms.h b/Eigen/src/Core/MatrixNorms.h
index 011aaf7..16c206b 100644
--- a/Eigen/src/Core/MatrixNorms.h
+++ b/Eigen/src/Core/MatrixNorms.h
@@ -169,8 +169,9 @@
   template <typename SumsDerived, typename Derived>
   EIGEN_DEVICE_FUNC void accumulate(DenseBase<SumsDerived>& sums, const DenseBase<Derived>& m, Index j0, Index j1,
                                     Index begin, Index end) {
-    accumulateCast(sums, j0, j1, begin, m.col(j0).segment(begin, end - begin).template cast<Scalar>(),
-                   m.col(j1).segment(begin, end - begin).template cast<Scalar>());
+    // Fixed-width blocks rather than col().segment(): with a zero-sized fixed dimension col() is not a vector.
+    accumulateCast(sums, j0, j1, begin, m.template block<Dynamic, 1>(begin, j0, end - begin, 1).template cast<Scalar>(),
+                   m.template block<Dynamic, 1>(begin, j1, end - begin, 1).template cast<Scalar>());
   }
 
  private:
@@ -337,18 +338,24 @@
     return impl.inRange(n) ? RealScalar(norm) : l1NormPerColumn();
   }
 
-  // One column at a time, the mirrored term read as a row; no workspace.
+  template <typename Xpr>
+  EIGEN_DEVICE_FUNC static L1NormAccumulator absSum(const Xpr& x) {
+    return x.template cast<L1NormScalar>().template lpNorm<1>();
+  }
+
+  // One column at a time, the mirrored term read as a row; no workspace. Fixed-width and fixed-height blocks rather
+  // than col()/row(): with a zero-sized fixed dimension those are not vectors, so head()/tail() would not compile.
   EIGEN_DEVICE_FUNC RealScalar l1NormPerColumn() const {
     L1NormAccumulator norm = L1NormAccumulator(0);
     const Index n = m_matrix.rows();
     for (Index col = 0; col < n; ++col) {
       L1NormAccumulator abs_col_sum;
       EIGEN_IF_CONSTEXPR (UpLo == Lower) {
-        abs_col_sum = m_matrix.col(col).tail(n - col).template cast<L1NormScalar>().template lpNorm<1>() +
-                      m_matrix.row(col).head(col).template cast<L1NormScalar>().template lpNorm<1>();
+        abs_col_sum = absSum(m_matrix.template block<Dynamic, 1>(col, col, n - col, 1)) +
+                      absSum(m_matrix.template block<1, Dynamic>(col, 0, 1, col));
       } else {
-        abs_col_sum = m_matrix.col(col).head(col).template cast<L1NormScalar>().template lpNorm<1>() +
-                      m_matrix.row(col).tail(n - col).template cast<L1NormScalar>().template lpNorm<1>();
+        abs_col_sum = absSum(m_matrix.template block<Dynamic, 1>(0, col, col, 1)) +
+                      absSum(m_matrix.template block<1, Dynamic>(col, col, 1, n - col));
       }
       norm = numext::maxi(norm, abs_col_sum);
     }
diff --git a/Eigen/src/Core/RealView.h b/Eigen/src/Core/RealView.h
index bea630c..8a6942b 100644
--- a/Eigen/src/Core/RealView.h
+++ b/Eigen/src/Core/RealView.h
@@ -128,6 +128,7 @@
   // If the first index is odd (imaginary), discard the first scalar
   // in 'result' and assign the missing scalar.
   // This operation is safe as the real component of the first scalar must exist.
+  // The complex load then starts one real scalar before the requested address, so it cannot keep an aligned mode.
 
   template <int LoadMode, typename PacketType>
   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packet(Index row, Index col) const {
@@ -138,7 +139,8 @@
     Index r = IsRowMajor ? row : row / 2;
     Index c = IsRowMajor ? col / 2 : col;
     bool p = (IsRowMajor ? col : row) & 1;
-    ComplexPacket cresult = BaseEvaluator::template packet<LoadMode, ComplexPacket>(r, c);
+    ComplexPacket cresult = p ? BaseEvaluator::template packet<Unaligned, ComplexPacket>(r, c)
+                              : BaseEvaluator::template packet<LoadMode, ComplexPacket>(r, c);
     PacketType result = preinterpret<PacketType>(cresult);
     if (p) {
       Scalar aux[RealPacketSize + 1];
@@ -157,9 +159,10 @@
     using ComplexPacket = typename find_packet_by_size<ComplexScalar, RealPacketSize / 2>::type;
     EIGEN_STATIC_ASSERT((find_packet_by_size<ComplexScalar, RealPacketSize / 2>::value),
                         MISSING COMPATIBLE COMPLEX PACKET TYPE)
-    ComplexPacket cresult = BaseEvaluator::template packet<LoadMode, ComplexPacket>(index / 2);
-    PacketType result = preinterpret<PacketType>(cresult);
     bool p = index & 1;
+    ComplexPacket cresult = p ? BaseEvaluator::template packet<Unaligned, ComplexPacket>(index / 2)
+                              : BaseEvaluator::template packet<LoadMode, ComplexPacket>(index / 2);
+    PacketType result = preinterpret<PacketType>(cresult);
     if (p) {
       Scalar aux[RealPacketSize + 1];
       pstoreu(aux, result);
@@ -186,16 +189,20 @@
     Index actualCount = actualEnd - actualBegin;
     Index r = IsRowMajor ? row : row / 2;
     Index c = IsRowMajor ? col / 2 : col;
-    ComplexPacket cresult =
-        BaseEvaluator::template packetSegment<LoadMode, ComplexPacket>(r, c, actualBegin / 2, actualCount / 2);
-    PacketType result = preinterpret<PacketType>(cresult);
     bool p = (IsRowMajor ? col : row) & 1;
+    ComplexPacket cresult =
+        p ? BaseEvaluator::template packetSegment<Unaligned, ComplexPacket>(r, c, actualBegin / 2, actualCount / 2)
+          : BaseEvaluator::template packetSegment<LoadMode, ComplexPacket>(r, c, actualBegin / 2, actualCount / 2);
+    PacketType result = preinterpret<PacketType>(cresult);
     if (p) {
       Scalar aux[RealPacketSize + 1] = {};
       pstoreu(aux, result);
-      Index lastr = IsRowMajor ? row : row + actualEnd - 1;
-      Index lastc = IsRowMajor ? col + actualEnd - 1 : col;
-      aux[actualEnd] = coeff(lastr, lastc);
+      // aux[actualEnd] is in the segment only when begin + count is even; otherwise it lies past the end.
+      if (actualEnd == begin + count) {
+        Index lastr = IsRowMajor ? row : row + actualEnd - 1;
+        Index lastc = IsRowMajor ? col + actualEnd - 1 : col;
+        aux[actualEnd] = coeff(lastr, lastc);
+      }
       result = ploadu<PacketType>(aux + 1);
     }
     return result;
@@ -210,18 +217,83 @@
     Index actualBegin = numext::round_down(begin, 2);
     Index actualEnd = numext::round_down(begin + count + 1, 2);
     Index actualCount = actualEnd - actualBegin;
-    ComplexPacket cresult =
-        BaseEvaluator::template packetSegment<LoadMode, ComplexPacket>(index / 2, actualBegin / 2, actualCount / 2);
-    PacketType result = preinterpret<PacketType>(cresult);
     bool p = index & 1;
+    ComplexPacket cresult =
+        p ? BaseEvaluator::template packetSegment<Unaligned, ComplexPacket>(index / 2, actualBegin / 2, actualCount / 2)
+          : BaseEvaluator::template packetSegment<LoadMode, ComplexPacket>(index / 2, actualBegin / 2, actualCount / 2);
+    PacketType result = preinterpret<PacketType>(cresult);
     if (p) {
       Scalar aux[RealPacketSize + 1] = {};
       pstoreu(aux, result);
-      aux[actualEnd] = coeff(index + actualEnd - 1);
+      if (actualEnd == begin + count) aux[actualEnd] = coeff(index + actualEnd - 1);
       result = ploadu<PacketType>(aux + 1);
     }
     return result;
   }
+
+  // Stores of whole complex coefficients go to the underlying evaluator as complex packets; a store that starts or ends
+  // inside a coefficient (an odd real index or count) falls back to scalar stores. The swap kernel requires these.
+
+  template <int StoreMode, typename PacketType>
+  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void writePacket(Index row, Index col, const PacketType& x) {
+    constexpr int RealPacketSize = unpacket_traits<PacketType>::size;
+    using ComplexPacket = typename find_packet_by_size<ComplexScalar, RealPacketSize / 2>::type;
+    if (((IsRowMajor ? col : row) & 1) == 0) {
+      BaseEvaluator::template writePacket<StoreMode, ComplexPacket>(
+          IsRowMajor ? row : row / 2, IsRowMajor ? col / 2 : col, preinterpret<ComplexPacket>(x));
+    } else {
+      writePacketSegment<StoreMode, PacketType>(row, col, x, 0, RealPacketSize);
+    }
+  }
+
+  template <int StoreMode, typename PacketType>
+  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void writePacket(Index index, const PacketType& x) {
+    constexpr int RealPacketSize = unpacket_traits<PacketType>::size;
+    using ComplexPacket = typename find_packet_by_size<ComplexScalar, RealPacketSize / 2>::type;
+    if ((index & 1) == 0) {
+      BaseEvaluator::template writePacket<StoreMode, ComplexPacket>(index / 2, preinterpret<ComplexPacket>(x));
+    } else {
+      writePacketSegment<StoreMode, PacketType>(index, x, 0, RealPacketSize);
+    }
+  }
+
+  template <int StoreMode, typename PacketType>
+  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void writePacketSegment(Index row, Index col, const PacketType& x, Index begin,
+                                                                Index count) {
+    constexpr int RealPacketSize = unpacket_traits<PacketType>::size;
+    using ComplexPacket = typename find_packet_by_size<ComplexScalar, RealPacketSize / 2>::type;
+    EIGEN_STATIC_ASSERT((find_packet_by_size<ComplexScalar, RealPacketSize / 2>::value),
+                        MISSING COMPATIBLE COMPLEX PACKET TYPE)
+    const Index inner = IsRowMajor ? col : row;
+    if (((inner | begin | count) & 1) == 0) {
+      const Index r = IsRowMajor ? row : row / 2;
+      const Index c = IsRowMajor ? col / 2 : col;
+      BaseEvaluator::template writePacketSegment<StoreMode, ComplexPacket>(r, c, preinterpret<ComplexPacket>(x),
+                                                                           begin / 2, count / 2);
+    } else {
+      Scalar aux[RealPacketSize];
+      pstoreu(aux, x);
+      for (Index i = begin; i < begin + count; ++i)
+        coeffRef(IsRowMajor ? row : row + i, IsRowMajor ? col + i : col) = aux[i];
+    }
+  }
+
+  template <int StoreMode, typename PacketType>
+  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void writePacketSegment(Index index, const PacketType& x, Index begin,
+                                                                Index count) {
+    constexpr int RealPacketSize = unpacket_traits<PacketType>::size;
+    using ComplexPacket = typename find_packet_by_size<ComplexScalar, RealPacketSize / 2>::type;
+    EIGEN_STATIC_ASSERT((find_packet_by_size<ComplexScalar, RealPacketSize / 2>::value),
+                        MISSING COMPATIBLE COMPLEX PACKET TYPE)
+    if (((index | begin | count) & 1) == 0) {
+      BaseEvaluator::template writePacketSegment<StoreMode, ComplexPacket>(index / 2, preinterpret<ComplexPacket>(x),
+                                                                           begin / 2, count / 2);
+    } else {
+      Scalar aux[RealPacketSize];
+      pstoreu(aux, x);
+      for (Index i = begin; i < begin + count; ++i) coeffRef(index + i) = aux[i];
+    }
+  }
 };
 
 }  // namespace internal
diff --git a/Eigen/src/Core/Ref.h b/Eigen/src/Core/Ref.h
index 39e618c..3e725ad 100644
--- a/Eigen/src/Core/Ref.h
+++ b/Eigen/src/Core/Ref.h
@@ -140,10 +140,10 @@
 
     const bool swap_stride = (transpose != storage_differs);
 
-    // Determine expr's actual strides, resolving any defaults if zero.
-    const Index expr_inner_actual = resolveInnerStride(expr.innerStride());
-    const Index expr_outer_actual = resolveOuterStride(expr_inner_actual, expr.outerStride(), expr.rows(), expr.cols(),
-                                                       Expression::IsVectorAtCompileTime != 0, expr_row_major);
+    // Unlike a compile-time stride of 0, a runtime stride of 0 is not a default: it repeats coefficients, as in
+    // Map<const VectorXd, 0, InnerStride<>>(&x, n, InnerStride<>(0)).
+    const Index expr_inner_actual = expr.innerStride();
+    const Index expr_outer_actual = expr.outerStride();
 
     // If this is a column-major row vector or row-major column vector, the inner-stride
     // is arbitrary, so set it to either the compile-time inner stride or 1.
diff --git a/Eigen/src/Core/Reshaped.h b/Eigen/src/Core/Reshaped.h
index 4a94cd1..6cc21a8 100644
--- a/Eigen/src/Core/Reshaped.h
+++ b/Eigen/src/Core/Reshaped.h
@@ -161,8 +161,6 @@
   using MatrixTypeNested = typename internal::ref_selector<XprType>::non_const_type;
   using NestedExpression = internal::remove_all_t<XprType>;
 
-  class InnerIterator;
-
   /** Fixed-size constructor
    */
   EIGEN_DEVICE_FUNC constexpr inline ReshapedImpl_dense(XprType& xpr) : m_xpr(xpr), m_rows(Rows), m_cols(Cols) {}
@@ -307,7 +305,9 @@
   };
 
   EIGEN_DEVICE_FUNC constexpr explicit reshaped_evaluator(const XprType& xpr)
-      : m_argImpl(xpr.nestedExpression()), m_xpr(xpr) {
+      : m_argImpl(xpr.nestedExpression()),
+        m_innerSize(Order == ColMajor ? xpr.rows() : xpr.cols()),
+        m_nestedInnerSize(Order == ColMajor ? xpr.nestedExpression().rows() : xpr.nestedExpression().cols()) {
     EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
   }
 
@@ -320,18 +320,18 @@
   // the nested evaluator's linear index of that element.
   EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Index linear_index(Index rowId, Index colId) const {
     EIGEN_IF_CONSTEXPR (Order == ColMajor) {
-      return colId * m_xpr.rows() + rowId;
+      return colId * m_innerSize.value() + rowId;
     } else {
-      return colId + rowId * m_xpr.cols();
+      return colId + rowId * m_innerSize.value();
     }
   }
 
   EIGEN_DEVICE_FUNC constexpr inline RowCol index_remap(Index rowId, Index colId) const {
     const Index nth_elem_idx = linear_index(rowId, colId);
     EIGEN_IF_CONSTEXPR (Order == ColMajor) {
-      return RowCol(nth_elem_idx % m_xpr.nestedExpression().rows(), nth_elem_idx / m_xpr.nestedExpression().rows());
+      return RowCol(nth_elem_idx % m_nestedInnerSize.value(), nth_elem_idx / m_nestedInnerSize.value());
     } else {
-      return RowCol(nth_elem_idx / m_xpr.nestedExpression().cols(), nth_elem_idx % m_xpr.nestedExpression().cols());
+      return RowCol(nth_elem_idx / m_nestedInnerSize.value(), nth_elem_idx % m_nestedInnerSize.value());
     }
   }
 
@@ -476,7 +476,11 @@
   }
 
   evaluator<ArgType> m_argImpl;
-  const XprType& m_xpr;
+  // Sizes are copied rather than read through the expression: select() evaluates a rewritten temporary expression
+  // that does not outlive this evaluator.
+  const variable_if_dynamic<Index, Order == ColMajor ? Rows : Cols> m_innerSize;
+  const variable_if_dynamic<Index, Order == ColMajor ? ArgType::RowsAtCompileTime : ArgType::ColsAtCompileTime>
+      m_nestedInnerSize;
 };
 
 template <typename ArgType, int Rows, int Cols, int Order>
diff --git a/Eigen/src/Core/Reverse.h b/Eigen/src/Core/Reverse.h
index 70a57f5..5309d50 100644
--- a/Eigen/src/Core/Reverse.h
+++ b/Eigen/src/Core/Reverse.h
@@ -130,14 +130,16 @@
 EIGEN_DEVICE_FUNC inline void DenseBase<Derived>::reverseInPlace() {
   constexpr int HalfRowsAtCompileTime = RowsAtCompileTime == Dynamic ? Dynamic : RowsAtCompileTime / 2;
   constexpr int HalfColsAtCompileTime = ColsAtCompileTime == Dynamic ? Dynamic : ColsAtCompileTime / 2;
+  // The middle column or row is a block rather than col()/row(): both branches compile for every shape, and with a
+  // zero-sized fixed dimension col()/row() is not a vector, so head()/tail() would not compile.
   if (cols() > rows()) {
     Index half = cols() / 2;
     this->template leftCols<HalfColsAtCompileTime>(half).swap(
         this->template rightCols<HalfColsAtCompileTime>(half).reverse());
     if ((cols() % 2) == 1) {
       Index half2 = rows() / 2;
-      col(half).template head<HalfRowsAtCompileTime>(half2).swap(
-          col(half).template tail<HalfRowsAtCompileTime>(half2).reverse());
+      this->template block<HalfRowsAtCompileTime, 1>(0, half, half2, 1)
+          .swap(this->template block<HalfRowsAtCompileTime, 1>(rows() - half2, half, half2, 1).reverse());
     }
   } else {
     Index half = rows() / 2;
@@ -145,8 +147,8 @@
         this->template bottomRows<HalfRowsAtCompileTime>(half).reverse());
     if ((rows() % 2) == 1) {
       Index half2 = cols() / 2;
-      row(half).template head<HalfColsAtCompileTime>(half2).swap(
-          row(half).template tail<HalfColsAtCompileTime>(half2).reverse());
+      this->template block<1, HalfColsAtCompileTime>(half, 0, 1, half2)
+          .swap(this->template block<1, HalfColsAtCompileTime>(half, cols() - half2, 1, half2).reverse());
     }
   }
 }
diff --git a/Eigen/src/Core/SelfAdjointView.h b/Eigen/src/Core/SelfAdjointView.h
index 401b68c..18850c3 100644
--- a/Eigen/src/Core/SelfAdjointView.h
+++ b/Eigen/src/Core/SelfAdjointView.h
@@ -134,7 +134,9 @@
   EIGEN_DEVICE_FUNC constexpr const MatrixTypeNestedCleaned& _expression() const noexcept { return m_matrix; }
 
   EIGEN_DEVICE_FUNC constexpr const MatrixTypeNestedCleaned& nestedExpression() const noexcept { return m_matrix; }
-  EIGEN_DEVICE_FUNC constexpr MatrixTypeNestedCleaned& nestedExpression() noexcept { return m_matrix; }
+  EIGEN_DEVICE_FUNC constexpr std::remove_reference_t<MatrixTypeNested>& nestedExpression() noexcept {
+    return m_matrix;
+  }
 
   EIGEN_DEVICE_FUNC const SelfAdjointView<
       const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(MatrixType, Scalar, internal::scalar_product_op), UpLo>
diff --git a/Eigen/src/Core/Transpose.h b/Eigen/src/Core/Transpose.h
index 62319b2..cb32472 100644
--- a/Eigen/src/Core/Transpose.h
+++ b/Eigen/src/Core/Transpose.h
@@ -275,8 +275,10 @@
       }
     }
   }
+  // Blocks rather than row()/col(): with a zero-sized fixed dimension those are not vectors and head() would not
+  // compile.
   for (Index row = row_start; row < m.rows(); ++row) {
-    m.matrix().row(row).head(row).swap(m.matrix().col(row).head(row).transpose());
+    m.matrix().block(row, 0, 1, row).swap(m.matrix().block(0, row, row, 1).transpose());
   }
 }
 
diff --git a/Eigen/src/Core/TriangularInPlace.h b/Eigen/src/Core/TriangularInPlace.h
index 69499ba..be22d2c 100644
--- a/Eigen/src/Core/TriangularInPlace.h
+++ b/Eigen/src/Core/TriangularInPlace.h
@@ -70,11 +70,16 @@
   }
 }
 
+// With a zero-sized fixed dimension there is nothing to invert, and col() is not a vector, so the kernels' column
+// tails would not compile.
+template <unsigned int Mode, typename MatrixType, std::enable_if_t<MatrixType::SizeAtCompileTime == 0, int> = 0>
+EIGEN_DEVICE_FUNC void triangular_inverse_lower(MatrixType&) {}
+
 // Blocked lower triangular inverse, block columns bottom-up as in LAPACK xTRTRI. With
 // L = [[L11, 0], [L21, L22]] the inverse is [[L11^-1, 0], [-L22^-1 L21 L11^-1, L22^-1]]; walking upwards
 // makes X22 = L22^-1 available first, so the large operation is the TRMM X22 L21 rather than a solve
 // against L22, and Eigen's TRMM runs closer to GEMM rate than its TRSM does on a 128-column operand.
-template <unsigned int Mode, typename MatrixType>
+template <unsigned int Mode, typename MatrixType, std::enable_if_t<MatrixType::SizeAtCompileTime != 0, int> = 0>
 EIGEN_DEVICE_FUNC void triangular_inverse_lower(MatrixType& mat) {
   eigen_assert(mat.rows() == mat.cols());
   const Index n = mat.rows();
diff --git a/Eigen/src/Core/TriangularMatrix.h b/Eigen/src/Core/TriangularMatrix.h
index 2aea7f0..0d083aa 100644
--- a/Eigen/src/Core/TriangularMatrix.h
+++ b/Eigen/src/Core/TriangularMatrix.h
@@ -453,7 +453,7 @@
   EIGEN_DEVICE_FUNC constexpr const NestedExpression& nestedExpression() const noexcept { return m_matrix; }
 
   /** \returns a reference to the nested expression */
-  EIGEN_DEVICE_FUNC constexpr NestedExpression& nestedExpression() noexcept { return m_matrix; }
+  EIGEN_DEVICE_FUNC constexpr MatrixTypeNestedNonRef& nestedExpression() noexcept { return m_matrix; }
 
   template <typename Other>
   EIGEN_DEVICE_FUNC inline Solve<TriangularView, Other> solve(const MatrixBase<Other>& other) const {
diff --git a/test/basicstuff.cpp b/test/basicstuff.cpp
index 115b1c9..472a8d7 100644
--- a/test/basicstuff.cpp
+++ b/test/basicstuff.cpp
@@ -380,6 +380,16 @@
   VERIFY(!it);
 }
 
+// nestedExpression() is callable on a non-const unary expression, whose operand is always nested as const.
+template <typename = void>
+void unaryNestedExpression() {
+  const Matrix3f m = Matrix3f::Random();
+  auto negated = -m;
+  VERIFY(&negated.nestedExpression() == &m);
+  auto absSum = (m + m).cwiseAbs();
+  VERIFY_IS_EQUAL(absSum.nestedExpression().rows(), m.rows());
+}
+
 EIGEN_DECLARE_TEST(basicstuff) {
   for (int i = 0; i < g_repeat; i++) {
     CALL_SUBTEST_1(basicStuff(Matrix<float, 1, 1>()));
@@ -408,6 +418,7 @@
   }
 
   CALL_SUBTEST_1(denseInnerIteratorAdvance<>());
+  CALL_SUBTEST_1(unaryNestedExpression<>());
   CALL_SUBTEST_1(fixedSizeMatrixConstruction<unsigned char>());
   CALL_SUBTEST_1(fixedSizeMatrixConstruction<float>());
   CALL_SUBTEST_1(fixedSizeMatrixConstruction<double>());
diff --git a/test/indexed_view.cpp b/test/indexed_view.cpp
index 1c33727..f83c8dc 100644
--- a/test/indexed_view.cpp
+++ b/test/indexed_view.cpp
@@ -995,6 +995,33 @@
   VERIFY_IS_EQUAL(z, expected);
 }
 
+// Vector-shaped views provide linear coeffRef(), like every other writable vector expression.
+void check_vector_coeffRef() {
+  MatrixXd A = MatrixXd::Random(6, 5);
+  const std::vector<int> rows = {4, 0, 2};
+  auto col = A.col(1)(rows);
+  col.coeffRef(1) = 1.5;
+  VERIFY_IS_EQUAL(A(0, 1), 1.5);
+  const auto& constCol = col;
+  VERIFY_IS_EQUAL(constCol.coeffRef(2), A(2, 1));
+  auto row = A.row(3)(placeholders::lastN(3, 2));
+  row.coeffRef(0) = 2.5;
+  VERIFY_IS_EQUAL(A(3, 0), 2.5);
+  VERIFY_IS_EQUAL(row.coeffRef(2), A(3, 4));
+
+  // makeHouseholder() writes its essential part through coeffRef(i).
+  const VectorXd x = VectorXd::Random(4);
+  VectorXd expected(3), essential = VectorXd::Zero(6);
+  double tau, beta, tauRef, betaRef;
+  x.makeHouseholder(expected, tauRef, betaRef);
+  const std::vector<int> slots = {5, 1, 3};
+  auto essentialView = essential(slots);
+  x.makeHouseholder(essentialView, tau, beta);
+  VERIFY_IS_EQUAL(tau, tauRef);
+  VERIFY_IS_EQUAL(beta, betaRef);
+  VERIFY_IS_EQUAL(VectorXd(essential(slots)), expected);
+}
+
 EIGEN_DECLARE_TEST(indexed_view) {
   for (int i = 0; i < g_repeat; i++) {
     CALL_SUBTEST_1(check_indexed_view());
@@ -1008,6 +1035,7 @@
   CALL_SUBTEST_1((check_indexed_view_strides<Matrix<double, 6, 8>>()));
   CALL_SUBTEST_1((check_indexed_view_strides<Matrix<double, 6, 8, RowMajor>>()));
   CALL_SUBTEST_1(check_aliasing());
+  CALL_SUBTEST_1(check_vector_coeffRef());
 
   // static checks of some internals:
   STATIC_CHECK((internal::is_valid_index_type<int>::value));
diff --git a/test/realview.cpp b/test/realview.cpp
index ee5b5a9..db3e2c8 100644
--- a/test/realview.cpp
+++ b/test/realview.cpp
@@ -148,6 +148,16 @@
   }
   VERIFY_IS_CWISE_EQUAL(C, D);
 
+  // swap() through the views: these maps start at an odd real offset, and a reversed view has no direct access.
+  C.setRandom();
+  T C0 = C, D0 = D;
+  C.realView().swap(D.realView());
+  VERIFY_IS_CWISE_EQUAL(C, D0);
+  VERIFY_IS_CWISE_EQUAL(D, C0);
+  C.reverse().realView().swap(D.realView());
+  VERIFY_IS_CWISE_EQUAL(C, T(C0.reverse()));
+  VERIFY_IS_CWISE_EQUAL(D, T(D0.reverse()));
+
   C = A;
 
   for (Index c = 0; c < realViewCols - 1; c++) {
@@ -197,9 +207,68 @@
   static void run() {}
 };
 
+// Complex sequence c(i) = (i, -i) whose scalar path records the largest index it is asked for.
+template <typename ComplexScalar>
+struct index_recording_complex_op {
+  Index* maxIndex;
+  ComplexScalar operator()(Index i) const {
+    *maxIndex = numext::maxi(*maxIndex, i);
+    return value(i);
+  }
+  template <typename Packet>
+  Packet packetOp(Index i) const {
+    ComplexScalar values[internal::unpacket_traits<Packet>::size];
+    for (int k = 0; k < internal::unpacket_traits<Packet>::size; ++k) values[k] = value(i + k);
+    return internal::ploadu<Packet>(values);
+  }
+  static ComplexScalar value(Index i) {
+    using RealScalar = typename NumTraits<ComplexScalar>::Real;
+    return ComplexScalar(RealScalar(i), RealScalar(-i));
+  }
+};
+template <typename ComplexScalar>
+struct Eigen::internal::functor_traits<index_recording_complex_op<ComplexScalar>> {
+  static constexpr int Cost = 1;
+  static constexpr bool PacketAccess = true;
+  static constexpr bool IsRepeatable = true;
+};
+
+// A segment starting at an odd (imaginary) position must not read the coefficient past its end.
+template <typename ComplexScalar>
+void test_odd_segment_bounds() {
+  using namespace internal;
+  using RealScalar = typename NumTraits<ComplexScalar>::Real;
+  using ComplexPacket = typename packet_traits<ComplexScalar>::type;
+  using RealPacket = typename unpacket_traits<ComplexPacket>::as_real;
+  constexpr Index RealSize = 2 * unpacket_traits<ComplexPacket>::size;
+  const Index n = RealSize;
+  Index maxIndex = -1;
+  const auto sequence = VectorX<ComplexScalar>::NullaryExpr(n, index_recording_complex_op<ComplexScalar>{&maxIndex});
+  evaluator<RealView<const std::decay_t<decltype(sequence)>>> eval(sequence.realView());
+  VectorX<RealScalar> expected(2 * n), actual(RealSize);
+  for (Index i = 0; i < n; ++i) expected.template segment<2>(2 * i) << RealScalar(i), RealScalar(-i);
+  for (Index count = 1; count <= RealSize; ++count) {
+    const Index offset = 2 * n - count;
+    RealPacket res = eval.template packetSegment<Unaligned, RealPacket>(offset, 0, count);
+    pstoreSegment(actual.data(), res, 0, count);
+    VERIFY_IS_CWISE_EQUAL(actual.head(count), expected.segment(offset, count));
+    VERIFY(maxIndex < n);
+  }
+}
+
+template <typename ComplexScalar, bool Enable = internal::packet_traits<ComplexScalar>::Vectorizable>
+struct test_odd_segment_bounds_impl {
+  static void run() { test_odd_segment_bounds<ComplexScalar>(); }
+};
+template <typename ComplexScalar>
+struct test_odd_segment_bounds_impl<ComplexScalar, false> {
+  static void run() {}
+};
+
 template <typename ComplexScalar>
 void test_edge_cases(const ComplexScalar&) {
   test_edge_cases_impl<ComplexScalar>::run();
+  test_odd_segment_bounds_impl<ComplexScalar>::run();
 }
 
 // The parts of non-adjacent complex coefficients have no uniform stride, so their RealView has no direct access.
diff --git a/test/ref.cpp b/test/ref.cpp
index 21cfc30..6cc0d4d 100644
--- a/test/ref.cpp
+++ b/test/ref.cpp
@@ -249,6 +249,25 @@
   VERIFY(ref.data() == data);
 }
 
+// A runtime stride of 0 repeats a coefficient; a Ref must not read it as the default stride.
+void test_ref_zero_strides() {
+  const float data[6] = {1, 2, 3, 4, 5, 6};
+  Map<const VectorXf, 0, InnerStride<>> repeated(data, 4, InnerStride<>(0));
+  const Ref<const VectorXf, 0, InnerStride<>> repeatedRef(repeated);
+  VERIFY_IS_EQUAL(repeatedRef, VectorXf::Constant(4, data[0]));
+
+  Map<const MatrixXf, 0, OuterStride<>> repeatedCols(data, 2, 3, OuterStride<>(0));
+  const MatrixXf expectedCols = Vector2f(data[0], data[1]).replicate(1, 3);
+  const Ref<const MatrixXf, 0, OuterStride<>> colsRef(repeatedCols);
+  VERIFY_IS_EQUAL(colsRef, expectedCols);
+  const Ref<const MatrixXf, 0, Stride<Dynamic, Dynamic>> colsStrideRef(repeatedCols);
+  VERIFY_IS_EQUAL(colsStrideRef, expectedCols);
+
+  const VectorXf v = VectorXf::Random(5);
+  const Ref<const VectorXf, 0, InnerStride<>> indexedRef(v(seqN(2, 3, fix<0>)));
+  VERIFY_IS_EQUAL(indexedRef, VectorXf::Constant(3, v(2)));
+}
+
 template <typename B>
 EIGEN_DONT_INLINE void call_ref_1(Ref<VectorXf> a, const B &b) {
   VERIFY_IS_EQUAL(a, b);
@@ -435,4 +454,5 @@
   CALL_SUBTEST_10(test_contiguous_ref_no_copy(MatrixXd(9, 5)));
   CALL_SUBTEST_10(test_contiguous_ref_no_copy(Matrix3d()));
   CALL_SUBTEST_10(test_ref_alignment_matching());
+  CALL_SUBTEST_10(test_ref_zero_strides());
 }
diff --git a/test/reshape.cpp b/test/reshape.cpp
index eaf3e24..6852b9b 100644
--- a/test/reshape.cpp
+++ b/test/reshape.cpp
@@ -416,6 +416,44 @@
   VERIFY_IS_EQUAL(source, originalSource);
 }
 
+// select() evaluates a rewritten temporary of a packet-capable expression, so the evaluator of a coefficient-based
+// reshape must not read through the expression it was built from.
+template <int Order>
+void reshape_evaluator_outlives_expression() {
+  const MatrixXd a = MatrixXd::Random(4, 6), b = MatrixXd::Random(4, 6);
+  using Reshape = std::decay_t<decltype((a + b).reshaped<Order>(6, 4))>;
+  const MatrixXd expected = MatrixXd(a + b).reshaped<Order>(6, 4);
+
+  alignas(Reshape) unsigned char storage[sizeof(Reshape)];
+  Reshape* reshape = new (storage) Reshape((a + b).reshaped<Order>(6, 4));
+  internal::evaluator<Reshape> eval(*reshape);
+  reshape->~Reshape();
+  std::memset(storage, 0xff, sizeof(storage));
+  for (Index j = 0; j < 4; ++j)
+    for (Index i = 0; i < 6; ++i) VERIFY_IS_EQUAL(eval.coeff(i, j), expected(i, j));
+
+  const ArrayXXd x = ArrayXXd::Random(6, 4), y = ArrayXXd::Random(6, 4);
+  ArrayXXd selected = ArrayXXd::Zero(8, 5);
+  selected.block(1, 1, 6, 4) = ((a + b).reshaped<Order>(6, 4).array() < x).select(x, y);
+  VERIFY_IS_CWISE_EQUAL(selected.block(1, 1, 6, 4), (expected.array() < x).select(x, y).eval());
+}
+
+// A coefficient-based reshape exposes DenseBase's InnerIterator, which blueNorm() uses.
+void reshape_inner_iterator() {
+  const MatrixXf a = MatrixXf::Random(4, 3);
+  const MatrixXf rowMajorCopy = a.reshaped<RowMajor>(3, 4);
+  const auto view = a.reshaped<RowMajor>(3, 4);
+  using View = std::decay_t<decltype(view)>;
+  VERIFY(!(internal::traits<View>::Flags & DirectAccessBit));
+  for (Index j = 0; j < view.outerSize(); ++j) {
+    Index count = 0;
+    for (View::InnerIterator it(view, j); it; ++it, ++count)
+      VERIFY_IS_EQUAL(it.value(), rowMajorCopy(it.row(), it.col()));
+    VERIFY_IS_EQUAL(count, view.innerSize());
+  }
+  VERIFY_IS_APPROX(view.blueNorm(), a.blueNorm());
+}
+
 EIGEN_DECLARE_TEST(reshape) {
   typedef Matrix<int, Dynamic, Dynamic, RowMajor> RowMatrixXi;
   typedef Matrix<int, 4, 4, RowMajor> RowMatrix4i;
@@ -445,6 +483,9 @@
   CALL_SUBTEST_8((reshape_vector_orders<int, ColMajor>()));
   CALL_SUBTEST_8((reshape_vector_orders<int, RowMajor>()));
   CALL_SUBTEST_8(reshape_reversed_swap());
+  CALL_SUBTEST_8(reshape_evaluator_outlives_expression<ColMajor>());
+  CALL_SUBTEST_8(reshape_evaluator_outlives_expression<RowMajor>());
+  CALL_SUBTEST_8(reshape_inner_iterator());
 
   for (int i = 0; i < g_repeat; i++) {
     CALL_SUBTEST_7(reshape_copies<float>(17, 13));
diff --git a/test/selfadjoint.cpp b/test/selfadjoint.cpp
index 708b102..359a8a0 100644
--- a/test/selfadjoint.cpp
+++ b/test/selfadjoint.cpp
@@ -141,6 +141,35 @@
   }
 }
 
+template <typename MatrixType>
+void selfadjoint_l1norm_empty(const MatrixType& m) {
+  typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
+  VERIFY_IS_EQUAL(m.template selfadjointView<Lower>().l1Norm(), RealScalar(0));
+  VERIFY_IS_EQUAL(m.template selfadjointView<Upper>().l1Norm(), RealScalar(0));
+}
+
+// With a zero-sized fixed dimension col() and row() are not vectors, so the kernels must not take their head() or
+// tail(); the row-major form also instantiates the streaming pass on the transpose.
+template <typename Scalar>
+void selfadjoint_l1norm_zero_sized() {
+  selfadjoint_l1norm_empty(Matrix<Scalar, 0, 0>());
+  selfadjoint_l1norm_empty(Matrix<Scalar, 0, 0, RowMajor>());
+  selfadjoint_l1norm_empty(Matrix<Scalar, Dynamic, 0, ColMajor, 0, 0>());
+}
+
+// nestedExpression() on a non-const view keeps the operand's constness: callable for a const operand, writable for a
+// mutable one.
+template <typename = void>
+void selfadjoint_nested_expression() {
+  const Matrix3d a = Matrix3d::Identity();
+  auto lower = a.selfadjointView<Lower>();
+  VERIFY(&lower.nestedExpression() == &a);
+  Matrix3d b = Matrix3d::Zero();
+  auto upper = b.selfadjointView<Upper>();
+  upper.nestedExpression()(0, 1) = 1;
+  VERIFY_IS_EQUAL(b(0, 1), 1.0);
+}
+
 void bug_159() {
   Matrix3d m = Matrix3d::Random().selfadjointView<Lower>();
   EIGEN_UNUSED_VARIABLE(m);
@@ -161,6 +190,7 @@
   }
 
   CALL_SUBTEST_1(bug_159());
+  CALL_SUBTEST_1(selfadjoint_nested_expression<>());
   CALL_SUBTEST_4(selfadjoint_l1norm_sizes<double>());
   CALL_SUBTEST_4(selfadjoint_l1norm_sizes<std::complex<double> >());
   CALL_SUBTEST_6(selfadjoint_l1norm_sizes<std::complex<float> >());
@@ -168,6 +198,10 @@
   CALL_SUBTEST_6(selfadjoint_l1norm_range<std::complex<float> >());
   CALL_SUBTEST_1(selfadjoint_l1norm_integer<short>());
   CALL_SUBTEST_1(selfadjoint_l1norm_integer<int>());
+  CALL_SUBTEST_1(selfadjoint_l1norm_zero_sized<int>());
+  CALL_SUBTEST_4(selfadjoint_l1norm_zero_sized<double>());
+  CALL_SUBTEST_4(selfadjoint_l1norm_zero_sized<std::complex<double> >());
+  CALL_SUBTEST_6(selfadjoint_l1norm_zero_sized<std::complex<float> >());
   CALL_SUBTEST_7(selfadjoint_l1norm_lowprec<half>());
   CALL_SUBTEST_7(selfadjoint_l1norm_lowprec<bfloat16>());
 }
diff --git a/test/triangular.cpp b/test/triangular.cpp
index b237c67..00a4ed1 100644
--- a/test/triangular.cpp
+++ b/test/triangular.cpp
@@ -655,6 +655,19 @@
   }
 }
 
+// nestedExpression() on a non-const view keeps the operand's constness: callable for a const operand, writable for a
+// mutable one.
+template <typename = void>
+void triangular_nested_expression() {
+  const Matrix3d a = Matrix3d::Identity();
+  auto lower = a.triangularView<Lower>();
+  VERIFY(&lower.nestedExpression() == &a);
+  Matrix3d b = Matrix3d::Zero();
+  auto upper = b.triangularView<Upper>();
+  upper.nestedExpression()(0, 1) = 1;
+  VERIFY_IS_EQUAL(b(0, 1), 1.0);
+}
+
 void bug_159() {
   Matrix3d m = Matrix3d::Random().triangularView<Lower>();
   EIGEN_UNUSED_VARIABLE(m);
@@ -701,6 +714,7 @@
   }
 
   CALL_SUBTEST_1(bug_159());
+  CALL_SUBTEST_1(triangular_nested_expression<>());
 
   // Triangular solve/product at blocking boundaries (deterministic, outside g_repeat).
   CALL_SUBTEST_11(triangular_at_blocking_boundaries<0>());
diff --git a/test/triangular_in_place.cpp b/test/triangular_in_place.cpp
index e1e510b..c7832a1 100644
--- a/test/triangular_in_place.cpp
+++ b/test/triangular_in_place.cpp
@@ -188,5 +188,8 @@
     CALL_SUBTEST_6((triangular_inverse_all_modes<Matrix<double, 1, 1>>(1)));
     CALL_SUBTEST_6(triangular_inverse_all_modes<Matrix4d>(4));
     CALL_SUBTEST_6((triangular_inverse_all_modes<Matrix<std::complex<float>, 8, 8>>(8)));
+    // With a zero-sized fixed dimension col() is not a vector; inverseInPlace() must still compile.
+    CALL_SUBTEST_6((triangular_inverse_all_modes<Matrix<double, 0, 0>>(0)));
+    CALL_SUBTEST_6((triangular_inverse_all_modes<Matrix<float, Dynamic, 0, ColMajor, 0, 0>>(0)));
   }
 }
diff --git a/test/zerosized.cpp b/test/zerosized.cpp
index 3ad3a89..ca3ebd4 100644
--- a/test/zerosized.cpp
+++ b/test/zerosized.cpp
@@ -43,6 +43,15 @@
     if (MatrixType::RowsAtCompileTime == Dynamic) VERIFY(t1.rows() == 0);
     if (MatrixType::ColsAtCompileTime == Dynamic) VERIFY(t1.cols() == 0);
 
+    MatrixType reversed = t1;
+    reversed.reverseInPlace();
+    VERIFY(reversed == t1.reverse());
+    if (t1.rows() == t1.cols()) {
+      MatrixType transposed = t1;
+      transposed.transposeInPlace();
+      VERIFY(transposed == t1.transpose());
+    }
+
     if (MatrixType::RowsAtCompileTime == Dynamic && MatrixType::ColsAtCompileTime == Dynamic) {
       MatrixType t2(0, 0);
       VERIFY(t2.rows() == 0);