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

More accurate suggestion for self. and Self:: #116086

Merged
merged 4 commits into from
Sep 24, 2023

Conversation

estebank
Copy link
Contributor

@estebank estebank commented Sep 23, 2023

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

Partially address #115992.

r? @compiler-errors

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 23, 2023
Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still doesn't give good suggestions for a resolver error w/ associated_fn_name without (), right? If so, pls file a follow-up bug. r=me

@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member

Also, this doesn't fix the other half of #115992, which is that struct shorthand syntax gets mangled by suggestion in resolution, right? Should probably not close that issue if so.

@compiler-errors
Copy link
Member

compiler-errors commented Sep 23, 2023

Would actually be nice if you committed the test in #115992 so I could see the answer to those two questions, even if those suggestions are still kinda busted. Could be marked w/ // known-bug or a FIXME or whatever if so.

@estebank
Copy link
Contributor Author

The output for the two cases is

error[E0425]: cannot find value `field` in this scope
  --> f73.rs:11:9
   |
6  |     fn field(&self) -> u32 {
   |        ----- a method by that name is available on `Self` here
...
11 |         field;
   |         ^^^^^ a field by this name exists in `Self`

error[E0425]: cannot find value `field` in this scope
  --> f73.rs:12:15
   |
6  |     fn field(&self) -> u32 {
   |        ----- a method by that name is available on `Self` here
...
12 |         Foo { field }
   |               ^^^^^ a field by this name exists in `Self`

Having the field and method named the same does add the somewhat misleading span though :-/

We could improve the situation by carrying more data in the diagnostic struct to mention more than one bit context, as we do now.

I'll add the test.

@estebank
Copy link
Contributor Author

Oh boy, that's fun

error[E0425]: cannot find value `field` in this scope
  --> tests/ui/resolve/field-and-method-in-self-not-available-in-assoc-fn.rs:16:15
   |
16 |         Foo { field } //~ ERROR cannot find value `field` in this scope
   |               ^^^^^
   |
help: consider using the method on `Self`
   |
16 |         Foo { self.field } //~ ERROR cannot find value `field` in this scope
   |               +++++
help: you might have meant to use the available field
   |
16 |         Foo { self.field } //~ ERROR cannot find value `field` in this scope
   |               ~~~~~~~~~~

@estebank
Copy link
Contributor Author

I'll address the "keep info on struct literal around" later, I know where to fix it but will likely take me longer than tonight.

@estebank
Copy link
Contributor Author

@bors r=compiler-errors

@bors
Copy link
Contributor

bors commented Sep 23, 2023

📌 Commit 7d8559a has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 23, 2023
@compiler-errors
Copy link
Member

@bors rollup

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request 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 added a commit to rust-lang-ci/rust that referenced this pull request Sep 24, 2023
…iaskrgr

Rollup of 5 pull requests

Successful merges:

 - rust-lang#116073 (Allow higher-ranked fn sigs in `ValuePairs`)
 - rust-lang#116082 (Tweak expected message to explain what it's actually signifying)
 - rust-lang#116086 (More accurate suggestion for `self.` and `Self::`)
 - rust-lang#116104 (Reuse calculate_debuginfo_offset for fragments.)
 - rust-lang#116106 (Migrate GUI colors test to original CSS color format)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 61b38b2 into rust-lang:master Sep 24, 2023
11 checks passed
@rustbot rustbot added this to the 1.74.0 milestone Sep 24, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request 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``
bors added a commit to rust-lang-ci/rust that referenced this pull request 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
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants