Skip to content

Error message when using inner static with type parameters is misleading #48427

Closed
@malbarbo

Description

@malbarbo

This code

fn x<T: Default>() {
    static a: T = T::default();
}

generates this error:

error[E0401]: can't use type parameters from outer function; try using a local type parameter instead
 --> src/main.rs:2:15
  |
2 |     static a: T = T::default();
  |               ^ use of type variable from outer function

error[E0401]: can't use type parameters from outer function; try using a local type parameter instead
 --> src/main.rs:2:19
  |
2 |     static a: T = T::default();
  |                   ^^^^^^^^^^ use of type variable from outer function

error: aborting due to 2 previous errors

which is misleading because a local type parameter is being used.

The original message was different.

I have one question: why this is forbidden? There is an issue with some discussion but I do not see why this is not possible.

Using static with type parameters is very useful, for example, defining thread local buffer for store temporary results (like in a sorting algorithm).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-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