Do not optimize MIR for comptime ConstFns - #161859
Conversation
|
cc @rust-lang/clippy |
|
Thanks for the pull request, and welcome! The Rust Project has assigned @khyperia (or someone else) to review your changes, you should hear from them (or someone else) within the next two weeks. Please see the contribution instructions and our LLM policy for more information. Why was this reviewer chosen?The reviewer was selected based on:
|
|
Please let me know if there are any changes I should make or if you want me to give more information, this is my first time contributing, so am happy to learn how I can improve! I based my PR on the comments I could find in rustc_mir_transform::optimized_mir() and rustc_mir_transform::inner_optimized_mir() which seemed to me to suggest that the panic/ICE is intentional, and it's the caller's responsibility not to try to optimize MIR for comptime functions. |
There was a problem hiding this comment.
this is my first time contributing, so am happy to learn how I can improve!
welcome!! thanks so much, this looks really great! ❤️
I think the main thing here is that having some tests would be really nice. For the MIR pretty change, creating a file named... hmm, idk, maybe tests/ui/comptime/emit-mir.rs, with the contents something like this:
//@ check-pass
//@ compile-flags: --emit=mir
#![feature(rustc_attrs)]
#[rustc_comptime]
fn f() {}
fn main() {}that file is basically a test form of what you wrote, of doing rustc ./tests/ui/comptime/comptime_method_bounds.rs --emit=mir.
|
@khyperia Thank you for the feedback/help/advice! I finally got around to making those changes. I changed it so both places check whether a function is marked Thank you for taking the time out to give so much help, and let me know if there's any other changes you'd like me to make! |
|
fantastic, thank you so much! @bors r+ rollup |
…peria Do not optimize MIR for comptime ConstFns Fixed rust-lang#161770: ICE when trying to run clippy on core, etc. Two small changes, both involve checking that optimized MIR is not requested for comptime functions. First change is to Clippy, changed so impl LateLintPass for RedundantClone checks whether a function is optimizable or not (i.e., whether it is both a ConstFn && it is a runtime not a comptime function). Second change is to the pretty print function write_mir_pretty in rustc_middle, which needs the same check to make sure it does not request optimization for comptime functions, even if they're ConstFn. Can check for the original bug with the following bootstrap.toml: profile = "compiler" change-id = 160100 rust.deny-warnings = false then run the following commands from 'rust' root: ``` shell # first failure RUSTFLAGS="--emit-mir" ./x.py clippy -- -Wclippy::redundant_clone # second failure rustc ./tests/ui/comptime/comptime_method_bounds.rs --emit=mir # third failure cd library/core RUSTFLAGES="--emit=mir" cargo check ``` The first failure is fixed with just the change to impl<'tcx> LateLintPass for RedundantClone, while the second and third failures are fixed by the change to write_mir_pretty() which previously was trying to optimize MIR for comptime functions.
…uwer Rollup of 21 pull requests Successful merges: - #158609 (Update sccache to 0.16.0) - #150075 (Implement clamp_to) - #159103 (fix(reborrow): recursive implementation) - #160562 (add target feature ABI checks for SPARC) - #160848 (std: avoid aliasing violations when wrapping opaque C types) - #161421 (Include startup crt objects on WASI for more outputs) - #161805 (Prefer ambiguous candidates when deduplicating traits in scope, so `ambiguous_glob_imported_traits` doesn't depend on import order) - #161862 (Put data segment in specified section with link_section on wasm) - #161866 (delegation: add tests fixating behavior of delegating to default trait implementations) - #161456 (reduce perf impact of scalar size checks) - #161528 (Add regression test to ensure optimal compilation) - #161666 (Print vendor instructions in `x vendor`) - #161730 (Improve type mismatch annotation for lets with block-wrapped initializers) - #161828 (Never type after-stabilization cleanup) - #161859 (Do not optimize MIR for comptime ConstFns) - #161860 (atomicptr.rs test: remove unused import) - #161870 (bind to [::1] instead of 127.0.0.1 in documentation examples for v6 UDP methods) - #161876 (rustdoc: Correctly handle when a macro generates multiple items in `--generate-macro-expansion`) - #161889 (Add link to ownership section in ptr::read docs) - #161890 (rustdoc: some clarifying comments) - #161891 (Mark `extern_item_impls` feature as incomplete) Failed merges: - #161702 (Use `drop_guard` in some places in {core,alloc,std})
…uwer Rollup of 21 pull requests Successful merges: - #150075 (Implement clamp_to) - #159103 (fix(reborrow): recursive implementation) - #160562 (add target feature ABI checks for SPARC) - #160848 (std: avoid aliasing violations when wrapping opaque C types) - #161421 (Include startup crt objects on WASI for more outputs) - #161805 (Prefer ambiguous candidates when deduplicating traits in scope, so `ambiguous_glob_imported_traits` doesn't depend on import order) - #161862 (Put data segment in specified section with link_section on wasm) - #161866 (delegation: add tests fixating behavior of delegating to default trait implementations) - #157218 (Track items behind `cfg_select` in the same way we do for `cfg`) - #161456 (reduce perf impact of scalar size checks) - #161528 (Add regression test to ensure optimal compilation) - #161666 (Print vendor instructions in `x vendor`) - #161730 (Improve type mismatch annotation for lets with block-wrapped initializers) - #161828 (Never type after-stabilization cleanup) - #161859 (Do not optimize MIR for comptime ConstFns) - #161860 (atomicptr.rs test: remove unused import) - #161870 (bind to [::1] instead of 127.0.0.1 in documentation examples for v6 UDP methods) - #161876 (rustdoc: Correctly handle when a macro generates multiple items in `--generate-macro-expansion`) - #161889 (Add link to ownership section in ptr::read docs) - #161890 (rustdoc: some clarifying comments) - #161891 (Mark `extern_item_impls` feature as incomplete) Failed merges: - #161702 (Use `drop_guard` in some places in {core,alloc,std})
Rollup merge of #161859 - aaronrumph:issue-161770-fix, r=khyperia Do not optimize MIR for comptime ConstFns Fixed #161770: ICE when trying to run clippy on core, etc. Two small changes, both involve checking that optimized MIR is not requested for comptime functions. First change is to Clippy, changed so impl LateLintPass for RedundantClone checks whether a function is optimizable or not (i.e., whether it is both a ConstFn && it is a runtime not a comptime function). Second change is to the pretty print function write_mir_pretty in rustc_middle, which needs the same check to make sure it does not request optimization for comptime functions, even if they're ConstFn. Can check for the original bug with the following bootstrap.toml: profile = "compiler" change-id = 160100 rust.deny-warnings = false then run the following commands from 'rust' root: ``` shell # first failure RUSTFLAGS="--emit-mir" ./x.py clippy -- -Wclippy::redundant_clone # second failure rustc ./tests/ui/comptime/comptime_method_bounds.rs --emit=mir # third failure cd library/core RUSTFLAGES="--emit=mir" cargo check ``` The first failure is fixed with just the change to impl<'tcx> LateLintPass for RedundantClone, while the second and third failures are fixed by the change to write_mir_pretty() which previously was trying to optimize MIR for comptime functions.
View all comments
Fixed #161770: ICE when trying to run clippy on core, etc. Two small changes, both involve checking that optimized MIR is not requested for comptime functions.
First change is to Clippy, changed so impl LateLintPass for RedundantClone checks whether a function is optimizable or not (i.e., whether it is both a ConstFn && it is a runtime not a comptime function).
Second change is to the pretty print function write_mir_pretty in rustc_middle, which needs the same check to make sure it does not request optimization for comptime functions, even if they're ConstFn.
Can check for the original bug with the following bootstrap.toml:
profile = "compiler"
change-id = 160100
rust.deny-warnings = false
then run the following commands from 'rust' root:
The first failure is fixed with just the change to impl<'tcx> LateLintPass for RedundantClone, while the second and third failures are fixed by the change to write_mir_pretty() which previously was trying to optimize MIR for comptime functions.