Skip to content

Commit 0ec40c1

Browse files
authored
Rollup merge of #45660 - Cldfire:suggest-rename-import, r=estebank
Suggest renaming import if names clash Closes #32354. The output for the example in the issue looks like this: ``` ~/p/local-rust-dev-testing ❯❯❯ cargo +local-s1 build Compiling local-rust-dev-testing v0.1.0 (file:///home/cldfire/programming_projects/local-rust-dev-testing) error[E0252]: the name `ConstructorExtension` is defined multiple times --> src/main.rs:49:5 | 48 | use extension1::ConstructorExtension; | -------------------------------- previous import of the trait `ConstructorExtension` here 49 | use extension2::ConstructorExtension; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ConstructorExtension` reimported here | = note: `ConstructorExtension` 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 | 49 | use extension2::ConstructorExtension as OtherConstructorExtension; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ... ``` This is my first PR that touches the compiler in any way, so if there's something else I need to do here (e.g. add a test), please let me know :).
2 parents 63ad129 + 61396a8 commit 0ec40c1

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub mod extension1 {
12+
pub trait ConstructorExtension {}
13+
}
14+
15+
pub mod extension2 {
16+
pub trait ConstructorExtension {}
17+
}
18+
19+
use extension1::ConstructorExtension;
20+
use extension2::ConstructorExtension;
21+
22+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0252]: the name `ConstructorExtension` is defined multiple times
2+
--> $DIR/issue-32354-suggest-import-rename.rs:20:5
3+
|
4+
19 | use extension1::ConstructorExtension;
5+
| -------------------------------- previous import of the trait `ConstructorExtension` here
6+
20 | use extension2::ConstructorExtension;
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ConstructorExtension` reimported here
8+
|
9+
= note: `ConstructorExtension` must be defined only once in the type namespace of this module
10+
help: You can use `as` to change the binding name of the import
11+
|
12+
20 | use extension2::ConstructorExtension as OtherConstructorExtension;
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: aborting due to previous error
16+

0 commit comments

Comments
 (0)