diff options
| author | Michael Goulet <michael@errs.io> | 2022-12-27 07:05:45 +0000 |
|---|---|---|
| committer | Michael Goulet <michael@errs.io> | 2022-12-27 07:05:45 +0000 |
| commit | d2404d6dca59d34d5b9f0c66b425b08229f20f4b (patch) | |
| tree | 723f17825489931226a86ac4d14c8b0032d5cd91 | |
| parent | 9e2536b9389f56386d7f722b403d9730911ee811 (diff) | |
Dont clobber `as ..` rename in import suggestion
| -rw-r--r-- | compiler/rustc_resolve/src/diagnostics.rs | 9 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/imports.rs | 6 | ||||
| -rw-r--r-- | src/test/ui/imports/bad-import-with-rename.rs | 16 | ||||
| -rw-r--r-- | src/test/ui/imports/bad-import-with-rename.stderr | 25 | ||||
| -rw-r--r-- | src/test/ui/test-attrs/inaccessible-test-modules.stderr | 4 |
5 files changed, 55 insertions, 5 deletions
diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 2e28dad52c7..c8b96aae7a6 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -161,6 +161,7 @@ impl<'a> Resolver<'a> { found_use, DiagnosticMode::Normal, path, + None, ); err.emit(); } else if let Some((span, msg, sugg, appl)) = suggestion { @@ -690,6 +691,7 @@ impl<'a> Resolver<'a> { FoundUse::Yes, DiagnosticMode::Pattern, vec![], + None, ); } err @@ -1344,6 +1346,7 @@ impl<'a> Resolver<'a> { FoundUse::Yes, DiagnosticMode::Normal, vec![], + None, ); 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>, candidates: &[ImportSuggestion], mode: DiagnosticMode, + append: Option<&str>, ) { show_candidates( session, @@ -2336,6 +2340,7 @@ pub(crate) fn import_candidates( FoundUse::Yes, mode, vec![], + append, ); } @@ -2353,10 +2358,12 @@ fn show_candidates( found_use: FoundUse, mode: DiagnosticMode, path: Vec<Segment>, + append: Option<&str>, ) { if candidates.is_empty() { return; } + let append = append.unwrap_or(""); let mut accessible_path_strings: Vec<(String, &str, Option<DefId>, &Option<String>)> = Vec::new(); @@ -2417,7 +2424,7 @@ fn show_candidates( // produce an additional newline to separate the new use statement // from the directly following item. 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( diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index b3593fc9e47..d99c1cb6d3c 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -547,15 +547,16 @@ impl<'a, 'b> ImportResolver<'a, 'b> { if let Some(candidates) = &err.candidates { match &import.kind { - ImportKind::Single { nested: false, .. } => import_candidates( + ImportKind::Single { nested: false, source, target, .. } => import_candidates( self.r.session, &self.r.untracked.source_span, &mut diag, Some(err.span), &candidates, DiagnosticMode::Import, + (source != target).then(|| format!(" as {target}")).as_deref(), ), - ImportKind::Single { nested: true, .. } => { + ImportKind::Single { nested: true, source, target, .. } => { import_candidates( self.r.session, &self.r.untracked.source_span, @@ -563,6 +564,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { None, &candidates, DiagnosticMode::Normal, + (source != target).then(|| format!(" as {target}")).as_deref(), ); } _ => {} diff --git a/src/test/ui/imports/bad-import-with-rename.rs b/src/test/ui/imports/bad-import-with-rename.rs new file mode 100644 index 00000000000..ffe56916f92 --- /dev/null +++ b/src/test/ui/imports/bad-import-with-rename.rs @@ -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() {} diff --git a/src/test/ui/imports/bad-import-with-rename.stderr b/src/test/ui/imports/bad-import-with-rename.stderr new file mode 100644 index 00000000000..cace2a7a51c --- /dev/null +++ b/src/test/ui/imports/bad-import-with-rename.stderr @@ -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`. diff --git a/src/test/ui/test-attrs/inaccessible-test-modules.stderr b/src/test/ui/test-attrs/inaccessible-test-modules.stderr index 0c16ecd4c86..17ea648aad6 100644 --- a/src/test/ui/test-attrs/inaccessible-test-modules.stderr +++ b/src/test/ui/test-attrs/inaccessible-test-modules.stderr @@ -19,8 +19,8 @@ LL | use test as y; | ~~~~ help: consider importing this module instead | -LL | use test::test; - | ~~~~~~~~~~~ +LL | use test::test as y; + | ~~~~~~~~~~~~~~~~ error: aborting due to 2 previous errors |
