Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MVP of associated constants for const generics #54

Open
programmerjake opened this issue Feb 27, 2024 · 1 comment
Open

MVP of associated constants for const generics #54

programmerjake opened this issue Feb 27, 2024 · 1 comment
Labels
C-design-docs Category: This is part of our design documentation

Comments

@programmerjake
Copy link
Member

What is this

This is a design document for const generics. Any discussions about its content should be on zulip. The conclusions of these discussions should then be edited back into this issue. Please do not post any comments directly in this issue.

Content

originally on Rust Internals

I think it may be a good idea to have a MVP for allowing associated constants to be used in const generics, basically we'd introduce a attribute #[usable_in_const_generic] (to be bikesheded) where all associated constants decorated with that attribute are restricted such that they're usable in const generics:

pub trait MyTrait {
    #[usable_in_const_generic]
    const C: usize;
}

pub struct S<const C: usize>;

impl<const C: usize> S<C> {
    #[usable_in_const_generic]
    pub const C2: usize = C; // works since C can be used in a const generic
}

impl<const C: usize> MyTrait for S<C> {
    const C: usize = C; // works since C can be used in a const generic
    // const C: usize = C + 1; // doesn't work without `#![feature(generic_const_exprs)]`
}

pub struct S2<T: MyTrait>([u8; T::C]); // works, since C has #[usable_in_const_generic]
pub struct S3<T: MyTrait>([u8; T::C2]); // works, since C2 also has #[usable_in_const_generic]

the idea is that #[usable_in_const_generic] would be stabilized to allow much more ergonomic use of const generics with associated consts rather than having to use an associated type C = Const<42> instead of const C: usize = 42.

If at some later point const generics no longer need to be restricted (which seems doubtful considering the lack of even theoretical progress on generic_const_exprs in years), then usable_in_const_generic would become a no-op and be (soft-?)deprecated and removed from the next edition.

@programmerjake programmerjake added the C-design-docs Category: This is part of our design documentation label Feb 27, 2024
@BoxyUwU
Copy link
Member

BoxyUwU commented Jun 26, 2024

👍 I think this is pretty similar to the plan for min_generic_const_exprs, never ended up seeing this until now xd

Going to write a full document outlining the plan for min_generic_const_exprs soon that also outlines all the issues with generic_const_exprs and why we need to do something like this. (basically a much more filled out design doc)

Wish I had gotten around to reading this beforehand though because we'd have been able to start on this a few months ago instead lol Thanks for the idea regardless, it's a good one and solves a lot of problems with generic_const_exprs 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-design-docs Category: This is part of our design documentation
Projects
None yet
Development

No branches or pull requests

2 participants