Skip to content

Use of Self::SOME_ASSOC_CONST inside a function-local const calls Self a "generic parameter from outer function" #109596

Closed
@thomcc

Description

@thomcc

Code

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=01d9dd114b740509a3538ce91aa2bf6a.

#[repr(u8)]
pub enum Foo { A, B, C }

impl Foo {
    pub const COUNT: usize = 3;
    pub fn blah() {
        const _: () = assert!(Self::COUNT < (u8::MAX as usize));
    }
}

A slightly more realistic (but also slightly larger) example is here: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=01d9dd114b740509a3538ce91aa2bf6a (this is more or less code that I tried to emit from a macro).

Current output

error[E0401]: can't use generic parameters from outer function
 --> src/lib.rs:7:31
  |
4 | impl Foo {
  | ---- `Self` type implicitly declared here, by this `impl`
...
7 |         const _: () = assert!(Self::COUNT < (u8::MAX as usize));
  |                               ^^^^^^^^^^^
  |                               |
  |                               use of generic parameter from outer function
  |                               use a type here instead

For more information about this error, try `rustc --explain E0401`.
error: could not compile `playground` due to previous error

Desired output

error[...]: can't use `Self` type inside function-local `const`.

Or something. I don't have strong feelings.

It should probably suggest replacing Self::COUNT with Foo::COUNT though, since that's enough to fix this.

Rationale and extra context

Self here is not a generic parameter from the outer function (it's the implementing type from the impl block, although I'm not really sure that's a particularly clear way to refer to it either). Either way, the current message is wrong and misleading.

Anything else?

Happens on nightly (rustc 1.70.0-nightly (8be3c2bda 2023-03-24)) and on stable (1.68.1).

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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions