Dont clobber as .. rename in import suggestion

This commit is contained in:
Michael Goulet 2022-12-27 07:05:45 +00:00
parent 9e2536b938
commit d2404d6dca
5 changed files with 55 additions and 5 deletions

View file

@ -161,6 +161,7 @@ impl<'a> Resolver<'a> {
found_use, found_use,
DiagnosticMode::Normal, DiagnosticMode::Normal,
path, path,
None,
); );
err.emit(); err.emit();
} else if let Some((span, msg, sugg, appl)) = suggestion { } else if let Some((span, msg, sugg, appl)) = suggestion {
@ -690,6 +691,7 @@ impl<'a> Resolver<'a> {
FoundUse::Yes, FoundUse::Yes,
DiagnosticMode::Pattern, DiagnosticMode::Pattern,
vec![], vec![],
None,
); );
} }
err err
@ -1344,6 +1346,7 @@ impl<'a> Resolver<'a> {
FoundUse::Yes, FoundUse::Yes,
DiagnosticMode::Normal, DiagnosticMode::Normal,
vec![], vec![],
None,
); );
if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) { if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) {
@ -2325,6 +2328,7 @@ pub(crate) fn import_candidates(
use_placement_span: Option<Span>, use_placement_span: Option<Span>,
candidates: &[ImportSuggestion], candidates: &[ImportSuggestion],
mode: DiagnosticMode, mode: DiagnosticMode,
append: Option<&str>,
) { ) {
show_candidates( show_candidates(
session, session,
@ -2336,6 +2340,7 @@ pub(crate) fn import_candidates(
FoundUse::Yes, FoundUse::Yes,
mode, mode,
vec![], vec![],
append,
); );
} }
@ -2353,10 +2358,12 @@ fn show_candidates(
found_use: FoundUse, found_use: FoundUse,
mode: DiagnosticMode, mode: DiagnosticMode,
path: Vec<Segment>, path: Vec<Segment>,
append: Option<&str>,
) { ) {
if candidates.is_empty() { if candidates.is_empty() {
return; return;
} }
let append = append.unwrap_or("");
let mut accessible_path_strings: Vec<(String, &str, Option<DefId>, &Option<String>)> = let mut accessible_path_strings: Vec<(String, &str, Option<DefId>, &Option<String>)> =
Vec::new(); Vec::new();
@ -2417,7 +2424,7 @@ fn show_candidates(
// produce an additional newline to separate the new use statement // produce an additional newline to separate the new use statement
// from the directly following item. // from the directly following item.
let additional_newline = if let FoundUse::Yes = found_use { "" } else { "\n" }; let additional_newline = if let FoundUse::Yes = found_use { "" } else { "\n" };
candidate.0 = format!("{}{};\n{}", add_use, &candidate.0, additional_newline); candidate.0 = format!("{add_use}{}{append};\n{additional_newline}", &candidate.0);
} }
err.span_suggestions( err.span_suggestions(

View file

@ -547,15 +547,16 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
if let Some(candidates) = &err.candidates { if let Some(candidates) = &err.candidates {
match &import.kind { match &import.kind {
ImportKind::Single { nested: false, .. } => import_candidates( ImportKind::Single { nested: false, source, target, .. } => import_candidates(
self.r.session, self.r.session,
&self.r.untracked.source_span, &self.r.untracked.source_span,
&mut diag, &mut diag,
Some(err.span), Some(err.span),
&candidates, &candidates,
DiagnosticMode::Import, DiagnosticMode::Import,
(source != target).then(|| format!(" as {target}")).as_deref(),
), ),
ImportKind::Single { nested: true, .. } => { ImportKind::Single { nested: true, source, target, .. } => {
import_candidates( import_candidates(
self.r.session, self.r.session,
&self.r.untracked.source_span, &self.r.untracked.source_span,
@ -563,6 +564,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
None, None,
&candidates, &candidates,
DiagnosticMode::Normal, DiagnosticMode::Normal,
(source != target).then(|| format!(" as {target}")).as_deref(),
); );
} }
_ => {} _ => {}

View file

@ -0,0 +1,16 @@
mod A {
pub type B = ();
pub type B2 = ();
}
mod C {
use crate::D::B as _;
//~^ ERROR unresolved import `crate::D::B`
use crate::D::B2;
//~^ ERROR unresolved import `crate::D::B2`
}
mod D {}
fn main() {}

View file

@ -0,0 +1,25 @@
error[E0432]: unresolved import `crate::D::B`
--> $DIR/bad-import-with-rename.rs:7:9
|
LL | use crate::D::B as _;
| ^^^^^^^^^^^^^^^^ no `B` in `D`
|
help: consider importing this type alias instead
|
LL | use A::B as _;
| ~~~~~~~~~~
error[E0432]: unresolved import `crate::D::B2`
--> $DIR/bad-import-with-rename.rs:10:9
|
LL | use crate::D::B2;
| ^^^^^^^^^^^^ no `B2` in `D`
|
help: consider importing this type alias instead
|
LL | use A::B2;
| ~~~~~~
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0432`.

View file

@ -19,8 +19,8 @@ LL | use test as y;
| ~~~~ | ~~~~
help: consider importing this module instead help: consider importing this module instead
| |
LL | use test::test; LL | use test::test as y;
| ~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~
error: aborting due to 2 previous errors error: aborting due to 2 previous errors