Skip to content

unhelpful error message when declaring constant with type parameter's associated constant #53241

Closed
@tspiteri

Description

@tspiteri

For this code:

trait Foo {
    const BAR: i32;
}
impl Foo for i32 {
    const BAR: i32 = 42;
}
fn baz<F: Foo>(a: F) {
    const QUX: i32 = F::BAR;
    println!("{}", QUX);
}
fn main() {
    baz(1i32);
}

I get the error message:

| fn baz<F: Foo>(a: F) {
|    --- - type variable from outer function
|    |
|    try adding a local type parameter in this method instead
|     const QUX: i32 = F::BAR;
|                      ^^^^^^ use of type variable from outer function

QUX is not a method and cannot have local type parameters; I don't think constants can be generic. So the error message is confusing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.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