Fix ICE when const closure appears inside a non-const trait method#154957
Fix ICE when const closure appears inside a non-const trait method#154957rust-bors[bot] merged 1 commit intorust-lang:mainfrom
Conversation
|
rustbot has assigned @dingxiangfei2009. Use Why was this reviewer chosen?The reviewer was selected based on:
|
There was a problem hiding this comment.
I'm starting to think it would be better to explain why this patch includes two tests. These are both reproduction cases from the original issue, but I added both because the paths for determining parent constness differ until hir_body_const_context() returns None.
6493583 to
394131f
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. |
|
Yea, it's def a better fix than what the other PRs attempted. I tried finding a way that rejects this earlier, but it doesn't seem obvious to me how to do it robustly. Your fix feels like it carries a risk because we are ignoring a buggy situation, but maybe that can be alleviated by asserting that an error has already been reported |
I see, in that case, would using edit: like BodyOwnerKind::Closure if self.is_const_fn(def_id) => {
if let Some(ctx) = self.hir_body_const_context(self.local_parent(local_def_id)) {
return Some(ctx);
}
self.dcx().span_delayed_bug(
self.def_span(def_id),
"msg",
);
return Some(ConstContext::ConstFn);
} |
|
That works, but in this case we can even just be an assert that errors were reported. We have like 2 of them in rustc, and I'm on mobile, so I can't find the function rn, but there is a function for getting an ErrorReported if any errors were emitted |
394131f to
9be8fca
Compare
Maybe you mean |
Rollup of 7 pull requests Successful merges: - #155469 (Account for titlecase in casing lints) - #155644 (delegation: support self ty propagation for functions in free to trait reuse) - #154957 (Fix ICE when const closure appears inside a non-const trait method) - #155442 (Change keyword order for `impl` restrictions) - #155561 (Use singular wording for single _ placeholders in type suggestions) - #155637 (Fix E0191 suggestion for empty dyn trait args) - #155661 (Remove `AttributeLintKind` variants - part 6)
Rollup merge of #154957 - lapla-cogito:issue_153891, r=oli-obk Fix ICE when const closure appears inside a non-const trait method Fixes #153891 `hir_body_const_context()` unconditionally delegated to the parent's const context for const closures, returning `None` when the parent had no const context. This caused `mir_const_qualif()` to hit a `span_bug!`, since `mir_promoted()` had already decided to call it based on the closure's own syntactic constness. Fall back to `ConstContext::ConstFn` when the parent's const context is `None`, so that the const closure body is still properly const-checked rather than triggering an ICE. Examining [another attempt](https://github.com/rust-lang/rust/pull/153900/changes) at this issue (which has already been closed), I thought that its approach represents a workaround fix to avoid inconsistencies in the caller of `mir_promoted()`, whereas I think the correct behavior is for `hir_body_const_context()` itself to return the proper value.
Fixes #153891
hir_body_const_context()unconditionally delegated to the parent's const context for const closures, returningNonewhen the parent had no const context. This causedmir_const_qualif()to hit aspan_bug!, sincemir_promoted()had already decided to call it based on the closure's own syntactic constness. Fall back toConstContext::ConstFnwhen the parent's const context isNone, so that the const closure body is still properly const-checked rather than triggering an ICE.Examining another attempt at this issue (which has already been closed), I thought that its approach represents a workaround fix to avoid inconsistencies in the caller of
mir_promoted(), whereas I think the correct behavior is forhir_body_const_context()itself to return the proper value.