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

Point only to the identifiers in the typo suggestions of shadowed names instead of the entire struct #103560

Merged
merged 4 commits into from
Oct 30, 2022

Conversation

zbyrn
Copy link
Contributor

@zbyrn zbyrn commented Oct 26, 2022

Fixes #103358.

As discussed in the issue, the Span of the candidate Ident for a typo replacement is stored alongside its Symbol in TypoSuggestion. Then, the span of the identifier is what the "you might have meant to refer to" note is pointed at, rather than the entire struct definition.

Comments in #103111 and the issue both suggest that it is desirable to:

  1. include names defined in the same crate as the typo,
  2. ignore names defined elsewhere such as in std, and
  3. include names introduced indirectly via use.

Since a name from another crate but introduced via use has non-local def_id, to achieve this, a suggestion is displayed if either the def_id of the suggested name is local, or the span of the suggested name is in the same file as the typo itself.

Some UI tests have also been modified to reflect this change.

r? @cjgillot

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Oct 26, 2022
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @cjgillot (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 26, 2022
if let TypoCandidate::Shadowed(res, Some(sugg_span)) = typo_sugg
&& res
.opt_def_id()
.map_or(false, |id| id.is_local() || is_in_same_file(span, sugg_span))
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you have an example where removing the is_local and is_in_same_file filters give the wrong suggestions?
TBH, I'm not fond of the filename-based logic. We should either formulate the logic in terms of modules, or drop is_in_same_file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When we shadow something in another crate, the suggestion would potentially point at a distant file in which the shadowed name is defined. For example, compiling

mod m {
    pub enum MyEnum {
        Result
    }
}

use m::MyEnum::Result;

fn foo() -> Result<u8, str> {
    unimplemented!()
}

fn main() { }

will give the following error message:

error[E0573]: expected type, found variant `Result`
  --> test.rs:9:13
   |
9  | fn foo() -> Result<u8, str> {
   |             ^^^^^^^^^^^^^^^ not a type
   |
  ::: /[REDACTED]/rust/library/std/src/prelude/v1.rs:35:24
   |
35 | pub use crate::result::Result::{self, Err, Ok};
   |                        ------ you might have meant to refer to this enum
   |
help: consider importing one of these items instead
   |
7  | use std::fmt::Result;
   |
7  | use std::io::Result;
   |
7  | use std::result::Result;
   |
7  | use std::thread::Result;
   |

error: aborting due to previous error

For more information about this error, try `rustc --explain E0573`.

Note that the suggestion here points at something in the implementation of std. If this is okay, we could just remove both checks and simplify the code. The suggestion given here is not wrong technically, but I think it is not very helpful either.

I'm not a fan of the filename hack as well, and it was certainly a last resort as I'm still familiarizing myself with the code base. Could we achieve the same effect by comparing the ctx's of the two spans?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On a second thought, we could actually use the span of the resolver's module span (self.r.graph_root.span) to test whether the underlined definition is in the current module. Pushing a change and amending this PR shortly

Copy link
Contributor

Choose a reason for hiding this comment

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

graph_root.span is the span of the lib.rs file. Using it to decide is definitely wrong.
The former version of this PR is the better middle ground.
Could you remove the last commit, and I'll approve the PR. Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, my bad for misunderstanding what graph_root means! A reversion has been pushed.

Thanks for your comments and your approval!

@zbyrn zbyrn requested a review from cjgillot October 28, 2022 14:54
@zbyrn
Copy link
Contributor Author

zbyrn commented Oct 28, 2022

@cjgillot I believe everything is ready

@cjgillot
Copy link
Contributor

Could you squash the commits to remove the back-and-forth?
Thanks.

@zbyrn
Copy link
Contributor Author

zbyrn commented Oct 29, 2022

@cjgillot Done! Sorry for the confusion

@cjgillot
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Oct 29, 2022

📌 Commit 0eaf6d5 has been approved by cjgillot

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 Oct 29, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 30, 2022
Rollup of 5 pull requests

Successful merges:

 - rust-lang#93582 (Allow `impl Fn() -> impl Trait` in return position)
 - rust-lang#103560 (Point only to the identifiers in the typo suggestions of shadowed names instead of the entire struct)
 - rust-lang#103588 (rustdoc: add missing URL redirect)
 - rust-lang#103689 (Do fewer passes and generally be more efficient when filtering tests)
 - rust-lang#103740 (rustdoc: remove unnecessary CSS `.search-results { padding-bottom }`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 3143472 into rust-lang:master Oct 30, 2022
@rustbot rustbot added this to the 1.67.0 milestone Oct 30, 2022
Aaron1011 pushed a commit to Aaron1011/rust that referenced this pull request Jan 6, 2023
Point only to the identifiers in the typo suggestions of shadowed names instead of the entire struct

Fixes rust-lang#103358.

As discussed in the issue, the `Span` of the candidate `Ident` for a typo replacement is stored alongside its `Symbol` in `TypoSuggestion`. Then, the span of the identifier is what the "you might have meant to refer to" note is pointed at, rather than the entire struct definition.

Comments in rust-lang#103111 and the issue both suggest that it is desirable to:
1. include names defined in the same crate as the typo,
2. ignore names defined elsewhere such as in `std`, _and_
3. include names introduced indirectly via `use`.

Since a name from another crate but introduced via `use` has non-local `def_id`, to achieve this, a suggestion is displayed if either the `def_id` of the suggested name is local, or the `span` of the suggested name is in the same file as the typo itself.

Some UI tests have also been modified to reflect this change.

r? `@cjgillot`
Aaron1011 pushed a commit to Aaron1011/rust that referenced this pull request Jan 6, 2023
Rollup of 5 pull requests

Successful merges:

 - rust-lang#93582 (Allow `impl Fn() -> impl Trait` in return position)
 - rust-lang#103560 (Point only to the identifiers in the typo suggestions of shadowed names instead of the entire struct)
 - rust-lang#103588 (rustdoc: add missing URL redirect)
 - rust-lang#103689 (Do fewer passes and generally be more efficient when filtering tests)
 - rust-lang#103740 (rustdoc: remove unnecessary CSS `.search-results { padding-bottom }`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
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.

Point at shadowed name binding in you might have meant to refer to suggestion
5 participants