-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
#45829 when a renamed import conflict with a previous import #55113
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @zackmdavis (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
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.
Thanks for the fix! I left a few comments inline.
src/librustc_resolve/lib.rs
Outdated
suggested_name, | ||
), | ||
(Some(_), true) => format!("{} as {};", | ||
&snippet[..snippet.find(" as ").unwrap()], |
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.
Here (and above) this is not going to work if the snippet is "use\tfoo\tas\tbar;"
.
I'm looking at ImportDirectiveSubclass
and what Ident
already has a span
field, so we can use target.span
for the single use
case. For extern crate
, we should modify ImportDirectiveSubclass::ExternCrate(Option<Name>)
to be ImportDirectiveSubclass::ExternCrate(Option<Ident>)
. By doing that you won't have to peek into the snippet to find the appropriate Span
to use in the suggestion, nor peek into the span to be able to tell wether there's an as
rename happening. Changing ExternCrate
will have some knock down effects, but they should be minor.
subclass: ImportDirectiveSubclass::ExternCrate(orig_name), | ||
subclass: ImportDirectiveSubclass::ExternCrate { | ||
source: orig_name, | ||
target: ident, |
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.
source is the optional original name if the import is renamed, target is the name it's imported at (so the original name if there was no rename)
src/librustc_resolve/lib.rs
Outdated
if let ( | ||
Ok(snippet), | ||
NameBindingKind::Import { directive, ..}, | ||
_x @ 1 ... std::u32::MAX, |
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.
this filter out implicit import, like std
&rename_msg, | ||
match (&directive.subclass, snippet.ends_with(";"), snippet.as_ref()) { | ||
(ImportDirectiveSubclass::SingleImport { .. }, false, "self") => | ||
format!("self as {}", suggested_name), |
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.
imports with self
are a simple case, as it's only possible in the form import foo::{ self }
help: You can use `as` to change the binding name of the import | ||
| | ||
LL | as other_std// Copyright 2014 The Rust Project Developers. See the COPYRIGHT | ||
| ^^^^^^^^^^^^ |
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.
this was actually another incorrect suggestion that is now not present
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.
Looking good!
A couple of nitpicks and then I can approve.
Great work on getting this done!
pub struct B; | ||
} | ||
|
||
use foo::{A, A}; |
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.
This is a good test to add. In this case it might be nice not to suggest renaming, as it is more likely a duplicated import, but adding that is something best left to a follow up PR. This would also ideally affect the case where we import both use foo
and use foo::{ self }
.
src/librustc_resolve/lib.rs
Outdated
if let ( | ||
Ok(snippet), | ||
NameBindingKind::Import { directive, ..}, | ||
_x @ 1 ... std::u32::MAX, |
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.
(..=
is the new, syntax for inclusive ranges, preferred over ...
.)
if let ( | ||
Ok(snippet), | ||
NameBindingKind::Import { directive, ..}, | ||
_dummy @ false, |
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.
Side note: This could have been just false
:)
@bors r+ |
📌 Commit 8fe6688 has been approved by |
#45829 when a renamed import conflict with a previous import Fix the suggestion when a renamed import conflict. It check if the snipped contains `" as "`, and if so uses everything before for the suggestion.
☀️ Test successful - status-appveyor, status-travis |
Fix the suggestion when a renamed import conflict.
It check if the snipped contains
" as "
, and if so uses everything before for the suggestion.