1
Fork 0

Auto merge of #109684 - fee1-dead-contrib:rv_const_range, r=Mark-Simulacrum

Revert #104100, Allow using `Range` as an `Iterator` in const contexts.

This fixes #109632.
This commit is contained in:
bors 2023-04-09 15:42:27 +00:00
commit 1c39afb375
14 changed files with 65 additions and 92 deletions

View file

@ -1,5 +1,4 @@
use crate::convert::TryFrom; use crate::convert::TryFrom;
use crate::marker::Destruct;
use crate::mem; use crate::mem;
use crate::num::NonZeroUsize; use crate::num::NonZeroUsize;
use crate::ops::{self, Try}; 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
/// The *successor* operation moves towards values that compare greater. /// The *successor* operation moves towards values that compare greater.
/// The *predecessor* operation moves towards values that compare lesser. /// The *predecessor* operation moves towards values that compare lesser.
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")] #[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
#[const_trait] pub trait Step: Clone + PartialOrd + Sized {
pub trait Step: ~const Clone + ~const PartialOrd + Sized {
/// Returns the number of *successor* steps required to get from `start` to `end`. /// Returns the number of *successor* steps required to get from `start` to `end`.
/// ///
/// Returns `None` if the number of steps would overflow `usize` /// Returns `None` if the number of steps would overflow `usize`
@ -237,8 +235,7 @@ macro_rules! step_integer_impls {
$( $(
#[allow(unreachable_patterns)] #[allow(unreachable_patterns)]
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")] #[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
#[rustc_const_unstable(feature = "const_iter", issue = "92476")] impl Step for $u_narrower {
impl const Step for $u_narrower {
step_identical_methods!(); step_identical_methods!();
#[inline] #[inline]
@ -270,8 +267,7 @@ macro_rules! step_integer_impls {
#[allow(unreachable_patterns)] #[allow(unreachable_patterns)]
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")] #[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
#[rustc_const_unstable(feature = "const_iter", issue = "92476")] impl Step for $i_narrower {
impl const Step for $i_narrower {
step_identical_methods!(); step_identical_methods!();
#[inline] #[inline]
@ -335,8 +331,7 @@ macro_rules! step_integer_impls {
$( $(
#[allow(unreachable_patterns)] #[allow(unreachable_patterns)]
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")] #[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
#[rustc_const_unstable(feature = "const_iter", issue = "92476")] impl Step for $u_wider {
impl const Step for $u_wider {
step_identical_methods!(); step_identical_methods!();
#[inline] #[inline]
@ -361,8 +356,7 @@ macro_rules! step_integer_impls {
#[allow(unreachable_patterns)] #[allow(unreachable_patterns)]
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")] #[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
#[rustc_const_unstable(feature = "const_iter", issue = "92476")] impl Step for $i_wider {
impl const Step for $i_wider {
step_identical_methods!(); step_identical_methods!();
#[inline] #[inline]
@ -412,8 +406,7 @@ step_integer_impls! {
} }
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")] #[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
#[rustc_const_unstable(feature = "const_iter", issue = "92476")] impl Step for char {
impl const Step for char {
#[inline] #[inline]
fn steps_between(&start: &char, &end: &char) -> Option<usize> { fn steps_between(&start: &char, &end: &char) -> Option<usize> {
let start = start as u32; let start = start as u32;
@ -431,7 +424,6 @@ impl const Step for char {
} }
#[inline] #[inline]
#[rustc_allow_const_fn_unstable(const_try)]
fn forward_checked(start: char, count: usize) -> Option<char> { fn forward_checked(start: char, count: usize) -> Option<char> {
let start = start as u32; let start = start as u32;
let mut res = Step::forward_checked(start, count)?; let mut res = Step::forward_checked(start, count)?;
@ -448,7 +440,6 @@ impl const Step for char {
} }
#[inline] #[inline]
#[rustc_allow_const_fn_unstable(const_try)]
fn backward_checked(start: char, count: usize) -> Option<char> { fn backward_checked(start: char, count: usize) -> Option<char> {
let start = start as u32; let start = start as u32;
let mut res = Step::backward_checked(start, count)?; let mut res = Step::backward_checked(start, count)?;
@ -524,7 +515,6 @@ macro_rules! range_incl_exact_iter_impl {
} }
/// Specialization implementations for `Range`. /// Specialization implementations for `Range`.
#[const_trait]
trait RangeIteratorImpl { trait RangeIteratorImpl {
type Item; type Item;
@ -539,7 +529,7 @@ trait RangeIteratorImpl {
fn spec_advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize>; fn spec_advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize>;
} }
impl<A: ~const Step + ~const Destruct> const RangeIteratorImpl for ops::Range<A> { impl<A: Step> RangeIteratorImpl for ops::Range<A> {
type Item = A; type Item = A;
#[inline] #[inline]
@ -625,7 +615,7 @@ impl<A: ~const Step + ~const Destruct> const RangeIteratorImpl for ops::Range<A>
} }
} }
impl<T: ~const TrustedStep + ~const Destruct> const RangeIteratorImpl for ops::Range<T> { impl<T: TrustedStep> RangeIteratorImpl for ops::Range<T> {
#[inline] #[inline]
fn spec_next(&mut self) -> Option<T> { fn spec_next(&mut self) -> Option<T> {
if self.start < self.end { if self.start < self.end {
@ -713,8 +703,7 @@ impl<T: ~const TrustedStep + ~const Destruct> const RangeIteratorImpl for ops::R
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_iter", issue = "92476")] impl<A: Step> Iterator for ops::Range<A> {
impl<A: ~const Step + ~const Destruct> const Iterator for ops::Range<A> {
type Item = A; type Item = A;
#[inline] #[inline]
@ -824,8 +813,7 @@ range_incl_exact_iter_impl! {
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_iter", issue = "92476")] impl<A: Step> DoubleEndedIterator for ops::Range<A> {
impl<A: ~const Step + ~const Destruct> const DoubleEndedIterator for ops::Range<A> {
#[inline] #[inline]
fn next_back(&mut self) -> Option<A> { fn next_back(&mut self) -> Option<A> {
self.spec_next_back() self.spec_next_back()

View file

@ -1,4 +1,3 @@
use crate::marker::Destruct;
use crate::num::NonZeroUsize; use crate::num::NonZeroUsize;
use crate::ops::{ControlFlow, Try}; use crate::ops::{ControlFlow, Try};
@ -39,7 +38,6 @@ use crate::ops::{ControlFlow, Try};
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "DoubleEndedIterator")] #[cfg_attr(not(test), rustc_diagnostic_item = "DoubleEndedIterator")]
#[const_trait]
pub trait DoubleEndedIterator: Iterator { pub trait DoubleEndedIterator: Iterator {
/// Removes and returns an element from the end of the iterator. /// Removes and returns an element from the end of the iterator.
/// ///
@ -136,10 +134,7 @@ pub trait DoubleEndedIterator: Iterator {
/// [`Err(k)`]: Err /// [`Err(k)`]: Err
#[inline] #[inline]
#[unstable(feature = "iter_advance_by", reason = "recently added", issue = "77404")] #[unstable(feature = "iter_advance_by", reason = "recently added", issue = "77404")]
fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> {
where
Self::Item: ~const Destruct,
{
for i in 0..n { for i in 0..n {
if self.next_back().is_none() { if self.next_back().is_none() {
// SAFETY: `i` is always less than `n`. // SAFETY: `i` is always less than `n`.
@ -192,7 +187,6 @@ pub trait DoubleEndedIterator: Iterator {
/// ``` /// ```
#[inline] #[inline]
#[stable(feature = "iter_nth_back", since = "1.37.0")] #[stable(feature = "iter_nth_back", since = "1.37.0")]
#[rustc_do_not_const_check]
fn nth_back(&mut self, n: usize) -> Option<Self::Item> { fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
if self.advance_back_by(n).is_err() { if self.advance_back_by(n).is_err() {
return None; return None;
@ -232,7 +226,6 @@ pub trait DoubleEndedIterator: Iterator {
/// ``` /// ```
#[inline] #[inline]
#[stable(feature = "iterator_try_fold", since = "1.27.0")] #[stable(feature = "iterator_try_fold", since = "1.27.0")]
#[rustc_do_not_const_check]
fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R
where where
Self: Sized, Self: Sized,
@ -304,7 +297,6 @@ pub trait DoubleEndedIterator: Iterator {
#[doc(alias = "foldr")] #[doc(alias = "foldr")]
#[inline] #[inline]
#[stable(feature = "iter_rfold", since = "1.27.0")] #[stable(feature = "iter_rfold", since = "1.27.0")]
#[rustc_do_not_const_check]
fn rfold<B, F>(mut self, init: B, mut f: F) -> B fn rfold<B, F>(mut self, init: B, mut f: F) -> B
where where
Self: Sized, Self: Sized,
@ -360,7 +352,6 @@ pub trait DoubleEndedIterator: Iterator {
/// ``` /// ```
#[inline] #[inline]
#[stable(feature = "iter_rfind", since = "1.27.0")] #[stable(feature = "iter_rfind", since = "1.27.0")]
#[rustc_do_not_const_check]
fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item> fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
where where
Self: Sized, Self: Sized,

View file

@ -1,6 +1,5 @@
use crate::array; use crate::array;
use crate::cmp::{self, Ordering}; use crate::cmp::{self, Ordering};
use crate::marker::Destruct;
use crate::num::NonZeroUsize; use crate::num::NonZeroUsize;
use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, Residual, Try}; use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, Residual, Try};
@ -340,10 +339,8 @@ pub trait Iterator {
/// ``` /// ```
#[inline] #[inline]
#[unstable(feature = "iter_advance_by", reason = "recently added", issue = "77404")] #[unstable(feature = "iter_advance_by", reason = "recently added", issue = "77404")]
fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> #[rustc_do_not_const_check]
where fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> {
Self::Item: ~const Destruct,
{
for i in 0..n { for i in 0..n {
if self.next().is_none() { if self.next().is_none() {
// SAFETY: `i` is always less than `n`. // SAFETY: `i` is always less than `n`.
@ -394,10 +391,8 @@ pub trait Iterator {
/// ``` /// ```
#[inline] #[inline]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
fn nth(&mut self, n: usize) -> Option<Self::Item> #[rustc_do_not_const_check]
where fn nth(&mut self, n: usize) -> Option<Self::Item> {
Self::Item: ~const Destruct,
{
self.advance_by(n).ok()?; self.advance_by(n).ok()?;
self.next() self.next()
} }

View file

@ -86,5 +86,4 @@ pub unsafe trait InPlaceIterable: Iterator {}
/// for details. Consumers are free to rely on the invariants in unsafe code. /// for details. Consumers are free to rely on the invariants in unsafe code.
#[unstable(feature = "trusted_step", issue = "85731")] #[unstable(feature = "trusted_step", issue = "85731")]
#[rustc_specialization_trait] #[rustc_specialization_trait]
#[const_trait] pub unsafe trait TrustedStep: Step {}
pub unsafe trait TrustedStep: ~const Step {}

View file

@ -123,11 +123,9 @@
#![feature(const_index_range_slice_index)] #![feature(const_index_range_slice_index)]
#![feature(const_inherent_unchecked_arith)] #![feature(const_inherent_unchecked_arith)]
#![feature(const_int_unchecked_arith)] #![feature(const_int_unchecked_arith)]
#![feature(const_intoiterator_identity)]
#![feature(const_intrinsic_forget)] #![feature(const_intrinsic_forget)]
#![feature(const_ipv4)] #![feature(const_ipv4)]
#![feature(const_ipv6)] #![feature(const_ipv6)]
#![feature(const_iter)]
#![feature(const_likely)] #![feature(const_likely)]
#![feature(const_maybe_uninit_uninit_array)] #![feature(const_maybe_uninit_uninit_array)]
#![feature(const_maybe_uninit_as_mut_ptr)] #![feature(const_maybe_uninit_as_mut_ptr)]

View file

@ -1,36 +0,0 @@
#[test]
fn const_manual_iter() {
struct S(bool);
impl const Iterator for S {
type Item = ();
fn next(&mut self) -> Option<Self::Item> {
if self.0 == false {
self.0 = true;
Some(())
} else {
None
}
}
}
const {
let mut val = S(false);
assert!(val.next().is_some());
assert!(val.next().is_none());
assert!(val.next().is_none());
}
}
#[test]
fn const_range() {
const {
let mut arr = [0; 3];
for i in 0..arr.len() {
arr[i] = i;
}
assert!(arr[0] == 0);
assert!(arr[1] == 1);
assert!(arr[2] == 2);
}
}

View file

@ -20,8 +20,6 @@ mod range;
mod sources; mod sources;
mod traits; mod traits;
mod consts;
use core::cell::Cell; use core::cell::Cell;
use core::convert::TryFrom; use core::convert::TryFrom;
use core::iter::*; use core::iter::*;

View file

@ -12,11 +12,8 @@
#![feature(const_caller_location)] #![feature(const_caller_location)]
#![feature(const_cell_into_inner)] #![feature(const_cell_into_inner)]
#![feature(const_convert)] #![feature(const_convert)]
#![feature(const_for)]
#![feature(const_hash)] #![feature(const_hash)]
#![feature(const_heap)] #![feature(const_heap)]
#![feature(const_intoiterator_identity)]
#![feature(const_iter)]
#![feature(const_maybe_uninit_as_mut_ptr)] #![feature(const_maybe_uninit_as_mut_ptr)]
#![feature(const_maybe_uninit_assume_init_read)] #![feature(const_maybe_uninit_assume_init_read)]
#![feature(const_nonnull_new)] #![feature(const_nonnull_new)]

View file

@ -7,6 +7,7 @@ const fn f(x: usize) -> usize {
//~| ERROR `for` is not allowed in a `const fn` //~| ERROR `for` is not allowed in a `const fn`
//~| ERROR mutable references are not allowed in constant functions //~| ERROR mutable references are not allowed in constant functions
//~| ERROR cannot call non-const fn //~| ERROR cannot call non-const fn
//~| ERROR the trait bound
sum += i; sum += i;
} }
sum sum

View file

@ -5,7 +5,7 @@ LL | / for i in 0..x {
LL | | LL | |
LL | | LL | |
LL | | LL | |
LL | | ... |
LL | | sum += i; LL | | sum += i;
LL | | } LL | | }
| |_____^ | |_____^
@ -33,6 +33,19 @@ LL | for i in 0..x {
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information = note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0277]: the trait bound `std::ops::Range<usize>: Iterator` is not satisfied
--> $DIR/const-fn-error.rs:5:14
|
LL | for i in 0..x {
| ^^^^ `std::ops::Range<usize>` is not an iterator
|
= help: the trait `~const Iterator` is not implemented for `std::ops::Range<usize>`
note: the trait `Iterator` is implemented for `std::ops::Range<usize>`, but that implementation is not `const`
--> $DIR/const-fn-error.rs:5:14
|
LL | for i in 0..x {
| ^^^^
error[E0015]: cannot call non-const fn `<std::ops::Range<usize> as Iterator>::next` in constant functions error[E0015]: cannot call non-const fn `<std::ops::Range<usize> as Iterator>::next` in constant functions
--> $DIR/const-fn-error.rs:5:14 --> $DIR/const-fn-error.rs:5:14
| |
@ -42,7 +55,7 @@ LL | for i in 0..x {
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
error: aborting due to 4 previous errors error: aborting due to 5 previous errors
Some errors have detailed explanations: E0015, E0658. Some errors have detailed explanations: E0015, E0277, E0658.
For more information about an error, try `rustc --explain E0015`. For more information about an error, try `rustc --explain E0015`.

View file

@ -5,6 +5,7 @@ const _: () = {
for _ in 0..5 {} for _ in 0..5 {}
//~^ error: cannot call //~^ error: cannot call
//~| error: cannot convert //~| error: cannot convert
//~| error: the trait bound
}; };
fn main() {} fn main() {}

View file

@ -9,6 +9,19 @@ note: impl defined here, but it is not `const`
= note: calls in constants are limited to constant functions, tuple structs and tuple variants = note: calls in constants are limited to constant functions, tuple structs and tuple variants
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
error[E0277]: the trait bound `std::ops::Range<i32>: Iterator` is not satisfied
--> $DIR/const-for.rs:5:14
|
LL | for _ in 0..5 {}
| ^^^^ `std::ops::Range<i32>` is not an iterator
|
= help: the trait `~const Iterator` is not implemented for `std::ops::Range<i32>`
note: the trait `Iterator` is implemented for `std::ops::Range<i32>`, but that implementation is not `const`
--> $DIR/const-for.rs:5:14
|
LL | for _ in 0..5 {}
| ^^^^
error[E0015]: cannot call non-const fn `<std::ops::Range<i32> as Iterator>::next` in constants error[E0015]: cannot call non-const fn `<std::ops::Range<i32> as Iterator>::next` in constants
--> $DIR/const-for.rs:5:14 --> $DIR/const-for.rs:5:14
| |
@ -18,6 +31,7 @@ LL | for _ in 0..5 {}
= note: calls in constants are limited to constant functions, tuple structs and tuple variants = note: calls in constants are limited to constant functions, tuple structs and tuple variants
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
error: aborting due to 2 previous errors error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0015`. Some errors have detailed explanations: E0015, E0277.
For more information about an error, try `rustc --explain E0015`.

View file

@ -228,4 +228,5 @@ fn evens_squared(n: usize) -> _ {
const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x); const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x);
//~^ ERROR the trait bound //~^ ERROR the trait bound
//~| ERROR the trait bound
//~| ERROR the placeholder //~| ERROR the placeholder

View file

@ -437,6 +437,19 @@ LL | fn evens_squared(n: usize) -> _ {
| not allowed in type signatures | not allowed in type signatures
| help: replace with an appropriate return type: `impl Iterator<Item = usize>` | help: replace with an appropriate return type: `impl Iterator<Item = usize>`
error[E0277]: the trait bound `std::ops::Range<{integer}>: Iterator` is not satisfied
--> $DIR/typeck_type_placeholder_item.rs:229:22
|
LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x);
| ^^^^^^ `std::ops::Range<{integer}>` is not an iterator
|
= help: the trait `~const Iterator` is not implemented for `std::ops::Range<{integer}>`
note: the trait `Iterator` is implemented for `std::ops::Range<{integer}>`, but that implementation is not `const`
--> $DIR/typeck_type_placeholder_item.rs:229:14
|
LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x);
| ^^^^^^^
error[E0277]: the trait bound `Filter<std::ops::Range<{integer}>, [closure@$DIR/typeck_type_placeholder_item.rs:229:29: 229:32]>: Iterator` is not satisfied error[E0277]: the trait bound `Filter<std::ops::Range<{integer}>, [closure@$DIR/typeck_type_placeholder_item.rs:229:29: 229:32]>: Iterator` is not satisfied
--> $DIR/typeck_type_placeholder_item.rs:229:45 --> $DIR/typeck_type_placeholder_item.rs:229:45
| |
@ -664,7 +677,7 @@ LL | const D: _ = 42;
| not allowed in type signatures | not allowed in type signatures
| help: replace with the correct type: `i32` | help: replace with the correct type: `i32`
error: aborting due to 72 previous errors error: aborting due to 73 previous errors
Some errors have detailed explanations: E0121, E0277, E0282, E0403. Some errors have detailed explanations: E0121, E0277, E0282, E0403.
For more information about an error, try `rustc --explain E0121`. For more information about an error, try `rustc --explain E0121`.