Skip to content

Commit e952db8

Browse files
committed
Auto merge of #81732 - m-ou-se:inherit-overflow-checks, r=Mark-Simulacrum
Use `#[rustc_inherit_overflow_checks]` instead of Add::add etc. See #81721
2 parents 24bfcee + 053769d commit e952db8

File tree

4 files changed

+65
-38
lines changed

4 files changed

+65
-38
lines changed

library/core/src/iter/adapters/enumerate.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::iter::adapters::{zip::try_get_unchecked, SourceIter, TrustedRandomAccess};
22
use crate::iter::{FusedIterator, InPlaceIterable, TrustedLen};
3-
use crate::ops::{Add, AddAssign, Try};
3+
use crate::ops::Try;
44

55
/// An iterator that yields the current count and the element during iteration.
66
///
@@ -39,11 +39,11 @@ where
3939
///
4040
/// Might panic if the index of the element overflows a `usize`.
4141
#[inline]
42+
#[rustc_inherit_overflow_checks]
4243
fn next(&mut self) -> Option<(usize, <I as Iterator>::Item)> {
4344
let a = self.iter.next()?;
4445
let i = self.count;
45-
// Possible undefined overflow.
46-
AddAssign::add_assign(&mut self.count, 1);
46+
self.count += 1;
4747
Some((i, a))
4848
}
4949

@@ -53,11 +53,11 @@ where
5353
}
5454

5555
#[inline]
56+
#[rustc_inherit_overflow_checks]
5657
fn nth(&mut self, n: usize) -> Option<(usize, I::Item)> {
5758
let a = self.iter.nth(n)?;
58-
// Possible undefined overflow.
59-
let i = Add::add(self.count, n);
60-
self.count = Add::add(i, 1);
59+
let i = self.count + n;
60+
self.count = i + 1;
6161
Some((i, a))
6262
}
6363

@@ -78,10 +78,10 @@ where
7878
count: &'a mut usize,
7979
mut fold: impl FnMut(Acc, (usize, T)) -> R + 'a,
8080
) -> impl FnMut(Acc, T) -> R + 'a {
81+
#[rustc_inherit_overflow_checks]
8182
move |acc, item| {
8283
let acc = fold(acc, (*count, item));
83-
// Possible undefined overflow.
84-
AddAssign::add_assign(count, 1);
84+
*count += 1;
8585
acc
8686
}
8787
}
@@ -99,25 +99,26 @@ where
9999
mut count: usize,
100100
mut fold: impl FnMut(Acc, (usize, T)) -> Acc,
101101
) -> impl FnMut(Acc, T) -> Acc {
102+
#[rustc_inherit_overflow_checks]
102103
move |acc, item| {
103104
let acc = fold(acc, (count, item));
104-
// Possible undefined overflow.
105-
AddAssign::add_assign(&mut count, 1);
105+
count += 1;
106106
acc
107107
}
108108
}
109109

110110
self.iter.fold(init, enumerate(self.count, fold))
111111
}
112112

113+
#[rustc_inherit_overflow_checks]
113114
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> <Self as Iterator>::Item
114115
where
115116
Self: TrustedRandomAccess,
116117
{
117118
// SAFETY: the caller must uphold the contract for
118119
// `Iterator::__iterator_get_unchecked`.
119120
let value = unsafe { try_get_unchecked(&mut self.iter, idx) };
120-
(Add::add(self.count, idx), value)
121+
(self.count + idx, value)
121122
}
122123
}
123124

library/core/src/iter/range.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::char;
22
use crate::convert::TryFrom;
33
use crate::mem;
4-
use crate::ops::{self, Add, Sub, Try};
4+
use crate::ops::{self, Try};
55

66
use super::{FusedIterator, TrustedLen};
77

@@ -201,23 +201,25 @@ macro_rules! step_identical_methods {
201201

202202
#[inline]
203203
#[allow(arithmetic_overflow)]
204+
#[rustc_inherit_overflow_checks]
204205
fn forward(start: Self, n: usize) -> Self {
205206
// In debug builds, trigger a panic on overflow.
206207
// This should optimize completely out in release builds.
207208
if Self::forward_checked(start, n).is_none() {
208-
let _ = Add::add(Self::MAX, 1);
209+
let _ = Self::MAX + 1;
209210
}
210211
// Do wrapping math to allow e.g. `Step::forward(-128i8, 255)`.
211212
start.wrapping_add(n as Self)
212213
}
213214

214215
#[inline]
215216
#[allow(arithmetic_overflow)]
217+
#[rustc_inherit_overflow_checks]
216218
fn backward(start: Self, n: usize) -> Self {
217219
// In debug builds, trigger a panic on overflow.
218220
// This should optimize completely out in release builds.
219221
if Self::backward_checked(start, n).is_none() {
220-
let _ = Sub::sub(Self::MIN, 1);
222+
let _ = Self::MIN - 1;
221223
}
222224
// Do wrapping math to allow e.g. `Step::backward(127i8, 255)`.
223225
start.wrapping_sub(n as Self)

library/core/src/iter/traits/accum.rs

+40-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::iter;
22
use crate::num::Wrapping;
3-
use crate::ops::{Add, Mul};
43

54
/// Trait to represent types that can be created by summing up an iterator.
65
///
@@ -37,34 +36,49 @@ pub trait Product<A = Self>: Sized {
3736
fn product<I: Iterator<Item = A>>(iter: I) -> Self;
3837
}
3938

40-
// N.B., explicitly use Add and Mul here to inherit overflow checks
4139
macro_rules! integer_sum_product {
4240
(@impls $zero:expr, $one:expr, #[$attr:meta], $($a:ty)*) => ($(
4341
#[$attr]
4442
impl Sum for $a {
4543
fn sum<I: Iterator<Item=Self>>(iter: I) -> Self {
46-
iter.fold($zero, Add::add)
44+
iter.fold(
45+
$zero,
46+
#[rustc_inherit_overflow_checks]
47+
|a, b| a + b,
48+
)
4749
}
4850
}
4951

5052
#[$attr]
5153
impl Product for $a {
5254
fn product<I: Iterator<Item=Self>>(iter: I) -> Self {
53-
iter.fold($one, Mul::mul)
55+
iter.fold(
56+
$one,
57+
#[rustc_inherit_overflow_checks]
58+
|a, b| a * b,
59+
)
5460
}
5561
}
5662

5763
#[$attr]
5864
impl<'a> Sum<&'a $a> for $a {
5965
fn sum<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
60-
iter.fold($zero, Add::add)
66+
iter.fold(
67+
$zero,
68+
#[rustc_inherit_overflow_checks]
69+
|a, b| a + b,
70+
)
6171
}
6272
}
6373

6474
#[$attr]
6575
impl<'a> Product<&'a $a> for $a {
6676
fn product<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
67-
iter.fold($one, Mul::mul)
77+
iter.fold(
78+
$one,
79+
#[rustc_inherit_overflow_checks]
80+
|a, b| a * b,
81+
)
6882
}
6983
}
7084
)*);
@@ -83,28 +97,44 @@ macro_rules! float_sum_product {
8397
#[stable(feature = "iter_arith_traits", since = "1.12.0")]
8498
impl Sum for $a {
8599
fn sum<I: Iterator<Item=Self>>(iter: I) -> Self {
86-
iter.fold(0.0, Add::add)
100+
iter.fold(
101+
0.0,
102+
#[rustc_inherit_overflow_checks]
103+
|a, b| a + b,
104+
)
87105
}
88106
}
89107

90108
#[stable(feature = "iter_arith_traits", since = "1.12.0")]
91109
impl Product for $a {
92110
fn product<I: Iterator<Item=Self>>(iter: I) -> Self {
93-
iter.fold(1.0, Mul::mul)
111+
iter.fold(
112+
1.0,
113+
#[rustc_inherit_overflow_checks]
114+
|a, b| a * b,
115+
)
94116
}
95117
}
96118

97119
#[stable(feature = "iter_arith_traits", since = "1.12.0")]
98120
impl<'a> Sum<&'a $a> for $a {
99121
fn sum<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
100-
iter.fold(0.0, Add::add)
122+
iter.fold(
123+
0.0,
124+
#[rustc_inherit_overflow_checks]
125+
|a, b| a + b,
126+
)
101127
}
102128
}
103129

104130
#[stable(feature = "iter_arith_traits", since = "1.12.0")]
105131
impl<'a> Product<&'a $a> for $a {
106132
fn product<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
107-
iter.fold(1.0, Mul::mul)
133+
iter.fold(
134+
1.0,
135+
#[rustc_inherit_overflow_checks]
136+
|a, b| a * b,
137+
)
108138
}
109139
}
110140
)*)

library/core/src/iter/traits/iterator.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// can't split that into multiple files.
44

55
use crate::cmp::{self, Ordering};
6-
use crate::ops::{Add, ControlFlow, Try};
6+
use crate::ops::{ControlFlow, Try};
77

88
use super::super::TrustedRandomAccess;
99
use super::super::{Chain, Cloned, Copied, Cycle, Enumerate, Filter, FilterMap, Fuse};
@@ -242,13 +242,11 @@ pub trait Iterator {
242242
where
243243
Self: Sized,
244244
{
245-
#[inline]
246-
fn add1<T>(count: usize, _: T) -> usize {
247-
// Might overflow.
248-
Add::add(count, 1)
249-
}
250-
251-
self.fold(0, add1)
245+
self.fold(
246+
0,
247+
#[rustc_inherit_overflow_checks]
248+
|count, _| count + 1,
249+
)
252250
}
253251

254252
/// Consumes the iterator, returning the last element.
@@ -2475,13 +2473,9 @@ pub trait Iterator {
24752473
fn check<T>(
24762474
mut predicate: impl FnMut(T) -> bool,
24772475
) -> impl FnMut(usize, T) -> ControlFlow<usize, usize> {
2478-
// The addition might panic on overflow
2476+
#[rustc_inherit_overflow_checks]
24792477
move |i, x| {
2480-
if predicate(x) {
2481-
ControlFlow::Break(i)
2482-
} else {
2483-
ControlFlow::Continue(Add::add(i, 1))
2484-
}
2478+
if predicate(x) { ControlFlow::Break(i) } else { ControlFlow::Continue(i + 1) }
24852479
}
24862480
}
24872481

0 commit comments

Comments
 (0)