Skip to content

implement const Iterator for Range - #156216

Merged
rust-bors[bot] merged 5 commits into
rust-lang:mainfrom
Randl:const-for-impl
Sep 16, 2026
Merged

rust-bors[bot] merged 5 commits into
rust-lang:mainfrom
Randl:const-for-impl

Conversation

@Randl

@Randl Randl commented May 6, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

This allows the use of for i in i..n in const.

This I believe is interesting enough to be justified under #155816

r? @oli-obk

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 6, 2026
@Randl

Randl commented May 6, 2026

Copy link
Copy Markdown
Contributor Author

Another issue I noticed is that while is const-stable, but for isn't. So in some places, reverting the const-hack would make currently const-stable functions const-unstable. Alternatively, we wait until Iterator is const-stable.

@rust-log-analyzer

This comment has been minimized.

@Randl
Randl force-pushed the const-for-impl branch from d4ee14e to 6115579 Compare May 6, 2026 02:08
@theemathas

Copy link
Copy Markdown
Contributor

Does this run into #148200 for types that implement non-const TrustedStep but implements const Step?

@rust-log-analyzer

This comment has been minimized.

@Randl
Randl force-pushed the const-for-impl branch from 6115579 to 58f0c19 Compare May 6, 2026 04:23
@Randl

Randl commented May 6, 2026

Copy link
Copy Markdown
Contributor Author

@theemathas
Can you give maybe an example you had in mind? I'm not sure I can think of one. If I understand correctly, there won't be any non-const functions involved in this case, since the entire implementation will be in the const Step?

@theemathas

Copy link
Copy Markdown
Contributor

Maybe there's actually not a problem with specialization here? I suppose it's kind of fine to call a function that requires const TrustedStep even though the type only actually implements TrustedStep + const Step, since TrustedStep itself doesn't have any methods.

Comment thread library/core/src/tuple.rs Resolved
@Randl
Randl force-pushed the const-for-impl branch from 58f0c19 to ccbb26c Compare May 6, 2026 08:18
Comment thread library/core/src/iter/range.rs Outdated
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: Step> DoubleEndedIterator for ops::Range<A> {
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
impl<A: [const] Step + [const] Destruct> const DoubleEndedIterator for ops::Range<A> {

@oli-obk oli-obk May 6, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change needed for the MVP? should at least be a separate commit unless the rest of this commit requires it

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, in fact, required since last() is implemented using next_back(), so it needs to be constified too.

@fee1-dead

Copy link
Copy Markdown
Member

I feel iffy on introducing additional const impls that use specialization without us fixing specialization first

@oli-obk

oli-obk commented May 13, 2026

Copy link
Copy Markdown
Contributor

@rustbot blocked #156454

@rustbot rustbot added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 13, 2026
@Filiprogrammer

Filiprogrammer commented Jun 20, 2026 •

Copy link
Copy Markdown
Contributor

This no longer builds since bc2112e.

error: expected a trait, found type
   --> /rustc/1df360b6a885af638e597c167cbb537b3b2137cd/library/core/src/iter/range.rs:819:42
    |
819 | impl<A: [const] Step + [const] Destruct> const RangeIteratorImpl for ops::Range<A> {
    |                                          ^^^^^^^^^^^^^^^^^^^^^^^

error: expected a trait, found type
   --> /rustc/1df360b6a885af638e597c167cbb537b3b2137cd/library/core/src/iter/traits/iterator.rs:321:44
    |
321 |         impl<I: [const] Iterator + ?Sized> const SpecAdvanceBy for I
    |                                            ^^^^^^^^^^^^^^^^^^^

Probably related to a88521a.

Comment thread library/core/src/iter/range.rs Resolved
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Randl

Randl commented Jun 20, 2026

Copy link
Copy Markdown
Contributor Author

Looks like genuine ICE for me, I may take a look at it tomorrow

@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rust-bors rust-bors Bot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 16, 2026
Zalathar added a commit to Zalathar/rust that referenced this pull request Sep 16, 2026
implement const Iterator for Range

This allows the use of `for i in i..n` in `const`.

This I believe is interesting enough to be justified under rust-lang#155816

r? @oli-obk
rust-bors Bot pushed a commit that referenced this pull request Sep 16, 2026
Rollup of 7 pull requests

Successful merges:

 - #156216 (implement const Iterator for Range)
 - #162748 (Enable LLVM Thin LTO for LoongArch64)
 - #162769 (Yeet the `DeepRegionResolver` (earlier called `OpportunisticRegionResolver`))
 - #162800 (Cleanups related to RefDecodable)
 - #162826 (wasm: fix ABI for enums with integer layout and ZST fields)
 - #159359 ([rustc_public] Enhance PassMode API)
 - #162818 (Add regression test for malformed RPITIT bound ICE with the new solver)
rust-bors Bot pushed a commit that referenced this pull request Sep 16, 2026
Rollup of 10 pull requests

Successful merges:

 - #156216 (implement const Iterator for Range)
 - #160697 (rustc: Tweak the effect of `--jobs` on frontend parallelism)
 - #162748 (Enable LLVM Thin LTO for LoongArch64)
 - #162769 (Yeet the `DeepRegionResolver` (earlier called `OpportunisticRegionResolver`))
 - #162794 (Simplify `ast::UseTreeKind`)
 - #162800 (Cleanups related to RefDecodable)
 - #162826 (wasm: fix ABI for enums with integer layout and ZST fields)
 - #159359 ([rustc_public] Enhance PassMode API)
 - #162196 (Offload cmake cleanups)
 - #162818 (Add regression test for malformed RPITIT bound ICE with the new solver)
@rust-bors
rust-bors Bot merged commit a705949 into rust-lang:main Sep 16, 2026
13 checks passed
rust-bors Bot pushed a commit that referenced this pull request Sep 16, 2026
Rollup merge of #156216 - Randl:const-for-impl, r=clarfonthey

implement const Iterator for Range

This allows the use of `for i in i..n` in `const`.

This I believe is interesting enough to be justified under #155816

r? @oli-obk
@rustbot rustbot added this to the 1.100.0 milestone Sep 16, 2026
pull Bot pushed a commit to LeeeeeeM/miri that referenced this pull request Sep 17, 2026
Rollup of 10 pull requests

Successful merges:

 - rust-lang/rust#156216 (implement const Iterator for Range)
 - rust-lang/rust#160697 (rustc: Tweak the effect of `--jobs` on frontend parallelism)
 - rust-lang/rust#162748 (Enable LLVM Thin LTO for LoongArch64)
 - rust-lang/rust#162769 (Yeet the `DeepRegionResolver` (earlier called `OpportunisticRegionResolver`))
 - rust-lang/rust#162794 (Simplify `ast::UseTreeKind`)
 - rust-lang/rust#162800 (Cleanups related to RefDecodable)
 - rust-lang/rust#162826 (wasm: fix ABI for enums with integer layout and ZST fields)
 - rust-lang/rust#159359 ([rustc_public] Enhance PassMode API)
 - rust-lang/rust#162196 (Offload cmake cleanups)
 - rust-lang/rust#162818 (Add regression test for malformed RPITIT bound ICE with the new solver)
pull Bot pushed a commit to asukaminato0721/rust-clippy that referenced this pull request Sep 17, 2026
Rollup of 10 pull requests

Successful merges:

 - rust-lang/rust#156216 (implement const Iterator for Range)
 - rust-lang/rust#160697 (rustc: Tweak the effect of `--jobs` on frontend parallelism)
 - rust-lang/rust#162748 (Enable LLVM Thin LTO for LoongArch64)
 - rust-lang/rust#162769 (Yeet the `DeepRegionResolver` (earlier called `OpportunisticRegionResolver`))
 - rust-lang/rust#162794 (Simplify `ast::UseTreeKind`)
 - rust-lang/rust#162800 (Cleanups related to RefDecodable)
 - rust-lang/rust#162826 (wasm: fix ABI for enums with integer layout and ZST fields)
 - rust-lang/rust#159359 ([rustc_public] Enhance PassMode API)
 - rust-lang/rust#162196 (Offload cmake cleanups)
 - rust-lang/rust#162818 (Add regression test for malformed RPITIT bound ICE with the new solver)
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Sep 17, 2026
Rollup of 10 pull requests

Successful merges:

 - rust-lang/rust#156216 (implement const Iterator for Range)
 - rust-lang/rust#160697 (rustc: Tweak the effect of `--jobs` on frontend parallelism)
 - rust-lang/rust#162748 (Enable LLVM Thin LTO for LoongArch64)
 - rust-lang/rust#162769 (Yeet the `DeepRegionResolver` (earlier called `OpportunisticRegionResolver`))
 - rust-lang/rust#162794 (Simplify `ast::UseTreeKind`)
 - rust-lang/rust#162800 (Cleanups related to RefDecodable)
 - rust-lang/rust#162826 (wasm: fix ABI for enums with integer layout and ZST fields)
 - rust-lang/rust#159359 ([rustc_public] Enhance PassMode API)
 - rust-lang/rust#162196 (Offload cmake cleanups)
 - rust-lang/rust#162818 (Add regression test for malformed RPITIT bound ICE with the new solver)
makai410 pushed a commit to makai410/rust that referenced this pull request Oct 1, 2026
Rollup of 10 pull requests

Successful merges:

 - rust-lang#156216 (implement const Iterator for Range)
 - rust-lang#160697 (rustc: Tweak the effect of `--jobs` on frontend parallelism)
 - rust-lang#162748 (Enable LLVM Thin LTO for LoongArch64)
 - rust-lang#162769 (Yeet the `DeepRegionResolver` (earlier called `OpportunisticRegionResolver`))
 - rust-lang#162794 (Simplify `ast::UseTreeKind`)
 - rust-lang#162800 (Cleanups related to RefDecodable)
 - rust-lang#162826 (wasm: fix ABI for enums with integer layout and ZST fields)
 - rust-lang#159359 ([rustc_public] Enhance PassMode API)
 - rust-lang#162196 (Offload cmake cleanups)
 - rust-lang#162818 (Add regression test for malformed RPITIT bound ICE with the new solver)
makai410 pushed a commit to makai410/rustc_public that referenced this pull request Oct 1, 2026
Rollup of 10 pull requests

Successful merges:

 - rust-lang/rust#156216 (implement const Iterator for Range)
 - rust-lang/rust#160697 (rustc: Tweak the effect of `--jobs` on frontend parallelism)
 - rust-lang/rust#162748 (Enable LLVM Thin LTO for LoongArch64)
 - rust-lang/rust#162769 (Yeet the `DeepRegionResolver` (earlier called `OpportunisticRegionResolver`))
 - rust-lang/rust#162794 (Simplify `ast::UseTreeKind`)
 - rust-lang/rust#162800 (Cleanups related to RefDecodable)
 - rust-lang/rust#162826 (wasm: fix ABI for enums with integer layout and ZST fields)
 - rust-lang/rust#159359 ([rustc_public] Enhance PassMode API)
 - rust-lang/rust#162196 (Offload cmake cleanups)
 - rust-lang/rust#162818 (Add regression test for malformed RPITIT bound ICE with the new solver)
clarfonthey added a commit to clarfonthey/rust that referenced this pull request Nov 22, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 24, 2026
…ohnTitor,JonathanBrouwer

Revert "implement const Iterator for Range - rust-lang#156216"

Potential fix for rust-lang#166229. Had to manually resolve some merge conflicts, but, range-diff does appear to verify that this does fully revert the original PR rust-lang#156216.
rust-bors Bot pushed a commit that referenced this pull request Nov 24, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - #166340 (Revert "implement const Iterator for Range - #156216")
 - #165345 (Remove the 'eq', 'eq_trace', and 'goals_to_obligations' methods from 'At')
 - #165538 (Supertraits returns Unnormalized)
 - #165794 (`consider_impl_candidate` tweaks)
 - #160752 (Avoid inference constraints from opaque blanket impls)
 - #163187 (add `scalbn` and `ilogb` methods on floats)
 - #164541 (Add regression test for #152415)
 - #164543 (Add regression test for #151316)
 - #165920 (Introduce compute_abi helper and ComputeCallConv trait)
 - #166313 (Move `rustdoc-ui` tests in the right folder)
 - #166420 ([rustdoc] Fix search index generation: if crate content didn't change, it should generate the same search index)
 - #166485 (Allow passing `--exact-match` and `--purge` to `x perf`)
rust-bors Bot pushed a commit that referenced this pull request Nov 24, 2026
Rollup merge of #166340 - clarfonthey:revert-iter-range, r=JohnTitor,JonathanBrouwer

Revert "implement const Iterator for Range - #156216"

Potential fix for #166229. Had to manually resolve some merge conflicts, but, range-diff does appear to verify that this does fully revert the original PR #156216.
rust-bors Bot pushed a commit that referenced this pull request Nov 24, 2026
[stable] prepare Rust 1.100.1

* #166340
  * This was done through a clean revert of #156216, rather than a cherry-pick of #166340, because that had significant conflicts
* #165997
  * This had an easily resolved merge conflict as well

r? @cuviper @clarfonthey 
cc @rust-lang/release
rust-bors Bot pushed a commit that referenced this pull request Nov 24, 2026
[stable] prepare Rust 1.100.1

* #166340
  * This was done through a clean revert of #156216, rather than a cherry-pick of #166340, because that had significant conflicts
* #165997
  * This had an easily resolved merge conflict as well
* #166302

r? @cuviper @clarfonthey 
cc @rust-lang/release
TDecking pushed a commit to TDecking/miri that referenced this pull request Nov 25, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - rust-lang/rust#166340 (Revert "implement const Iterator for Range - rust-lang/rust#156216")
 - rust-lang/rust#165345 (Remove the 'eq', 'eq_trace', and 'goals_to_obligations' methods from 'At')
 - rust-lang/rust#165538 (Supertraits returns Unnormalized)
 - rust-lang/rust#165794 (`consider_impl_candidate` tweaks)
 - rust-lang/rust#160752 (Avoid inference constraints from opaque blanket impls)
 - rust-lang/rust#163187 (add `scalbn` and `ilogb` methods on floats)
 - rust-lang/rust#164541 (Add regression test for rust-lang/rust#152415)
 - rust-lang/rust#164543 (Add regression test for rust-lang/rust#151316)
 - rust-lang/rust#165920 (Introduce compute_abi helper and ComputeCallConv trait)
 - rust-lang/rust#166313 (Move `rustdoc-ui` tests in the right folder)
 - rust-lang/rust#166420 ([rustdoc] Fix search index generation: if crate content didn't change, it should generate the same search index)
 - rust-lang/rust#166485 (Allow passing `--exact-match` and `--purge` to `x perf`)
rust-bors Bot pushed a commit that referenced this pull request Nov 25, 2026
[stable] prepare Rust 1.100.1



* #166340
  * This was done through a clean revert of #156216, rather than a cherry-pick of #166340, because that had significant conflicts
* #165997
  * This had an easily resolved merge conflict as well
* #166302

r? @cuviper @clarfonthey 
cc @rust-lang/release
mehmettoplu-rs pushed a commit to mehmettoplu-rs/rustc_codegen_gcc that referenced this pull request Nov 27, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - rust-lang/rust#166340 (Revert "implement const Iterator for Range - rust-lang/rust#156216")
 - rust-lang/rust#165345 (Remove the 'eq', 'eq_trace', and 'goals_to_obligations' methods from 'At')
 - rust-lang/rust#165538 (Supertraits returns Unnormalized)
 - rust-lang/rust#165794 (`consider_impl_candidate` tweaks)
 - rust-lang/rust#160752 (Avoid inference constraints from opaque blanket impls)
 - rust-lang/rust#163187 (add `scalbn` and `ilogb` methods on floats)
 - rust-lang/rust#164541 (Add regression test for rust-lang/rust#152415)
 - rust-lang/rust#164543 (Add regression test for rust-lang/rust#151316)
 - rust-lang/rust#165920 (Introduce compute_abi helper and ComputeCallConv trait)
 - rust-lang/rust#166313 (Move `rustdoc-ui` tests in the right folder)
 - rust-lang/rust#166420 ([rustdoc] Fix search index generation: if crate content didn't change, it should generate the same search index)
 - rust-lang/rust#166485 (Allow passing `--exact-match` and `--purge` to `x perf`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants