Skip to content

Commit 9e2536b

Browse files
Note alternative import candidates in nested use tree
1 parent 564435f commit 9e2536b

File tree

4 files changed

+61
-15
lines changed

4 files changed

+61
-15
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2309,7 +2309,7 @@ enum FoundUse {
23092309
}
23102310

23112311
/// Whether a binding is part of a pattern or a use statement. Used for diagnostics.
2312-
enum DiagnosticMode {
2312+
pub(crate) enum DiagnosticMode {
23132313
Normal,
23142314
/// The binding is part of a pattern
23152315
Pattern,
@@ -2324,6 +2324,7 @@ pub(crate) fn import_candidates(
23242324
// This is `None` if all placement locations are inside expansions
23252325
use_placement_span: Option<Span>,
23262326
candidates: &[ImportSuggestion],
2327+
mode: DiagnosticMode,
23272328
) {
23282329
show_candidates(
23292330
session,
@@ -2333,7 +2334,7 @@ pub(crate) fn import_candidates(
23332334
candidates,
23342335
Instead::Yes,
23352336
FoundUse::Yes,
2336-
DiagnosticMode::Import,
2337+
mode,
23372338
vec![],
23382339
);
23392340
}

compiler/rustc_resolve/src/imports.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! A bunch of methods and structures more or less related to resolving imports.
22
3-
use crate::diagnostics::{import_candidates, Suggestion};
3+
use crate::diagnostics::{import_candidates, DiagnosticMode, Suggestion};
44
use crate::Determinacy::{self, *};
55
use crate::Namespace::*;
66
use crate::{module_to_string, names_to_string, ImportSuggestion};
@@ -402,7 +402,7 @@ struct UnresolvedImportError {
402402
label: Option<String>,
403403
note: Option<String>,
404404
suggestion: Option<Suggestion>,
405-
candidate: Option<Vec<ImportSuggestion>>,
405+
candidates: Option<Vec<ImportSuggestion>>,
406406
}
407407

408408
pub struct ImportResolver<'a, 'b> {
@@ -489,7 +489,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
489489
label: None,
490490
note: None,
491491
suggestion: None,
492-
candidate: None,
492+
candidates: None,
493493
};
494494
// FIXME: there should be a better way of doing this than
495495
// formatting this as a string then checking for `::`
@@ -545,15 +545,26 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
545545
diag.multipart_suggestion(&msg, suggestions, applicability);
546546
}
547547

548-
if let Some(candidate) = &err.candidate {
548+
if let Some(candidates) = &err.candidates {
549549
match &import.kind {
550550
ImportKind::Single { nested: false, .. } => import_candidates(
551551
self.r.session,
552552
&self.r.untracked.source_span,
553553
&mut diag,
554554
Some(err.span),
555-
&candidate,
555+
&candidates,
556+
DiagnosticMode::Import,
556557
),
558+
ImportKind::Single { nested: true, .. } => {
559+
import_candidates(
560+
self.r.session,
561+
&self.r.untracked.source_span,
562+
&mut diag,
563+
None,
564+
&candidates,
565+
DiagnosticMode::Normal,
566+
);
567+
}
557568
_ => {}
558569
}
559570
}
@@ -717,14 +728,14 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
717728
String::from("a similar path exists"),
718729
Applicability::MaybeIncorrect,
719730
)),
720-
candidate: None,
731+
candidates: None,
721732
},
722733
None => UnresolvedImportError {
723734
span,
724735
label: Some(label),
725736
note: None,
726737
suggestion,
727-
candidate: None,
738+
candidates: None,
728739
},
729740
};
730741
return Some(err);
@@ -771,7 +782,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
771782
)),
772783
note: None,
773784
suggestion: None,
774-
candidate: None,
785+
candidates: None,
775786
});
776787
}
777788
}
@@ -953,7 +964,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
953964
label: Some(label),
954965
note,
955966
suggestion,
956-
candidate: if !parent_suggestion.is_empty() {
967+
candidates: if !parent_suggestion.is_empty() {
957968
Some(parent_suggestion)
958969
} else {
959970
None
+15-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1+
// edition: 2021
2+
13
#![allow(unused)]
24

35
mod A {
46
pub(crate) type AA = ();
7+
pub(crate) type BB = ();
8+
9+
mod A2 {
10+
use super::{super::C::D::AA, AA as _};
11+
//~^ ERROR unresolved import
12+
}
513
}
614

7-
mod C {}
15+
mod C {
16+
pub mod D {}
17+
}
818

919
mod B {
1020
use crate::C::{self, AA};
11-
//~^ ERROR unresolved import `crate::C::AA`
21+
//~^ ERROR unresolved import
22+
23+
use crate::{A, C::BB};
24+
//~^ ERROR unresolved import
1225
}
1326

1427
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
1+
error[E0432]: unresolved import `super::super::C::D::AA`
2+
--> $DIR/bad-import-in-nested.rs:10:21
3+
|
4+
LL | use super::{super::C::D::AA, AA as _};
5+
| ^^^^^^^^^^^^^^^ no `AA` in `C::D`
6+
|
7+
= note: consider importing this type alias instead:
8+
crate::A::AA
9+
110
error[E0432]: unresolved import `crate::C::AA`
2-
--> $DIR/bad-import-in-nested.rs:10:26
11+
--> $DIR/bad-import-in-nested.rs:20:26
312
|
413
LL | use crate::C::{self, AA};
514
| ^^ no `AA` in `C`
15+
|
16+
= note: consider importing this type alias instead:
17+
crate::A::AA
18+
19+
error[E0432]: unresolved import `crate::C::BB`
20+
--> $DIR/bad-import-in-nested.rs:23:20
21+
|
22+
LL | use crate::{A, C::BB};
23+
| ^^^^^ no `BB` in `C`
24+
|
25+
= note: consider importing this type alias instead:
26+
crate::A::BB
627

7-
error: aborting due to previous error
28+
error: aborting due to 3 previous errors
829

930
For more information about this error, try `rustc --explain E0432`.

0 commit comments

Comments
 (0)