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

Generic associated consts can't currently be used to parameterize fixed array lengths #42863

Closed
bstrie opened this issue Jun 23, 2017 · 6 comments
Labels
A-associated-items Area: Associated items (types, constants & functions) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@bstrie
Copy link
Contributor

bstrie commented Jun 23, 2017

The following program works, proving that associated consts ought to be able to be used to successfully parameterize the size of fixed-length arrays:

#![feature(associated_consts)]

trait Const {
    const VALUE: usize;
}

struct Foo;

impl Const for Foo {
    const VALUE: usize = 4;
}

fn test(x: [u8; Foo::VALUE]) {
    for _ in x.iter() {
        println!("hello");
    }
}

fn main() {
    let x = [1,2,3,4];
    test(x);
}

The following program ought to compile as well, but doesn't:

#![feature(associated_consts)]

trait Const {
    const VALUE: usize;
}

fn test<T: Const>(_: [u8; T::VALUE]) {}

fn main() {}

Output:

rustc 1.20.0-nightly (ab5bec255 2017-06-22)
error[E0599]: no associated item named `VALUE` found for type `T` in the current scope
 --> <anon>:7:27
  |
7 | fn test<T: Const>(_: [u8; T::VALUE]) {}
  |                           ^^^^^^^^
  |
  = help: items from traits can only be used if the trait is implemented and in scope
  = note: the following trait defines an item `VALUE`, perhaps you need to implement it:
          candidate #1: `Const`

Slightly altering the program to more fully qualify the associated const also doesn't work, though yields a different error message:

#![feature(associated_consts)]

trait Const {
    const VALUE: usize;
}

fn test<T: Const>(_: [u8; <T as Const>::VALUE]) {}

fn main() {}

Output:

rustc 1.20.0-nightly (ab5bec255 2017-06-22)
error[E0277]: the trait bound `T: Const` is not satisfied
 --> <anon>:7:27
  |
7 | fn test<T: Const>(_: [u8; <T as Const>::VALUE]) {}
  |                           ^^^^^^^^^^^^^^^^^^^ the trait `Const` is not implemented for `T`
  |
  = help: consider adding a `where T: Const` bound
  = note: required by `Const::VALUE`

Presumably both these programs should work, yes?

@bstrie
Copy link
Contributor Author

bstrie commented Jun 23, 2017

cc @eddyb, @ubsan

@bstrie bstrie added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-associated-items Area: Associated items (types, constants & functions) labels Jun 23, 2017
@strega-nil
Copy link
Contributor

It should be written "associated consts can't be used as an argument to fixed size arrays" :)

parameters != arguments :P

@eddyb
Copy link
Member

eddyb commented Jun 23, 2017

This is a known issue mentioned in the tracking issue for associated constants.

@bstrie
Copy link
Contributor Author

bstrie commented Jun 23, 2017

Indeed, this appears to be similar to #39211 and #34344, though the fact there are four different compiler error messages between these three issues suggests these are exercising different paths. Would be nice to consolidate all four code examples into one issue for the sake of regression tests, if someone wants to arbitrarily pick one.

@vcfxb
Copy link
Contributor

vcfxb commented Mar 29, 2019

Is this any closer to being fixed?
Is there anything I can do to help?

@eddyb
Copy link
Member

eddyb commented Mar 30, 2019

Closing as duplicate of #43408.

@alfriadox The canonical issue is #43408, and while it's closer to being fixed, @nikomatsakis hasn't really let people attack the problem directly for the past few years, so it's been a waiting game.
(The solution involves "lazy normalization", a slowly approaching rustc typesystem feature)

@eddyb eddyb closed this as completed Mar 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants