Fix variance issues with Mat and MatMut.#806
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a variance soundness issue in Mat<T>, MatRef<'a, T>, and MatMut<'a, T> where Mat and MatMut were previously covariant in T due to the bare repr: T value field, allowing unsafe lifetime shrinkage. The fix adds PhantomData markers to enforce correct variance, and adds compile-fail tests to prevent future regression.
Changes:
Mat<T>is made invariant inTby adding aPhantomData<fn(T) -> T>field.MatMut<'a, T>is made invariant inTby changing_lifetimefromPhantomData<&'a mut [u8]>toPhantomData<&'a mut T>.MatRef<'a, T>'s_lifetimeis updated fromPhantomData<&'a [u8]>toPhantomData<&'a T>for semantic clarity (variance is unchanged), and compile-fail + inline covariance assertion tests are added.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
diskann-quantization/src/multi_vector/matrix.rs |
Adds _invariant to Mat, updates _lifetime in MatRef and MatMut, and adds inline covariance assertion functions for MatRef/MatMut in 'a. |
diskann-quantization/tests/compile-fail/multi/mat_invariant.rs |
New compile-fail test asserting that Mat<T> is invariant in T. |
diskann-quantization/tests/compile-fail/multi/mat_invariant.stderr |
Expected compiler output for the mat_invariant compile-fail test. |
diskann-quantization/tests/compile-fail/multi/matmut_invariant.rs |
New compile-fail test asserting that MatMut<'a, T> is invariant in T. |
diskann-quantization/tests/compile-fail/multi/matmut_invariant.stderr |
Expected compiler output for the matmut_invariant compile-fail test. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report❌ Patch coverage is
❌ Your patch status has failed because the patch coverage (25.00%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #806 +/- ##
==========================================
- Coverage 90.66% 89.45% -1.21%
==========================================
Files 432 432
Lines 79433 79452 +19
==========================================
- Hits 72019 71075 -944
- Misses 7414 8377 +963
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Ensure that
MatandMatMutare invariant inT. Without this change, the two compile-fail tests succeed, which could allow objects with shorter lifetimes to be assigned into aMat/MatRefand lead to a dangling reference.This is certainly a corner case since in all our current implementations, the representations
Tare'static, but it doesn't hurt to be extra careful here.