-
-
Notifications
You must be signed in to change notification settings - Fork 14.3k
#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 4 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 |
|---|---|---|
|
|
@@ -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(), | ||
|
|
@@ -4783,10 +4774,9 @@ 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"; | ||
| 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()) { | ||
| if let Ok(snippet) = cm.span_to_snippet(binding.span) { | ||
| let suggested_name = if name.as_str().chars().next().unwrap().is_uppercase() { | ||
| format!("Other{}", name) | ||
| } else { | ||
|
|
@@ -4796,12 +4786,25 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> { | |
| err.span_suggestion_with_applicability( | ||
| binding.span, | ||
| rename_msg, | ||
| if snippet.ends_with(';') { | ||
| format!("{} as {};", &snippet[..snippet.len() - 1], suggested_name) | ||
| } else { | ||
| format!("{} as {}", snippet, suggested_name) | ||
| match ( | ||
| snippet.split_whitespace().find(|w| *w == "as"), | ||
| snippet.ends_with(";") | ||
| ) { | ||
| (Some(_), false) => format!("{} as {}", | ||
| &snippet[..snippet.find(" as ").unwrap()], | ||
mockersf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| suggested_name, | ||
| ), | ||
| (Some(_), true) => format!("{} as {};", | ||
| &snippet[..snippet.find(" as ").unwrap()], | ||
|
||
| suggested_name, | ||
| ), | ||
| (None, false) => format!("{} as {}", snippet, suggested_name), | ||
| (None, true) => format!("{} as {};", | ||
| &snippet[..snippet.len() - 1], | ||
| suggested_name | ||
| ), | ||
| }, | ||
| Applicability::MachineApplicable, | ||
| Applicability::MaybeIncorrect, | ||
| ); | ||
| } else { | ||
| err.span_label(binding.span, rename_msg); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
| // file at the top-level directory of this distribution and at | ||
| // http://rust-lang.org/COPYRIGHT. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
| // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
| // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
| // option. This file may not be copied, modified, or distributed | ||
| // except according to those terms. | ||
|
|
||
| pub const FOO: usize = *&0; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
| // file at the top-level directory of this distribution and at | ||
| // http://rust-lang.org/COPYRIGHT. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
| // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
| // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
| // option. This file may not be copied, modified, or distributed | ||
| // except according to those terms. | ||
|
|
||
| pub const FOO: usize = *&0; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
| // file at the top-level directory of this distribution and at | ||
| // http://rust-lang.org/COPYRIGHT. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
| // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
| // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
| // option. This file may not be copied, modified, or distributed | ||
| // except according to those terms. | ||
|
|
||
| mod foo { | ||
| pub struct A; | ||
| pub struct B; | ||
| } | ||
|
|
||
| use foo::{A, B as A}; | ||
|
|
||
| fn main() {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| error[E0252]: the name `A` is defined multiple times | ||
| --> $DIR/issue-45829.rs:16:14 | ||
| | | ||
| LL | use foo::{A, B as A}; | ||
| | - ^^^^^^ `A` reimported here | ||
| | | | ||
| | previous import of the type `A` here | ||
| | | ||
| = note: `A` must be defined only once in the type namespace of this module | ||
| help: you can use `as` to change the binding name of the import | ||
| | | ||
| LL | use foo::{A, B as OtherA}; | ||
| | ^^^^^^^^^^^ | ||
|
|
||
| error: aborting due to previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0252`. |
Uh oh!
There was an error while loading. Please reload this page.