Skip to content

dont handle bool transmute #140431

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: master
Choose a base branch
from

Conversation

bend-n
Copy link
Contributor

@bend-n bend-n commented Apr 29, 2025

removes transmute(u8) -> bool suggestion due to ambiguity, leave it for clippy

elaboration on ambiguity in question:
transmute::<u8, bool>(x) will codegen to an assume(u8 < 2);
_ == 1 or _ != 0 or _ % 2 == 0 would remove that assumption
match _ { x @ (0 | 1) => x == 1, _ => std::hint::unreachable_unchecked() } is very verbose

@rustbot label L-unnecessary_transmutes

@rustbot
Copy link
Collaborator

rustbot commented Apr 29, 2025

r? @Nadrieril

rustbot has assigned @Nadrieril.
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 Apr 29, 2025
@rustbot
Copy link
Collaborator

rustbot commented Apr 29, 2025

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot
Copy link
Collaborator

rustbot commented Apr 29, 2025

Unknown labels: L-unnecessary-transmute

@rustbot
Copy link
Collaborator

rustbot commented Apr 29, 2025

Unknown labels: L-unnecessary-transmutes

@rustbot rustbot added the L-unnecessary_transmutes Lint: unnecessary_transmutes label Apr 29, 2025
@oli-obk
Copy link
Contributor

oli-obk commented Apr 29, 2025

Is there context/discussions that led to this change?

@bend-n
Copy link
Contributor Author

bend-n commented Apr 29, 2025

@Nadrieril
Copy link
Member

I don't know how we decide on this sort of thing.

I personally expect that this suggestion is useful, I can see myself thinking in terms of bytes and forgetting that there's a safe way to do this conversion. At the same time if I'm really doing byte-conversion shenanigans, writing _ == 1 doesn't correctly express my intent.

I'd personally err on the beginner-friendlier side, I've seen colleagues reach for transmute way too happily.

@bend-n
Copy link
Contributor Author

bend-n commented Apr 29, 2025

well this already exists in clippy, as transmute_num_to_bool

@Nadrieril
Copy link
Member

ok, that convinces me. very unsure on what basis I should decide whether to merge tho; @oli-obk do you know the etiquette? also wdyt of this?

@asquared31415
Copy link
Contributor

Personally i think that when you're trying to give the compiler more information to optimize with, you should expect to have to write a little more or silence a warning because you're effectively saying "no, i really mean to do this, compiler." I don't know whether this should be demoted to only being in clippy, as the suggested change produces something with identical semantics and no possibility of UB on misuse.

@oli-obk
Copy link
Contributor

oli-obk commented Apr 30, 2025

I'm really doing byte-conversion shenanigans, writing _ == 1 doesn't correctly express my intent.

The alternative is UB, so I don't see why we'd remove this lint. As asquared said, when you want this opt hint, use expect and state why you consider this sound

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
L-unnecessary_transmutes Lint: unnecessary_transmutes 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.
5 participants