Skip to content

Experiment: Const iterrator range with #[rustc_do_not_const_check] #107647

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/rustc_hir_typeck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_session::config;
use rustc_session::Session;
use rustc_span::def_id::{DefId, LocalDefId};
use rustc_span::Span;
use rustc_span::{sym, Span};

#[macro_export]
macro_rules! type_error_struct {
Expand Down Expand Up @@ -201,6 +201,11 @@ fn typeck_with_fallback<'tcx>(

let typeck_results = Inherited::build(tcx, def_id).enter(|inh| {
let param_env = tcx.param_env(def_id);
let param_env = if tcx.has_attr(def_id.to_def_id(), sym::rustc_do_not_const_check) {
param_env.without_const()
} else {
param_env
};
let mut fcx = FnCtxt::new(&inh, param_env, def_id);

if let Some(hir::FnSig { header, decl, .. }) = fn_sig {
Expand Down
111 changes: 103 additions & 8 deletions library/core/src/iter/range.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::char;
use crate::convert::TryFrom;
use crate::marker::Destruct;
use crate::mem;
use crate::ops::{self, Try};

Expand All @@ -21,7 +22,8 @@ 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 *predecessor* operation moves towards values that compare lesser.
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
pub trait Step: Clone + PartialOrd + Sized {
#[const_trait]
pub trait Step: ~const Clone + ~const PartialOrd + Sized {
/// Returns the number of *successor* steps required to get from `start` to `end`.
///
/// Returns `None` if the number of steps would overflow `usize`
Expand Down Expand Up @@ -235,7 +237,8 @@ macro_rules! step_integer_impls {
$(
#[allow(unreachable_patterns)]
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
impl Step for $u_narrower {
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
impl const Step for $u_narrower {
step_identical_methods!();

#[inline]
Expand Down Expand Up @@ -267,7 +270,8 @@ macro_rules! step_integer_impls {

#[allow(unreachable_patterns)]
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
impl Step for $i_narrower {
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
impl const Step for $i_narrower {
step_identical_methods!();

#[inline]
Expand Down Expand Up @@ -331,7 +335,8 @@ macro_rules! step_integer_impls {
$(
#[allow(unreachable_patterns)]
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
impl Step for $u_wider {
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
impl const Step for $u_wider {
step_identical_methods!();

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

#[allow(unreachable_patterns)]
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
impl Step for $i_wider {
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
impl const Step for $i_wider {
step_identical_methods!();

#[inline]
Expand Down Expand Up @@ -406,7 +412,8 @@ step_integer_impls! {
}

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

#[inline]
#[rustc_allow_const_fn_unstable(const_try)]
fn forward_checked(start: char, count: usize) -> Option<char> {
let start = start as u32;
let mut res = Step::forward_checked(start, count)?;
Expand All @@ -440,6 +448,7 @@ impl Step for char {
}

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

/// Specialization implementations for `Range`.
#[const_trait]
trait RangeIteratorImpl {
type Item;

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

impl<A: Step> RangeIteratorImpl for ops::Range<A> {
impl<A: ~const Step + ~const Destruct> const RangeIteratorImpl for ops::Range<A> {
type Item = A;

#[inline]
Expand Down Expand Up @@ -615,7 +625,7 @@ impl<A: Step> RangeIteratorImpl for ops::Range<A> {
}
}

impl<T: TrustedStep> RangeIteratorImpl for ops::Range<T> {
impl<T: ~const TrustedStep + ~const Destruct> const RangeIteratorImpl for ops::Range<T> {
#[inline]
fn spec_next(&mut self) -> Option<T> {
if self.start < self.end {
Expand Down Expand Up @@ -703,6 +713,70 @@ impl<T: TrustedStep> RangeIteratorImpl for ops::Range<T> {
}

#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
#[cfg(not(bootstrap))]
impl<A: ~const Step + ~const Destruct> const Iterator for ops::Range<A> {
type Item = A;

#[inline]
fn next(&mut self) -> Option<A> {
self.spec_next()
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
if self.start < self.end {
let hint = Step::steps_between(&self.start, &self.end);
(hint.unwrap_or(usize::MAX), hint)
} else {
(0, Some(0))
}
}

#[inline]
fn nth(&mut self, n: usize) -> Option<A> {
self.spec_nth(n)
}

#[inline]
fn last(mut self) -> Option<A> {
self.next_back()
}

#[inline]
fn min(mut self) -> Option<A> {
self.next()
}

#[inline]
fn max(mut self) -> Option<A> {
self.next_back()
}

#[inline]
fn is_sorted(self) -> bool {
true
}

#[inline]
fn advance_by(&mut self, n: usize) -> Result<(), usize> {
self.spec_advance_by(n)
}

#[inline]
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item
where
Self: TrustedRandomAccessNoCoerce,
{
// SAFETY: The TrustedRandomAccess contract requires that callers only pass an index
// that is in bounds.
// Additionally Self: TrustedRandomAccess is only implemented for Copy types
// which means even repeated reads of the same index would be safe.
unsafe { Step::forward_unchecked(self.start.clone(), idx) }
}
}
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg(bootstrap)]
impl<A: Step> Iterator for ops::Range<A> {
type Item = A;

Expand Down Expand Up @@ -813,6 +887,27 @@ range_incl_exact_iter_impl! {
}

#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
#[cfg(not(bootstrap))]
impl<A: ~const Step + ~const Destruct> const DoubleEndedIterator for ops::Range<A> {
#[inline]
fn next_back(&mut self) -> Option<A> {
self.spec_next_back()
}

#[inline]
fn nth_back(&mut self, n: usize) -> Option<A> {
self.spec_nth_back(n)
}

#[inline]
fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
self.spec_advance_back_by(n)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
#[cfg(bootstrap)]
impl<A: Step> DoubleEndedIterator for ops::Range<A> {
#[inline]
fn next_back(&mut self) -> Option<A> {
Expand Down
11 changes: 10 additions & 1 deletion library/core/src/iter/traits/double_ended.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::marker::Destruct;
use crate::ops::{ControlFlow, Try};

/// An iterator able to yield elements from both ends.
Expand Down Expand Up @@ -37,6 +38,7 @@ use crate::ops::{ControlFlow, Try};
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "DoubleEndedIterator")]
#[cfg_attr(not(bootstrap), const_trait)]
pub trait DoubleEndedIterator: Iterator {
/// Removes and returns an element from the end of the iterator.
///
Expand Down Expand Up @@ -131,7 +133,10 @@ pub trait DoubleEndedIterator: Iterator {
/// [`Err(k)`]: Err
#[inline]
#[unstable(feature = "iter_advance_by", reason = "recently added", issue = "77404")]
fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
fn advance_back_by(&mut self, n: usize) -> Result<(), usize>
where
Self::Item: ~const Destruct,
{
for i in 0..n {
self.next_back().ok_or(i)?;
}
Expand Down Expand Up @@ -181,6 +186,7 @@ pub trait DoubleEndedIterator: Iterator {
/// ```
#[inline]
#[stable(feature = "iter_nth_back", since = "1.37.0")]
#[rustc_do_not_const_check]
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
self.advance_back_by(n).ok()?;
self.next_back()
Expand Down Expand Up @@ -218,6 +224,7 @@ pub trait DoubleEndedIterator: Iterator {
/// ```
#[inline]
#[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
where
Self: Sized,
Expand Down Expand Up @@ -289,6 +296,7 @@ pub trait DoubleEndedIterator: Iterator {
#[doc(alias = "foldr")]
#[inline]
#[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
where
Self: Sized,
Expand Down Expand Up @@ -344,6 +352,7 @@ pub trait DoubleEndedIterator: Iterator {
/// ```
#[inline]
#[stable(feature = "iter_rfind", since = "1.27.0")]
#[rustc_do_not_const_check]
fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
where
Self: Sized,
Expand Down
Loading