Skip to content

Commit 1b659d6

Browse files
committed
Address review comments and cleanup code
1 parent ac4b685 commit 1b659d6

File tree

4 files changed

+59
-62
lines changed

4 files changed

+59
-62
lines changed

Diff for: src/librustc_resolve/lib.rs

+45-48
Original file line numberDiff line numberDiff line change
@@ -5134,62 +5134,59 @@ impl<'a> Resolver<'a> {
51345134
);
51355135

51365136
// See https://github.com/rust-lang/rust/issues/32354
5137-
if old_binding.is_import() || new_binding.is_import() {
5138-
let binding = if new_binding.is_import() && !new_binding.span.is_dummy() {
5139-
new_binding
5137+
let directive = match (&new_binding.kind, &old_binding.kind) {
5138+
(NameBindingKind::Import { directive, .. }, _) if !new_binding.span.is_dummy() =>
5139+
Some((directive, new_binding.span)),
5140+
(_, NameBindingKind::Import { directive, .. }) if !old_binding.span.is_dummy() =>
5141+
Some((directive, old_binding.span)),
5142+
_ => None,
5143+
};
5144+
if let Some((directive, binding_span)) = directive {
5145+
let suggested_name = if name.as_str().chars().next().unwrap().is_uppercase() {
5146+
format!("Other{}", name)
51405147
} else {
5141-
old_binding
5148+
format!("other_{}", name)
51425149
};
51435150

5144-
let cm = self.session.source_map();
5145-
let rename_msg = "you can use `as` to change the binding name of the import";
5146-
5147-
if let (
5148-
Ok(snippet),
5149-
NameBindingKind::Import { directive, ..},
5150-
false,
5151-
false,
5152-
) = (
5153-
cm.span_to_snippet(binding.span),
5154-
binding.kind.clone(),
5155-
binding.span.is_dummy(),
5156-
binding.span.ctxt().outer().expn_info().is_some(),
5157-
) {
5158-
let suggested_name = if name.as_str().chars().next().unwrap().is_uppercase() {
5159-
format!("Other{}", name)
5160-
} else {
5161-
format!("other_{}", name)
5162-
};
5151+
let mut suggestion = None;
5152+
match directive.subclass {
5153+
ImportDirectiveSubclass::SingleImport { type_ns_only: true, .. } =>
5154+
suggestion = Some(format!("self as {}", suggested_name)),
5155+
ImportDirectiveSubclass::SingleImport { source, .. } => {
5156+
if let Some(pos) = source.span.hi().0.checked_sub(binding_span.lo().0)
5157+
.map(|pos| pos as usize) {
5158+
if let Ok(snippet) = self.session.source_map()
5159+
.span_to_snippet(binding_span) {
5160+
if pos <= snippet.len() {
5161+
suggestion = Some(format!(
5162+
"{} as {}{}",
5163+
&snippet[..pos],
5164+
suggested_name,
5165+
if snippet.ends_with(";") { ";" } else { "" }
5166+
))
5167+
}
5168+
}
5169+
}
5170+
}
5171+
ImportDirectiveSubclass::ExternCrate { source, target, .. } =>
5172+
suggestion = Some(format!(
5173+
"extern crate {} as {};",
5174+
source.unwrap_or(target.name),
5175+
suggested_name,
5176+
)),
5177+
_ => unreachable!(),
5178+
}
51635179

5180+
let rename_msg = "you can use `as` to change the binding name of the import";
5181+
if let Some(suggestion) = suggestion {
51645182
err.span_suggestion_with_applicability(
5165-
binding.span,
5166-
&rename_msg,
5167-
match directive.subclass {
5168-
ImportDirectiveSubclass::SingleImport { type_ns_only: true, .. } =>
5169-
format!("self as {}", suggested_name),
5170-
ImportDirectiveSubclass::SingleImport { source, .. } =>
5171-
format!(
5172-
"{} as {}{}",
5173-
&snippet[..((source.span.hi().0 - binding.span.lo().0) as usize)],
5174-
suggested_name,
5175-
if snippet.ends_with(";") {
5176-
";"
5177-
} else {
5178-
""
5179-
}
5180-
),
5181-
ImportDirectiveSubclass::ExternCrate { source, target, .. } =>
5182-
format!(
5183-
"extern crate {} as {};",
5184-
source.unwrap_or(target.name),
5185-
suggested_name,
5186-
),
5187-
_ => unreachable!(),
5188-
},
5183+
binding_span,
5184+
rename_msg,
5185+
suggestion,
51895186
Applicability::MaybeIncorrect,
51905187
);
51915188
} else {
5192-
err.span_label(binding.span, rename_msg);
5189+
err.span_label(binding_span, rename_msg);
51935190
}
51945191
}
51955192

Diff for: src/test/ui/issues/issue-56411.rs

+3-3
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!");

Diff for: src/test/ui/issues/issue-56411.stderr

+11-11
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

File renamed without changes.

0 commit comments

Comments
 (0)