Skip to content

Commit

Permalink
Rollup merge of rust-lang#45660 - Cldfire:suggest-rename-import, r=es…
Browse files Browse the repository at this point in the history
…tebank

Suggest renaming import if names clash

Closes rust-lang#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 :).
  • Loading branch information
kennytm authored Nov 1, 2017
2 parents 63ad129 + 61396a8 commit 0ec40c1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3606,12 +3606,12 @@ impl<'a> Resolver<'a> {
}
}

fn report_conflict(&mut self,
fn report_conflict<'b>(&mut self,
parent: Module,
ident: Ident,
ns: Namespace,
new_binding: &NameBinding,
old_binding: &NameBinding) {
new_binding: &NameBinding<'b>,
old_binding: &NameBinding<'b>) {
// Error on the second of two conflicting names
if old_binding.span.lo() > new_binding.span.lo() {
return self.report_conflict(parent, ident, ns, old_binding, new_binding);
Expand Down Expand Up @@ -3683,6 +3683,26 @@ impl<'a> Resolver<'a> {
old_noun, old_kind, name));
}

// See https://github.com/rust-lang/rust/issues/32354
if old_binding.is_import() || new_binding.is_import() {
let binding = if new_binding.is_import() {
new_binding
} else {
old_binding
};

let cm = self.session.codemap();
let rename_msg = "You can use `as` to change the binding name of the import";

if let Ok(snippet) = cm.span_to_snippet(binding.span) {
err.span_suggestion(binding.span,
rename_msg,
format!("{} as Other{}", snippet, name));
} else {
err.span_label(binding.span, rename_msg);
}
}

err.emit();
self.name_already_seen.insert(name, span);
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/ui/suggestions/issue-32354-suggest-import-rename.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2017 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 mod extension1 {
pub trait ConstructorExtension {}
}

pub mod extension2 {
pub trait ConstructorExtension {}
}

use extension1::ConstructorExtension;
use extension2::ConstructorExtension;

fn main() {}
16 changes: 16 additions & 0 deletions src/test/ui/suggestions/issue-32354-suggest-import-rename.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0252]: the name `ConstructorExtension` is defined multiple times
--> $DIR/issue-32354-suggest-import-rename.rs:20:5
|
19 | use extension1::ConstructorExtension;
| -------------------------------- previous import of the trait `ConstructorExtension` here
20 | 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
|
20 | use extension2::ConstructorExtension as OtherConstructorExtension;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

0 comments on commit 0ec40c1

Please sign in to comment.