Skip to content

Add very basic "comptime" fn implementation#148820

Open
oli-obk wants to merge 7 commits intorust-lang:mainfrom
oli-obk:comptime
Open

Add very basic "comptime" fn implementation#148820
oli-obk wants to merge 7 commits intorust-lang:mainfrom
oli-obk:comptime

Conversation

@oli-obk
Copy link
Copy Markdown
Contributor

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

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

rustbot commented Nov 11, 2025

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.

Comment thread compiler/rustc_hir/src/hir.rs
@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
Copy link
Copy Markdown
Collaborator

rustbot commented Nov 21, 2025

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.

@rustbot

This comment has been minimized.

}
}
} else if ccx.tcx.constness(callee) != hir::Constness::Const {
} else if ccx.tcx.constness(callee) != hir::Constness::Maybe {
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.

This is a bit sus

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.

What part? The rename or the inequality?

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.

The inequality. I feel like this should be if == hir::Constness::NotConst?

Comment thread compiler/rustc_hir/src/hir.rs Outdated
Comment thread compiler/rustc_metadata/src/rmeta/encoder.rs Outdated
Comment thread compiler/rustc_metadata/src/rmeta/table.rs Outdated
Comment thread compiler/rustc_mir_transform/src/lib.rs Outdated
let num_args = fields.len();
let func =
if context == hir::Constness::Const { called_in_const } else { called_at_rt };
if context == hir::Constness::Maybe { called_in_const } else { called_at_rt };
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.

I feel like we shouldn't add another variant to hir::Constness given how this changes a lot of other things which might be a headache to update if we were to add comptime impls in the future, etc. Would it make sense to keep the hir:Constness::{,Not}Const, and extract the comptime enum specifically to a new enum? Might be a bit more work to do but worth it IMO, and might be better than doing it in a followup.

Copy link
Copy Markdown
Contributor Author

@oli-obk oli-obk Dec 3, 2025

Choose a reason for hiding this comment

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

I can draft up a version of that, but mostly as a counterexample to why we shouldn't do that. This enum mirrors the BoundConstness perfectly (and those should probably be merged), because there are only those 3 options. Having two separate enums creates an odd space where we need to reject an illegal state in the type system instead of just in the parser or ast lowering

x not const const
not comptime fn const fn
comptime comptime fn ???

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.

If you're worried about footguns I think we could try to remove impl PartialEq for Constness and require exhaustive handling at all sites.

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.

Alternatively I can make the "alwaysness" of the Comptime variant be a field on the Const/Maybe variant. This way you can't accidentally forget to handle it, but Const and Maybe end up generally handled together unless explicitly differentiated

This comment was marked as resolved.

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 made this change, which made the "renaming Constness variants to match BoundConstness" change a little weird, so I removed that and will look into it in another PR. Maybe by changing BoundConstness to match Constness instead of the other way around

@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 Nov 28, 2025
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Nov 28, 2025

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

@bors
Copy link
Copy Markdown
Collaborator

bors commented Nov 30, 2025

☔ The latest upstream changes (presumably #149478) made this pull request unmergeable. Please resolve the merge conflicts.

@rustbot

This comment has been minimized.

@bors
Copy link
Copy Markdown
Collaborator

bors commented Dec 16, 2025

☔ The latest upstream changes (presumably #143924) made this pull request unmergeable. Please resolve the merge conflicts.

@izagawd
Copy link
Copy Markdown
Contributor

izagawd commented Mar 18, 2026

Is this PR still intended to move forward, or has the work been superseded elsewhere?

@oli-obk
Copy link
Copy Markdown
Contributor Author

oli-obk commented Mar 19, 2026

I have some review comments to address and some heavy rebasing to do :D

Comment on lines +7 to +10
#[rustc_comptime]
fn always_const<T: const Trait>() {
T::method()
}
Copy link
Copy Markdown
Contributor

@bushrat011899 bushrat011899 May 1, 2026

Choose a reason for hiding this comment

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

No Action Required: Makes me wish the current const modifier was actually named const?, to imply it can be called in comptime, and can be called at runtime. That way, const would mean must be called in comptime, and the absence of const means can't be called in comptime. It doesn't really matter, since this is a compiler feature, but it'd be nicer for users to understand what's going on.

View changes since the review

Comment on lines +3 to +5
#[rustc_comptime]
const fn foo() {}
//~^ ERROR a function cannot be both `comptime` and `const`

This comment was marked as resolved.

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.

It's actually easier this way for the compiler, because it's now trivial to reject [const] bounds

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.

It's also nowhere near the final syntax, just sth to enable the feature

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.

Ahh ok, I assumed this was mostly a stylistic choice. If it's more consistent with the rest of the compiler that makes sense.

Comment thread compiler/rustc_mir_transform/src/lib.rs Outdated
let num_args = fields.len();
let func =
if context == hir::Constness::Const { called_in_const } else { called_at_rt };
if context == hir::Constness::Maybe { called_in_const } else { called_at_rt };

This comment was marked as resolved.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 3, 2026

This PR changes rustc_public

cc @celinval, @ouz-a, @makai410

@rustbot

This comment has been minimized.

@oli-obk oli-obk force-pushed the comptime branch 2 times, most recently from 01088b9 to d092d4e Compare May 3, 2026 09:58
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 3, 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.

@rust-log-analyzer
Copy link
Copy Markdown
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)

error[E0433]: cannot find type `Constness` in this scope
   --> compiler/rustc_passes/src/stability.rs:209:54
    |
209 |                 && matches!(fn_sig.header.constness, Constness::Const { .. })
    |                                                      ^^^^^^^^^ use of undeclared type `Constness`
    |
help: consider importing this enum
    |
  4 + use rustc_hir::Constness;
    |

error[E0433]: cannot find type `Constness` in this scope
   --> compiler/rustc_passes/src/stability.rs:231:46
    |
231 |         && matches!(fn_sig.header.constness, Constness::Const { .. })
    |                                              ^^^^^^^^^ use of undeclared type `Constness`
    |
help: consider importing this enum
    |
  4 + use rustc_hir::Constness;
    |

error[E0433]: cannot find type `Constness` in this scope
   --> compiler/rustc_passes/src/stability.rs:387:51
    |
387 |             && !matches!(fn_sig.header.constness, Constness::Const { .. })
    |                                                   ^^^^^^^^^ use of undeclared type `Constness`
    |
help: consider importing this enum
    |
  4 + use rustc_hir::Constness;

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