Skip to content

Should lossy conversions be compiler warning #2784

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
elichai opened this issue Oct 16, 2019 · 10 comments
Open

Should lossy conversions be compiler warning #2784

elichai opened this issue Oct 16, 2019 · 10 comments
Labels
A-conversions Conversion related proposals & ideas A-lint Proposals relating to lints / warnings / clippy. T-lang Relevant to the language team, which will review and decide on the RFC.

Comments

@elichai
Copy link

elichai commented Oct 16, 2019

Hi,
Should lossy conversions (i.e. let a = 300u16 as u8) be a compile time warning?
that way people can opt in with #![deny(cast_lossy)]

Otherwise maybe a way to make generic that have size constraints?

I'm working on a new library where I'm casting a bunch of stuff to usize and for example usize::from(0u32) isn't a thing. even though it should work on both 32bit and 64bit machines.

So right now I have a bunch of static asserts to check that size_of<usize> => size_of<u32>().

Edit: Related to #2715

@sfackler
Copy link
Member

That would break backwards compatibility.

@elichai
Copy link
Author

elichai commented Oct 16, 2019

I know. :\

@sfackler
Copy link
Member

It sounds like the answer to the question is no then, right?

@elichai
Copy link
Author

elichai commented Oct 16, 2019

I still think it's worth discussing it. maybe there are ways to do something similar without doing a breaking change (like generic constraints on the from function)

@elichai
Copy link
Author

elichai commented Oct 16, 2019

Maybe a compile time warning at least. (the compiler can easily check the sizes of the actual types in compile time, even if it's platform dependent)

Edit: that's actually interesting IMHO. that way it can be an error opt-in with #[deny(cast_lossy)]

@elichai elichai changed the title Should lossy conversions be debug panic Should lossy conversions be compiler warning Oct 17, 2019
@elichai
Copy link
Author

elichai commented Oct 17, 2019

I edited the original post

@sinkuu
Copy link
Contributor

sinkuu commented Oct 17, 2019

Is this clippy lint what you want? https://rust-lang.github.io/rust-clippy/master/#cast_possible_truncation

@elichai
Copy link
Author

elichai commented Oct 17, 2019

A. it doesn't seem to work with all primitives: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4408dede32fead57acb6c9d3f0192073.
B. I want it part of the compiler so I can make sure my code will never compile(or produce a warning in the softer case) in machines where the conversion is lossy (i.e. especially when casting usize or raw pointers where the sizes are different per machine)

@SimonSapin
Copy link
Contributor

My preferred approach would be to add APIs for all remaining combinations of types that as supports but do not have a corresponding API yet (similar to From impls for lossless integer conversions), then deprecate as entirely. (Possibly in a future language edition.)

However there is a lot of design work needed to get those APIs right.

@jonas-schievink jonas-schievink added A-conversions Conversion related proposals & ideas A-lint Proposals relating to lints / warnings / clippy. T-lang Relevant to the language team, which will review and decide on the RFC. labels Nov 15, 2019
@scottmcm
Copy link
Member

While I always support moving people away from as, there needs to be a way to do these things that's not #[allow].

If anyone wishes to make progress on this, I suggest trying to unblock (at least parts of) #2484

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-conversions Conversion related proposals & ideas A-lint Proposals relating to lints / warnings / clippy. T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

6 participants