Skip to content

Don't call mir_const_qualif on functions without const bodies#153900

Closed
ViewWay wants to merge 1 commit intorust-lang:mainfrom
ViewWay:fix/ice-153891-mir-const
Closed

Don't call mir_const_qualif on functions without const bodies#153900
ViewWay wants to merge 1 commit intorust-lang:mainfrom
ViewWay:fix/ice-153891-mir-const

Conversation

@ViewWay
Copy link

@ViewWay ViewWay commented Mar 15, 2026

Fixes #153891.

When mir_promoted checked tcx.constness(def) for functions/closures, it returned Constness::Const for trait methods in const traits even when they don't have an actual const body.

The fix uses tcx.hir_body_const_context(def).is_some() instead, which correctly returns None for functions that don't have a const body to check. This matches what mir_const_qualif itself uses internally (ConstCx::new calls hir_body_const_context), ensuring we only call mir_const_qualif when it won't ICE.

@rustbot
Copy link
Collaborator

rustbot commented Mar 15, 2026

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

HIR ty lowering was modified

cc @fmease

Some changes occurred in compiler/rustc_sanitizers

cc @rcvalle

@rustbot rustbot added PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 15, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 15, 2026

r? @jackh726

rustbot has assigned @jackh726.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 15 candidates

@rustbot

This comment has been minimized.

Copy link
Member

@fmease fmease left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the love of god, please drop your cherry-pick of the commit I authored in open PR RUST-153738. It makes no sense whatsoever to take ownership of it & to include it here and in your ~5 other PRs. Your PRs can't proceed otherwise.

View changes since this review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 15, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 15, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

The issue occurred when mir_promoted checked tcx.constness(def) for
functions/closures, which returns Constness::Const for trait methods
in const traits even when they don't have an actual const body.

The fix uses tcx.hir_body_const_context(def).is_some() instead, which
correctly returns None for functions that don't have a const body to
check. This matches what mir_const_qualif itself uses internally
(ConstCx::new calls hir_body_const_context), ensuring we only call
mir_const_qualif when it won't ICE.
@rustbot
Copy link
Collaborator

rustbot commented Mar 15, 2026

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.

@oli-obk oli-obk closed this Mar 15, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Mar 15, 2026
@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-21-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
Executing "/scripts/stage_2_test_set1.sh"
+ /scripts/stage_2_test_set1.sh
PR_CI_JOB set; skipping tidy
+ '[' 1 == 1 ']'
+ echo 'PR_CI_JOB set; skipping tidy'
+ SKIP_TIDY='--skip tidy'
+ ../x.py --stage 2 test --skip tidy --skip compiler --skip src
##[group]Building bootstrap
    Finished `dev` profile [unoptimized] target(s) in 0.04s
##[endgroup]
downloading https://static.rust-lang.org/dist/2026-03-05/rustfmt-nightly-aarch64-unknown-linux-gnu.tar.xz
---

failures:

---- [ui] tests/ui/mir/ice-mir-const-qualif-153891.rs stdout ----
Saved the actual stderr to `/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/mir/ice-mir-const-qualif-153891/ice-mir-const-qualif-153891.stderr`
normalized stderr:
error[E0379]: functions in traits cannot be declared const
##[error]  --> $DIR/ice-mir-const-qualif-153891.rs:9:5
   |
LL |     const fn test() {
---

error[E0658]: const closures are experimental
##[error]  --> $DIR/ice-mir-const-qualif-153891.rs:10:10
   |
LL |         (const || {})()
   |          ^^^^^
   |
   = note: see issue #106003 <https://github.com/rust-lang/rust/issues/106003> for more information
   = help: add `#![feature(const_closures)]` to the crate attributes to enable
   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0379, E0658.
For more information about an error, try `rustc --explain E0379`.
---
To only update this specific test, also pass `--test-args mir/ice-mir-const-qualif-153891.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/mir/ice-mir-const-qualif-153891.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/mir/ice-mir-const-qualif-153891" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error[E0379]: functions in traits cannot be declared const
##[error]  --> /checkout/tests/ui/mir/ice-mir-const-qualif-153891.rs:9:5
   |
---

error[E0658]: const closures are experimental
##[error]  --> /checkout/tests/ui/mir/ice-mir-const-qualif-153891.rs:10:10
   |
LL |         (const || {})()
   |          ^^^^^
   |
   = note: see issue #106003 <https://github.com/rust-lang/rust/issues/106003> for more information
   = help: add `#![feature(const_closures)]` to the crate attributes to enable
   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0379, E0658.
For more information about an error, try `rustc --explain E0379`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PG-exploit-mitigations Project group: Exploit mitigations 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]: mir_const_qualif should only be called on const fns and const items

6 participants