Skip to content

Commit ff83240

Browse files
committed
Suggest renaming import if names clash
1 parent 6713736 commit ff83240

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/librustc_resolve/lib.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -3606,12 +3606,12 @@ impl<'a> Resolver<'a> {
36063606
}
36073607
}
36083608

3609-
fn report_conflict(&mut self,
3609+
fn report_conflict<'b>(&mut self,
36103610
parent: Module,
36113611
ident: Ident,
36123612
ns: Namespace,
3613-
new_binding: &NameBinding,
3614-
old_binding: &NameBinding) {
3613+
new_binding: &NameBinding<'b>,
3614+
old_binding: &NameBinding<'b>) {
36153615
// Error on the second of two conflicting names
36163616
if old_binding.span.lo() > new_binding.span.lo() {
36173617
return self.report_conflict(parent, ident, ns, old_binding, new_binding);
@@ -3683,6 +3683,26 @@ impl<'a> Resolver<'a> {
36833683
old_noun, old_kind, name));
36843684
}
36853685

3686+
// See https://github.com/rust-lang/rust/issues/32354
3687+
if old_binding.is_import() || new_binding.is_import() {
3688+
let binding = if new_binding.is_import() {
3689+
new_binding
3690+
} else {
3691+
old_binding
3692+
};
3693+
3694+
let cm = self.session.codemap();
3695+
let rename_msg = "You can use `as` to change the binding name of the import";
3696+
3697+
if let Ok(snippet) = cm.span_to_snippet(binding.span) {
3698+
err.span_suggestion(binding.span,
3699+
rename_msg,
3700+
format!("{} as Other{}", snippet, name));
3701+
} else {
3702+
err.span_label(binding.span, rename_msg);
3703+
}
3704+
}
3705+
36863706
err.emit();
36873707
self.name_already_seen.insert(name, span);
36883708
}

0 commit comments

Comments
 (0)