Skip to content

Commit e4120ee

Browse files
petrochenkovpietroalbini
authored andcommitted
Address review comments and cleanup code
1 parent 8a1b7da commit e4120ee

File tree

4 files changed

+59
-62
lines changed

4 files changed

+59
-62
lines changed

src/librustc_resolve/lib.rs

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5090,62 +5090,59 @@ impl<'a> Resolver<'a> {
50905090
}
50915091

50925092
// See https://github.com/rust-lang/rust/issues/32354
5093-
if old_binding.is_import() || new_binding.is_import() {
5094-
let binding = if new_binding.is_import() && !new_binding.span.is_dummy() {
5095-
new_binding
5093+
let directive = match (&new_binding.kind, &old_binding.kind) {
5094+
(NameBindingKind::Import { directive, .. }, _) if !new_binding.span.is_dummy() =>
5095+
Some((directive, new_binding.span)),
5096+
(_, NameBindingKind::Import { directive, .. }) if !old_binding.span.is_dummy() =>
5097+
Some((directive, old_binding.span)),
5098+
_ => None,
5099+
};
5100+
if let Some((directive, binding_span)) = directive {
5101+
let suggested_name = if name.as_str().chars().next().unwrap().is_uppercase() {
5102+
format!("Other{}", name)
50965103
} else {
5097-
old_binding
5104+
format!("other_{}", name)
50985105
};
50995106

5100-
let cm = self.session.source_map();
5101-
let rename_msg = "you can use `as` to change the binding name of the import";
5102-
5103-
if let (
5104-
Ok(snippet),
5105-
NameBindingKind::Import { directive, ..},
5106-
false,
5107-
false,
5108-
) = (
5109-
cm.span_to_snippet(binding.span),
5110-
binding.kind.clone(),
5111-
binding.span.is_dummy(),
5112-
binding.span.ctxt().outer().expn_info().is_some(),
5113-
) {
5114-
let suggested_name = if name.as_str().chars().next().unwrap().is_uppercase() {
5115-
format!("Other{}", name)
5116-
} else {
5117-
format!("other_{}", name)
5118-
};
5107+
let mut suggestion = None;
5108+
match directive.subclass {
5109+
ImportDirectiveSubclass::SingleImport { type_ns_only: true, .. } =>
5110+
suggestion = Some(format!("self as {}", suggested_name)),
5111+
ImportDirectiveSubclass::SingleImport { source, .. } => {
5112+
if let Some(pos) = source.span.hi().0.checked_sub(binding_span.lo().0)
5113+
.map(|pos| pos as usize) {
5114+
if let Ok(snippet) = self.session.source_map()
5115+
.span_to_snippet(binding_span) {
5116+
if pos <= snippet.len() {
5117+
suggestion = Some(format!(
5118+
"{} as {}{}",
5119+
&snippet[..pos],
5120+
suggested_name,
5121+
if snippet.ends_with(";") { ";" } else { "" }
5122+
))
5123+
}
5124+
}
5125+
}
5126+
}
5127+
ImportDirectiveSubclass::ExternCrate { source, target, .. } =>
5128+
suggestion = Some(format!(
5129+
"extern crate {} as {};",
5130+
source.unwrap_or(target.name),
5131+
suggested_name,
5132+
)),
5133+
_ => unreachable!(),
5134+
}
51195135

5136+
let rename_msg = "you can use `as` to change the binding name of the import";
5137+
if let Some(suggestion) = suggestion {
51205138
err.span_suggestion_with_applicability(
5121-
binding.span,
5122-
&rename_msg,
5123-
match directive.subclass {
5124-
ImportDirectiveSubclass::SingleImport { type_ns_only: true, .. } =>
5125-
format!("self as {}", suggested_name),
5126-
ImportDirectiveSubclass::SingleImport { source, .. } =>
5127-
format!(
5128-
"{} as {}{}",
5129-
&snippet[..((source.span.hi().0 - binding.span.lo().0) as usize)],
5130-
suggested_name,
5131-
if snippet.ends_with(";") {
5132-
";"
5133-
} else {
5134-
""
5135-
}
5136-
),
5137-
ImportDirectiveSubclass::ExternCrate { source, target, .. } =>
5138-
format!(
5139-
"extern crate {} as {};",
5140-
source.unwrap_or(target.name),
5141-
suggested_name,
5142-
),
5143-
_ => unreachable!(),
5144-
},
5139+
binding_span,
5140+
rename_msg,
5141+
suggestion,
51455142
Applicability::MaybeIncorrect,
51465143
);
51475144
} else {
5148-
err.span_label(binding.span, rename_msg);
5145+
err.span_label(binding_span, rename_msg);
51495146
}
51505147
}
51515148

src/test/ui/issues/issue-56411.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ macro_rules! import {
33
$(
44
mod $name;
55
pub use self::$name;
6-
//~^ ERROR the name `issue_56411` is defined multiple times
7-
//~| ERROR `issue_56411` is private, and cannot be re-exported
6+
//~^ ERROR the name `issue_56411_aux` is defined multiple times
7+
//~| ERROR `issue_56411_aux` is private, and cannot be re-exported
88

99
)*
1010
}
1111
}
1212

13-
import!(issue_56411);
13+
import!(issue_56411_aux);
1414

1515
fn main() {
1616
println!("Hello, world!");

src/test/ui/issues/issue-56411.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
error[E0255]: the name `issue_56411` is defined multiple times
1+
error[E0255]: the name `issue_56411_aux` is defined multiple times
22
--> $DIR/issue-56411.rs:5:21
33
|
44
LL | mod $name;
5-
| ---------- previous definition of the module `issue_56411` here
5+
| ---------- previous definition of the module `issue_56411_aux` here
66
LL | pub use self::$name;
77
| ^^^^^^^^^^^
88
| |
9-
| `issue_56411` reimported here
9+
| `issue_56411_aux` reimported here
1010
| you can use `as` to change the binding name of the import
1111
...
12-
LL | import!(issue_56411);
13-
| --------------------- in this macro invocation
12+
LL | import!(issue_56411_aux);
13+
| ------------------------- in this macro invocation
1414
|
15-
= note: `issue_56411` must be defined only once in the type namespace of this module
15+
= note: `issue_56411_aux` must be defined only once in the type namespace of this module
1616

17-
error[E0365]: `issue_56411` is private, and cannot be re-exported
17+
error[E0365]: `issue_56411_aux` is private, and cannot be re-exported
1818
--> $DIR/issue-56411.rs:5:21
1919
|
2020
LL | pub use self::$name;
21-
| ^^^^^^^^^^^ re-export of private `issue_56411`
21+
| ^^^^^^^^^^^ re-export of private `issue_56411_aux`
2222
...
23-
LL | import!(issue_56411);
24-
| --------------------- in this macro invocation
23+
LL | import!(issue_56411_aux);
24+
| ------------------------- in this macro invocation
2525
|
26-
= note: consider declaring type or module `issue_56411` with `pub`
26+
= note: consider declaring type or module `issue_56411_aux` with `pub`
2727

2828
error: aborting due to 2 previous errors
2929

0 commit comments

Comments
 (0)