rust-lang / rust-lang/rust

Unroll factor in `#[unroll]` attributes should accept any const expr, not just literals

Open
#158,161 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-attributes C-feature-request F-loop-hints T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Related to the #![feature(loop_hints)] attributes: https://github.com/rust-lang/rust/issues/156874

It would be nice if the N in #[unroll(N)] could take an arbitrary const expression, not just an integer literal:

fn foo<const UNROLL_FACTOR: usize>() {
    #[unroll(UNROLL_FACTOR)]
    loop {
        ...
    }
}

If that's not possible for technical reasons, another possibility would be a std::hint::unroll function which takes a const parameter and a closure:

fn foo<const UNROLL_FACTOR: usize>() {
    std::hint::unroll::<UNROLL_FACTOR>(|| loop {
        ...
    });
}

This would make it easier to experiment with different unroll factors without having to duplicate the same function for each unroll factor

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

Start by reading the related #![feature(loop_hints)] issue, #156874, and the current #[unroll] attribute behavior. Determine whether arbitrary const expressions can be accepted or whether the proposed std::hint::unroll alternative is needed. Done means const-generic unroll factors work without duplicating functions, with coverage for the supported form.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.