Skip to content

Constraining generic parameters #23

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

Open
lcnr opened this issue Mar 23, 2022 · 0 comments
Open

Constraining generic parameters #23

lcnr opened this issue Mar 23, 2022 · 0 comments
Labels
A-generic-exprs Generic const expressions A-unification Unifying constants in the type system C-design-docs Category: This is part of our design documentation K-behavior Document Kind: regarding user visible behavior P-necessary Priority: will be needed at some point S-active

Comments

@lcnr
Copy link
Contributor

lcnr commented Mar 23, 2022

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

Given an impl, the compiler has to be able to decide the generic arguments used by that impl.
Consider the following snippet:

struct Weird<const N: usize>;

impl<const A: usize, const B: usize> Weird<{ A + B }> {
    fn returns_a() -> usize {
        A
    }
}

When calling Weird::<3>::returns_a(), there is no way to restrict the generic parameters A or B so this has to error.
If a generic parameter is used by an injective expression, then we could allow this. The most relevant case here are
constructors:

struct UsesOption<const N: Option<usize>>;
impl<const N: usize> UsesOption<{ Some(N) }> {}

Here it is very clear which N we should use given UsesOption::<{ Some(3) }>.

TODO: blocked on structural equality.

@lcnr lcnr added C-design-docs Category: This is part of our design documentation A-const-param-types K-behavior Document Kind: regarding user visible behavior P-necessary Priority: will be needed at some point labels Mar 23, 2022
@rust-lang rust-lang locked and limited conversation to collaborators Mar 23, 2022
@lcnr lcnr added S-active A-generic-exprs Generic const expressions A-unification Unifying constants in the type system and removed A-const-param-types labels Mar 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-generic-exprs Generic const expressions A-unification Unifying constants in the type system C-design-docs Category: This is part of our design documentation K-behavior Document Kind: regarding user visible behavior P-necessary Priority: will be needed at some point S-active
Projects
None yet
Development

No branches or pull requests

1 participant