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

Inherit overflow checks for sum and product #36372

Merged
merged 2 commits into from
Sep 15, 2016

Conversation

sfackler
Copy link
Member

@sfackler sfackler commented Sep 10, 2016

We have previously documented the fact that these will panic on overflow, but I think this behavior is what people actually want/expect. #[rustc_inherit_overflow_checks] didn't exist when we discussed these for stabilization.

r? @alexcrichton

Closes #35807

fn sum<I: Iterator<Item=$a>>(iter: I) -> $a {
iter.fold(0, |a, b| {
a.checked_add(b).expect("overflow in sum")
Add::add(a, b)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not what I meant, although it would work (even without any added rustc_inherit_overflow_checks).
The intention was to replace the closure, i.e. iter.fold(0, Add::add).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I seem to remember that at one point that syntax decayed to function pointers - is that not the case anymore?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That hasn't been the case since some time before 1.0, at the type level, and since March or so this year as far as the low-level representation and optimizations go.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, awesome, updated.

macro_rules! integer_sum_product {
($($a:ident)*) => ($(
#[stable(feature = "iter_arith_traits", since = "1.12.0")]
impl Sum for $a {
#[rustc_inherit_overflow_checks]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure you can remove the attribute now (not that it ever worked, AFAICT).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, yeah, just missed this one.

@alexcrichton
Copy link
Member

Is this something we actually want to guarantee? I thought MIR made it so that we couldn't do this and there's a "weird hack" to get it to work in some circumstances that we want to remove at some point.

@alexcrichton alexcrichton added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Sep 11, 2016
@eddyb
Copy link
Member

eddyb commented Sep 11, 2016

@alexcrichton The only way to remove the hack IMO is to have separate builds of libstd, with and without debug assertions.

Either way the version of sum and product here won't change behavior if such a change occurs.

@alexcrichton
Copy link
Member

@eddyb could you clarify what you mean by "won't change behavior if such a change occurs"?

I was under the impression that we don't want to proliferate the semantics of #[rustc_inherit_overflow_checks] and would instead prefer to direct energy toward multiple builds of std.

@eddyb
Copy link
Member

eddyb commented Sep 12, 2016

@alexcrichton This PR (except for a line that should've been removed) doesn't need the attribute and solely relies on the ops traits (Add and Mul) for builtin types having the right semantics.

If this was a larger patch I'd agree, but I don't see how this one would pose a problem.
Probably better to wait for discussion by the libs team either way.

@alexcrichton
Copy link
Member

Right yeah this'll be sufficient without propagating the attributes themselves, but it propagates the semantics which we're unsure about.

I was personally under the impression that #35807 is already a closed bug in that the semantics are exactly as documented, the iterator adaptors panic on overflow. We can and probably should discuss in @rust-lang/libs though!

@alexcrichton
Copy link
Member

Discussed at libs triage the decision was to merge. @sfackler could you remove the attribute here and I'll merge?

The point which swayed me was that the actual interface to this, cargo build and cargo build --release will not change eventually once we ship debug builds of libstd, so this seems reasonable in the interim.

@sfackler
Copy link
Member Author

Updated

@alexcrichton
Copy link
Member

@bors: r+

@bors
Copy link
Contributor

bors commented Sep 12, 2016

📌 Commit 7bd25a3 has been approved by alexcrichton

@sfackler sfackler added the relnotes Marks issues that should be documented in the release notes of the next release. label Sep 12, 2016
@bors
Copy link
Contributor

bors commented Sep 14, 2016

⌛ Testing commit 7bd25a3 with merge 140fbfc...

@bors
Copy link
Contributor

bors commented Sep 14, 2016

💔 Test failed - auto-win-gnu-32-opt-rustbuild

@alexcrichton
Copy link
Member

@bors: retry

On Tue, Sep 13, 2016 at 7:22 PM, bors notifications@github.com wrote:

💔 Test failed - auto-win-gnu-32-opt-rustbuild
https://buildbot.rust-lang.org/builders/auto-win-gnu-32-opt-rustbuild/builds/2491


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#36372 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAD95JBhVAZ5eU1yNkP0nyWWoIkJJawnks5qp1p6gaJpZM4J5oR4
.

@bors
Copy link
Contributor

bors commented Sep 15, 2016

⌛ Testing commit 7bd25a3 with merge e2c64d1...

bors added a commit that referenced this pull request Sep 15, 2016
Inherit overflow checks for sum and product

We have previously documented the fact that these will panic on overflow, but I think this behavior is what people actually want/expect. `#[rustc_inherit_overflow_checks]` didn't exist when we discussed these for stabilization.

r? @alexcrichton

Closes #35807
@bors bors merged commit 7bd25a3 into rust-lang:master Sep 15, 2016
@sfackler sfackler deleted the sum-prod-overflow branch September 15, 2016 23:45
@cuviper
Copy link
Member

cuviper commented Sep 20, 2016

once we ship debug builds of libstd

BTW, is there a tracking issue for this? It will affect us distro packagers...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes Marks issues that should be documented in the release notes of the next release. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants