Skip to content
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

Allow transmuting generic pattern types to and from their base #136179

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Jan 28, 2025

Pattern types always have the same size as their base type, so we can just ignore the pattern and look at the base type for figuring out whether transmuting is possible.

@rustbot
Copy link
Collaborator

rustbot commented Jan 28, 2025

r? @BoxyUwU

rustbot has assigned @BoxyUwU.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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 Jan 28, 2025
@BoxyUwU
Copy link
Member

BoxyUwU commented Jan 28, 2025

How does this handle types containing pattern types, e.g.

#![feature(pattern_types, pattern_type_macro)]

use std::pat::pattern_type;

fn foo<const N: u32>() {
    std::mem::transmute::<Option<u32>, Option<pattern_type!(u32 is N..)>>(loop {});
}

Which should not compile as those two options have different sizes when N > 0

@BoxyUwU BoxyUwU 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 Jan 28, 2025
@oli-obk
Copy link
Contributor Author

oli-obk commented Jan 28, 2025

These are still rejected accidentally. So good and bad. I think we can land this because there's no code path that could accidentally accept anything here (we explicitly check for pointers)

@oli-obk oli-obk added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 29, 2025
@BoxyUwU
Copy link
Member

BoxyUwU commented Jan 29, 2025

So I guess basically all the complex SizeSkeleton stuff exists just to handle pointers in transmute checks more cleverly, everything else is just handled by layout_of or simply =='ing the source/target types in the transmute. So maybe the thing that's actually wrong would be:

#![feature(pattern_types, pattern_type_macro)]

use std::pat::pattern_type;

struct Pointee<T>(T);

fn foo<T, U>() {
    std::mem::transmute::<
        Option<*mut Pointee<T>>,
        Option<
            pattern_type!(*mut Pointee<U> is /* nonnull */>
            ),
        >,
    >(loop {});
}

fn main() {}

Which would just be a matter of checking if the pattern type is a nonnull pattern refinement and changing the SizeSkeleton::Pointer to have nonzero set, similar to how the SizeSkeleton for newtype adts will check for layout_scalar_valid_range... 🤔

Correct me if I'm wrong but I don't think you even support pointer refinement in pattern types right now so it seems like kind of moot to try and handle this correctly rn.

@BoxyUwU
Copy link
Member

BoxyUwU commented Jan 29, 2025

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jan 29, 2025

📌 Commit a639e27 has been approved by BoxyUwU

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 29, 2025
@oli-obk
Copy link
Contributor Author

oli-obk commented Jan 29, 2025

Correct me if I'm wrong but I don't think you even support pointer refinement in pattern types right now so it seems like kind of moot to try and handle this correctly rn.

Yes, we'll have to look into that when we get there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

4 participants