Skip to content

Fix variance issues with Mat and MatMut.#806

Merged
hildebrandmw merged 1 commit intomainfrom
mhildebr/variance
Mar 2, 2026
Merged

Fix variance issues with Mat and MatMut.#806
hildebrandmw merged 1 commit intomainfrom
mhildebr/variance

Conversation

@hildebrandmw
Copy link
Contributor

Ensure that Mat and MatMut are invariant in T. Without this change, the two compile-fail tests succeed, which could allow objects with shorter lifetimes to be assigned into a Mat/MatRef and lead to a dangling reference.

This is certainly a corner case since in all our current implementations, the representations T are 'static, but it doesn't hurt to be extra careful here.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in T by adding a PhantomData<fn(T) -> T> field.
  • MatMut<'a, T> is made invariant in T by changing _lifetime from PhantomData<&'a mut [u8]> to PhantomData<&'a mut T>.
  • MatRef<'a, T>'s _lifetime is updated from PhantomData<&'a [u8]> to PhantomData<&'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-commenter
Copy link

Codecov Report

❌ Patch coverage is 25.00000% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.45%. Comparing base (9b2fd20) to head (68f4cf3).

Files with missing lines Patch % Lines
diskann-quantization/src/multi_vector/matrix.rs 25.00% 15 Missing ⚠️

❌ 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

Impacted file tree graph

@@            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     
Flag Coverage Δ
miri 89.45% <25.00%> (-1.21%) ⬇️
unittests 89.30% <25.00%> (-1.34%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
diskann-quantization/src/multi_vector/matrix.rs 95.16% <25.00%> (-2.13%) ⬇️

... and 37 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hildebrandmw hildebrandmw merged commit f206264 into main Mar 2, 2026
28 checks passed
@hildebrandmw hildebrandmw deleted the mhildebr/variance branch March 2, 2026 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants