Skip to content

Teach transmute_{ref,mut}! to handle slice DSTs #2428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

joshlf
Copy link
Member

@joshlf joshlf commented Mar 7, 2025

This requires us to generalize our prior support for transmuting between
unsized types. In particular, we previously used the SizeEq trait to
denote that two types have equal sizes in the face of a cast operation
(in particular, that *const T as *const U preserves referent size). In
this commit, we add support for metadata fix-up, which means that we
support casts for which *const T as *const U does not preserve
referent size. Instead, we compute an affine function at compile time
and apply it at runtime - computing the destination type's metadata as a
function of the source metadata, dst_meta = A + src_meta * B. A and
B are computed at compile time.

We generalize SizeEq to permit its cast_from_raw method to perform a
runtime metadata fix-up operation.

Makes progress on #1817

Co-authored-by: Jack Wrenn jswrenn@amazon.com


This PR is on branch transmute-ref-dst.

@joshlf joshlf force-pushed the Ib4bc62202e0b3b09d155333b525087f7aa8f02c2 branch 6 times, most recently from 829b199 to a7c40db Compare March 8, 2025 16:58
@joshlf joshlf force-pushed the I70d5aa5ace6bd2e39e679eac7f00a66d4b843d57 branch from 4d09d7c to f2ec335 Compare March 8, 2025 16:58
Base automatically changed from I70d5aa5ace6bd2e39e679eac7f00a66d4b843d57 to main March 8, 2025 19:16
@joshlf joshlf force-pushed the Ib4bc62202e0b3b09d155333b525087f7aa8f02c2 branch 3 times, most recently from 98bbab7 to 5196fac Compare March 9, 2025 01:10
@joshlf joshlf force-pushed the Ib4bc62202e0b3b09d155333b525087f7aa8f02c2 branch 2 times, most recently from 398966d to 976b234 Compare March 9, 2025 03:03
@joshlf joshlf changed the base branch from main to I691b42ce8c0c3c6e5990e7684fc66f8f5dd73d85 March 9, 2025 03:03
@joshlf joshlf force-pushed the Ib4bc62202e0b3b09d155333b525087f7aa8f02c2 branch 4 times, most recently from bd3b50a to e972a2c Compare March 9, 2025 06:47
@joshlf joshlf force-pushed the Ib4bc62202e0b3b09d155333b525087f7aa8f02c2 branch 2 times, most recently from e2b7f7d to 459de15 Compare March 11, 2025 18:51
Base automatically changed from I691b42ce8c0c3c6e5990e7684fc66f8f5dd73d85 to main March 11, 2025 19:21
@joshlf joshlf force-pushed the Ib4bc62202e0b3b09d155333b525087f7aa8f02c2 branch from 459de15 to 424ddb4 Compare March 11, 2025 20:19
@joshlf joshlf changed the base branch from main to Icdb256acfdb274f34312cf5b216a02ca426338a1 March 11, 2025 20:19
@joshlf joshlf force-pushed the Ib4bc62202e0b3b09d155333b525087f7aa8f02c2 branch from ac7fd3f to 85cbc73 Compare March 25, 2025 19:17
@joshlf joshlf force-pushed the Ib4bc62202e0b3b09d155333b525087f7aa8f02c2 branch 2 times, most recently from 5ebdc1a to 7d27689 Compare March 25, 2025 19:36
Comment on lines 323 to 325
/// Implementations of `cast_from_raw` must satisfy that method's safety
/// post-condition.
pub unsafe trait SizeCompat<Src: ?Sized> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't need to do this now, but it'd be more idiomatic to name this CastFrom, after its method (and source of safety conditions). That name also conveys directionality, unlike SizeCompat.

@joshlf joshlf force-pushed the Ib4bc62202e0b3b09d155333b525087f7aa8f02c2 branch 2 times, most recently from c5ae106 to 2d585bc Compare March 25, 2025 19:48
jswrenn
jswrenn previously approved these changes Mar 25, 2025
@joshlf joshlf force-pushed the Ib4bc62202e0b3b09d155333b525087f7aa8f02c2 branch 3 times, most recently from 3931fcf to db9d6c0 Compare March 25, 2025 20:11
@codecov-commenter
Copy link

codecov-commenter commented Mar 25, 2025

Codecov Report

Attention: Patch coverage is 72.80453% with 96 lines in your changes missing coverage. Please review.

Project coverage is 89.80%. Comparing base (c2bfb85) to head (454f974).

Files with missing lines Patch % Lines
src/layout.rs 40.29% 40 Missing ⚠️
src/macros.rs 75.49% 25 Missing ⚠️
src/impls.rs 0.00% 14 Missing ⚠️
src/doctests.rs 0.00% 5 Missing ⚠️
src/pointer/transmute.rs 44.44% 5 Missing ⚠️
src/lib.rs 55.55% 4 Missing ⚠️
src/util/macros.rs 91.89% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2428      +/-   ##
==========================================
- Coverage   90.36%   89.80%   -0.57%     
==========================================
  Files          19       20       +1     
  Lines        7808     8059     +251     
==========================================
+ Hits         7056     7237     +181     
- Misses        752      822      +70     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

t
}
}

// TODO: Update all `TransmuteFrom` safety proofs.
Copy link
Member Author

Choose a reason for hiding this comment

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

TODO

@joshlf joshlf force-pushed the Ib4bc62202e0b3b09d155333b525087f7aa8f02c2 branch from db9d6c0 to d170d54 Compare March 25, 2025 20:40
@joshlf joshlf dismissed jswrenn’s stale review March 25, 2025 20:41

Dismissed because we forgot this important TODO: #2428 (comment)

@joshlf joshlf force-pushed the Ib4bc62202e0b3b09d155333b525087f7aa8f02c2 branch from d170d54 to 5d7c23b Compare March 28, 2025 00:11
@joshlf joshlf force-pushed the Ib4bc62202e0b3b09d155333b525087f7aa8f02c2 branch 2 times, most recently from 85b4437 to 522d450 Compare March 28, 2025 19:41
@joshlf joshlf force-pushed the Ib4bc62202e0b3b09d155333b525087f7aa8f02c2 branch 3 times, most recently from fbb37b8 to bb9b06d Compare March 28, 2025 23:31
@joshlf joshlf force-pushed the Ib4bc62202e0b3b09d155333b525087f7aa8f02c2 branch from bb9b06d to 38c910a Compare April 7, 2025 22:33
This requires us to generalize our prior support for transmuting between
unsized types. In particular, we previously used the `SizeEq` trait to
denote that two types have equal sizes in the face of a cast operation
(in particular, that `*const T as *const U` preserves referent size). In
this commit, we add support for metadata fix-up, which means that we
support casts for which `*const T as *const U` does *not* preserve
referent size. Instead, we compute an affine function at compile time
and apply it at runtime - computing the destination type's metadata as a
function of the source metadata, `dst_meta = A + src_meta * B`. `A` and
`B` are computed at compile time.

We generalize `SizeEq` to permit its `cast_from_raw` method to perform a
runtime metadata fix-up operation.

Makes progress on #1817

Co-authored-by: Jack Wrenn <jswrenn@amazon.com>
gherrit-pr-id: Ib4bc62202e0b3b09d155333b525087f7aa8f02c2
@joshlf joshlf force-pushed the Ib4bc62202e0b3b09d155333b525087f7aa8f02c2 branch from 38c910a to 454f974 Compare April 7, 2025 23:35
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.

3 participants