Skip to content

#45829 when a renamed import conflict with a previous import #55113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4796,10 +4796,18 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
err.span_suggestion_with_applicability(
binding.span,
rename_msg,
if snippet.ends_with(';') {
format!("{} as {};", &snippet[..snippet.len() - 1], suggested_name)
if snippet.contains(" as ") {
format!(
"{} as {}",
&snippet[..snippet.find(" as ").unwrap()],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here (and above) this is not going to work if the snippet is "use\tfoo\tas\tbar;".


I'm looking at ImportDirectiveSubclass and what Ident already has a span field, so we can use target.span for the single use case. For extern crate, we should modify ImportDirectiveSubclass::ExternCrate(Option<Name>) to be ImportDirectiveSubclass::ExternCrate(Option<Ident>). By doing that you won't have to peek into the snippet to find the appropriate Span to use in the suggestion, nor peek into the span to be able to tell wether there's an as rename happening. Changing ExternCrate will have some knock down effects, but they should be minor.

suggested_name,
)
} else {
format!("{} as {}", snippet, suggested_name)
if snippet.ends_with(';') {
format!("{} as {};", &snippet[..snippet.len() - 1], suggested_name)
} else {
format!("{} as {}", snippet, suggested_name)
}
},
Applicability::MachineApplicable,
);
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/issues/issue-45829/auxiliary/issue_45829_a.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2018 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 const FOO: usize = *&0;
11 changes: 11 additions & 0 deletions src/test/ui/issues/issue-45829/auxiliary/issue_45829_b.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2018 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 const FOO: usize = *&0;
16 changes: 16 additions & 0 deletions src/test/ui/issues/issue-45829/rename-extern-vs-use.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2018 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.

// aux-build:issue_45829_b.rs

use std;
extern crate issue_45829_b as std;

fn main() {}
27 changes: 27 additions & 0 deletions src/test/ui/issues/issue-45829/rename-extern-vs-use.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error[E0259]: the name `std` is defined multiple times
--> $DIR/rename-extern-vs-use.rs:14:1
|
LL | extern crate issue_45829_b as std;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| `std` reimported here
| You can use `as` to change the binding name of the import
|
= note: `std` must be defined only once in the type namespace of this module

error[E0254]: the name `std` is defined multiple times
--> $DIR/rename-extern-vs-use.rs:13:5
|
LL | use std;
| ^^^ `std` reimported here
|
= note: `std` 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
|
LL | use std as other_std;
| ^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

Some errors occurred: E0254, E0259.
For more information about an error, try `rustc --explain E0254`.
17 changes: 17 additions & 0 deletions src/test/ui/issues/issue-45829/rename-extern.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2018 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.

// aux-build:issue_45829_a.rs
// aux-build:issue_45829_b.rs

extern crate issue_45829_a;
extern crate issue_45829_b as issue_45829_a;

fn main() {}
16 changes: 16 additions & 0 deletions src/test/ui/issues/issue-45829/rename-extern.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0259]: the name `issue_45829_a` is defined multiple times
--> $DIR/rename-extern.rs:15:1
|
LL | extern crate issue_45829_a;
| --------------------------- previous import of the extern crate `issue_45829_a` here
LL | extern crate issue_45829_b as issue_45829_a;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| `issue_45829_a` reimported here
| You can use `as` to change the binding name of the import
|
= note: `issue_45829_a` must be defined only once in the type namespace of this module

error: aborting due to previous error

For more information about this error, try `rustc --explain E0259`.
16 changes: 16 additions & 0 deletions src/test/ui/issues/issue-45829/rename-use-vs-extern.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2018 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.

// aux-build:issue_45829_b.rs

extern crate issue_45829_b;
use std as issue_45829_b;

fn main() {}
17 changes: 17 additions & 0 deletions src/test/ui/issues/issue-45829/rename-use-vs-extern.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0254]: the name `issue_45829_b` is defined multiple times
--> $DIR/rename-use-vs-extern.rs:14:5
|
LL | extern crate issue_45829_b;
| --------------------------- previous import of the extern crate `issue_45829_b` here
LL | use std as issue_45829_b;
| ^^^^^^^^^^^^^^^^^^^^ `issue_45829_b` reimported here
|
= note: `issue_45829_b` 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
|
LL | use std as other_issue_45829_b;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0254`.
13 changes: 13 additions & 0 deletions src/test/ui/issues/issue-45829/rename-with-path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2018 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.

use std::{collections::HashMap as A, sync::Arc as A};

fn main() {}
17 changes: 17 additions & 0 deletions src/test/ui/issues/issue-45829/rename-with-path.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0252]: the name `A` is defined multiple times
--> $DIR/rename-with-path.rs:11:38
|
LL | use std::{collections::HashMap as A, sync::Arc as A};
| ------------------------- ^^^^^^^^^^^^^^ `A` reimported here
| |
| previous import of the type `A` here
|
= note: `A` 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
|
LL | use std::{collections::HashMap as A, sync::Arc as OtherA};
| ^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0252`.
16 changes: 16 additions & 0 deletions src/test/ui/issues/issue-45829/rename.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2018 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.

use core;
use std as core;

fn main() {
1 + 1;
}
17 changes: 17 additions & 0 deletions src/test/ui/issues/issue-45829/rename.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0252]: the name `core` is defined multiple times
--> $DIR/rename.rs:12:5
|
LL | use core;
| ---- previous import of the module `core` here
LL | use std as core;
| ^^^^^^^^^^^ `core` reimported here
|
= note: `core` 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
|
LL | use std as other_core;
| ^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0252`.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ LL | use std::slice as std; //~ ERROR the name `std` is defined multiple times
= note: `std` 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
|
LL | use std::slice as std as other_std; //~ ERROR the name `std` is defined multiple times
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | use std::slice as other_std; //~ ERROR the name `std` is defined multiple times
| ^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

Expand Down