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

Cannot declare const in function scope that depends on generic impl const #64201

Closed
ObsidianMinor opened this issue Sep 6, 2019 · 1 comment

Comments

@ObsidianMinor
Copy link
Contributor

The compiler misreports the following as error 401, saying you can't use generic parameters from an outer function.

trait X {
    const Y: i32;
}

trait Z {
    fn a() -> i32;
}

struct B<T> {
    t: std::marker::PhantomData<T>
}

impl<T: X> Z for B<T> {
    fn a() -> i32 {
        const C: i32 = T::Y;

        C
    }
}
error[E0401]: can't use generic parameters from outer function
  --> src/lib.rs:15:24
   |
13 | impl<T: X> Z for B<T> {
   |      - type parameter from outer function
14 |     fn a() -> i32 {
   |        - try adding a local generic parameter in this method instead
15 |         const C: i32 = T::Y;
   |                        ^^^^ use of generic parameter from outer function

This is limiting because if you're implementing a trait like X over an external type like Vec there's no way to declare a const that depends on the impl generic type. You can't make the const in the function scope, you can't make it in the impl scope, and you can't make it outside of the impl block since it relies on the generic parameter.

@Centril
Copy link
Contributor

Centril commented Sep 6, 2019

Duplicate of #57775.

@Centril Centril closed this as completed Sep 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants