-
Notifications
You must be signed in to change notification settings - Fork 13.4k
#45829 when a renamed import conflict with a previous import #55113
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
Changes from 5 commits
cac95ee
ad4cea4
2ba567f
4520b30
9eacd68
8fe6688
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1234,7 +1234,7 @@ impl<'a> NameBinding<'a> { | |
match self.kind { | ||
NameBindingKind::Import { | ||
directive: &ImportDirective { | ||
subclass: ImportDirectiveSubclass::ExternCrate(_), .. | ||
subclass: ImportDirectiveSubclass::ExternCrate { .. }, .. | ||
}, .. | ||
} => true, | ||
_ => false, | ||
|
@@ -1248,15 +1248,6 @@ impl<'a> NameBinding<'a> { | |
} | ||
} | ||
|
||
fn is_renamed_extern_crate(&self) -> bool { | ||
if let NameBindingKind::Import { directive, ..} = self.kind { | ||
if let ImportDirectiveSubclass::ExternCrate(Some(_)) = directive.subclass { | ||
return true; | ||
} | ||
} | ||
false | ||
} | ||
|
||
fn is_glob_import(&self) -> bool { | ||
match self.kind { | ||
NameBindingKind::Import { directive, .. } => directive.is_glob(), | ||
|
@@ -3803,7 +3794,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> { | |
if let NameBindingKind::Import { directive: d, .. } = binding.kind { | ||
// Careful: we still want to rewrite paths from | ||
// renamed extern crates. | ||
if let ImportDirectiveSubclass::ExternCrate(None) = d.subclass { | ||
if let ImportDirectiveSubclass::ExternCrate { source: None, .. } = d.subclass { | ||
return | ||
} | ||
} | ||
|
@@ -4783,10 +4774,17 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> { | |
}; | ||
|
||
let cm = self.session.source_map(); | ||
let rename_msg = "You can use `as` to change the binding name of the import"; | ||
|
||
if let (Ok(snippet), false) = (cm.span_to_snippet(binding.span), | ||
binding.is_renamed_extern_crate()) { | ||
let rename_msg = "you can use `as` to change the binding name of the import"; | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. this filter out implicit import, like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ( |
||
) = ( | ||
cm.span_to_snippet(binding.span), | ||
binding.kind.clone(), | ||
binding.span.hi().0, | ||
mockersf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) { | ||
let suggested_name = if name.as_str().chars().next().unwrap().is_uppercase() { | ||
format!("Other{}", name) | ||
} else { | ||
|
@@ -4795,13 +4793,31 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> { | |
|
||
err.span_suggestion_with_applicability( | ||
mockersf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
binding.span, | ||
rename_msg, | ||
if snippet.ends_with(';') { | ||
format!("{} as {};", &snippet[..snippet.len() - 1], suggested_name) | ||
} else { | ||
format!("{} as {}", snippet, suggested_name) | ||
&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 commentThe reason will be displayed to describe this comment to others. Learn more. imports with |
||
(ImportDirectiveSubclass::SingleImport { source, .. }, false, _) => | ||
format!( | ||
"{} as {}", | ||
&snippet[..((source.span.hi().0 - binding.span.lo().0) as usize)], | ||
suggested_name, | ||
), | ||
(ImportDirectiveSubclass::SingleImport { source, .. }, true, _) => | ||
format!( | ||
"{} as {};", | ||
&snippet[..((source.span.hi().0 - binding.span.lo().0) as usize)], | ||
suggested_name, | ||
), | ||
mockersf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(ImportDirectiveSubclass::ExternCrate { source, target, .. }, _, _) => | ||
format!( | ||
"extern crate {} as {};", | ||
source.unwrap_or(target.name), | ||
suggested_name, | ||
), | ||
(_, _, _) => unreachable!(), | ||
}, | ||
Applicability::MachineApplicable, | ||
Applicability::MaybeIncorrect, | ||
); | ||
} else { | ||
err.span_label(binding.span, rename_msg); | ||
|
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)