Add very basic "comptime" fn implementation by oli-obk · Pull Request #148820 · rust-lang/rust

8 min read Original article ↗

@rustbot rustbot added A-attributes

Area: Attributes (`#[…]`, `#![…]`)

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-rustdoc

Relevant to the rustdoc team, which will review and decide on the PR/issue.

T-rustdoc-frontend

Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

T-rustfmt

Relevant to the rustfmt team, which will review and decide on the PR/issue.

labels

Nov 11, 2025

RalfJung

fmease

fmease

fee1-dead

jdonszelmann

jdonszelmann

jdonszelmann

jdonszelmann

@rust-bors rust-bors Bot 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-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

labels

Jun 8, 2026

@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

Jun 8, 2026

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request

Jun 8, 2026
Add very basic "comptime" fn implementation

Implements functions that cannot be called at runtime (and thus also not from normal const functions, as those could be called from runtime).

This is done via the internal attribute `rustc_comptime` that can be added to normal functions, turning them into compile-time-only functions.

Because @fee1-dead and @compiler-errors did amazing work, we even get trait bounds that work inside comptime fns: via unconditionally-const `const Trait` bounds.

Use cases are

* rust-lang#146923
* const heap intrinsics
* and the other various intrinsics (e.g. size_of 😆) that will just ICE in codegen or panic at runtime if they actually end up in runtime code

project goal issue: rust-lang/rust-project-goals#406

no tracking issue until we have a feature gate and some sort of syntax

cc @scottmcm as the T-lang goal champion

This was referenced

Jun 8, 2026

rust-bors Bot pushed a commit that referenced this pull request

Jun 8, 2026
…uwer

Rollup of 13 pull requests

Successful merges:

 - #147302 (asm! support for the Xtensa architecture)
 - #148820 (Add very basic "comptime" fn implementation)
 - #157299 (Fix unstable diagnostics in tests)
 - #143511 (Improve TLS codegen by marking the panic/init path as cold)
 - #154608 (Add `_value` API for number literals in proc-macro)
 - #156762 (xfs support in `test_rename_directory_to_non_empty_directory`)
 - #157300 (Relax test requirements for consistency)
 - #157383 (tests: codegen-llvm: Ignore BPF targets in c-variadic-opt)
 - #157413 (fix: don't suggest .into_iter() for .cloned()/.copied() on non-reference Option)
 - #157578 (Fix diagnostics for non-exhaustive destructuring assignments (#157553))
 - #157587 (explain that the size_of constant also serves to avoid optimizing away 'unused' size_of calls)
 - #157596 (test: remove ineffective link-extern-crate-with-drop-type test)
 - #157602 (rustdoc: Remove unnecessary fast path)

rust-timer added a commit that referenced this pull request

Jun 8, 2026
Rollup merge of #148820 - oli-obk:comptime, r=fee1-dead

Add very basic "comptime" fn implementation

Implements functions that cannot be called at runtime (and thus also not from normal const functions, as those could be called from runtime).

This is done via the internal attribute `rustc_comptime` that can be added to normal functions, turning them into compile-time-only functions.

Because @fee1-dead and @compiler-errors did amazing work, we even get trait bounds that work inside comptime fns: via unconditionally-const `const Trait` bounds.

Use cases are

* #146923
* const heap intrinsics
* and the other various intrinsics (e.g. size_of 😆) that will just ICE in codegen or panic at runtime if they actually end up in runtime code

project goal issue: rust-lang/rust-project-goals#406

no tracking issue until we have a feature gate and some sort of syntax

cc @scottmcm as the T-lang goal champion

jhpratt added a commit to jhpratt/rust that referenced this pull request

Jun 10, 2026
…e1-dead

Start using comptime for reflection intrinsics and their wrapper functions

r? @fee1-dead

follow-up to rust-lang#148820

I needed to add some more rustc code because turns out I forgot to add tests to the previous PR which actually codegen while using comptime fns. Check builds never try to create optimized MIR, so no code ever hit the assert that prevents creating optimized MIR for comptime fns.

rust-bors Bot pushed a commit that referenced this pull request

Jun 10, 2026
Start using comptime for reflection intrinsics and their wrapper functions

r? @fee1-dead 

follow-up to #148820 

I needed to add some more rustc code because turns out I forgot to add tests to the previous PR which actually codegen while using comptime fns. Check builds never try to create optimized MIR, so no code ever hit the assert that prevents creating optimized MIR for comptime fns.

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request

Jun 10, 2026
…e1-dead

Start using comptime for reflection intrinsics and their wrapper functions

r? @fee1-dead

follow-up to rust-lang#148820

I needed to add some more rustc code because turns out I forgot to add tests to the previous PR which actually codegen while using comptime fns. Check builds never try to create optimized MIR, so no code ever hit the assert that prevents creating optimized MIR for comptime fns.

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request

Jun 10, 2026
…e1-dead

Start using comptime for reflection intrinsics and their wrapper functions

r? @fee1-dead

follow-up to rust-lang#148820

I needed to add some more rustc code because turns out I forgot to add tests to the previous PR which actually codegen while using comptime fns. Check builds never try to create optimized MIR, so no code ever hit the assert that prevents creating optimized MIR for comptime fns.

jhpratt added a commit to jhpratt/rust that referenced this pull request

Jun 10, 2026
…e1-dead

Start using comptime for reflection intrinsics and their wrapper functions

r? @fee1-dead

follow-up to rust-lang#148820

I needed to add some more rustc code because turns out I forgot to add tests to the previous PR which actually codegen while using comptime fns. Check builds never try to create optimized MIR, so no code ever hit the assert that prevents creating optimized MIR for comptime fns.

jhpratt added a commit to jhpratt/rust that referenced this pull request

Jun 11, 2026
…e1-dead

Start using comptime for reflection intrinsics and their wrapper functions

r? @fee1-dead

follow-up to rust-lang#148820

I needed to add some more rustc code because turns out I forgot to add tests to the previous PR which actually codegen while using comptime fns. Check builds never try to create optimized MIR, so no code ever hit the assert that prevents creating optimized MIR for comptime fns.

jhpratt added a commit to jhpratt/rust that referenced this pull request

Jun 11, 2026
…e1-dead

Start using comptime for reflection intrinsics and their wrapper functions

r? @fee1-dead

follow-up to rust-lang#148820

I needed to add some more rustc code because turns out I forgot to add tests to the previous PR which actually codegen while using comptime fns. Check builds never try to create optimized MIR, so no code ever hit the assert that prevents creating optimized MIR for comptime fns.

jhpratt added a commit to jhpratt/rust that referenced this pull request

Jun 11, 2026
…e1-dead

Start using comptime for reflection intrinsics and their wrapper functions

r? @fee1-dead

follow-up to rust-lang#148820

I needed to add some more rustc code because turns out I forgot to add tests to the previous PR which actually codegen while using comptime fns. Check builds never try to create optimized MIR, so no code ever hit the assert that prevents creating optimized MIR for comptime fns.

jhpratt added a commit to jhpratt/rust that referenced this pull request

Jun 11, 2026
…e1-dead

Start using comptime for reflection intrinsics and their wrapper functions

r? @fee1-dead

follow-up to rust-lang#148820

I needed to add some more rustc code because turns out I forgot to add tests to the previous PR which actually codegen while using comptime fns. Check builds never try to create optimized MIR, so no code ever hit the assert that prevents creating optimized MIR for comptime fns.

jhpratt added a commit to jhpratt/rust that referenced this pull request

Jun 11, 2026
…e1-dead

Start using comptime for reflection intrinsics and their wrapper functions

r? @fee1-dead

follow-up to rust-lang#148820

I needed to add some more rustc code because turns out I forgot to add tests to the previous PR which actually codegen while using comptime fns. Check builds never try to create optimized MIR, so no code ever hit the assert that prevents creating optimized MIR for comptime fns.

rust-timer added a commit that referenced this pull request

Jun 11, 2026
Rollup merge of #157647 - oli-obk:start-using-comptime, r=fee1-dead

Start using comptime for reflection intrinsics and their wrapper functions

r? @fee1-dead

follow-up to #148820

I needed to add some more rustc code because turns out I forgot to add tests to the previous PR which actually codegen while using comptime fns. Check builds never try to create optimized MIR, so no code ever hit the assert that prevents creating optimized MIR for comptime fns.