Skip to content

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

Open
oli-obk wants to merge 2 commits intorust-lang:mainfrom
oli-obk:const-closures-everywhere
Open

Check closure's constness validity in the constness query#155772
oli-obk wants to merge 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

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 added 2 commits April 25, 2026 20:54
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 415454f to ae91b21 Compare April 25, 2026 18:56
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 25, 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 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
Copy link
Copy Markdown
Collaborator

The job tidy failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)

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

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-author Status: This is awaiting some action (such as code changes or more information) from the author. 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

7 participants