Skip to content

Conversation

@fee1-dead
Copy link
Member

@fee1-dead fee1-dead commented Jan 18, 2026

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

@rustbot
Copy link
Collaborator

rustbot commented Jan 18, 2026

Some changes occurred in compiler/rustc_hir/src/attrs

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) 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. labels Jan 18, 2026
@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jan 18, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 18, 2026

r? @scottmcm

rustbot has assigned @scottmcm.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@fmease fmease added the PG-const-traits Project group: Const traits label Jan 18, 2026
/// ```
#[inline]
#[unstable(feature = "iter_next_chunk", issue = "98326")]
#[rustc_non_const_trait_method]
Copy link
Member

Choose a reason for hiding this comment

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

Please also update the dev-guide docs to explain the attribute.


trace!("attempting to call a trait method");
let trait_is_const = tcx.is_const_trait(trait_did);
let is_const = tcx.constness(callee) == hir::Constness::Const;
Copy link
Member

Choose a reason for hiding this comment

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

The code looks odd in combination with the comment just above it. The comment says "we can't look at the callee" and then the code proceeds to look at the callee...

Copy link
Member Author

Choose a reason for hiding this comment

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

Can't determine the actual callee means we're calling Trait::func instead of <T as Trait>::func, here we're just looking at the constness of the trait method, although maybe I can adjust the comment above

@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 18, 2026

☔ The latest upstream changes (presumably #151291) made this pull request unmergeable. Please resolve the merge conflicts.

rustc_non_const_trait_method, AttributeType::Normal, template!(Word),
ErrorFollowing, EncodeCrossCrate::No,
"`#[rustc_non_const_trait_method]` should only used by the standard library to mark trait methods \
as non-const to allow large traits to easier transition to const"
Copy link
Contributor

Choose a reason for hiding this comment

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

"traits an easier transition" or "traits to transition more easily"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) PG-const-traits Project group: Const traits 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants