Skip to content

Enforce that we don't generate code for comptime fns - #159777

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
oli-obk:really-comptime-this-time
Jul 27, 2026
Merged

Enforce that we don't generate code for comptime fns#159777
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
oli-obk:really-comptime-this-time

Conversation

@oli-obk

@oli-obk oli-obk commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

... by ICEing if you try to compute a symbol for them. There are various other ways we can assert it, but they were all a bit icky or already unreachable.

r? @fmease (since you asked about it in #159727 (comment))

@rustbot

rustbot commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @RalfJung, @lcnr

@rustbot rustbot added 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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 23, 2026
@oli-obk

oli-obk commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 23, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jul 23, 2026
Enforce that we don't generate code for comptime fns
@RalfJung

Copy link
Copy Markdown
Member

Sounds like Miri's find_mir_or_eval_fn should also ICE in that case?

@oli-obk

oli-obk commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

I'm not worried about miri being able to reach this. There are enough safeguards to fail compilation before that happens. This was just about codegen picking what to codegen via various independent means, and I overlooked one.

@rust-bors

rust-bors Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 2fa05b3 (2fa05b353b7076021011f04d6af42b83619e91ce)
Base parent: 165cce8 (165cce8d820b229af8f6a8226cf0b910b57600ff)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (2fa05b3): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This perf run didn't have relevant results for this metric.

Max RSS (memory usage)

Results (primary -3.7%, secondary 2.5%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.1% [1.1%, 1.1%] 1
Regressions ❌
(secondary)
2.5% [2.5%, 2.5%] 1
Improvements ✅
(primary)
-8.5% [-8.5%, -8.5%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -3.7% [-8.5%, 1.1%] 2

Cycles

This perf run didn't have relevant results for this metric.

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 485.902s -> 487.995s (0.43%)
Artifact size: 387.66 MiB -> 387.64 MiB (-0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 23, 2026
Comment on lines +86 to +90
DefKind::Fn | DefKind::AssocFn
if tcx.constness(def_id) == hir::Constness::Const { always: true } =>
{
return None;
}

@fmease fmease Jul 26, 2026

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.

From what I can gather, you modified this function so (among other things) tcx.is_reachable_non_generic in RootCollector::is_root returns false for comptime functions when collecting mono items (collect_roots < collect_crate_mono_items).

However, under MonoItemCollectionStrategy::Eager (-Clink-dead-code) is_root doesn't consider is_reachable_non_generic, meaning the following two snippets of code both ICE in debug mode if -Clink-dead-code is passed

#![feature(rustc_attrs)] // for `rustc_comptime`
#![crate_type = "lib"]

#[rustc_comptime]
pub fn f() { std::mem::offset_of!(((),), 0); }
//        => std::intrinsics::offset_of::<((),)>(0, 0)
#![feature(rustc_attrs)] // for `rustc_comptime`
#![crate_type = "lib"]

#[rustc_comptime]
pub fn f() { size_of::<()>(); }

with

thread 'rustc' (60874) panicked at compiler/rustc_symbol_mangling/src/lib.rs:246:9:
assertion failed: tcx.constness(instance.def_id()) != hir::Constness::Const { always: true }

The code above doesn't ICE in release mode.

However, calling these comptime intrinsics directly if -Clink-dead-code is passed also ICEs in release mode but it requires feature core_intrinsics of course & they already ICE on nightly, so this might be considered WONTFIX?:

#![feature(rustc_attrs)] // for `rustc_comptime`
#![feature(core_intrinsics)] // well...
#![crate_type = "lib"]

#[rustc_comptime] pub fn f() { std::intrinsics::size_of::<()>(); }
error: internal compiler error: compiler/rustc_codegen_ssa/src/mir/intrinsic.rs:140:21: Nullary intrinsic size_of must be called in a const block. If you are seeing this message from code outside the standard library, the unstable implementation details of the relevant intrinsic may have changed. Consider using stable APIs instead. If you are adding a new nullary intrinsic that is inherently a runtime intrinsic, update this check.
 --> <anon>:1:32
  |
1 | #[rustc_comptime] pub fn f() { std::intrinsics::size_of::<()>(); }
  |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#![feature(rustc_attrs)] // for `rustc_comptime`
#![feature(core_intrinsics)] // well...
#![crate_type = "lib"]

#[rustc_comptime] pub fn f() { std::intrinsics::offset_of::<((),)>(0, 0); }
error: internal compiler error: compiler/rustc_codegen_ssa/src/mir/block.rs:1075:37: intrinsic offset_of must be overridden by codegen backend, but isn't
 --> <anon>:1:32
  |
1 | #[rustc_comptime] pub fn f() { std::intrinsics::offset_of::<((),)>(0, 0); }
  |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@fmease fmease Jul 26, 2026

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.

If you consider this acceptable for the time being (after all, use of -Clink-dead-code seems discouraged & it seems to require #![feature(rustc_attrs)] unless there's a different way to repro it w/o nightly features?), feel free to r=me w/ or w/o a FIXME.

I'm not super accustomed to this part of the compiler, so the fact that the current approach ICEs under -Clink-dead-code naively leads me to believe that there might be better places to put the Const { always: true } check(s)...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I initially had the check only in monomorphization, but that didn't work out fully (got linker errors 😨 ). So I decided to just add the ICE and avoid it, which immediately pointed me to the reachable_non_generics query. That eagerly collects the symbols (for stable sorting), so I can't prevent it just at the call sites.

Managed to confuse myself a few times in between, but I ended up removing all "redundant" (or not redundant, as you figured out) checks from mono collection. So we may need both after all. While I never understood the reason for link-dead-code's existence, I'll try to not break it more than it is considering the many issues about it.

@fmease fmease 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 Jul 26, 2026
@fmease

fmease commented Jul 26, 2026

Copy link
Copy Markdown
Member

Thanks, I love it!

r=me w/ or w/o a FIXME if you don't consider #159777 (comment) to be a concern for the time being.

@oli-obk
oli-obk force-pushed the really-comptime-this-time branch from 8648a76 to 4609fbc Compare July 27, 2026 15:22
@oli-obk

oli-obk commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

The extra check only runs in eager mode, so we don't pay the cost for it except in link-dead-code mode, which... is already expensive as it is.

@bors r=fmease

@rust-bors

rust-bors Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 4609fbc has been approved by fmease

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

Reason for tree closure: spurious failures

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 27, 2026
rust-bors Bot pushed a commit that referenced this pull request Jul 27, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - #160009 (`rust-analyzer` subtree update)
 - #159833 (ci: Make the `x86_64-gnu-parallel-frontend` job non-optional)
 - #158873 (applying `/Brepro` in bootstrap on MSVC and adding a run-make test.)
 - #159777 (Enforce that we don't generate code for comptime fns)
 - #159997 (On many bindings with move error, limit the number of `Span`s)
 - #158205 (fix: defer const normalize in coherence mode)
 - #159312 (Windows implementation for stdio set/take/replace)
 - #159772 (Fix lowering of resolved const inference variables)
 - #159785 (Share _Unwind_Exception definition between native and wasm)
 - #159968 (Fix the const impl suggestion)
 - #159974 (Update thinvec to 0.2.19)
 - #160013 (tests/ui: Ignore one query cycle test in parallel frontend mode)
 - #160016 (compiletest: do not talk about JSON when the user never sees any)
 - #160018 (test suite: add ARM case to ABI-required target feature check for -Ctarget-cpu)
@rust-bors
rust-bors Bot merged commit e51da70 into rust-lang:main Jul 27, 2026
13 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Jul 27, 2026
rust-timer added a commit that referenced this pull request Jul 27, 2026
Rollup merge of #159777 - oli-obk:really-comptime-this-time, r=fmease

Enforce that we don't generate code for comptime fns

... by ICEing if you try to compute a symbol for them. There are various other ways we can assert it, but they were all a bit icky or already unreachable.

r? @fmease (since you asked about it in #159727 (comment))
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-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants