rust-lang / rust-lang/rust

`const_eval_select` cannot be used to call `#[rustc_comptime]` function from const

Open
#157,927 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-const-eval C-bug needs-triage
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

@oli-obk said this should work and I should open an issue if it didn't https://hachyderm.io/@oli/116733007519699753
Took me some time until I found the time to try.

I tried this code:

#![feature(rustc_attrs, core_intrinsics)]
#![allow(internal_features)]

use std::intrinsics::const_eval_select;

fn main() {
    const VAL: u64 = use_appropriate_impl(42);
    let val = use_appropriate_impl(42);

    println!("Comptime result: {VAL}, runtime result: {val}");
}

const fn use_appropriate_impl(i: u32) -> u64 {
    const_eval_select((i,), comptime_compatible_but_slow, runtime_only_impl)
}

#[rustc_comptime]
fn comptime_compatible_but_slow(mut i: u32) -> u64 {
    let mut sum = 0;

    while i > 0 {
        i -= 1;
        sum += (i * i) as u64
    }

    sum
}

fn runtime_only_impl(i: u32) -> u64 {
    (0..i).map(|i| u64::from(i * i)).sum()
}

I expected to see this happen: Compiles and prints "Comptime result: 23821, runtime result: 23821"

Instead, this happened: Compilation fails with the following error

   Compiling comptime-const-eval-select v0.1.0 (E:\Git\comptime-const-eval-select)
error[E0277]: the trait bound `fn(u32) -> u64 {comptime_compatible_but_slow}: const FnOnce(u32)` is not satisfied                                                                                                                                                                                       
    --> src\main.rs:14:29
     |
  14 |     const_eval_select((i,), comptime_compatible_but_slow, runtime_only_impl)
     |     -----------------       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     |     |
     |     required by a bound introduced by this call
     |
note: required by a bound in `const_eval_select`
    --> C:\Users\Bennet\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\intrinsics\mod.rs:2408:8
     |
2401 | pub const fn const_eval_select<ARG: Tuple, F, G, RET>(
     |              ----------------- required by a bound in this function
...
2408 |     F: const FnOnce<ARG, Output = RET>;
     |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `const_eval_select`

For more information about this error, try `rustc --explain E0277`.                                                                                                                                                                                                                                     
error: could not compile `comptime-const-eval-select` (bin "comptime-const-eval-select") due to 1 previous error    
Meta

rustc --version --verbose:

rustc 1.98.0-nightly (3daae5e42 2026-06-14)
binary: rustc
commit-hash: 3daae5e42ec9ba435212987331af1b7b8634fa90
commit-date: 2026-06-14
host: x86_64-pc-windows-msvc
release: 1.98.0-nightly
LLVM version: 22.1.6

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example from the issue with the nightly compiler, then inspect core/src/intrinsics/mod.rs around const_eval_select and its const FnOnce bound. Compare the handling of the #[rustc_comptime] function with the reported call, and consider the issue done when the example compiles and prints matching comptime and runtime results.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.