-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Conversation
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. |
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)) |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!
@cjgillot I believe everything is ready |
Could you squash the commits to remove the back-and-forth? |
c147c9d
to
0eaf6d5
Compare
@cjgillot Done! Sorry for the confusion |
@bors r+ |
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
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`
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
Fixes #103358.
As discussed in the issue, the
Span
of the candidateIdent
for a typo replacement is stored alongside itsSymbol
inTypoSuggestion
. 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:
std
, anduse
.Since a name from another crate but introduced via
use
has non-localdef_id
, to achieve this, a suggestion is displayed if either thedef_id
of the suggested name is local, or thespan
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