Skip to content

clarify passage on problematic use of transmute #486

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

Conversation

createyourpersonalaccount

No description provided.

@traviscross
Copy link
Contributor

Thanks for the PR.

I kind of see what this is trying to get at, but it would need to say a lot more to make this point correctly. And it may not be worth doing that since the elaboration is already there on the linked pages. So we're going to close this.

@createyourpersonalaccount
Copy link
Author

@traviscross The problem is the verb produces, as it's not always true. Why not be correct in technical documentation? Don't let perfect be the enemy of good.

@traviscross
Copy link
Contributor

Maybe it'd help if you wrote out a code example of the case you're trying to cover here.

@createyourpersonalaccount
Copy link
Author

createyourpersonalaccount commented Apr 15, 2025

@traviscross I have a &mut [MaybeUninit<u8>] that I want to use as a read buffer:

unsafe { transmute::<&mut [MaybeUninit<u8>], &mut [u8]>(buf.mut_slice()) }

Now this value is passed into .read(). As I understand it, there's no explicit lifetime here, it's part of a struct method, so the elision rules apply and it is not an unbounded lifetime. When I read the old passage (that I'm trying to correct) I had to think twice and ask around to make sure that I'm not making a mistake, because indeed "produces" means in all cases, and as far as I could tell, the textualist interpretation says my code is wrong.

@traviscross
Copy link
Contributor

traviscross commented Apr 15, 2025

If you could write out a full example including the call to read and the enclosing function, that'd be helpful, as then I can explain in terms of that.

Please write it in a self-contained way that works in the playground.

@createyourpersonalaccount
Copy link
Author

createyourpersonalaccount commented Apr 15, 2025

@traviscross
Copy link
Contributor

traviscross commented Apr 16, 2025

In your example, the lifetime of that slice is indeed unbounded. To see this, add, e.g.:

        fn is_unbounded<T>(_: T) where for<'a> T: 'a {}
        is_unbounded(slice);

Playground link

That doesn't necessarily mean you're doing anything wrong, but the lifetime there is not bounded.

@createyourpersonalaccount
Copy link
Author

I see what you mean. I tried to think of more involved examples to make my point, but I realized that all my valid examples would end up coercing the lifetime, either implicitly or explicitly, from unbounded to named. So I suppose the wording is correct: transmute will produce unbounded lifetimes if that's the types given to it.

Are you still sure that no clarification is needed? Perhaps one more sentence can be added. Every other bullet point has at least two sentences. "The transmuted value should not be used in contexts where the original lifetime would be rejected." or so?

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.

2 participants