Skip to content

add hint for E0401 when defining a const inside a func #132958

@lolbinarycat

Description

@lolbinarycat

Code

struct Foo<const N: usize>;

impl<const N: usize> Foo<N> {
    fn get_n_plus_one() -> usize {
        const M: usize = N + 1;
        M
    }
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0401]: can't use generic parameters from outer item
 --> src/lib.rs:5:26
  |
3 | impl<const N: usize> Foo<N> {
  |            - const parameter from outer item
4 |     fn get_n_plus_one() -> usize {
5 |         const M: usize = N + 1;
  |                          ^ use of generic parameter from outer item
  |
  = note: a `const` is a separate item from the item that contains it

Desired output

Compiling playground v0.0.1 (/playground)
error[E0401]: can't use generic parameters from outer item
 --> src/lib.rs:5:26
  |
3 | impl<const N: usize> Foo<N> {
  |            - const parameter from outer item
4 |     fn get_n_plus_one() -> usize {
5 |         const M: usize = N + 1;
  |                          ^ use of generic parameter from outer item
  |
  = note: a `const` is a separate item from the item that contains it
    help: associated constants and let statements can use const paramaters, consider using one of those.

Rationale and extra context

No response

Other cases

Rust Version

rustc 1.84.0-nightly (759e07f06 2024-10-30)
binary: rustc
commit-hash: 759e07f063fb8e6306ff1bdaeb70af56a878b415
commit-date: 2024-10-30
host: x86_64-unknown-linux-gnu
release: 1.84.0-nightly
LLVM version: 19.1.1

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions