Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upclarify extreme operator behaviour #1237
Conversation
eddyb
reviewed
Aug 5, 2015
| enabled this will panic. When checking is disabled this will two's complement | ||
| wrap. | ||
| - The operations `/`, `%` are nonsensical for the arguments `INT_MIN` and `-1`. | ||
| When this occurs there is an unconditional panic. |
This comment has been minimized.
This comment has been minimized.
eddyb
Aug 5, 2015
Member
Maybe division and remainder by 0 are "nonsensical", but INT_MIN / -1 is an overflow, the same way -INT_MIN is (which isn't caught, not even in a debug build).
This comment has been minimized.
This comment has been minimized.
nagisa
Aug 5, 2015
Contributor
rust-lang/rust#24500 and rust-lang/rust#23154 and rust-lang/rust#22020 all seem to involve unary negation checking for overflow, but in practice this seems to be not working indeed in the context of const-eval.
This comment has been minimized.
This comment has been minimized.
eddyb
Aug 5, 2015
Member
-std::i32::MIN does give a const-eval error, but let m = std::i32::MIN; -m doesn't (so there's no runtime checking).
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
eddyb
Aug 5, 2015
Member
Oops, I used playbot - does it run in release mode?
[17:13] <eddyb> playbot: let x = std::i32::MIN; -x
[17:13] [Notice] -playbot to #rust-internals- -2147483648
This comment has been minimized.
This comment has been minimized.
nikomatsakis
Aug 7, 2015
Contributor
Regarding -INT_MIN and INT_MIN/-1 -- for whatever reason, division had code to check and panic, but multiplication did two's complement wrapping. We have preserved that behavior, afaik, inconsistent or not, and hence these edits correctly describe the current situation.
eddyb
reviewed
Aug 5, 2015
| - Shift operations (`<<`, `>>`) can shift a value of width `N` by more | ||
| than `N` bits. | ||
| than `N` bits. This is prevented by unconditionally masking the bits |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
nikomatsakis
Aug 7, 2015
Contributor
What is "this", that is prevented here?
The undefined behavior that results from an overlong shift, presumably.
This comment has been minimized.
This comment has been minimized.
eddyb
Aug 7, 2015
Member
Yes, that's what I assume it means, but there's no description of that undefined behavior which "this" could refer to.
This comment has been minimized.
This comment has been minimized.
Gankro
Aug 7, 2015
Author
Contributor
Strictly speaking at this level Undefined Behaviour in LLVM in not an object -- this is Rust's semantics. Will clarify.
nagisa
reviewed
Aug 5, 2015
| overflow. | ||
| - The operations `+`, `-`, `*`, can underflow and overflow. When checking is | ||
| enabled this will panic. When checking is disabled this will two's complement | ||
| wrap. |
This comment has been minimized.
This comment has been minimized.
nagisa
Aug 5, 2015
Contributor
Do we guarantee this for platforms where arithmetic is not two’s complement?
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Gankro
Aug 5, 2015
Author
Contributor
Yeah. We also don't acknowledged floating point rounding modes for similar reasons.
This comment has been minimized.
This comment has been minimized.
oli-obk
Aug 6, 2015
Contributor
actually this is currently only the behavior when the operation cannot be constant evaluated. Even with checking disabled, const evaluatable cases will cause a compiler error. see #1229 for discussion
nrc
added
the
T-lang
label
Aug 6, 2015
nikomatsakis
self-assigned this
Aug 6, 2015
This comment has been minimized.
This comment has been minimized.
|
Clarified. |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
commented on text/0560-integer-overflow.md in 32ed8d4
Aug 7, 2015
|
seems fine, but it may be worth nothing that this is the behavior that the X86 does (and Java, as well, not coincidentally I think) |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
replied
Aug 7, 2015
|
(or at least a logical extension of it) |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
commented on text/0560-integer-overflow.md in 32ed8d4
Aug 7, 2015
|
Similarly, the reason here is that LLVM's |
This comment has been minimized.
This comment has been minimized.
|
As @bill-myers originally pointed out here |
oli-obk
referenced this pull request
Aug 18, 2015
Merged
turn statically known erroneous code into a warning and continue normal code-generation #1229
This comment has been minimized.
This comment has been minimized.
|
Hear ye, hear ye. This RFC is entering final comment period. |
This comment has been minimized.
This comment has been minimized.
|
@glaebhoerl true. I guess we could consider altering the behavior, but I think this RFC is basically aiming at documenting existing behavior, and that particular result is long-standing. |
This comment has been minimized.
This comment has been minimized.
|
@nikomatsakis Then shall I open a separate issue to keep track of that possibility? |
This comment has been minimized.
This comment has been minimized.
|
On Sat, Sep 05, 2015 at 01:53:14AM -0700, Gábor Lehel wrote:
Sure. |
glaebhoerl
referenced this pull request
Sep 9, 2015
Open
Should `INT_MIN % -1` return 0 instead of panicking? #1276
nikomatsakis
added
the
final-comment-period
label
Sep 17, 2015
This comment has been minimized.
This comment has been minimized.
|
Huzzah! The language design team has decided to accept this RFC. |
Gankro commentedAug 4, 2015
This is just clarifying things which were agreed on in various places but poorly specified.
rendered draft