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

Suggest Self::CONST when trying to use an associated constant lexically #107199

Closed
kpreid opened this issue Jan 22, 2023 · 2 comments · Fixed by #107204
Closed

Suggest Self::CONST when trying to use an associated constant lexically #107199

kpreid opened this issue Jan 22, 2023 · 2 comments · Fixed by #107204
Assignees
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

@kpreid
Copy link
Contributor

kpreid commented Jan 22, 2023

Code

struct Foo;
// const A_CONST: usize = 1;
impl Foo {
    const A_CONST: usize = 1;
    fn foo() -> usize {
        // const A_CONST: usize = 1;
        A_CONST
    }
}

Current output

error[E0425]: cannot find value `A_CONST` in this scope
 --> src/lib.rs:7:9
  |
7 |         A_CONST
  |         ^^^^^^^ not found in this scope

Desired output

help: try referring to the associated constant: `Self::A_CONST`

Rationale and extra context

Associated constants work entirely differently from constants at a higher scope (module) or lower (function) which are looked up lexically — if either of the other two declarations in the sample code are uncommented, compilation succeeds. This may be surprising to beginners (example that inspired this post), and the “what they meant” is fairly obvious, so this should be a good opportunity to help in the error message.

Additionally, if the suggestion is machine-applicable then it can be used to help refactor code when a constant was intentionally converted into an associated constant.

Other cases

No response

Anything else?

No response

@kpreid kpreid 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 22, 2023
@kpreid
Copy link
Contributor Author

kpreid commented Jan 22, 2023

Further precedent: the compiler already provides this help for calling associated functions.

struct Foo;
impl Foo {
    fn bar() {}
    fn foo() {
        bar();
    }
}
error[E0425]: cannot find function `bar` in this scope
 --> src/lib.rs:5:9
  |
5 |         bar();
  |         ^^^ not found in this scope
  |
help: consider using the associated function
  |
5 |         Self::bar();
  |         ~~~~~~~~~

@euclio
Copy link
Contributor

euclio commented Jan 22, 2023

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 25, 2023
…etrochenkov

suggest qualifying bare associated constants

Fixes rust-lang#107199.
@bors bors closed this as completed in a8e8406 Jan 26, 2023
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.

2 participants