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

Compile or improve error message for static Self #119936

Closed
rdbo opened this issue Jan 13, 2024 · 1 comment · Fixed by #119939
Closed

Compile or improve error message for static Self #119936

rdbo opened this issue Jan 13, 2024 · 1 comment · Fixed by #119939
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@rdbo
Copy link

rdbo commented Jan 13, 2024

Code

impl MyStruct {
    fn instance() -> &'static Self {
        static MYSTRUCT: OnceLock<Self> = OnceLock::new();
        MYSTRUCT.get_or_init(|| Self { msg: "Hello world!" })
    }
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0401]: can't use generic parameters from outer item
  --> src/main.rs:10:35
   |
8  | impl MyStruct {
   | ---- `Self` type implicitly declared here, by this `impl`
9  |     fn instance() -> &'static Self {
10 |         static MYSTRUCT: OnceLock<Self> = OnceLock::new();
   |                                   ^^^^
   |                                   |
   |                                   use of generic parameter from outer item
   |                                   refer to the type directly here instead

Desired output

It should either compile, or have a different message

Rationale and extra context

The message says use of generic parameter from outer item - refer to the type directly here instead
This makes you believe that Self is a generic type, but it isn't. MyStruct can also be known at compile time at doesn't have generics attached to it, so maybe it could even compile.

Other cases

Changing the `Self` to `MyStruct`, which is the same thing, will make the code compile.

Rust Version

$ rustc --version --verbose
rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: x86_64-unknown-linux-musl
release: 1.75.0
LLVM version: 17.0.6

Anything else?

No response

@rdbo rdbo 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 Jan 13, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 13, 2024
@clubby789
Copy link
Contributor

Looks like essentially the same issue as #109596

@clubby789 clubby789 added D-confusing Diagnostics: Confusing error or lint that should be reworked. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jan 13, 2024
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 D-confusing Diagnostics: Confusing error or lint that should be reworked. 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.

3 participants