Skip to content

Commit cac95ee

Browse files
committed
rust-lang#45829 when a renamed import conflict with a previous import
1 parent 5a52983 commit cac95ee

14 files changed

+207
-5
lines changed

src/librustc_resolve/lib.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -4796,10 +4796,18 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
47964796
err.span_suggestion_with_applicability(
47974797
binding.span,
47984798
rename_msg,
4799-
if snippet.ends_with(';') {
4800-
format!("{} as {};", &snippet[..snippet.len() - 1], suggested_name)
4799+
if snippet.contains(" as ") {
4800+
format!(
4801+
"{} as {}",
4802+
&snippet[..snippet.find(" as ").unwrap()],
4803+
suggested_name,
4804+
)
48014805
} else {
4802-
format!("{} as {}", snippet, suggested_name)
4806+
if snippet.ends_with(';') {
4807+
format!("{} as {};", &snippet[..snippet.len() - 1], suggested_name)
4808+
} else {
4809+
format!("{} as {}", snippet, suggested_name)
4810+
}
48034811
},
48044812
Applicability::MachineApplicable,
48054813
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2018 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 const FOO: usize = *&0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2018 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 const FOO: usize = *&0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2018 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+
// aux-build:issue_45829_b.rs
12+
13+
use std;
14+
extern crate issue_45829_b as std;
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0259]: the name `std` is defined multiple times
2+
--> $DIR/rename-extern-vs-use.rs:14:1
3+
|
4+
LL | extern crate issue_45829_b as std;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| `std` reimported here
8+
| You can use `as` to change the binding name of the import
9+
|
10+
= note: `std` must be defined only once in the type namespace of this module
11+
12+
error[E0254]: the name `std` is defined multiple times
13+
--> $DIR/rename-extern-vs-use.rs:13:5
14+
|
15+
LL | use std;
16+
| ^^^ `std` reimported here
17+
|
18+
= note: `std` must be defined only once in the type namespace of this module
19+
help: You can use `as` to change the binding name of the import
20+
|
21+
LL | use std as other_std;
22+
| ^^^^^^^^^^^^^^^^
23+
24+
error: aborting due to 2 previous errors
25+
26+
Some errors occurred: E0254, E0259.
27+
For more information about an error, try `rustc --explain E0254`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2018 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+
// aux-build:issue_45829_a.rs
12+
// aux-build:issue_45829_b.rs
13+
14+
extern crate issue_45829_a;
15+
extern crate issue_45829_b as issue_45829_a;
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0259]: the name `issue_45829_a` is defined multiple times
2+
--> $DIR/rename-extern.rs:15:1
3+
|
4+
LL | extern crate issue_45829_a;
5+
| --------------------------- previous import of the extern crate `issue_45829_a` here
6+
LL | extern crate issue_45829_b as issue_45829_a;
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8+
| |
9+
| `issue_45829_a` reimported here
10+
| You can use `as` to change the binding name of the import
11+
|
12+
= note: `issue_45829_a` must be defined only once in the type namespace of this module
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0259`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2018 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+
// aux-build:issue_45829_b.rs
12+
13+
extern crate issue_45829_b;
14+
use std as issue_45829_b;
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0254]: the name `issue_45829_b` is defined multiple times
2+
--> $DIR/rename-use-vs-extern.rs:14:5
3+
|
4+
LL | extern crate issue_45829_b;
5+
| --------------------------- previous import of the extern crate `issue_45829_b` here
6+
LL | use std as issue_45829_b;
7+
| ^^^^^^^^^^^^^^^^^^^^ `issue_45829_b` reimported here
8+
|
9+
= note: `issue_45829_b` 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+
LL | use std as other_issue_45829_b;
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0254`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2018 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+
use std::{collections::HashMap as A, sync::Arc as A};
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0252]: the name `A` is defined multiple times
2+
--> $DIR/rename-with-path.rs:11:38
3+
|
4+
LL | use std::{collections::HashMap as A, sync::Arc as A};
5+
| ------------------------- ^^^^^^^^^^^^^^ `A` reimported here
6+
| |
7+
| previous import of the type `A` here
8+
|
9+
= note: `A` 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+
LL | use std::{collections::HashMap as A, sync::Arc as OtherA};
13+
| ^^^^^^^^^^^^^^^^^^^
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0252`.
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2018 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+
use core;
12+
use std as core;
13+
14+
fn main() {
15+
1 + 1;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0252]: the name `core` is defined multiple times
2+
--> $DIR/rename.rs:12:5
3+
|
4+
LL | use core;
5+
| ---- previous import of the module `core` here
6+
LL | use std as core;
7+
| ^^^^^^^^^^^ `core` reimported here
8+
|
9+
= note: `core` 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+
LL | use std as other_core;
13+
| ^^^^^^^^^^^^^^^^^
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0252`.

src/test/ui/resolve/resolve-conflict-import-vs-extern-crate.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ LL | use std::slice as std; //~ ERROR the name `std` is defined multiple times
77
= note: `std` must be defined only once in the type namespace of this module
88
help: You can use `as` to change the binding name of the import
99
|
10-
LL | use std::slice as std as other_std; //~ ERROR the name `std` is defined multiple times
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
LL | use std::slice as other_std; //~ ERROR the name `std` is defined multiple times
11+
| ^^^^^^^^^^^^^^^^^^^^^^^
1212

1313
error: aborting due to previous error
1414

0 commit comments

Comments
 (0)