Check closure's constness validity in the constness query#155772
Check closure's constness validity in the constness query#155772oli-obk wants to merge 2 commits intorust-lang:mainfrom
Conversation
|
r? @mati865 rustbot has assigned @mati865. Use Why was this reviewer chosen?The reviewer was selected based on:
|
| | | ||
| LL | (const || { (()).foo() })(); | ||
| | ^^^^^ | ||
| | ^^^^^^^^ |
There was a problem hiding this comment.
The only downside of this PR is that we highlight more than the const part of the closure now
| 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"); |
There was a problem hiding this comment.
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
| match c.constness { | ||
| Constness::Const => if tcx.hir_body_const_context(tcx.local_parent(def_id)).is_none() { |
There was a problem hiding this comment.
I would use if let Constness::Const = c.constness && tcx.hir_body_const_context(tcx.local_parent(def_id)).is_none() here
|
Reminder, once the PR becomes ready for a review, use |
ee5b899 to
415454f
Compare
|
@bors r=fee1-dead rollup |
This comment has been minimized.
This comment has been minimized.
instead of during ast lowering, where it's not easily possible to obtain all the right information in time
415454f to
ae91b21
Compare
|
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. |
|
@bors r=fee1-dead rollup |
|
The job Click to see the possible cause of the failure (guessed by this bot) |
… 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.
|
This pull request was unapproved. This PR was contained in a rollup (#155791), which was unapproved. |
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.