diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index a93cc7ad751..9d97997b1da 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -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()], + 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, ); diff --git a/src/test/ui/issues/issue-45829/auxiliary/issue_45829_a.rs b/src/test/ui/issues/issue-45829/auxiliary/issue_45829_a.rs new file mode 100644 index 00000000000..56eb1541e1f --- /dev/null +++ b/src/test/ui/issues/issue-45829/auxiliary/issue_45829_a.rs @@ -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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +pub const FOO: usize = *&0; diff --git a/src/test/ui/issues/issue-45829/auxiliary/issue_45829_b.rs b/src/test/ui/issues/issue-45829/auxiliary/issue_45829_b.rs new file mode 100644 index 00000000000..56eb1541e1f --- /dev/null +++ b/src/test/ui/issues/issue-45829/auxiliary/issue_45829_b.rs @@ -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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +pub const FOO: usize = *&0; diff --git a/src/test/ui/issues/issue-45829/rename-extern-vs-use.rs b/src/test/ui/issues/issue-45829/rename-extern-vs-use.rs new file mode 100644 index 00000000000..3d3e03aa350 --- /dev/null +++ b/src/test/ui/issues/issue-45829/rename-extern-vs-use.rs @@ -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 or the MIT license +// , 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() {} diff --git a/src/test/ui/issues/issue-45829/rename-extern-vs-use.stderr b/src/test/ui/issues/issue-45829/rename-extern-vs-use.stderr new file mode 100644 index 00000000000..f723abd3301 --- /dev/null +++ b/src/test/ui/issues/issue-45829/rename-extern-vs-use.stderr @@ -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`. diff --git a/src/test/ui/issues/issue-45829/rename-extern.rs b/src/test/ui/issues/issue-45829/rename-extern.rs new file mode 100644 index 00000000000..7c3d9724005 --- /dev/null +++ b/src/test/ui/issues/issue-45829/rename-extern.rs @@ -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 or the MIT license +// , 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() {} diff --git a/src/test/ui/issues/issue-45829/rename-extern.stderr b/src/test/ui/issues/issue-45829/rename-extern.stderr new file mode 100644 index 00000000000..e82e99588c1 --- /dev/null +++ b/src/test/ui/issues/issue-45829/rename-extern.stderr @@ -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`. diff --git a/src/test/ui/issues/issue-45829/rename-use-vs-extern.rs b/src/test/ui/issues/issue-45829/rename-use-vs-extern.rs new file mode 100644 index 00000000000..1cc261ed922 --- /dev/null +++ b/src/test/ui/issues/issue-45829/rename-use-vs-extern.rs @@ -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 or the MIT license +// , 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() {} diff --git a/src/test/ui/issues/issue-45829/rename-use-vs-extern.stderr b/src/test/ui/issues/issue-45829/rename-use-vs-extern.stderr new file mode 100644 index 00000000000..eff5d8a2cb6 --- /dev/null +++ b/src/test/ui/issues/issue-45829/rename-use-vs-extern.stderr @@ -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`. diff --git a/src/test/ui/issues/issue-45829/rename-with-path.rs b/src/test/ui/issues/issue-45829/rename-with-path.rs new file mode 100644 index 00000000000..dbe8733735e --- /dev/null +++ b/src/test/ui/issues/issue-45829/rename-with-path.rs @@ -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 or the MIT license +// , 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() {} diff --git a/src/test/ui/issues/issue-45829/rename-with-path.stderr b/src/test/ui/issues/issue-45829/rename-with-path.stderr new file mode 100644 index 00000000000..bfeb879e6e5 --- /dev/null +++ b/src/test/ui/issues/issue-45829/rename-with-path.stderr @@ -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`. diff --git a/src/test/ui/issues/issue-45829/rename.rs b/src/test/ui/issues/issue-45829/rename.rs new file mode 100644 index 00000000000..7c6d87b1d20 --- /dev/null +++ b/src/test/ui/issues/issue-45829/rename.rs @@ -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 or the MIT license +// , 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; +} diff --git a/src/test/ui/issues/issue-45829/rename.stderr b/src/test/ui/issues/issue-45829/rename.stderr new file mode 100644 index 00000000000..1ebd73a55a9 --- /dev/null +++ b/src/test/ui/issues/issue-45829/rename.stderr @@ -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`. diff --git a/src/test/ui/resolve/resolve-conflict-import-vs-extern-crate.stderr b/src/test/ui/resolve/resolve-conflict-import-vs-extern-crate.stderr index 9978e75af30..be3600da51d 100644 --- a/src/test/ui/resolve/resolve-conflict-import-vs-extern-crate.stderr +++ b/src/test/ui/resolve/resolve-conflict-import-vs-extern-crate.stderr @@ -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