Add better error reporting for StorageIndex overflow

libeigen/eigen!2492

Co-authored-by: Charles Schlosser <cs.schlosser@gmail.com>
diff --git a/Eigen/src/SparseCore/SparseMatrix.h b/Eigen/src/SparseCore/SparseMatrix.h
index 2248def..f04e989 100644
--- a/Eigen/src/SparseCore/SparseMatrix.h
+++ b/Eigen/src/SparseCore/SparseMatrix.h
@@ -1147,7 +1147,9 @@
   for (InputIterator it(begin); it != end; ++it) {
     eigen_assert(it->row() >= 0 && it->row() < mat.rows() && it->col() >= 0 && it->col() < mat.cols());
     StorageIndex j = convert_index<StorageIndex>(IsRowMajor ? it->col() : it->row());
-    if (nonZeros == NumTraits<StorageIndex>::highest()) internal::throw_std_bad_alloc();
+    eigen_assert(nonZeros < NumTraits<StorageIndex>::highest() &&
+                 "non-zero count exceeds StorageIndex range, use a wider StorageIndex (e.g. int64_t)");
+    if (nonZeros >= NumTraits<StorageIndex>::highest()) internal::throw_std_bad_alloc();
     trmat.outerIndexPtr()[j + 1]++;
     nonZeros++;
   }
@@ -1201,7 +1203,9 @@
     // identify duplicates by examining previous location
     bool duplicate = (previous_j == j) && (previous_i == i);
     if (!duplicate) {
-      if (nonZeros == NumTraits<StorageIndex>::highest()) internal::throw_std_bad_alloc();
+      eigen_assert(nonZeros < NumTraits<StorageIndex>::highest() &&
+                   "non-zero count exceeds StorageIndex range, use a wider StorageIndex (e.g. int64_t)");
+      if (nonZeros >= NumTraits<StorageIndex>::highest()) internal::throw_std_bad_alloc();
       nonZeros++;
       mat.outerIndexPtr()[j + 1]++;
       previous_j = j;