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

struct initialization incorrectly suggests using assoc function #115992

Closed
lcnr opened this issue Sep 20, 2023 · 1 comment · Fixed by #116089
Closed

struct initialization incorrectly suggests using assoc function #115992

lcnr opened this issue Sep 20, 2023 · 1 comment · Fixed by #116089
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@lcnr
Copy link
Contributor

lcnr commented Sep 20, 2023

Code

struct Foo {
    field: u32,
}

impl Foo {
    fn field(&self) -> u32 {
        self.field
    }

    fn new() -> Foo {
        Foo { field }
    }
}

Current output

error[E0425]: cannot find value `field` in this scope
  --> src/lib.rs:11:15
   |
11 |         Foo { field }
   |               ^^^^^ a field by this name exists in `Self`
   |
help: consider using the associated function
   |
11 |         Foo { self.field }
   |               +++++

We should not suggest using self.field inside of a struct initializer.

@lcnr lcnr 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 Sep 20, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 20, 2023
@compiler-errors
Copy link
Member

This is two problems:

  1. field -> self.field breaks the field initializer shorthand
  2. We shouldn't suggest self. unless we know this is call expr.

(2.) happens even without initializers:

struct Foo {
    field: u32,
}

impl Foo {
    fn field(&self) -> u32 {
        self.field
    }

    fn new() {
        field;
    }
}
error[[E0425]](https://doc.rust-lang.org/stable/error_codes/E0425.html): cannot find value `field` in this scope
  --> src/lib.rs:11:9
   |
11 |         field;
   |         ^^^^^ a field by this name exists in `Self`
   |
help: consider using the associated function
   |
11 |         self.field;
   |         +++++

@compiler-errors compiler-errors added D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 20, 2023
@estebank estebank self-assigned this Sep 23, 2023
estebank added a commit to estebank/rust that referenced this issue Sep 23, 2023
estebank added a commit to estebank/rust that referenced this issue Sep 23, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Sep 23, 2023
…errors

More accurate suggestion for `self.` and `Self::`

Detect that we can't suggest `self.` in an associated function without `&self` receiver.

Partially address rust-lang#115992.

r? `@compiler-errors`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Sep 23, 2023
…errors

More accurate suggestion for `self.` and `Self::`

Detect that we can't suggest `self.` in an associated function without `&self` receiver.

Partially address rust-lang#115992.

r? ``@compiler-errors``
@bors bors closed this as completed in c4a4926 Sep 24, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Sep 24, 2023
Rollup merge of rust-lang#116086 - estebank:issue-115992, r=compiler-errors

More accurate suggestion for `self.` and `Self::`

Detect that we can't suggest `self.` in an associated function without `&self` receiver.

Partially address rust-lang#115992.

r? ``@compiler-errors``
estebank added a commit to estebank/rust that referenced this issue Sep 25, 2023
bors added a commit to rust-lang-ci/rust that referenced this issue Sep 29, 2023
…errors

When suggesting `self.x` for `S { x }`, use `S { x: self.x }`

Fix rust-lang#115992.

r? `@compiler-errors`

Follow up to rust-lang#116086.
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-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. 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