Skip to content

transmutability: model NonZero scalar ranges#148449

Open
joshlf wants to merge 1 commit into
rust-lang:mainfrom
joshlf:transmute-nonzero
Open

transmutability: model NonZero scalar ranges#148449
joshlf wants to merge 1 commit into
rust-lang:mainfrom
joshlf:transmute-nonzero

Conversation

@joshlf

@joshlf joshlf commented Nov 3, 2025

Copy link
Copy Markdown
Contributor

Teach rustc_transmute to model scalar validity ranges for transparent
std NonZero wrappers, including integer NonZero types and NonZero.
Also handle scalar-backed ADT layouts whose fields are represented with
FieldsShape::Primitive.

Thread the proving body through transmutability obligations so the trait
solver can evaluate destination ADT, variant, and field safety
invariants relative to the caller module. This keeps private fields and
unsafe fields caller-sensitive in both old and next solver paths.

Add UI coverage for std NonZero integers, NonZero, option/array/
tuple compositions, same-width signed/unsigned conversions, reference
safety invariants, and field visibility barriers.

Tracking issue: #99571

r? @jswrenn

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 3, 2025

pub fn is_transmutable<Src, Dst>()
where
Dst: TransmuteFrom<Src, { Assume::NOTHING }>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You probably want to Assume::SAFETY here, otherwise you're just getting sniped by field privacy.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Doesn't look like it – see the .stderr output.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Well that's not good; indicates we need to thread in the field visibility.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done – added a test to make sure we don't regress this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

While you're poking around here, can you check that we didn't regress this on regular structs too? I think we might've.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment thread compiler/rustc_transmute/src/layout/tree.rs Outdated
@joshlf joshlf force-pushed the transmute-nonzero branch from 834cd59 to fb550be Compare November 3, 2025 17:51

pub fn is_transmutable<Src, Dst>()
where
Dst: TransmuteFrom<Src, { Assume::NOTHING }>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Well that's not good; indicates we need to thread in the field visibility.

Comment thread compiler/rustc_transmute/src/layout/tree.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 3, 2025
@joshlf joshlf force-pushed the transmute-nonzero branch from fb550be to c9ec73b Compare November 3, 2025 19:25
@joshlf

joshlf commented Nov 5, 2025

Copy link
Copy Markdown
Contributor Author

@jswrenn While we're here, I've been experimenting with what it'd take to get this to the finish line (wiring things up so that the real std::num::NonZero types work). Here's where I've gotten so far. It seems that the compiler represents core::num::nonzero::ZeroablePrimitive::NonZeroInner as being an alias straight to the inner type (e.g. u8) without indirecting via the struct that has the scalar range annotation (e.g. NonZeroU8Inner).

@rust-bors

This comment has been minimized.

@joshlf joshlf force-pushed the transmute-nonzero branch from c9ec73b to 54f6002 Compare June 5, 2026 19:28
@rustbot

rustbot commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to the core trait solver

cc @rust-lang/initiative-trait-system-refactor

@rustbot rustbot added the WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) label Jun 5, 2026
@rustbot

rustbot commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rustbot

This comment has been minimized.

@joshlf joshlf force-pushed the transmute-nonzero branch from 54f6002 to 67f9776 Compare June 5, 2026 19:42
@joshlf

joshlf commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

Updated this PR to support NonZero types.

@rust-log-analyzer

This comment has been minimized.

@joshlf joshlf force-pushed the transmute-nonzero branch from 67f9776 to 0b2b704 Compare June 5, 2026 21:53
@joshlf joshlf changed the title transmutability: Support types with restricted validity ranges transmutability: model NonZero scalar ranges Jun 5, 2026
@rust-log-analyzer

This comment has been minimized.

Teach rustc_transmute to model scalar validity ranges for transparent
std NonZero wrappers, including integer NonZero types and NonZero<char>.
Also handle scalar-backed ADT layouts whose fields are represented with
FieldsShape::Primitive.

Thread the proving body through transmutability obligations so the trait
solver can evaluate destination ADT, variant, and field safety
invariants relative to the caller module. This keeps private fields and
unsafe fields caller-sensitive in both old and next solver paths.

Add UI coverage for std NonZero integers, NonZero<char>, option/array/
tuple compositions, same-width signed/unsigned conversions, reference
safety invariants, and field visibility barriers.
@joshlf joshlf force-pushed the transmute-nonzero branch from 0b2b704 to e499678 Compare June 5, 2026 23:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants