Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
37 changes: 20 additions & 17 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1248,15 +1248,6 @@ impl<'a> NameBinding<'a> {
}
}

fn is_renamed_extern_crate(&self) -> bool {
if let NameBindingKind::Import { directive, ..} = self.kind {
if let ImportDirectiveSubclass::ExternCrate(Some(_)) = directive.subclass {
return true;
}
}
false
}

fn is_glob_import(&self) -> bool {
match self.kind {
NameBindingKind::Import { directive, .. } => directive.is_glob(),
Expand Down Expand Up @@ -4783,10 +4774,9 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
};

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

if let (Ok(snippet), false) = (cm.span_to_snippet(binding.span),
binding.is_renamed_extern_crate()) {
if let Ok(snippet) = cm.span_to_snippet(binding.span) {
let suggested_name = if name.as_str().chars().next().unwrap().is_uppercase() {
format!("Other{}", name)
} else {
Expand All @@ -4796,12 +4786,25 @@ 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)
} else {
format!("{} as {}", snippet, suggested_name)
match (
snippet.split_whitespace().find(|w| *w == "as"),
snippet.ends_with(";")
) {
(Some(_), false) => format!("{} as {}",
&snippet[..snippet.find(" as ").unwrap()],
suggested_name,
),
(Some(_), true) => 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,
),
(None, false) => format!("{} as {}", snippet, suggested_name),
(None, true) => format!("{} as {};",
&snippet[..snippet.len() - 1],
suggested_name
),
},
Applicability::MachineApplicable,
Applicability::MaybeIncorrect,
);
} else {
err.span_label(binding.span, rename_msg);
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/blind/blind-item-block-item-shadow.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | use foo::Bar;
| ^^^^^^^^ `Bar` reimported here
|
= note: `Bar` must be defined only once in the type namespace of this block
help: You can use `as` to change the binding name of the import
help: you can use `as` to change the binding name of the import
|
LL | use foo::Bar as OtherBar;
| ^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/blind/blind-item-item-shadow.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | use foo::foo;
| ^^^^^^^^ `foo` reimported here
|
= note: `foo` 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
help: you can use `as` to change the binding name of the import
|
LL | use foo::foo as other_foo;
| ^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/double-import.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | use sub2::foo; //~ ERROR the name `foo` is defined multiple times
| ^^^^^^^^^ `foo` reimported here
|
= note: `foo` must be defined only once in the value namespace of this module
help: You can use `as` to change the binding name of the import
help: you can use `as` to change the binding name of the import
|
LL | use sub2::foo as other_foo; //~ ERROR the name `foo` is defined multiple times
| ^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/double-type-import.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | use self::bar::X;
| ^^^^^^^^^^^^ `X` reimported here
|
= note: `X` 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
help: you can use `as` to change the binding name of the import
|
LL | use self::bar::X as OtherX;
| ^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/duplicate/duplicate-check-macro-exports.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | macro_rules! panic { () => {} } //~ ERROR the name `panic` is defined multi
| ^^^^^^^^^^^^^^^^^^ `panic` redefined here
|
= note: `panic` must be defined only once in the macro namespace of this module
help: You can use `as` to change the binding name of the import
help: you can use `as` to change the binding name of the import
|
LL | pub use std::panic as other_panic;
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0252.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | use bar::baz; //~ ERROR E0252
| ^^^^^^^^ `baz` reimported here
|
= note: `baz` 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
help: you can use `as` to change the binding name of the import
|
LL | use bar::baz as other_baz; //~ ERROR E0252
| ^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0254.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | use foo::alloc;
| ^^^^^^^^^^ `alloc` reimported here
|
= note: `alloc` 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
help: you can use `as` to change the binding name of the import
|
LL | use foo::alloc as other_alloc;
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0255.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | fn foo() {} //~ ERROR E0255
| ^^^^^^^^ `foo` redefined here
|
= note: `foo` must be defined only once in the value namespace of this module
help: You can use `as` to change the binding name of the import
help: you can use `as` to change the binding name of the import
|
LL | use bar::foo as other_foo;
| ^^^^^^^^^^^^^^^^^^^^^
Expand Down
9 changes: 5 additions & 4 deletions src/test/ui/error-codes/E0259.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ LL | extern crate alloc;
| ------------------- previous import of the extern crate `alloc` here
LL |
LL | extern crate libc as alloc;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| `alloc` reimported here
| You can use `as` to change the binding name of the import
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `alloc` reimported here
|
= note: `alloc` 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 | extern crate libc as other_alloc;
|

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0260.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | mod alloc {
| ^^^^^^^^^ `alloc` redefined here
|
= note: `alloc` 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
help: you can use `as` to change the binding name of the import
|
LL | extern crate alloc as other_alloc;
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0430.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ LL | use std::fmt::{self, self}; //~ ERROR E0430
| previous import of the module `fmt` here
|
= note: `fmt` 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
help: you can use `as` to change the binding name of the import
|
LL | use std::fmt::{self, self as other_fmt}; //~ ERROR E0430
| ^^^^^^^^^^^^^^^^^
Expand Down
9 changes: 5 additions & 4 deletions src/test/ui/extern/extern-crate-rename.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ error[E0259]: the name `m1` is defined multiple times
LL | extern crate m1;
| ---------------- previous import of the extern crate `m1` here
LL | extern crate m2 as m1; //~ ERROR is defined multiple times
| ^^^^^^^^^^^^^^^^^^^^^^
| |
| `m1` reimported here
| You can use `as` to change the binding name of the import
| ^^^^^^^^^^^^^^^^^^^^^^ `m1` reimported here
|
= note: `m1` 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 | extern crate m2 as other_m1; //~ ERROR is defined multiple times
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/imports/duplicate.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | use a::foo; //~ ERROR the name `foo` is defined multiple times
| ^^^^^^ `foo` reimported here
|
= note: `foo` must be defined only once in the value namespace of this module
help: You can use `as` to change the binding name of the import
help: you can use `as` to change the binding name of the import
|
LL | use a::foo as other_foo; //~ ERROR the name `foo` is defined multiple times
| ^^^^^^^^^^^^^^^^^^^
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/issues/issue-19498.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | mod A {} //~ ERROR the name `A` is defined multiple times
| ^^^^^ `A` redefined 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
help: you can use `as` to change the binding name of the import
|
LL | use self::A as OtherA;
| ^^^^^^^^^^^^^^^^^
Expand All @@ -23,7 +23,7 @@ LL | pub mod B {} //~ ERROR the name `B` is defined multiple times
| ^^^^^^^^^ `B` redefined here
|
= note: `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
help: you can use `as` to change the binding name of the import
|
LL | use self::B as OtherB;
| ^^^^^^^^^^^^^^^^^
Expand All @@ -37,7 +37,7 @@ LL | mod D {} //~ ERROR the name `D` is defined multiple times
| ^^^^^ `D` redefined here
|
= note: `D` 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
help: you can use `as` to change the binding name of the import
|
LL | use C::D as OtherD;
| ^^^^^^^^^^^^^^
Expand Down
10 changes: 5 additions & 5 deletions src/test/ui/issues/issue-24081.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | type Add = bool; //~ ERROR the name `Add` is defined multiple times
| ^^^^^^^^^^^^^^^^ `Add` redefined here
|
= note: `Add` 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
help: you can use `as` to change the binding name of the import
|
LL | use std::ops::Add as OtherAdd;
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -23,7 +23,7 @@ LL | struct Sub { x: f32 } //~ ERROR the name `Sub` is defined multiple times
| ^^^^^^^^^^ `Sub` redefined here
|
= note: `Sub` 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
help: you can use `as` to change the binding name of the import
|
LL | use std::ops::Sub as OtherSub;
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -38,7 +38,7 @@ LL | enum Mul { A, B } //~ ERROR the name `Mul` is defined multiple times
| ^^^^^^^^ `Mul` redefined here
|
= note: `Mul` 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
help: you can use `as` to change the binding name of the import
|
LL | use std::ops::Mul as OtherMul;
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -53,7 +53,7 @@ LL | mod Div { } //~ ERROR the name `Div` is defined multiple times
| ^^^^^^^ `Div` redefined here
|
= note: `Div` 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
help: you can use `as` to change the binding name of the import
|
LL | use std::ops::Div as OtherDiv;
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -68,7 +68,7 @@ LL | trait Rem { } //~ ERROR the name `Rem` is defined multiple times
| ^^^^^^^^^ `Rem` redefined here
|
= note: `Rem` 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
help: you can use `as` to change the binding name of the import
|
LL | use std::ops::Rem as OtherRem;
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/issues/issue-25396.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | use bar::baz; //~ ERROR the name `baz` is defined multiple times
| ^^^^^^^^ `baz` reimported here
|
= note: `baz` 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
help: you can use `as` to change the binding name of the import
|
LL | use bar::baz as other_baz; //~ ERROR the name `baz` is defined multiple times
| ^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -21,7 +21,7 @@ LL | use bar::Quux; //~ ERROR the name `Quux` is defined multiple times
| ^^^^^^^^^ `Quux` reimported here
|
= note: `Quux` 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
help: you can use `as` to change the binding name of the import
|
LL | use bar::Quux as OtherQuux; //~ ERROR the name `Quux` is defined multiple times
| ^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -35,7 +35,7 @@ LL | use bar::blah; //~ ERROR the name `blah` is defined multiple times
| ^^^^^^^^^ `blah` reimported here
|
= note: `blah` 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
help: you can use `as` to change the binding name of the import
|
LL | use bar::blah as other_blah; //~ ERROR the name `blah` is defined multiple times
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -49,7 +49,7 @@ LL | use bar::WOMP; //~ ERROR the name `WOMP` is defined multiple times
| ^^^^^^^^^ `WOMP` reimported here
|
= note: `WOMP` must be defined only once in the value namespace of this module
help: You can use `as` to change the binding name of the import
help: you can use `as` to change the binding name of the import
|
LL | use bar::WOMP as OtherWOMP; //~ ERROR the name `WOMP` is defined multiple times
| ^^^^^^^^^^^^^^^^^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-26886.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | use std::sync::Arc; //~ ERROR the name `Arc` is defined multiple times
| ^^^^^^^^^^^^^^ `Arc` reimported here
|
= note: `Arc` 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
help: you can use `as` to change the binding name of the import
|
LL | use std::sync::Arc as OtherArc; //~ ERROR the name `Arc` is defined multiple times
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -22,7 +22,7 @@ LL | use std::sync; //~ ERROR the name `sync` is defined multiple times
| ^^^^^^^^^ `sync` reimported here
|
= note: `sync` 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
help: you can use `as` to change the binding name of the import
|
LL | use std::sync as other_sync; //~ ERROR the name `sync` is defined multiple times
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | use extension2::ConstructorExtension; //~ ERROR is defined multiple times
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `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
help: you can use `as` to change the binding name of the import
|
LL | use extension2::ConstructorExtension as OtherConstructorExtension; //~ ERROR is defined multiple times
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | extern crate 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
help: you can use `as` to change the binding name of the import
|
LL | extern crate std as other_std;
|
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;
18 changes: 18 additions & 0 deletions src/test/ui/issues/issue-45829/issue-45829.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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.

mod foo {
pub struct A;
pub struct B;
}

use foo::{A, B as A};

fn main() {}
17 changes: 17 additions & 0 deletions src/test/ui/issues/issue-45829/issue-45829.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/issue-45829.rs:16:14
|
LL | use foo::{A, B 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 foo::{A, B as OtherA};
| ^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0252`.
Loading