Skip to content

Conversation

@joshlf
Copy link
Contributor

@joshlf joshlf commented Nov 3, 2025

This commit moves us closer to supporting NonZero types, but does not get us all the way there.

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
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
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
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
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
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.

// types exist in the standard library which also
// use
// `#[rustc_layout_scalar_valid_range_(start|end)]`.
if let [field] = &variant.fields.as_slice().raw {
Copy link
Member

Choose a reason for hiding this comment

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

You can de-indent a bunch of this if you let-else:

Suggested change
if let [field] = &variant.fields.as_slice().raw {
let [field] = &variant.fields.as_slice().raw else { return Err(Err::NotYetSupported) };

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@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
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.

if hi_val == max_value
&& let ty::Uint(_) = *field_ty.kind()
{
Ok(Self::nonzero(field_size.bytes()))
Copy link
Member

Choose a reason for hiding this comment

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

The Tree returned here should be a Seq of three elements:

  • a Tree::Def(Def::Adt(..)) for the struct definition
  • a Tree::Def(Def::Field(..)) for the field definition
  • the Self::nonzero for the field layout

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@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
This commit moves us closer to supporting `NonZero` types, but does not
get us all the way there.
@joshlf joshlf force-pushed the transmute-nonzero branch from fb550be to c9ec73b Compare November 3, 2025 19:25
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants