Skip to content

Do not optimize MIR for comptime ConstFns - #161859

Merged
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
aaronrumph:issue-161770-fix
Aug 28, 2026
Merged

Do not optimize MIR for comptime ConstFns#161859
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
aaronrumph:issue-161770-fix

Conversation

@aaronrumph

@aaronrumph aaronrumph commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

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:

# 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.

@rustbot

rustbot commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

clippy is developed in its own repository. If possible, consider making this change to rust-lang/rust-clippy instead.

cc @rust-lang/clippy

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 27, 2026
@rustbot

rustbot commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

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:

  • Owners of files modified in this PR: compiler, mir
  • compiler, mir expanded to 75 candidates
  • Random selection from 19 candidates

@aaronrumph

Copy link
Copy Markdown
Contributor Author

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.

Comment thread compiler/rustc_middle/src/mir/pretty.rs Outdated

@khyperia khyperia left a comment

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.

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.

View changes since this review

Comment thread src/tools/clippy/clippy_lints/src/redundant_clone.rs Outdated
@aaronrumph

Copy link
Copy Markdown
Contributor Author

@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 #[rustc_comptime] using constness() as suggested, and added tests for both files I changed (which are passing on my machine). I formatted it as two new commits, the first has changes to [compiler/rustc_middle/src/mir/pretty.rs] and the accompanying test in [tests/ui/comptime/emit-mir.rs], and the second has the changes to [src/tools/clippy/clippy_lints/src/redundant_clone.rs] and the test (and accompanying files) in [src/tools/clippy/tests/ui/redundant_clone-comptime.rs]. I'm not sure if this was the right way to format my changes, so let me know if you would prefer if I squashed my changes into one commit!

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!

@khyperia

Copy link
Copy Markdown
Member

fantastic, thank you so much!

@bors r+ rollup

@rust-bors

rust-bors Bot commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 282a6c7 has been approved by khyperia

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-review Status: Awaiting review from the assignee but also interested parties. labels Aug 28, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 28, 2026
…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.
rust-bors Bot pushed a commit that referenced this pull request Aug 28, 2026
…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})
rust-bors Bot pushed a commit that referenced this pull request Aug 28, 2026
…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})
@rust-bors
rust-bors Bot merged commit db1db72 into rust-lang:main Aug 28, 2026
13 checks passed
@rustbot rustbot added this to the 1.100.0 milestone Aug 28, 2026
rust-bors Bot pushed a commit that referenced this pull request Aug 28, 2026
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.
@aaronrumph
aaronrumph deleted the issue-161770-fix branch August 29, 2026 00:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-clippy Relevant to the Clippy team. 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.

ICE: rustc_comptime: do not use 'optimized_mir' for constants when using --emit=mir/clippy on core

3 participants