implement const iterator using rustc_do_not_const_check#106541
implement const iterator using rustc_do_not_const_check#106541bors merged 2 commits intorust-lang:masterfrom
rustc_do_not_const_check#106541Conversation
|
r? @thomcc (rustbot has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
f402cc9 to
4d31609
Compare
This comment has been minimized.
This comment has been minimized.
4d31609 to
85ffe50
Compare
|
cc @oli-obk for compiler changes |
85ffe50 to
87a2cbe
Compare
|
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
|
If this approach is chosen, how is the plan to remove the workaround later? |
Yes. Once const closures hit beta. |
|
☔ The latest upstream changes (presumably #103902) made this pull request unmergeable. Please resolve the merge conflicts. |
|
This seems okay, as long as it doesn't affect the stable behaviour of course. But, what safety mechanisms in place to make sure it doesn't affect stable behaviour, neither now nor (accidentally) in the future? |
|
Could we also have a test to make sure this only affects default method implementations, not the functions overriding those implementations in an |
Were you talking about the As for the process of making
Yes, I will add that in a moment |
|
A test to make that |
87a2cbe to
3aeb43c
Compare
| /// ``` | ||
| #[inline] | ||
| #[stable(feature = "rust1", since = "1.0.0")] | ||
| #[rustc_do_not_const_check] |
There was a problem hiding this comment.
This is not needed as this is trivially const anyways.
| /// ``` | ||
| #[inline] | ||
| #[stable(feature = "rust1", since = "1.0.0")] | ||
| #[rustc_do_not_const_check] |
There was a problem hiding this comment.
Can be removed using const_closures
| #[inline] | ||
| #[stable(feature = "rust1", since = "1.0.0")] | ||
| #[rustc_do_not_const_check] | ||
| fn last(self) -> Option<Self::Item> |
There was a problem hiding this comment.
Can be removed simply by making the inner function const.
In fact looking at this most of those seem to be no longer needed now that const_closures is in bootstrap.
|
@chriss0612 I still think that this PR should be as minimal as possible and we can make followup PRs for it to do more things. Are there any concerns that are still blocking this? @rustbot ready |
Yeah that's fine, I mostly wanted to mention it. |
|
My understanding is this has gotten libs-api approval. The implementation seems fine to me, so @bors r+ |
…o, r=thomcc implement const iterator using `rustc_do_not_const_check` Previous experiment: rust-lang#102225. Explanation: rather than making all default methods work under `const` all at once, this uses `rustc_do_not_const_check` as a workaround to "trick" the compiler to not run any checks on those other default methods. Any const implementations are only required to implement the `next` method. Any actual calls to the trait methods other than `next` will either error in compile time (at CTFE runs), or run the methods correctly if they do not have any non-const operations. This is extremely easy to maintain, remove, or improve.
Rollup of 10 pull requests Successful merges: - rust-lang#106541 (implement const iterator using `rustc_do_not_const_check`) - rust-lang#106918 (Rebuild BinaryHeap on unwind from retain) - rust-lang#106923 (Restore behavior when primary bundle is missing) - rust-lang#108169 (Make query keys `Copy`) - rust-lang#108287 (Add test for bad cast with deferred projection equality) - rust-lang#108370 (std: time: Avoid to use "was created" in elapsed() description) - rust-lang#108377 (Fix ICE in 'duplicate diagnostic item' diagnostic) - rust-lang#108388 (parser: provide better suggestions and errors on closures with braces missing) - rust-lang#108391 (Fix `is_terminal`'s handling of long paths on Windows.) - rust-lang#108401 (diagnostics: remove inconsistent English article "this" from E0107) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
[rust-lang/rust#106541][1] added `#[const_trait]` to `Iterator` as well as internal attribute to bypass the constness check for other methods than `Iterator::next`. As a result, user code can now implement `const Iterator` in a normal way, i.e., just by implementing `Iterator::next`. [1]: rust-lang/rust#106541
…k, r=oli-obk constify `Iterator`, take IV Like its predecessors (rust-lang#92433, rust-lang#102225, rust-lang#106541), this PR allows one to make `Iterator` implementations `const`, and thus enables the ability to have `for` loops in `const` contexts. I've also included constifying `Option as IntoIterator`, `option::IntoIter as Iterator` as a minimal dogfooding example. But unlike its predecessors, it uses a new attribute (not unsound anymore!) that prevents any `Iterator` extension methods from being called. This is intentionally made minimal for an initial approval, the fun stuff like `.fold`, `Range as Iterator` could be done later. cc @rust-lang/wg-const-eval, cc @oli-obk to review the compiler parts cc rust-lang#92476
Rollup merge of #151281 - fee1-dead-contrib:push-zmqtzvuvlmuk, r=oli-obk constify `Iterator`, take IV Like its predecessors (#92433, #102225, #106541), this PR allows one to make `Iterator` implementations `const`, and thus enables the ability to have `for` loops in `const` contexts. I've also included constifying `Option as IntoIterator`, `option::IntoIter as Iterator` as a minimal dogfooding example. But unlike its predecessors, it uses a new attribute (not unsound anymore!) that prevents any `Iterator` extension methods from being called. This is intentionally made minimal for an initial approval, the fun stuff like `.fold`, `Range as Iterator` could be done later. cc @rust-lang/wg-const-eval, cc @oli-obk to review the compiler parts cc #92476
constify `Iterator`, take IV Like its predecessors (rust-lang/rust#92433, rust-lang/rust#102225, rust-lang/rust#106541), this PR allows one to make `Iterator` implementations `const`, and thus enables the ability to have `for` loops in `const` contexts. I've also included constifying `Option as IntoIterator`, `option::IntoIter as Iterator` as a minimal dogfooding example. But unlike its predecessors, it uses a new attribute (not unsound anymore!) that prevents any `Iterator` extension methods from being called. This is intentionally made minimal for an initial approval, the fun stuff like `.fold`, `Range as Iterator` could be done later. cc @rust-lang/wg-const-eval, cc @oli-obk to review the compiler parts cc rust-lang/rust#92476
Previous experiment: #102225.
Explanation: rather than making all default methods work under
constall at once, this usesrustc_do_not_const_checkas a workaround to "trick" the compiler to not run any checks on those other default methods. Any const implementations are only required to implement thenextmethod. Any actual calls to the trait methods other thannextwill either error in compile time (at CTFE runs), or run the methods correctly if they do not have any non-const operations. This is extremely easy to maintain, remove, or improve.