Always use ConstFn context for const closures#155808
Always use ConstFn context for const closures#155808lapla-cogito wants to merge 1 commit intorust-lang:mainfrom
ConstFn context for const closures#155808Conversation
|
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
|
|
|
Hmmm. We shouldn't ever invoke optimized_mir on const closures defined in const items I think? Tho I guess with an opaque type as the const's type we could indeed return a const closure. Or just via a const item of fn ptr type? I'll need to think about this some more, but if you have any thoughts about it lmk. Please add tests for both of these situations, too. |
This comment has been minimized.
This comment has been minimized.
dc90d1f to
48dc828
Compare
Is this because const items are only evaluated during CTFE (IIUC)? If so, the examples you mention are counterexamples.
I've added a test for the fn ptr type variant, which causes an ICE on current nightly. I also tried opaque-type variants, but they were rejected by unrelated checks before reaching the ICE path 🤔 |
|
I did some digging in the compiler, and I think the right solution is to treat all const closures as
|
This comment has been minimized.
This comment has been minimized.
BodyOwnerKind instead of const contextConstFn context for const closures
|
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. |
Always use `ConstFn` context for `const` closures fixes rust-lang#155803 Since rust-lang@e8a4611, `hir_body_const_context()` returned the parent's const context for a `const` closure. But a `const` closure can escape its enclosing body via a `fn`-pointer-typed const item or an opaque return type and be invoked at runtime, so it must be const-checked under the most restrictive `ConstFn` context like a regular `const fn`. r? oli-obk (since you authored the commit mentioned above, feel free to reroll)
Always use `ConstFn` context for `const` closures fixes rust-lang#155803 Since rust-lang@e8a4611, `hir_body_const_context()` returned the parent's const context for a `const` closure. But a `const` closure can escape its enclosing body via a `fn`-pointer-typed const item or an opaque return type and be invoked at runtime, so it must be const-checked under the most restrictive `ConstFn` context like a regular `const fn`. r? oli-obk (since you authored the commit mentioned above, feel free to reroll)
Rollup of 12 pull requests Successful merges: - #155341 (generic_const_args: allow paths to non type consts) - #156062 (Added command-line argument support for `wasm32-wali-linux-musl`) - #156159 ([AIX] add -bdbg:namedsects:ss link arg) - #156174 (Wasm: remove implicit `__heap_base`/`__data_end` exports) - #156186 (fix: remap ci-llvm debug paths via `-ffile-prefix-map`) - #156193 (port `rustc_ast*` crates from `box_` to `deref_patterns`) - #156201 (Don't run ui-fulldeps tests twice in stage 1) - #155808 (Always use `ConstFn` context for `const` closures) - #156105 (interpret: correctly deal with repr(transparent) enums) - #156148 (Use `all_impls` instead of handrolling it) - #156156 (Adjust getMCSubtargetInfo signature for LLVM 23+) - #156205 (move generalization test)
Always use `ConstFn` context for `const` closures fixes rust-lang#155803 Since rust-lang@e8a4611, `hir_body_const_context()` returned the parent's const context for a `const` closure. But a `const` closure can escape its enclosing body via a `fn`-pointer-typed const item or an opaque return type and be invoked at runtime, so it must be const-checked under the most restrictive `ConstFn` context like a regular `const fn`. r? oli-obk (since you authored the commit mentioned above, feel free to reroll)
Rollup of 15 pull requests Successful merges: - #151122 (fix: more descriptive error message for enum to integer) - #155341 (generic_const_args: allow paths to non type consts) - #156062 (Added command-line argument support for `wasm32-wali-linux-musl`) - #156159 ([AIX] add -bdbg:namedsects:ss link arg) - #156174 (Wasm: remove implicit `__heap_base`/`__data_end` exports) - #156186 (fix: remap ci-llvm debug paths via `-ffile-prefix-map`) - #156193 (port `rustc_ast*` crates from `box_` to `deref_patterns`) - #156201 (Don't run ui-fulldeps tests twice in stage 1) - #155808 (Always use `ConstFn` context for `const` closures) - #156105 (interpret: correctly deal with repr(transparent) enums) - #156148 (Use `all_impls` instead of handrolling it) - #156156 (Adjust getMCSubtargetInfo signature for LLVM 23+) - #156170 (add known-bug test for coroutine 'static-yields-non-'static unsoundness (#144442)) - #156195 (Move tests codegen) - #156205 (move generalization test)
fixes #155803
Since e8a4611,
hir_body_const_context()returned the parent's const context for aconstclosure. But aconstclosure can escape its enclosing body via afn-pointer-typed const item or an opaque return type and be invoked at runtime, so it must be const-checked under the most restrictiveConstFncontext like a regularconst fn.r? oli-obk (since you authored the commit mentioned above, feel free to reroll)