fix(Storage): check dtype in at<T> unconditionally (#965)#978
Conversation
The dtype-mismatch check was guarded by cytnx::User_debug (off by default), so Tensor::item<T>() and Storage::at<T>() silently reinterpreted storage as the wrong type in normal builds. data<T>() and back<T>() already checked unconditionally; at<T> now matches. Enabling the check unconditionally exposed two tests that were relying on the previously-silent type punning: - DenseUniTensorTest.at compared a const UniTensor against complex types without ever casting it to that dtype (it stayed Double). - Arnoldi_Gnd.Arnoldi_BK_test read the ComplexDouble output of linalg::Eig() with item<double>(); switched to item().real(). Co-Authored-By: Claude <noreply@anthropic.com>
…s; reuse shared dtype list in test The out-of-bound error in every at<T> specialization formatted cytnx_uint64 idx and unsigned long long size() through %d, printing garbage for values >= 2^31 (and formally UB in the printf-family formatter). Use %llu with explicit casts, and pin the error-path message with a test. AtDtypeMismatchThrowsForAllDtypes now iterates TestTools::dtype_list instead of re-declaring the same 11-dtype list, so the exhaustive test stays exhaustive if a dtype is ever added. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request removes the cytnx::User_debug guard around type mismatch checks in Storage_base::at<T> specializations, ensuring that type checks are always performed. It also updates out-of-bounds error messages to use %llu with explicit casts to unsigned long long. Corresponding unit tests have been added to verify that type mismatches throw std::logic_error and that out-of-bounds messages report the correct indices. Additionally, a test in Arnoldi_Ut_test.cpp was updated to use .item().real() to avoid type mismatch exceptions. There are no review comments, and we have no further feedback to provide.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #978 +/- ##
==========================================
+ Coverage 30.87% 30.91% +0.04%
==========================================
Files 229 229
Lines 34757 34759 +2
Branches 14409 14398 -11
==========================================
+ Hits 10730 10745 +15
+ Misses 16720 16708 -12
+ Partials 7307 7306 -1
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
|
Heads-up: #979 is stacked on this branch — it refactors the 11 copy-pasted |
Summary
Fixes #965.
Storage_base::at<T>. Everyat<T>specialization wrapped its dtype-mismatch check inif (cytnx::User_debug)(defaultfalse), so e.g.Tensor({1}, Type.Float).item<double>()reinterpreted a float buffer as double and returned garbage in normal builds. The checks now run unconditionally, matchingdata<T>()andback<T>(), which already checked unconditionally. Mismatchedat<T>/item<T>calls now throwstd::logic_errorinstead of silently type-punning.at<T>specializations formattedcytnx_uint64index andunsigned long longsize through%d(garbage for values ≥ 2³¹, UB in the formatter) — now%lluwith explicit casts.Test fallout fixed (call sites relying on the old silent behavior)
tests/linalg_test/Arnoldi_Ut_test.cpp:linalg::Max(block).item<double>()onlinalg::Eigoutput (ComplexDouble) — now reads.item().real().tests/DenseUniTensor_test.cpp(attest): the constcuttensor was constructed once with default dtype but read as every dtype in the loop — now cloned per-dtype.Tests
Storage.AtDtypeMismatchThrows,Tensor.ItemDtypeMismatchThrows— written red-first against the silent-garbage behavior.StorageTest.AtDtypeMismatchThrowsForAllDtypes— exhaustive 11×11 dtype/T matrix, iterating the sharedTestTools::dtype_list.StorageTest.AtOutOfBoundMessageReportsIndexAndSize— pins the bounds-error message content.Full suite on macOS/AppleClang: 1067 passed; the only failures are the 4 pre-existing
linalg_Test.*_return_err_*SVD-truncate failures tracked in #975 (verified identical without this change).A follow-up branch (in progress) consolidates the 11 copy-pasted
at<T>/back<T>/data<T>specializations into one template body viacy_typeid_v<T>so no future specialization can drop the check again.🤖 Generated with Claude Code