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

usize downcast issues #34150

Closed
est31 opened this issue Jun 7, 2016 · 4 comments
Closed

usize downcast issues #34150

est31 opened this issue Jun 7, 2016 · 4 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@est31
Copy link
Member

est31 commented Jun 7, 2016

The PR #33460 added something, which is good, but it also introduces problems with library authors who want to provide support (or don't want to provide support) for those platforms.

In my code I have added a macro:

macro_rules! convert_to_usize {
( $val:expr, $val_type:ident ) => { {
    let converted :usize = $val as usize;
    if $val != converted as $val_type {
        try!(Err(LibraryError::BufferNotAddressable));
    }
    converted
}}
}

Its very useful and nice.

Right now I only use it when the outcome of a calculation could theoretically be larger than the 32 bit address space, when converting u64 values to usize, as rust up to now only supported platforms with at least 32 bits.

Now it seems I'll have to add it to u32 -> usize conversions as well.

In order for me to do the transition some #![forbid(downcast_to_usize)] like feature would be nice, otherwise I'd miss something.

Also that feature would help library authors who wish to not support 16 bit architectures (due to the additional maintenance burden because now they had to watch every usize downcast) to simply add such a #![forbid(downcast_to_usize)] to their code, and it stops from compiling.

Its always better to have code not compile than having to explain things in the docs.

@taralx
Copy link
Contributor

taralx commented Jun 7, 2016

Honestly, a lint for truncating casts is probably worth having. I really wish there were different syntax for safe vs truncating casts.

@durka
Copy link
Contributor

durka commented Jun 8, 2016

@taralx clippy has a family of casting lints.

@taralx
Copy link
Contributor

taralx commented Jun 8, 2016

Clippy requires nightly. :-(

@Mark-Simulacrum Mark-Simulacrum added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label May 4, 2017
@Mark-Simulacrum
Copy link
Member

New lints require an RFC; please go through the process described here: https://github.com/rust-lang/rfcs#before-creating-an-rfc if you'd like to propose adding a truncation lint to Rust itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

4 participants