Skip to content

Commit 7cfe5de

Browse files
committed
Auto merge of rust-lang#55113 - mockersf:master, r=estebank
rust-lang#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

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 4 additions & 1 deletion
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(),

src/librustc_resolve/check_unused.rs

Lines changed: 1 addition & 1 deletion
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 => {

src/librustc_resolve/lib.rs

Lines changed: 36 additions & 21 deletions
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);

src/librustc_resolve/resolve_imports.rs

Lines changed: 5 additions & 2 deletions
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
}

src/test/ui/blind/blind-item-block-item-shadow.stderr

Lines changed: 1 addition & 1 deletion
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
| ^^^^^^^^^^^^^^^^^^^^

src/test/ui/blind/blind-item-item-shadow.stderr

Lines changed: 1 addition & 1 deletion
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
| ^^^^^^^^^^^^^^^^^^^^^

src/test/ui/double-import.stderr

Lines changed: 1 addition & 1 deletion
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
| ^^^^^^^^^^^^^^^^^^^^^^

src/test/ui/double-type-import.stderr

Lines changed: 1 addition & 1 deletion
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
| ^^^^^^^^^^^^^^^^^^^^^^

src/test/ui/duplicate/duplicate-check-macro-exports.stderr

Lines changed: 1 addition & 1 deletion
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
| ^^^^^^^^^^^^^^^^^^^^^^^^^

src/test/ui/error-codes/E0252.stderr

Lines changed: 1 addition & 1 deletion
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
| ^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)