Skip to content

Recent nightly doesn't support array length from indirectly referenced trait constant #67743

Closed
@azriel91

Description

@azriel91

Hiya, on rustc 1.42.0-nightly (a9dd56ff9 2019-12-30), compilation fails when defining an array whose length is from a constant from a trait from an associated type. The code compiles fine on rustc 1.42.0-nightly (0de96d37f 2019-12-19).

trait TraitA {
    const VALUE: usize;
}

struct A;
impl TraitA for A {
    const VALUE: usize = 1;
}

trait TraitB {
    type MyA: TraitA;
    const VALUE: usize = Self::MyA::VALUE;
}

struct B;
impl TraitB for B {
    type MyA = A;
}

fn main() {
    let _ = [0; A::VALUE]; // ok
    let _ = [0; B::VALUE]; // fails

    // error: array lengths can't depend on generic parameters
    //   --> src/main.rs:22:17
    //    |
    // 22 |     let _ = [0; B::VALUE];
    //    |                 ^^^^^^^^
    //
}

The error message:

error: array lengths can't depend on generic parameters
  --> src/main.rs:22:17
   |
22 |     let _ = [0; B::VALUE];
   |                 ^^^^^^^^

It seems similar to #67739, but in this case, size_of isn't being used. It may also be a duplicate of #43408.

It also compiles fine on playpen nightly: 2019-12-29 da3629b05.

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions