Skip to content

Commit 75dc99b

Browse files
authored
Rollup merge of #122461 - the8472:fix-step-forward-unchecked, r=Amanieu
fix unsoundness in Step::forward_unchecked for signed integers Fixes #122420 ```rust pub fn foo(a: i8, b: u8) -> i8 { unsafe { a.checked_add_unsigned(b).unwrap_unchecked() } } ``` still compiles down to a single arithmetic instruction ([godbolt](https://rust.godbolt.org/z/qsd3xYWfE)). But we may be losing some loop optimizations if llvm can no longer easily derive that it's a finite counted loop from the no-wrapping flags.
2 parents bdf84ea + d3cab9f commit 75dc99b

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

library/core/src/iter/range.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,25 @@ pub trait Step: Clone + PartialOrd + Sized {
183183
}
184184
}
185185

186-
// These are still macro-generated because the integer literals resolve to different types.
187-
macro_rules! step_identical_methods {
186+
// Separate impls for signed ranges because the distance within a signed range can be larger
187+
// than the signed::MAX value. Therefore `as` casting to the signed type would be incorrect.
188+
macro_rules! step_signed_methods {
189+
($unsigned: ty) => {
190+
#[inline]
191+
unsafe fn forward_unchecked(start: Self, n: usize) -> Self {
192+
// SAFETY: the caller has to guarantee that `start + n` doesn't overflow.
193+
unsafe { start.checked_add_unsigned(n as $unsigned).unwrap_unchecked() }
194+
}
195+
196+
#[inline]
197+
unsafe fn backward_unchecked(start: Self, n: usize) -> Self {
198+
// SAFETY: the caller has to guarantee that `start - n` doesn't overflow.
199+
unsafe { start.checked_sub_unsigned(n as $unsigned).unwrap_unchecked() }
200+
}
201+
};
202+
}
203+
204+
macro_rules! step_unsigned_methods {
188205
() => {
189206
#[inline]
190207
unsafe fn forward_unchecked(start: Self, n: usize) -> Self {
@@ -197,7 +214,12 @@ macro_rules! step_identical_methods {
197214
// SAFETY: the caller has to guarantee that `start - n` doesn't overflow.
198215
unsafe { start.unchecked_sub(n as Self) }
199216
}
217+
};
218+
}
200219

220+
// These are still macro-generated because the integer literals resolve to different types.
221+
macro_rules! step_identical_methods {
222+
() => {
201223
#[inline]
202224
#[allow(arithmetic_overflow)]
203225
#[rustc_inherit_overflow_checks]
@@ -238,6 +260,7 @@ macro_rules! step_integer_impls {
238260
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
239261
impl Step for $u_narrower {
240262
step_identical_methods!();
263+
step_unsigned_methods!();
241264

242265
#[inline]
243266
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
@@ -270,6 +293,7 @@ macro_rules! step_integer_impls {
270293
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
271294
impl Step for $i_narrower {
272295
step_identical_methods!();
296+
step_signed_methods!($u_narrower);
273297

274298
#[inline]
275299
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
@@ -334,6 +358,7 @@ macro_rules! step_integer_impls {
334358
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
335359
impl Step for $u_wider {
336360
step_identical_methods!();
361+
step_unsigned_methods!();
337362

338363
#[inline]
339364
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
@@ -359,6 +384,7 @@ macro_rules! step_integer_impls {
359384
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
360385
impl Step for $i_wider {
361386
step_identical_methods!();
387+
step_signed_methods!($u_wider);
362388

363389
#[inline]
364390
fn steps_between(start: &Self, end: &Self) -> Option<usize> {

library/core/tests/iter/range.rs

+5
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,11 @@ fn test_range_advance_by() {
325325
assert_eq!(Ok(()), r.advance_back_by(usize::MAX));
326326

327327
assert_eq!((r.start, r.end), (0u128 + usize::MAX as u128, u128::MAX - usize::MAX as u128));
328+
329+
// issue 122420, Step::forward_unchecked was unsound for signed integers
330+
let mut r = -128i8..127;
331+
assert_eq!(Ok(()), r.advance_by(200));
332+
assert_eq!(r.next(), Some(72));
328333
}
329334

330335
#[test]
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
The loop took around 7s
1+
The loop took around 12s

0 commit comments

Comments
 (0)