Skip to content

Do not suggest "using a local generic parameter" for constants #115720

Closed
@MaxVerevkin

Description

@MaxVerevkin

Code

fn main() {}

trait Trait {
    const X: u32;
}

impl Trait for () {
    const X: u32 = 0;
}

impl<T: Trait> Trait for [T] {
    const X: u32 = {
        const TEMP: u32 = T::X + 1;
        TEMP
    };
}

Current output

error[E0401]: can't use generic parameters from outer function
  --> src/main.rs:13:27
   |
11 | impl<T: Trait> Trait for [T] {
   |      - type parameter from outer function
12 |     const X: u32 = {
13 |         const TEMP: u32 = T::X + 1;
   |                   -       ^^^^ use of generic parameter from outer function
   |                   |
   |                   help: try using a local generic parameter instead: `<T>`

For more information about this error, try `rustc --explain E0401`.

Desired output

I'm not sure what the output should be (I'm not even sure why this does not compile), but using a local generic parameter does not make sense for me. It seems like the error message thinks this code is inside a function.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions