Skip to content

Conversation

@oli-obk
Copy link
Contributor

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

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
Copy link
Collaborator

rustbot commented Nov 11, 2025

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
Copy link
Collaborator

rustbot commented Nov 11, 2025

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
Copy link
Collaborator

rustbot commented Nov 11, 2025

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Comment on lines +4230 to 4231
Comptime,
Const,
Copy link
Member

Choose a reason for hiding this comment

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

Seems worth doc comments distinguishing these two.

let fn_span = self.token.span;
let header = self.parse_fn_front_matter(vis, case, FrontMatterParsingMode::Function)?; // `const ... fn`
let mut header = self.parse_fn_front_matter(vis, case, FrontMatterParsingMode::Function)?; // `const ... fn`
if let Some(attr) = attrs.iter().find(|attr| attr.has_name(sym::rustc_comptime)) {
Copy link
Member

@fmease fmease Nov 11, 2025

Choose a reason for hiding this comment

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

This doesn't account for #[cfg_attr(…, rustc_comptime)]. I would just leave the parser and ast::Const alone and do the lowering to hir::Constness (which would still have Comptime) in AST lowering. This should simplify things a lot.

Even if you anticipate it being turned into a keyword / qualifier eventually, you can't assume that and it would be super easy anyway to update the parser & AST once it's truly necessary.

@rust-log-analyzer
Copy link
Collaborator

The job pr-check-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
   Compiling anyhow v1.0.99
error[E0026]: variant `rustc_hir::ConstContext::Const` does not have a field named `inline`
   --> src/tools/clippy/clippy_utils/src/lib.rs:251:29
    |
251 |         Static(_) | Const { inline: _ } => true,
    |                             ^^^^^^
    |                             |
    |                             variant `rustc_hir::ConstContext::Const` does not have this field
    |                             help: `rustc_hir::ConstContext::Const` has a field named `allow_const_fn_promotion`

error[E0027]: pattern does not mention field `allow_const_fn_promotion`
   --> src/tools/clippy/clippy_utils/src/lib.rs:251:21
    |
251 |         Static(_) | Const { inline: _ } => true,
    |                     ^^^^^^^^^^^^^^^^^^^ missing field `allow_const_fn_promotion`
    |
help: include the missing field in the pattern
    |
251 |         Static(_) | Const { inline: _, allow_const_fn_promotion } => true,
    |                                      ++++++++++++++++++++++++++
help: if you don't care about this missing field, you can explicitly ignore it
    |
251 |         Static(_) | Const { inline: _, allow_const_fn_promotion: _ } => true,
    |                                      +++++++++++++++++++++++++++++
help: or always ignore missing fields here
    |
251 |         Static(_) | Const { inline: _, .. } => true,
    |                                      ++++

[RUSTC-TIMING] build_script_build test:false 0.196
    Checking unicode-width v0.2.2
[RUSTC-TIMING] toml_edit test:false 1.574

}

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

@fmease fmease Nov 11, 2025

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.

@jdonszelmann jdonszelmann self-assigned this Nov 11, 2025
@fee1-dead fee1-dead self-assigned this Nov 11, 2025
),
rustc_attr!(
rustc_comptime, Normal, template!(Word), ErrorFollowing,
EncodeCrossCrate::Yes,
Copy link
Member

Choose a reason for hiding this comment

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

It this necessary? The other queries would store this information

Comment on lines +922 to +928
if let hir::Constness::Comptime = self.tcx.constness(callee_did) {
match const_context {
Some(hir::ConstContext::Const { .. } | hir::ConstContext::Static(_)) => {}
Some(hir::ConstContext::ConstFn) | None => {
self.dcx().span_err(span, "comptime fns can only be called at compile time");
}
}
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't enforce cross crate if I use a comptime function in fn if my crate doesn't enable const_trait_impl.

Would it make sense to also add logic to constck? (check_const.rs)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well I'm working on turning this check on unconditionally. I can just move the comptime check before the feature gate check

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

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants