Skip to content

Commit d2404d6

Browse files
Dont clobber as .. rename in import suggestion
1 parent 9e2536b commit d2404d6

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ impl<'a> Resolver<'a> {
161161
found_use,
162162
DiagnosticMode::Normal,
163163
path,
164+
None,
164165
);
165166
err.emit();
166167
} else if let Some((span, msg, sugg, appl)) = suggestion {
@@ -690,6 +691,7 @@ impl<'a> Resolver<'a> {
690691
FoundUse::Yes,
691692
DiagnosticMode::Pattern,
692693
vec![],
694+
None,
693695
);
694696
}
695697
err
@@ -1344,6 +1346,7 @@ impl<'a> Resolver<'a> {
13441346
FoundUse::Yes,
13451347
DiagnosticMode::Normal,
13461348
vec![],
1349+
None,
13471350
);
13481351

13491352
if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) {
@@ -2325,6 +2328,7 @@ pub(crate) fn import_candidates(
23252328
use_placement_span: Option<Span>,
23262329
candidates: &[ImportSuggestion],
23272330
mode: DiagnosticMode,
2331+
append: Option<&str>,
23282332
) {
23292333
show_candidates(
23302334
session,
@@ -2336,6 +2340,7 @@ pub(crate) fn import_candidates(
23362340
FoundUse::Yes,
23372341
mode,
23382342
vec![],
2343+
append,
23392344
);
23402345
}
23412346

@@ -2353,10 +2358,12 @@ fn show_candidates(
23532358
found_use: FoundUse,
23542359
mode: DiagnosticMode,
23552360
path: Vec<Segment>,
2361+
append: Option<&str>,
23562362
) {
23572363
if candidates.is_empty() {
23582364
return;
23592365
}
2366+
let append = append.unwrap_or("");
23602367

23612368
let mut accessible_path_strings: Vec<(String, &str, Option<DefId>, &Option<String>)> =
23622369
Vec::new();
@@ -2417,7 +2424,7 @@ fn show_candidates(
24172424
// produce an additional newline to separate the new use statement
24182425
// from the directly following item.
24192426
let additional_newline = if let FoundUse::Yes = found_use { "" } else { "\n" };
2420-
candidate.0 = format!("{}{};\n{}", add_use, &candidate.0, additional_newline);
2427+
candidate.0 = format!("{add_use}{}{append};\n{additional_newline}", &candidate.0);
24212428
}
24222429

24232430
err.span_suggestions(

compiler/rustc_resolve/src/imports.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -547,22 +547,24 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
547547

548548
if let Some(candidates) = &err.candidates {
549549
match &import.kind {
550-
ImportKind::Single { nested: false, .. } => import_candidates(
550+
ImportKind::Single { nested: false, source, target, .. } => import_candidates(
551551
self.r.session,
552552
&self.r.untracked.source_span,
553553
&mut diag,
554554
Some(err.span),
555555
&candidates,
556556
DiagnosticMode::Import,
557+
(source != target).then(|| format!(" as {target}")).as_deref(),
557558
),
558-
ImportKind::Single { nested: true, .. } => {
559+
ImportKind::Single { nested: true, source, target, .. } => {
559560
import_candidates(
560561
self.r.session,
561562
&self.r.untracked.source_span,
562563
&mut diag,
563564
None,
564565
&candidates,
565566
DiagnosticMode::Normal,
567+
(source != target).then(|| format!(" as {target}")).as_deref(),
566568
);
567569
}
568570
_ => {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
mod A {
2+
pub type B = ();
3+
pub type B2 = ();
4+
}
5+
6+
mod C {
7+
use crate::D::B as _;
8+
//~^ ERROR unresolved import `crate::D::B`
9+
10+
use crate::D::B2;
11+
//~^ ERROR unresolved import `crate::D::B2`
12+
}
13+
14+
mod D {}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0432]: unresolved import `crate::D::B`
2+
--> $DIR/bad-import-with-rename.rs:7:9
3+
|
4+
LL | use crate::D::B as _;
5+
| ^^^^^^^^^^^^^^^^ no `B` in `D`
6+
|
7+
help: consider importing this type alias instead
8+
|
9+
LL | use A::B as _;
10+
| ~~~~~~~~~~
11+
12+
error[E0432]: unresolved import `crate::D::B2`
13+
--> $DIR/bad-import-with-rename.rs:10:9
14+
|
15+
LL | use crate::D::B2;
16+
| ^^^^^^^^^^^^ no `B2` in `D`
17+
|
18+
help: consider importing this type alias instead
19+
|
20+
LL | use A::B2;
21+
| ~~~~~~
22+
23+
error: aborting due to 2 previous errors
24+
25+
For more information about this error, try `rustc --explain E0432`.

src/test/ui/test-attrs/inaccessible-test-modules.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ LL | use test as y;
1919
| ~~~~
2020
help: consider importing this module instead
2121
|
22-
LL | use test::test;
23-
| ~~~~~~~~~~~
22+
LL | use test::test as y;
23+
| ~~~~~~~~~~~~~~~~
2424

2525
error: aborting due to 2 previous errors
2626

0 commit comments

Comments
 (0)