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

Negative numbers in types like-uint #5477

Closed
tredoe opened this Issue Mar 21, 2013 · 20 comments

Comments

Projects
None yet
@tredoe
Copy link

tredoe commented Mar 21, 2013

The compiler should show a warning message when is used a negative number into a like-uint type

let n: u8 = -1;
println!("{}", n); // => 255
@jdm

This comment has been minimized.

Copy link
Contributor

jdm commented Mar 21, 2013

Probably related to #2438.

@recrack

This comment has been minimized.

Copy link
Contributor

recrack commented May 7, 2013

Should this be done in typeck, or is it best done in lint?

@Aatch

This comment has been minimized.

Copy link
Contributor

Aatch commented Jun 7, 2013

Nominating for production-ready.

@msullivan

This comment has been minimized.

Copy link
Contributor

msullivan commented Jul 26, 2013

Yeah, I agree with that nomination.

@cmr

This comment has been minimized.

Copy link
Member

cmr commented Aug 8, 2013

This should really be a lint I think

@graydon

This comment has been minimized.

Copy link
Contributor

graydon commented Aug 8, 2013

Relative of #4220 and #5551 so probably milestone #1.

@graydon

This comment has been minimized.

Copy link
Contributor

graydon commented Aug 8, 2013

accepted for well-defined milestone

@pcwalton

This comment has been minimized.

Copy link
Contributor

pcwalton commented Nov 22, 2013

Nominating because I believe we decided to not do this for negative numbers and the warning is already implemented for positive numbers. I think this can be closed.

@cmr

This comment has been minimized.

Copy link
Member

cmr commented Jan 19, 2014

I think we should have a lint for bare -1 when it infers to an unsigned type, but -1u should be allowed. Generally I think the more-obvious construction !0, !0 - 1 etc should be used instead of negative literals.

@fhahn

This comment has been minimized.

Copy link
Contributor

fhahn commented Jan 19, 2014

Could this lint be combined with the type_overflow lint? The message of that lint ("literal out of range for its type") would fit in my opinion and and it shouldn't be too hard to extend the code of this lint.

@cmr

This comment has been minimized.

Copy link
Member

cmr commented Jul 3, 2014

I think at this point we've gone whole-hog on accepting that not warning on this is a feature. Nominating for closure.

@cmr cmr added the I-nominated label Jul 3, 2014

@pnkfelix

This comment has been minimized.

Copy link
Member

pnkfelix commented Jul 3, 2014

Lints are not a language backwards-compatibility issue.

Recategorizing as P-low, not 1.0 milestone.

@mbrubeck

This comment has been minimized.

Copy link
Contributor

mbrubeck commented Feb 26, 2015

PistonDevelopers/image#305 is an example of a real-world bug that would have been caught by this warning. That bug involved code like std::iter::range_step(len, 0, -1) where len is unsigned.

@nagisa

This comment has been minimized.

Copy link
Contributor

nagisa commented Feb 28, 2015

@pnkfelix

This comment has been minimized.

Copy link
Member

pnkfelix commented Apr 2, 2015

see #23945; we're going further than a lint and moving to a feature-gate at this point.

The goals are two-fold:

  1. Removing unary-negation on unsigned integers does seem more consistent with our current overflow story, and
  2. if the community does strongly want -expr to work on unsigned integers, then this is a way to force the community to tell us. (Perhaps using stronger language than we might otherwise want, we shall see...)
@SiegeLord

This comment has been minimized.

Copy link
Contributor

SiegeLord commented Jul 13, 2015

Code like this compiles and runs without error in stable rustc today:

let a = -1; // 'a' gets inferred to u32, but the code compiles without error
let b: u32 = a;

Is the intention to stop that code from compiling or issue a runtime error?

@nagisa

This comment has been minimized.

Copy link
Contributor

nagisa commented Jul 13, 2015

Yes, although that would be somewhat of a breaking change.
On Jul 13, 2015 9:08 AM, "SiegeLord" notifications@github.com wrote:

Code like this compiles and runs without error in stable rustc today:

let a = -1; // A gets inferred to u32, but the code compiles without error
let b: u32 = a;

Is the intention to stop that code from compiling or issue a runtime error?


Reply to this email directly or view it on GitHub
#5477 (comment).

@pnkfelix

This comment has been minimized.

Copy link
Member

pnkfelix commented Jul 13, 2015

The intention was to force examples like above comment to be feature-gated (and thus you'd have to opt-in to be able to write them). But it seems like the feature-gating code in #23945 did not catch this case (or it has regressed since then, which seems less likely to me...)

@rnestler

This comment has been minimized.

Copy link
Contributor

rnestler commented Oct 27, 2015

@SiegeLord

let a = -1; // 'a' gets inferred to u32, but the code compiles without error
let b: u32 = a;

Gives a warning in stable and fails to compile in nightly for me on play.rust-lang.org

@steveklabnik

This comment has been minimized.

Copy link
Member

steveklabnik commented Oct 27, 2015

Yes, I would consider this fixed. The example is now fixed, it's behind a feature gate, and so when/if the gate is stabilized or removed, this will end up being discussed, so I think all of the concerns in this ticket are taken care of. Let me know if that's wrong!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.