Skip to content

Check closure's constness validity in the constness query#155772

Merged
rust-bors[bot] merged 2 commits intorust-lang:mainfrom
oli-obk:const-closures-everywhere
Apr 27, 2026
Merged

Check closure's constness validity in the constness query#155772
rust-bors[bot] merged 2 commits intorust-lang:mainfrom
oli-obk:const-closures-everywhere

Conversation

@oli-obk
Copy link
Copy Markdown
Contributor

@oli-obk oli-obk commented Apr 25, 2026

View all comments

fixes #155584

instead of checking during ast lowering, where it's not easily possible to obtain all the right information in time. While lowering an assoc item we don't know if the parent was a const trait or a const impl. Tracing this information is quite annoying, and complicates a lot of code, which checking here after the fact is trivial.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 25, 2026

Some changes occurred to the CTFE machinery

cc @RalfJung, @lcnr

@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. labels Apr 25, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 25, 2026

r? @mati865

rustbot has assigned @mati865.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 20 candidates

|
LL | (const || { (()).foo() })();
| ^^^^^
| ^^^^^^^^
Copy link
Copy Markdown
Contributor Author

@oli-obk oli-obk Apr 25, 2026

Choose a reason for hiding this comment

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

The only downside of this PR is that we highlight more than the const part of the closure now

View changes since the review

Copy link
Copy Markdown
Member

@fee1-dead fee1-dead left a comment

Choose a reason for hiding this comment

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

Node::Expr(e) if let ExprKind::Closure(c) = e.kind => {
match c.constness {
Constness::Const => if tcx.hir_body_const_context(tcx.local_parent(def_id)).is_none() {
tcx.dcx().span_err(tcx.def_span(def_id), "cannot use `const` closures outside of const contexts");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A hacky way could be to use https://doc.rust-lang.org/stable/nightly-rustc/rustc_span/source_map/struct.SourceMap.html#method.span_until_whitespace since we always know that const is the first keyword (for now).

But this is not necessary

Comment on lines +20 to +21
match c.constness {
Constness::Const => if tcx.hir_body_const_context(tcx.local_parent(def_id)).is_none() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would use if let Constness::Const = c.constness && tcx.hir_body_const_context(tcx.local_parent(def_id)).is_none() here

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 25, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 25, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@fee1-dead fee1-dead assigned fee1-dead and unassigned mati865 Apr 25, 2026
@oli-obk oli-obk force-pushed the const-closures-everywhere branch from ee5b899 to 415454f Compare April 25, 2026 12:53
@oli-obk
Copy link
Copy Markdown
Contributor Author

oli-obk commented Apr 25, 2026

@bors r=fee1-dead rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 25, 2026

📌 Commit 415454f has been approved by fee1-dead

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 25, 2026
@fmease fmease added the PG-const-traits Project group: Const traits label Apr 25, 2026
@rust-bors rust-bors Bot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 25, 2026
@rust-bors

This comment has been minimized.

@rust-bors rust-bors Bot removed the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Apr 25, 2026
@oli-obk oli-obk force-pushed the const-closures-everywhere branch from 415454f to ae91b21 Compare April 25, 2026 18:56
@rustbot

This comment has been minimized.

@oli-obk
Copy link
Copy Markdown
Contributor Author

oli-obk commented Apr 25, 2026

@bors r=fee1-dead rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 25, 2026

📌 Commit ae91b21 has been approved by fee1-dead

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 25, 2026
@rust-log-analyzer

This comment has been minimized.

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 25, 2026
… r=fee1-dead

Check closure's constness validity in the constness query

fixes rust-lang#155584

instead of checking during ast lowering, where it's not easily possible to obtain all the right information in time. While lowering an assoc item we don't know if the parent was a const trait or a const impl. Tracing this information is quite annoying, and complicates a lot of code, which checking here after the fact is trivial.
@JonathanBrouwer
Copy link
Copy Markdown
Contributor

@bors r-
#155791 (comment)

@rust-bors rust-bors Bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 25, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 25, 2026

This pull request was unapproved.

This PR was contained in a rollup (#155791), which was unapproved.

View changes since this unapproval

oli-obk added 2 commits April 27, 2026 07:43
instead of during ast lowering, where it's not easily possible to obtain all the right information in time
@oli-obk oli-obk force-pushed the const-closures-everywhere branch from ae91b21 to 8e1c34f Compare April 27, 2026 05:45
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 27, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@oli-obk
Copy link
Copy Markdown
Contributor Author

oli-obk commented Apr 27, 2026

@bors r=fee1-dead rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 27, 2026

📌 Commit 8e1c34f has been approved by fee1-dead

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 27, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 27, 2026

⌛ Testing commit 8e1c34f with merge 7a841ad...

Workflow: https://github.com/rust-lang/rust/actions/runs/24978916136

jhpratt added a commit to jhpratt/rust that referenced this pull request Apr 27, 2026
… r=fee1-dead

Check closure's constness validity in the constness query

fixes rust-lang#155584

instead of checking during ast lowering, where it's not easily possible to obtain all the right information in time. While lowering an assoc item we don't know if the parent was a const trait or a const impl. Tracing this information is quite annoying, and complicates a lot of code, which checking here after the fact is trivial.
rust-bors Bot pushed a commit that referenced this pull request Apr 27, 2026
Check closure's constness validity in the constness query

fixes #155584

instead of checking during ast lowering, where it's not easily possible to obtain all the right information in time. While lowering an assoc item we don't know if the parent was a const trait or a const impl. Tracing this information is quite annoying, and complicates a lot of code, which checking here after the fact is trivial.
@jhpratt
Copy link
Copy Markdown
Member

jhpratt commented Apr 27, 2026

@bors yield

to encompassing rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 27, 2026

Auto build was cancelled. Cancelled workflows:

The next pull request likely to be tested is #155857.

rust-bors Bot pushed a commit that referenced this pull request Apr 27, 2026
Rollup of 2 pull requests

Successful merges:

 - #155772 (Check closure's constness validity in the constness query)
 - #155839 (Remove unnecessary requires_lto: false for eBPF)
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 27, 2026

⌛ Testing commit 8e1c34f with merge 786c9b5...

Workflow: https://github.com/rust-lang/rust/actions/runs/24982905857

rust-bors Bot pushed a commit that referenced this pull request Apr 27, 2026
Check closure's constness validity in the constness query

fixes #155584

instead of checking during ast lowering, where it's not easily possible to obtain all the right information in time. While lowering an assoc item we don't know if the parent was a const trait or a const impl. Tracing this information is quite annoying, and complicates a lot of code, which checking here after the fact is trivial.
@jhpratt
Copy link
Copy Markdown
Member

jhpratt commented Apr 27, 2026

once again after a spurious failure

@bors yield

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 27, 2026

Auto build was cancelled. Cancelled workflows:

The next pull request likely to be tested is #155863.

rust-bors Bot pushed a commit that referenced this pull request Apr 27, 2026
Rollup of 3 pull requests

Successful merges:

 - #155772 (Check closure's constness validity in the constness query)
 - #155833 (CI: rfl: move job forward to Linux v7.0)
 - #155839 (Remove unnecessary requires_lto: false for eBPF)
@rust-bors rust-bors Bot merged commit 6897624 into rust-lang:main Apr 27, 2026
11 of 12 checks passed
@rustbot rustbot added this to the 1.97.0 milestone Apr 27, 2026
rust-timer added a commit that referenced this pull request Apr 27, 2026
Rollup merge of #155772 - oli-obk:const-closures-everywhere, r=fee1-dead

Check closure's constness validity in the constness query

fixes #155584

instead of checking during ast lowering, where it's not easily possible to obtain all the right information in time. While lowering an assoc item we don't know if the parent was a const trait or a const impl. Tracing this information is quite annoying, and complicates a lot of code, which checking here after the fact is trivial.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PG-const-traits Project group: Const traits S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

const closures can't be used in impl const

8 participants