Skip to content

Commit 7cfe5de

Browse files
committed
Auto merge of #55113 - mockersf:master, r=estebank
#45829 when a renamed import conflict with a previous import Fix the suggestion when a renamed import conflict. It check if the snipped contains `" as "`, and if so uses everything before for the suggestion.
2 parents d570b36 + 8fe6688 commit 7cfe5de

File tree

57 files changed

+485
-82
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+485
-82
lines changed

Diff for: src/librustc_resolve/build_reduced_graph.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
449449
id: item.id,
450450
parent,
451451
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
452-
subclass: ImportDirectiveSubclass::ExternCrate(orig_name),
452+
subclass: ImportDirectiveSubclass::ExternCrate {
453+
source: orig_name,
454+
target: ident,
455+
},
453456
root_span: item.span,
454457
span: item.span,
455458
module_path: Vec::new(),

Diff for: src/librustc_resolve/check_unused.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ pub fn check_crate(resolver: &mut Resolver, krate: &ast::Crate) {
144144
}
145145
}
146146
}
147-
ImportDirectiveSubclass::ExternCrate(_) => {
147+
ImportDirectiveSubclass::ExternCrate { .. } => {
148148
resolver.maybe_unused_extern_crates.push((directive.id, directive.span));
149149
}
150150
ImportDirectiveSubclass::MacroUse => {

Diff for: src/librustc_resolve/lib.rs

+36-21
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ impl<'a> NameBinding<'a> {
12341234
match self.kind {
12351235
NameBindingKind::Import {
12361236
directive: &ImportDirective {
1237-
subclass: ImportDirectiveSubclass::ExternCrate(_), ..
1237+
subclass: ImportDirectiveSubclass::ExternCrate { .. }, ..
12381238
}, ..
12391239
} => true,
12401240
_ => false,
@@ -1248,15 +1248,6 @@ impl<'a> NameBinding<'a> {
12481248
}
12491249
}
12501250

1251-
fn is_renamed_extern_crate(&self) -> bool {
1252-
if let NameBindingKind::Import { directive, ..} = self.kind {
1253-
if let ImportDirectiveSubclass::ExternCrate(Some(_)) = directive.subclass {
1254-
return true;
1255-
}
1256-
}
1257-
false
1258-
}
1259-
12601251
fn is_glob_import(&self) -> bool {
12611252
match self.kind {
12621253
NameBindingKind::Import { directive, .. } => directive.is_glob(),
@@ -3812,7 +3803,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
38123803
if let NameBindingKind::Import { directive: d, .. } = binding.kind {
38133804
// Careful: we still want to rewrite paths from
38143805
// renamed extern crates.
3815-
if let ImportDirectiveSubclass::ExternCrate(None) = d.subclass {
3806+
if let ImportDirectiveSubclass::ExternCrate { source: None, .. } = d.subclass {
38163807
return
38173808
}
38183809
}
@@ -4782,10 +4773,17 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
47824773
};
47834774

47844775
let cm = self.session.source_map();
4785-
let rename_msg = "You can use `as` to change the binding name of the import";
4786-
4787-
if let (Ok(snippet), false) = (cm.span_to_snippet(binding.span),
4788-
binding.is_renamed_extern_crate()) {
4776+
let rename_msg = "you can use `as` to change the binding name of the import";
4777+
4778+
if let (
4779+
Ok(snippet),
4780+
NameBindingKind::Import { directive, ..},
4781+
_dummy @ false,
4782+
) = (
4783+
cm.span_to_snippet(binding.span),
4784+
binding.kind.clone(),
4785+
binding.span.is_dummy(),
4786+
) {
47894787
let suggested_name = if name.as_str().chars().next().unwrap().is_uppercase() {
47904788
format!("Other{}", name)
47914789
} else {
@@ -4794,13 +4792,30 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
47944792

47954793
err.span_suggestion_with_applicability(
47964794
binding.span,
4797-
rename_msg,
4798-
if snippet.ends_with(';') {
4799-
format!("{} as {};", &snippet[..snippet.len() - 1], suggested_name)
4800-
} else {
4801-
format!("{} as {}", snippet, suggested_name)
4795+
&rename_msg,
4796+
match (&directive.subclass, snippet.as_ref()) {
4797+
(ImportDirectiveSubclass::SingleImport { .. }, "self") =>
4798+
format!("self as {}", suggested_name),
4799+
(ImportDirectiveSubclass::SingleImport { source, .. }, _) =>
4800+
format!(
4801+
"{} as {}{}",
4802+
&snippet[..((source.span.hi().0 - binding.span.lo().0) as usize)],
4803+
suggested_name,
4804+
if snippet.ends_with(";") {
4805+
";"
4806+
} else {
4807+
""
4808+
}
4809+
),
4810+
(ImportDirectiveSubclass::ExternCrate { source, target, .. }, _) =>
4811+
format!(
4812+
"extern crate {} as {};",
4813+
source.unwrap_or(target.name),
4814+
suggested_name,
4815+
),
4816+
(_, _) => unreachable!(),
48024817
},
4803-
Applicability::MachineApplicable,
4818+
Applicability::MaybeIncorrect,
48044819
);
48054820
} else {
48064821
err.span_label(binding.span, rename_msg);

Diff for: src/librustc_resolve/resolve_imports.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ pub enum ImportDirectiveSubclass<'a> {
5252
max_vis: Cell<ty::Visibility>, // The visibility of the greatest re-export.
5353
// n.b. `max_vis` is only used in `finalize_import` to check for re-export errors.
5454
},
55-
ExternCrate(Option<Name>),
55+
ExternCrate {
56+
source: Option<Name>,
57+
target: Ident,
58+
},
5659
MacroUse,
5760
}
5861

@@ -1336,7 +1339,7 @@ fn import_directive_subclass_to_string(subclass: &ImportDirectiveSubclass) -> St
13361339
match *subclass {
13371340
SingleImport { source, .. } => source.to_string(),
13381341
GlobImport { .. } => "*".to_string(),
1339-
ExternCrate(_) => "<extern crate>".to_string(),
1342+
ExternCrate { .. } => "<extern crate>".to_string(),
13401343
MacroUse => "#[macro_use]".to_string(),
13411344
}
13421345
}

Diff for: src/test/ui/blind/blind-item-block-item-shadow.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | use foo::Bar;
77
| ^^^^^^^^ `Bar` reimported here
88
|
99
= note: `Bar` must be defined only once in the type namespace of this block
10-
help: You can use `as` to change the binding name of the import
10+
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use foo::Bar as OtherBar;
1313
| ^^^^^^^^^^^^^^^^^^^^

Diff for: src/test/ui/blind/blind-item-item-shadow.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | use foo::foo;
88
| ^^^^^^^^ `foo` reimported here
99
|
1010
= note: `foo` must be defined only once in the type namespace of this module
11-
help: You can use `as` to change the binding name of the import
11+
help: you can use `as` to change the binding name of the import
1212
|
1313
LL | use foo::foo as other_foo;
1414
| ^^^^^^^^^^^^^^^^^^^^^

Diff for: src/test/ui/double-import.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | use sub2::foo; //~ ERROR the name `foo` is defined multiple times
77
| ^^^^^^^^^ `foo` reimported here
88
|
99
= note: `foo` must be defined only once in the value namespace of this module
10-
help: You can use `as` to change the binding name of the import
10+
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use sub2::foo as other_foo; //~ ERROR the name `foo` is defined multiple times
1313
| ^^^^^^^^^^^^^^^^^^^^^^

Diff for: src/test/ui/double-type-import.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | use self::bar::X;
77
| ^^^^^^^^^^^^ `X` reimported here
88
|
99
= note: `X` 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
10+
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use self::bar::X as OtherX;
1313
| ^^^^^^^^^^^^^^^^^^^^^^

Diff for: src/test/ui/duplicate/duplicate-check-macro-exports.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | macro_rules! panic { () => {} } //~ ERROR the name `panic` is defined multi
88
| ^^^^^^^^^^^^^^^^^^ `panic` redefined here
99
|
1010
= note: `panic` must be defined only once in the macro namespace of this module
11-
help: You can use `as` to change the binding name of the import
11+
help: you can use `as` to change the binding name of the import
1212
|
1313
LL | pub use std::panic as other_panic;
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^

Diff for: src/test/ui/error-codes/E0252.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | use bar::baz; //~ ERROR E0252
77
| ^^^^^^^^ `baz` reimported here
88
|
99
= note: `baz` 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
10+
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use bar::baz as other_baz; //~ ERROR E0252
1313
| ^^^^^^^^^^^^^^^^^^^^^

Diff for: src/test/ui/error-codes/E0254.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | use foo::alloc;
88
| ^^^^^^^^^^ `alloc` reimported here
99
|
1010
= note: `alloc` must be defined only once in the type namespace of this module
11-
help: You can use `as` to change the binding name of the import
11+
help: you can use `as` to change the binding name of the import
1212
|
1313
LL | use foo::alloc as other_alloc;
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^

Diff for: src/test/ui/error-codes/E0255.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | fn foo() {} //~ ERROR E0255
88
| ^^^^^^^^ `foo` redefined here
99
|
1010
= note: `foo` must be defined only once in the value namespace of this module
11-
help: You can use `as` to change the binding name of the import
11+
help: you can use `as` to change the binding name of the import
1212
|
1313
LL | use bar::foo as other_foo;
1414
| ^^^^^^^^^^^^^^^^^^^^^

Diff for: src/test/ui/error-codes/E0259.stderr

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ LL | extern crate alloc;
55
| ------------------- previous import of the extern crate `alloc` here
66
LL |
77
LL | extern crate libc as alloc;
8-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
9-
| |
10-
| `alloc` reimported here
11-
| You can use `as` to change the binding name of the import
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `alloc` reimported here
129
|
1310
= note: `alloc` must be defined only once in the type namespace of this module
11+
help: you can use `as` to change the binding name of the import
12+
|
13+
LL | extern crate libc as other_alloc;
14+
|
1415

1516
error: aborting due to previous error
1617

Diff for: src/test/ui/error-codes/E0260.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | mod alloc {
88
| ^^^^^^^^^ `alloc` redefined here
99
|
1010
= note: `alloc` must be defined only once in the type namespace of this module
11-
help: You can use `as` to change the binding name of the import
11+
help: you can use `as` to change the binding name of the import
1212
|
1313
LL | extern crate alloc as other_alloc;
1414
|

Diff for: src/test/ui/error-codes/E0430.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ LL | use std::fmt::{self, self}; //~ ERROR E0430
1515
| previous import of the module `fmt` here
1616
|
1717
= note: `fmt` must be defined only once in the type namespace of this module
18-
help: You can use `as` to change the binding name of the import
18+
help: you can use `as` to change the binding name of the import
1919
|
2020
LL | use std::fmt::{self, self as other_fmt}; //~ ERROR E0430
2121
| ^^^^^^^^^^^^^^^^^

Diff for: src/test/ui/extern/extern-crate-rename.stderr

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ error[E0259]: the name `m1` is defined multiple times
44
LL | extern crate m1;
55
| ---------------- previous import of the extern crate `m1` here
66
LL | extern crate m2 as m1; //~ ERROR is defined multiple times
7-
| ^^^^^^^^^^^^^^^^^^^^^^
8-
| |
9-
| `m1` reimported here
10-
| You can use `as` to change the binding name of the import
7+
| ^^^^^^^^^^^^^^^^^^^^^^ `m1` reimported here
118
|
129
= note: `m1` 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 | extern crate m2 as other_m1; //~ ERROR is defined multiple times
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1314

1415
error: aborting due to previous error
1516

Diff for: src/test/ui/imports/duplicate.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | use a::foo; //~ ERROR the name `foo` is defined multiple times
77
| ^^^^^^ `foo` reimported here
88
|
99
= note: `foo` must be defined only once in the value namespace of this module
10-
help: You can use `as` to change the binding name of the import
10+
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use a::foo as other_foo; //~ ERROR the name `foo` is defined multiple times
1313
| ^^^^^^^^^^^^^^^^^^^

Diff for: src/test/ui/issues/issue-19498.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | mod A {} //~ ERROR the name `A` is defined multiple times
88
| ^^^^^ `A` redefined here
99
|
1010
= note: `A` must be defined only once in the type namespace of this module
11-
help: You can use `as` to change the binding name of the import
11+
help: you can use `as` to change the binding name of the import
1212
|
1313
LL | use self::A as OtherA;
1414
| ^^^^^^^^^^^^^^^^^
@@ -23,7 +23,7 @@ LL | pub mod B {} //~ ERROR the name `B` is defined multiple times
2323
| ^^^^^^^^^ `B` redefined here
2424
|
2525
= note: `B` must be defined only once in the type namespace of this module
26-
help: You can use `as` to change the binding name of the import
26+
help: you can use `as` to change the binding name of the import
2727
|
2828
LL | use self::B as OtherB;
2929
| ^^^^^^^^^^^^^^^^^
@@ -37,7 +37,7 @@ LL | mod D {} //~ ERROR the name `D` is defined multiple times
3737
| ^^^^^ `D` redefined here
3838
|
3939
= note: `D` must be defined only once in the type namespace of this module
40-
help: You can use `as` to change the binding name of the import
40+
help: you can use `as` to change the binding name of the import
4141
|
4242
LL | use C::D as OtherD;
4343
| ^^^^^^^^^^^^^^

Diff for: src/test/ui/issues/issue-24081.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | type Add = bool; //~ ERROR the name `Add` is defined multiple times
88
| ^^^^^^^^^^^^^^^^ `Add` redefined here
99
|
1010
= note: `Add` must be defined only once in the type namespace of this module
11-
help: You can use `as` to change the binding name of the import
11+
help: you can use `as` to change the binding name of the import
1212
|
1313
LL | use std::ops::Add as OtherAdd;
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -23,7 +23,7 @@ LL | struct Sub { x: f32 } //~ ERROR the name `Sub` is defined multiple times
2323
| ^^^^^^^^^^ `Sub` redefined here
2424
|
2525
= note: `Sub` must be defined only once in the type namespace of this module
26-
help: You can use `as` to change the binding name of the import
26+
help: you can use `as` to change the binding name of the import
2727
|
2828
LL | use std::ops::Sub as OtherSub;
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -38,7 +38,7 @@ LL | enum Mul { A, B } //~ ERROR the name `Mul` is defined multiple times
3838
| ^^^^^^^^ `Mul` redefined here
3939
|
4040
= note: `Mul` must be defined only once in the type namespace of this module
41-
help: You can use `as` to change the binding name of the import
41+
help: you can use `as` to change the binding name of the import
4242
|
4343
LL | use std::ops::Mul as OtherMul;
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -53,7 +53,7 @@ LL | mod Div { } //~ ERROR the name `Div` is defined multiple times
5353
| ^^^^^^^ `Div` redefined here
5454
|
5555
= note: `Div` must be defined only once in the type namespace of this module
56-
help: You can use `as` to change the binding name of the import
56+
help: you can use `as` to change the binding name of the import
5757
|
5858
LL | use std::ops::Div as OtherDiv;
5959
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -68,7 +68,7 @@ LL | trait Rem { } //~ ERROR the name `Rem` is defined multiple times
6868
| ^^^^^^^^^ `Rem` redefined here
6969
|
7070
= note: `Rem` must be defined only once in the type namespace of this module
71-
help: You can use `as` to change the binding name of the import
71+
help: you can use `as` to change the binding name of the import
7272
|
7373
LL | use std::ops::Rem as OtherRem;
7474
| ^^^^^^^^^^^^^^^^^^^^^^^^^

Diff for: src/test/ui/issues/issue-25396.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | use bar::baz; //~ ERROR the name `baz` is defined multiple times
77
| ^^^^^^^^ `baz` reimported here
88
|
99
= note: `baz` 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
10+
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use bar::baz as other_baz; //~ ERROR the name `baz` is defined multiple times
1313
| ^^^^^^^^^^^^^^^^^^^^^
@@ -21,7 +21,7 @@ LL | use bar::Quux; //~ ERROR the name `Quux` is defined multiple times
2121
| ^^^^^^^^^ `Quux` reimported here
2222
|
2323
= note: `Quux` must be defined only once in the type namespace of this module
24-
help: You can use `as` to change the binding name of the import
24+
help: you can use `as` to change the binding name of the import
2525
|
2626
LL | use bar::Quux as OtherQuux; //~ ERROR the name `Quux` is defined multiple times
2727
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -35,7 +35,7 @@ LL | use bar::blah; //~ ERROR the name `blah` is defined multiple times
3535
| ^^^^^^^^^ `blah` reimported here
3636
|
3737
= note: `blah` must be defined only once in the type namespace of this module
38-
help: You can use `as` to change the binding name of the import
38+
help: you can use `as` to change the binding name of the import
3939
|
4040
LL | use bar::blah as other_blah; //~ ERROR the name `blah` is defined multiple times
4141
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -49,7 +49,7 @@ LL | use bar::WOMP; //~ ERROR the name `WOMP` is defined multiple times
4949
| ^^^^^^^^^ `WOMP` reimported here
5050
|
5151
= note: `WOMP` must be defined only once in the value namespace of this module
52-
help: You can use `as` to change the binding name of the import
52+
help: you can use `as` to change the binding name of the import
5353
|
5454
LL | use bar::WOMP as OtherWOMP; //~ ERROR the name `WOMP` is defined multiple times
5555
| ^^^^^^^^^^^^^^^^^^^^^^

Diff for: src/test/ui/issues/issue-26886.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | use std::sync::Arc; //~ ERROR the name `Arc` is defined multiple times
77
| ^^^^^^^^^^^^^^ `Arc` reimported here
88
|
99
= note: `Arc` 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
10+
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use std::sync::Arc as OtherArc; //~ ERROR the name `Arc` is defined multiple times
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -22,7 +22,7 @@ LL | use std::sync; //~ ERROR the name `sync` is defined multiple times
2222
| ^^^^^^^^^ `sync` reimported here
2323
|
2424
= note: `sync` must be defined only once in the type namespace of this module
25-
help: You can use `as` to change the binding name of the import
25+
help: you can use `as` to change the binding name of the import
2626
|
2727
LL | use std::sync as other_sync; //~ ERROR the name `sync` is defined multiple times
2828
| ^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)