Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
thomcc opened this issue Mar 25, 2023 · 5 comments · Fixed by #119939
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@thomcc
Copy link
Member

thomcc commented Mar 25, 2023

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).

@thomcc thomcc added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 25, 2023
@thomcc
Copy link
Member Author

thomcc commented Mar 25, 2023

Alternatively, perhaps this should work. Self::SOME_ASSOC_CONST is usable from other associated consts at impl block level, for example. I don't have strong feelings, though.

@compiler-errors
Copy link
Member

Alternatively, perhaps this should work. Self::SOME_ASSOC_CONST is usable from other associated consts at impl block level, for example. I don't have strong feelings, though.

That would require desugaring the const item into an associated const, which seems like a lot of complexity.

@thomcc
Copy link
Member Author

thomcc commented Mar 25, 2023

Yeah, that seems like a lang decision anyway. And it's mostly irrelevant if the error message is improved (esp. to suggest a fix in the cases it's just "change Self to the type name").

@scottmcm
Copy link
Member

scottmcm commented Mar 28, 2023

I think it's expected that this is an error -- if the person wants to use Self, it needs to be const { … } instead of a const item.

So updating the message for extra clarity is the way to go.

@Ved-s
Copy link

Ved-s commented Jul 29, 2023

got similar weird error today

struct Test {}

impl Test {
    pub fn static_fn() {}

    pub fn test() {
        fn nested_fn() {
            Self::static_fn();
        }
        nested_fn();
    }
}
error[E0401]: can't use generic parameters from outer function
   --> src/main.rs:985:13
    |
980 | impl Test {
    | ---- `Self` type implicitly declared here, by this `impl`
...
985 |             Self::static_fn();
    |             ^^^^^^^^^^^^^^^
    |             |
    |             use of generic parameter from outer function
    |             use a type here instead

I don't think Self is a generic parameter, should be like can't use Self from outer function

bors added a commit to rust-lang-ci/rust that referenced this issue Jan 14, 2024
… r=<try>

Improve 'generic param from outer item' error for `Self` and inside `static`/`const` items

Fixes rust-lang#109596
Fixes rust-lang#119936
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 6, 2024
…e, r=compiler-errors

Improve 'generic param from outer item' error for `Self` and inside `static`/`const` items

Fixes rust-lang#109596
Fixes rust-lang#119936
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 6, 2024
…e, r=compiler-errors

Improve 'generic param from outer item' error for `Self` and inside `static`/`const` items

Fixes rust-lang#109596
Fixes rust-lang#119936
@bors bors closed this as completed in 3c52832 Feb 7, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 7, 2024
Rollup merge of rust-lang#119939 - clubby789:static-const-generic-note, r=compiler-errors

Improve 'generic param from outer item' error for `Self` and inside `static`/`const` items

Fixes rust-lang#109596
Fixes rust-lang#119936
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants