Skip to content

Commit 1c39afb

Browse files
committed
Auto merge of rust-lang#109684 - fee1-dead-contrib:rv_const_range, r=Mark-Simulacrum
Revert rust-lang#104100, Allow using `Range` as an `Iterator` in const contexts. This fixes rust-lang#109632.
2 parents 7201301 + 2412f1b commit 1c39afb

14 files changed

+65
-92
lines changed

library/core/src/iter/range.rs

+10-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::convert::TryFrom;
2-
use crate::marker::Destruct;
32
use crate::mem;
43
use crate::num::NonZeroUsize;
54
use crate::ops::{self, Try};
@@ -22,8 +21,7 @@ unsafe_impl_trusted_step![char i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usi
2221
/// The *successor* operation moves towards values that compare greater.
2322
/// The *predecessor* operation moves towards values that compare lesser.
2423
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
25-
#[const_trait]
26-
pub trait Step: ~const Clone + ~const PartialOrd + Sized {
24+
pub trait Step: Clone + PartialOrd + Sized {
2725
/// Returns the number of *successor* steps required to get from `start` to `end`.
2826
///
2927
/// Returns `None` if the number of steps would overflow `usize`
@@ -237,8 +235,7 @@ macro_rules! step_integer_impls {
237235
$(
238236
#[allow(unreachable_patterns)]
239237
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
240-
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
241-
impl const Step for $u_narrower {
238+
impl Step for $u_narrower {
242239
step_identical_methods!();
243240

244241
#[inline]
@@ -270,8 +267,7 @@ macro_rules! step_integer_impls {
270267

271268
#[allow(unreachable_patterns)]
272269
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
273-
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
274-
impl const Step for $i_narrower {
270+
impl Step for $i_narrower {
275271
step_identical_methods!();
276272

277273
#[inline]
@@ -335,8 +331,7 @@ macro_rules! step_integer_impls {
335331
$(
336332
#[allow(unreachable_patterns)]
337333
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
338-
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
339-
impl const Step for $u_wider {
334+
impl Step for $u_wider {
340335
step_identical_methods!();
341336

342337
#[inline]
@@ -361,8 +356,7 @@ macro_rules! step_integer_impls {
361356

362357
#[allow(unreachable_patterns)]
363358
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
364-
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
365-
impl const Step for $i_wider {
359+
impl Step for $i_wider {
366360
step_identical_methods!();
367361

368362
#[inline]
@@ -412,8 +406,7 @@ step_integer_impls! {
412406
}
413407

414408
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
415-
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
416-
impl const Step for char {
409+
impl Step for char {
417410
#[inline]
418411
fn steps_between(&start: &char, &end: &char) -> Option<usize> {
419412
let start = start as u32;
@@ -431,7 +424,6 @@ impl const Step for char {
431424
}
432425

433426
#[inline]
434-
#[rustc_allow_const_fn_unstable(const_try)]
435427
fn forward_checked(start: char, count: usize) -> Option<char> {
436428
let start = start as u32;
437429
let mut res = Step::forward_checked(start, count)?;
@@ -448,7 +440,6 @@ impl const Step for char {
448440
}
449441

450442
#[inline]
451-
#[rustc_allow_const_fn_unstable(const_try)]
452443
fn backward_checked(start: char, count: usize) -> Option<char> {
453444
let start = start as u32;
454445
let mut res = Step::backward_checked(start, count)?;
@@ -524,7 +515,6 @@ macro_rules! range_incl_exact_iter_impl {
524515
}
525516

526517
/// Specialization implementations for `Range`.
527-
#[const_trait]
528518
trait RangeIteratorImpl {
529519
type Item;
530520

@@ -539,7 +529,7 @@ trait RangeIteratorImpl {
539529
fn spec_advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize>;
540530
}
541531

542-
impl<A: ~const Step + ~const Destruct> const RangeIteratorImpl for ops::Range<A> {
532+
impl<A: Step> RangeIteratorImpl for ops::Range<A> {
543533
type Item = A;
544534

545535
#[inline]
@@ -625,7 +615,7 @@ impl<A: ~const Step + ~const Destruct> const RangeIteratorImpl for ops::Range<A>
625615
}
626616
}
627617

628-
impl<T: ~const TrustedStep + ~const Destruct> const RangeIteratorImpl for ops::Range<T> {
618+
impl<T: TrustedStep> RangeIteratorImpl for ops::Range<T> {
629619
#[inline]
630620
fn spec_next(&mut self) -> Option<T> {
631621
if self.start < self.end {
@@ -713,8 +703,7 @@ impl<T: ~const TrustedStep + ~const Destruct> const RangeIteratorImpl for ops::R
713703
}
714704

715705
#[stable(feature = "rust1", since = "1.0.0")]
716-
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
717-
impl<A: ~const Step + ~const Destruct> const Iterator for ops::Range<A> {
706+
impl<A: Step> Iterator for ops::Range<A> {
718707
type Item = A;
719708

720709
#[inline]
@@ -824,8 +813,7 @@ range_incl_exact_iter_impl! {
824813
}
825814

826815
#[stable(feature = "rust1", since = "1.0.0")]
827-
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
828-
impl<A: ~const Step + ~const Destruct> const DoubleEndedIterator for ops::Range<A> {
816+
impl<A: Step> DoubleEndedIterator for ops::Range<A> {
829817
#[inline]
830818
fn next_back(&mut self) -> Option<A> {
831819
self.spec_next_back()

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

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::marker::Destruct;
21
use crate::num::NonZeroUsize;
32
use crate::ops::{ControlFlow, Try};
43

@@ -39,7 +38,6 @@ use crate::ops::{ControlFlow, Try};
3938
/// ```
4039
#[stable(feature = "rust1", since = "1.0.0")]
4140
#[cfg_attr(not(test), rustc_diagnostic_item = "DoubleEndedIterator")]
42-
#[const_trait]
4341
pub trait DoubleEndedIterator: Iterator {
4442
/// Removes and returns an element from the end of the iterator.
4543
///
@@ -136,10 +134,7 @@ pub trait DoubleEndedIterator: Iterator {
136134
/// [`Err(k)`]: Err
137135
#[inline]
138136
#[unstable(feature = "iter_advance_by", reason = "recently added", issue = "77404")]
139-
fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize>
140-
where
141-
Self::Item: ~const Destruct,
142-
{
137+
fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> {
143138
for i in 0..n {
144139
if self.next_back().is_none() {
145140
// SAFETY: `i` is always less than `n`.
@@ -192,7 +187,6 @@ pub trait DoubleEndedIterator: Iterator {
192187
/// ```
193188
#[inline]
194189
#[stable(feature = "iter_nth_back", since = "1.37.0")]
195-
#[rustc_do_not_const_check]
196190
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
197191
if self.advance_back_by(n).is_err() {
198192
return None;
@@ -232,7 +226,6 @@ pub trait DoubleEndedIterator: Iterator {
232226
/// ```
233227
#[inline]
234228
#[stable(feature = "iterator_try_fold", since = "1.27.0")]
235-
#[rustc_do_not_const_check]
236229
fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R
237230
where
238231
Self: Sized,
@@ -304,7 +297,6 @@ pub trait DoubleEndedIterator: Iterator {
304297
#[doc(alias = "foldr")]
305298
#[inline]
306299
#[stable(feature = "iter_rfold", since = "1.27.0")]
307-
#[rustc_do_not_const_check]
308300
fn rfold<B, F>(mut self, init: B, mut f: F) -> B
309301
where
310302
Self: Sized,
@@ -360,7 +352,6 @@ pub trait DoubleEndedIterator: Iterator {
360352
/// ```
361353
#[inline]
362354
#[stable(feature = "iter_rfind", since = "1.27.0")]
363-
#[rustc_do_not_const_check]
364355
fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
365356
where
366357
Self: Sized,

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

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::array;
22
use crate::cmp::{self, Ordering};
3-
use crate::marker::Destruct;
43
use crate::num::NonZeroUsize;
54
use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, Residual, Try};
65

@@ -340,10 +339,8 @@ pub trait Iterator {
340339
/// ```
341340
#[inline]
342341
#[unstable(feature = "iter_advance_by", reason = "recently added", issue = "77404")]
343-
fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize>
344-
where
345-
Self::Item: ~const Destruct,
346-
{
342+
#[rustc_do_not_const_check]
343+
fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> {
347344
for i in 0..n {
348345
if self.next().is_none() {
349346
// SAFETY: `i` is always less than `n`.
@@ -394,10 +391,8 @@ pub trait Iterator {
394391
/// ```
395392
#[inline]
396393
#[stable(feature = "rust1", since = "1.0.0")]
397-
fn nth(&mut self, n: usize) -> Option<Self::Item>
398-
where
399-
Self::Item: ~const Destruct,
400-
{
394+
#[rustc_do_not_const_check]
395+
fn nth(&mut self, n: usize) -> Option<Self::Item> {
401396
self.advance_by(n).ok()?;
402397
self.next()
403398
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,4 @@ pub unsafe trait InPlaceIterable: Iterator {}
8686
/// for details. Consumers are free to rely on the invariants in unsafe code.
8787
#[unstable(feature = "trusted_step", issue = "85731")]
8888
#[rustc_specialization_trait]
89-
#[const_trait]
90-
pub unsafe trait TrustedStep: ~const Step {}
89+
pub unsafe trait TrustedStep: Step {}

library/core/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,9 @@
123123
#![feature(const_index_range_slice_index)]
124124
#![feature(const_inherent_unchecked_arith)]
125125
#![feature(const_int_unchecked_arith)]
126-
#![feature(const_intoiterator_identity)]
127126
#![feature(const_intrinsic_forget)]
128127
#![feature(const_ipv4)]
129128
#![feature(const_ipv6)]
130-
#![feature(const_iter)]
131129
#![feature(const_likely)]
132130
#![feature(const_maybe_uninit_uninit_array)]
133131
#![feature(const_maybe_uninit_as_mut_ptr)]

library/core/tests/iter/consts.rs

-36
This file was deleted.

library/core/tests/iter/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ mod range;
2020
mod sources;
2121
mod traits;
2222

23-
mod consts;
24-
2523
use core::cell::Cell;
2624
use core::convert::TryFrom;
2725
use core::iter::*;

library/core/tests/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@
1212
#![feature(const_caller_location)]
1313
#![feature(const_cell_into_inner)]
1414
#![feature(const_convert)]
15-
#![feature(const_for)]
1615
#![feature(const_hash)]
1716
#![feature(const_heap)]
18-
#![feature(const_intoiterator_identity)]
19-
#![feature(const_iter)]
2017
#![feature(const_maybe_uninit_as_mut_ptr)]
2118
#![feature(const_maybe_uninit_assume_init_read)]
2219
#![feature(const_nonnull_new)]

tests/ui/consts/const-fn-error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const fn f(x: usize) -> usize {
77
//~| ERROR `for` is not allowed in a `const fn`
88
//~| ERROR mutable references are not allowed in constant functions
99
//~| ERROR cannot call non-const fn
10+
//~| ERROR the trait bound
1011
sum += i;
1112
}
1213
sum

tests/ui/consts/const-fn-error.stderr

+16-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | / for i in 0..x {
55
LL | |
66
LL | |
77
LL | |
8-
LL | |
8+
... |
99
LL | | sum += i;
1010
LL | | }
1111
| |_____^
@@ -33,6 +33,19 @@ LL | for i in 0..x {
3333
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
3434
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
3535

36+
error[E0277]: the trait bound `std::ops::Range<usize>: Iterator` is not satisfied
37+
--> $DIR/const-fn-error.rs:5:14
38+
|
39+
LL | for i in 0..x {
40+
| ^^^^ `std::ops::Range<usize>` is not an iterator
41+
|
42+
= help: the trait `~const Iterator` is not implemented for `std::ops::Range<usize>`
43+
note: the trait `Iterator` is implemented for `std::ops::Range<usize>`, but that implementation is not `const`
44+
--> $DIR/const-fn-error.rs:5:14
45+
|
46+
LL | for i in 0..x {
47+
| ^^^^
48+
3649
error[E0015]: cannot call non-const fn `<std::ops::Range<usize> as Iterator>::next` in constant functions
3750
--> $DIR/const-fn-error.rs:5:14
3851
|
@@ -42,7 +55,7 @@ LL | for i in 0..x {
4255
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
4356
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
4457

45-
error: aborting due to 4 previous errors
58+
error: aborting due to 5 previous errors
4659

47-
Some errors have detailed explanations: E0015, E0658.
60+
Some errors have detailed explanations: E0015, E0277, E0658.
4861
For more information about an error, try `rustc --explain E0015`.

tests/ui/consts/const-for.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const _: () = {
55
for _ in 0..5 {}
66
//~^ error: cannot call
77
//~| error: cannot convert
8+
//~| error: the trait bound
89
};
910

1011
fn main() {}

tests/ui/consts/const-for.stderr

+16-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ note: impl defined here, but it is not `const`
99
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
1010
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
1111

12+
error[E0277]: the trait bound `std::ops::Range<i32>: Iterator` is not satisfied
13+
--> $DIR/const-for.rs:5:14
14+
|
15+
LL | for _ in 0..5 {}
16+
| ^^^^ `std::ops::Range<i32>` is not an iterator
17+
|
18+
= help: the trait `~const Iterator` is not implemented for `std::ops::Range<i32>`
19+
note: the trait `Iterator` is implemented for `std::ops::Range<i32>`, but that implementation is not `const`
20+
--> $DIR/const-for.rs:5:14
21+
|
22+
LL | for _ in 0..5 {}
23+
| ^^^^
24+
1225
error[E0015]: cannot call non-const fn `<std::ops::Range<i32> as Iterator>::next` in constants
1326
--> $DIR/const-for.rs:5:14
1427
|
@@ -18,6 +31,7 @@ LL | for _ in 0..5 {}
1831
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
1932
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
2033

21-
error: aborting due to 2 previous errors
34+
error: aborting due to 3 previous errors
2235

23-
For more information about this error, try `rustc --explain E0015`.
36+
Some errors have detailed explanations: E0015, E0277.
37+
For more information about an error, try `rustc --explain E0015`.

tests/ui/typeck/typeck_type_placeholder_item.rs

+1
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,5 @@ fn evens_squared(n: usize) -> _ {
228228

229229
const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x);
230230
//~^ ERROR the trait bound
231+
//~| ERROR the trait bound
231232
//~| ERROR the placeholder

0 commit comments

Comments
 (0)