-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
implement const iterator using rustc_do_not_const_check
#106541
Conversation
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
@@ -218,6 +220,7 @@ pub trait Iterator { | |||
/// ``` | |||
#[inline] | |||
#[stable(feature = "rust1", since = "1.0.0")] | |||
#[rustc_do_not_const_check] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not needed as this is trivially const anyways.
@@ -255,6 +258,7 @@ pub trait Iterator { | |||
/// ``` | |||
#[inline] | |||
#[stable(feature = "rust1", since = "1.0.0")] | |||
#[rustc_do_not_const_check] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be removed using const_closures
@@ -285,6 +289,7 @@ pub trait Iterator { | |||
/// ``` | |||
#[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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
Previous experiment: #102225.
Explanation: rather than making all default methods work under
const
all at once, this usesrustc_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 thenext
method. Any actual calls to the trait methods other thannext
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.