Skip to content

Add very basic "comptime" fn implementation#148820

Merged
rust-bors[bot] merged 7 commits into
rust-lang:mainfrom
oli-obk:comptime
Jun 8, 2026
Merged

Add very basic "comptime" fn implementation#148820
rust-bors[bot] merged 7 commits into
rust-lang:mainfrom
oli-obk:comptime

Conversation

@oli-obk

@oli-obk oli-obk commented Nov 11, 2025

Copy link
Copy Markdown
Contributor

View all comments

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

  • Reflection MVP #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

@rustbot

rustbot commented Nov 11, 2025

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann

Some changes occurred in compiler/rustc_hir/src/attrs

cc @jdonszelmann

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

@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
@rustbot

rustbot commented Nov 11, 2025

Copy link
Copy Markdown
Collaborator

r? @JonathanBrouwer

rustbot has assigned @JonathanBrouwer.
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

@rust-log-analyzer

This comment has been minimized.

@rustbot

rustbot commented Nov 11, 2025

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Comment thread compiler/rustc_hir/src/hir.rs Outdated
Comment thread compiler/rustc_parse/src/parser/item.rs Outdated
@rust-log-analyzer

This comment has been minimized.

@@ -4225,13 +4227,15 @@ impl fmt::Display for Safety {

#[derive(Copy, Clone, PartialEq, Eq, Debug, Encodable, Decodable, HashStable_Generic)]
pub enum Constness {

@fmease fmease Nov 11, 2025

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.

Personally speaking I would rename the variants like so:

pub enum Constness {
    Always, // `#[rustc_comptime]`
    Maybe, // `const`
    Never,
}

to mirror BoundConstness which is Always (for const), Maybe (for [const]) and Never. This makes it crystal clear what they represent and leaves no room for ambiguity.

If it were up to me, I would even rename Constness to ItemConstness if traits will be allowed to be compiletime, too. Otherwise, I would split it into FnConstness and TraitConstness.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I prefer this naming over the suggestion I made in https://github.com/rust-lang/rust/pull/148820/files#r2517464261 actually

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.

Apparently this wasn't addressed? I would personally prefer the naming to be Const/Comptime/NotConst but Always is okay too (I don't prefer it though)

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.

@jdonszelmann jdonszelmann self-assigned this Nov 11, 2025
@fee1-dead fee1-dead self-assigned this Nov 11, 2025
Comment thread compiler/rustc_feature/src/builtin_attrs.rs Outdated
Comment thread compiler/rustc_hir_typeck/src/callee.rs Outdated
Comment thread compiler/rustc_ast/src/ast.rs
Comment thread compiler/rustc_hir/src/attrs/encode_cross_crate.rs Outdated
Comment thread compiler/rustc_hir_pretty/src/lib.rs Outdated
Comment thread compiler/rustc_middle/src/ty/mod.rs Outdated
@rustbot

rustbot commented Nov 21, 2025

Copy link
Copy Markdown
Collaborator

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred to constck

cc @fee1-dead

@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Nov 21, 2025
@rustbot

This comment has been minimized.

@oli-obk

oli-obk commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

@bors r=fee1-dead

@rust-bors

rust-bors Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

📌 Commit d129cd0 has been approved by fee1-dead

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 8, 2026
@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 commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

⚠️ A new commit 4089c2afc62524aa8bb2ecf17314bc39c43585c1 was pushed.

This pull request was unapproved.

@oli-obk

oli-obk commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

@bors r=fee1-dead

I edited the attribute name to match how the rustc_ attributes are represented internally

@rust-bors

rust-bors Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 4089c2a has been approved by fee1-dead

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-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
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-bors rust-bors Bot merged commit 1419d3c into rust-lang:main Jun 8, 2026
12 checks passed
@rustbot rustbot added this to the 1.98.0 milestone Jun 8, 2026
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
@Mark-Simulacrum

Copy link
Copy Markdown
Member

@rust-timer build 370b172

cc #157616 (comment)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (370b172): comparison URL.

Overall result: ❌ regressions - 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

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.3% [0.3%, 0.3%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary 2.1%, secondary 6.6%)

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

mean range count
Regressions ❌
(primary)
2.1% [2.1%, 2.1%] 1
Regressions ❌
(secondary)
6.6% [6.6%, 6.6%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.1% [2.1%, 2.1%] 1

Cycles

Results (primary -2.3%, secondary 15.7%)

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

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
15.7% [2.6%, 41.4%] 3
Improvements ✅
(primary)
-2.3% [-2.3%, -2.3%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.3% [-2.3%, -2.3%] 1

Binary size

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

Bootstrap: 517.572s -> 540.149s (4.36%)
Artifact size: 400.85 MiB -> 400.78 MiB (-0.02%)

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

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) A-rustdoc-json Area: Rustdoc JSON backend 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. 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.