rust-lang / rust-lang/rfcs

Add bound contraint for trait associated const

Open
#3,095 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

I recently post on rust forum about how to write constraint on associated const, and find out that there is no direct way to achieve this (even no RFC?).

For example, I have the following trait:

trait Foo {
    const FOO: usize;
}

And I want to have constraint for that like

fn foo<T: Foo>() where <T as Foo>::FOO < 4 {}

impl<T: Foo> SomeTrait for T where <T as Foo>::FOO.is_power_of_two() {}

const fn something_about_foo(foo: usize) -> bool { /* ... */ }
struct Bar<T: Foo> where something_about_foo(<T as Foo>::FOO) {}

Since we already have various mature bound constraint on associated type, there should be some bound constraint on associated const as well.

I also wonder how to design the syntax of associated const bound, since allowing any const expression in where clause may lead to hard parsing. Maybe a brace should be required?

Contributor guide

No contributing guide indexed for this repository

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 with the issue's Rust forum discussion and the associated-const examples to understand the proposed constraint forms and unresolved syntax concerns. A completed contribution would need a decided design for associated-const bounds, including how expressions are represented in where clauses, suitable for an RFC.

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
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.