Skip to content

Commit 7f45f53

Browse files
committed
store segment and module in UnresolvedImportError
1 parent b7dcabe commit 7f45f53

File tree

4 files changed

+98
-25
lines changed

4 files changed

+98
-25
lines changed

compiler/rustc_resolve/src/imports.rs

+39-16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_data_structures::fx::FxHashSet;
1919
use rustc_data_structures::intern::Interned;
2020
use rustc_errors::{codes::*, pluralize, struct_span_code_err, Applicability, MultiSpan};
2121
use rustc_hir::def::{self, DefKind, PartialRes};
22+
use rustc_hir::def_id::DefId;
2223
use rustc_middle::metadata::ModChild;
2324
use rustc_middle::metadata::Reexport;
2425
use rustc_middle::span_bug;
@@ -250,6 +251,9 @@ struct UnresolvedImportError {
250251
note: Option<String>,
251252
suggestion: Option<Suggestion>,
252253
candidates: Option<Vec<ImportSuggestion>>,
254+
segment: Option<Symbol>,
255+
/// comes from `PathRes::Failed { module }`
256+
module: Option<DefId>,
253257
}
254258

255259
// Reexports of the form `pub use foo as bar;` where `foo` is `extern crate foo;`
@@ -579,16 +583,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
579583
&import.kind,
580584
import.span,
581585
);
582-
let err = UnresolvedImportError {
583-
span: import.span,
584-
label: None,
585-
note: None,
586-
suggestion: None,
587-
candidates: None,
588-
};
589586
// FIXME: there should be a better way of doing this than
590587
// formatting this as a string then checking for `::`
591588
if path.contains("::") {
589+
let err = UnresolvedImportError {
590+
span: import.span,
591+
label: None,
592+
note: None,
593+
suggestion: None,
594+
candidates: None,
595+
segment: None,
596+
module: None,
597+
};
592598
errors.push((*import, err))
593599
}
594600
}
@@ -738,15 +744,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
738744
}
739745
}
740746

741-
match &import.kind {
742-
ImportKind::Single { source, .. } => {
743-
if let Some(ModuleOrUniformRoot::Module(module)) = import.imported_module.get()
744-
&& let Some(module) = module.opt_def_id()
745-
{
746-
self.find_cfg_stripped(&mut diag, &source.name, module)
747-
}
748-
}
749-
_ => {}
747+
if matches!(import.kind, ImportKind::Single { .. })
748+
&& let Some(segment) = err.segment
749+
&& let Some(module) = err.module
750+
{
751+
self.find_cfg_stripped(&mut diag, &segment, module)
750752
}
751753
}
752754

@@ -916,10 +918,17 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
916918
span,
917919
label,
918920
suggestion,
921+
module,
922+
segment_name,
919923
..
920924
} => {
921925
if no_ambiguity {
922926
assert!(import.imported_module.get().is_none());
927+
let module = if let Some(ModuleOrUniformRoot::Module(m)) = module {
928+
m.opt_def_id()
929+
} else {
930+
None
931+
};
923932
let err = match self.make_path_suggestion(
924933
span,
925934
import.module_path.clone(),
@@ -935,13 +944,17 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
935944
Applicability::MaybeIncorrect,
936945
)),
937946
candidates: None,
947+
segment: Some(segment_name),
948+
module,
938949
},
939950
None => UnresolvedImportError {
940951
span,
941952
label: Some(label),
942953
note: None,
943954
suggestion,
944955
candidates: None,
956+
segment: Some(segment_name),
957+
module,
945958
},
946959
};
947960
return Some(err);
@@ -990,6 +1003,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
9901003
note: None,
9911004
suggestion: None,
9921005
candidates: None,
1006+
segment: None,
1007+
module: None,
9931008
});
9941009
}
9951010
}
@@ -1199,6 +1214,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11991214
} else {
12001215
None
12011216
},
1217+
module: import.imported_module.get().and_then(|module| {
1218+
if let ModuleOrUniformRoot::Module(m) = module {
1219+
m.opt_def_id()
1220+
} else {
1221+
None
1222+
}
1223+
}),
1224+
segment: Some(ident.name),
12021225
})
12031226
} else {
12041227
// `resolve_ident_in_module` reported a privacy error.

compiler/rustc_resolve/src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,19 @@ enum PathResult<'a> {
415415
label: String,
416416
suggestion: Option<Suggestion>,
417417
is_error_from_last_segment: bool,
418+
/// The final module being resolved, for instance:
419+
///
420+
/// ```compile_fail
421+
/// mod a {
422+
/// mod b {
423+
/// mod c {}
424+
/// }
425+
/// }
426+
///
427+
/// use a::not_exist::c;
428+
/// ```
429+
///
430+
/// In this case, `module` will point to `a`.
418431
module: Option<ModuleOrUniformRoot<'a>>,
419432
/// The segment name of target
420433
segment_name: Symbol,

tests/ui/cfg/diagnostics-same-crate.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ pub mod inner {
44
//~^ NOTE found an item that was configured out
55

66
#[cfg(FALSE)]
7-
pub mod doesnt_exist { //~ NOTE found an item that was configured out
7+
pub mod doesnt_exist {
8+
//~^ NOTE found an item that was configured out
9+
//~| NOTE found an item that was configured out
10+
//~| NOTE found an item that was configured out
811
pub fn hello() {}
12+
pub mod hi {}
913
}
1014

1115
pub mod wrong {
@@ -20,6 +24,15 @@ pub mod inner {
2024
}
2125
}
2226

27+
mod placeholder {
28+
use super::inner::doesnt_exist;
29+
//~^ ERROR unresolved import `super::inner::doesnt_exist`
30+
//~| NOTE no `doesnt_exist` in `inner`
31+
use super::inner::doesnt_exist::hi;
32+
//~^ ERROR unresolved import `super::inner::doesnt_exist`
33+
//~| NOTE could not find `doesnt_exist` in `inner`
34+
}
35+
2336
#[cfg(i_dont_exist_and_you_can_do_nothing_about_it)]
2437
pub fn vanished() {}
2538

+32-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
1+
error[E0432]: unresolved import `super::inner::doesnt_exist`
2+
--> $DIR/diagnostics-same-crate.rs:28:9
3+
|
4+
LL | use super::inner::doesnt_exist;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ no `doesnt_exist` in `inner`
6+
|
7+
note: found an item that was configured out
8+
--> $DIR/diagnostics-same-crate.rs:7:13
9+
|
10+
LL | pub mod doesnt_exist {
11+
| ^^^^^^^^^^^^
12+
13+
error[E0432]: unresolved import `super::inner::doesnt_exist`
14+
--> $DIR/diagnostics-same-crate.rs:31:23
15+
|
16+
LL | use super::inner::doesnt_exist::hi;
17+
| ^^^^^^^^^^^^ could not find `doesnt_exist` in `inner`
18+
|
19+
note: found an item that was configured out
20+
--> $DIR/diagnostics-same-crate.rs:7:13
21+
|
22+
LL | pub mod doesnt_exist {
23+
| ^^^^^^^^^^^^
24+
125
error[E0433]: failed to resolve: could not find `doesnt_exist` in `inner`
2-
--> $DIR/diagnostics-same-crate.rs:37:12
26+
--> $DIR/diagnostics-same-crate.rs:50:12
327
|
428
LL | inner::doesnt_exist::hello();
529
| ^^^^^^^^^^^^ could not find `doesnt_exist` in `inner`
@@ -11,7 +35,7 @@ LL | pub mod doesnt_exist {
1135
| ^^^^^^^^^^^^
1236

1337
error[E0425]: cannot find function `uwu` in module `inner`
14-
--> $DIR/diagnostics-same-crate.rs:32:12
38+
--> $DIR/diagnostics-same-crate.rs:45:12
1539
|
1640
LL | inner::uwu();
1741
| ^^^ not found in `inner`
@@ -23,31 +47,31 @@ LL | pub fn uwu() {}
2347
| ^^^
2448

2549
error[E0425]: cannot find function `meow` in module `inner::right`
26-
--> $DIR/diagnostics-same-crate.rs:41:19
50+
--> $DIR/diagnostics-same-crate.rs:54:19
2751
|
2852
LL | inner::right::meow();
2953
| ^^^^ not found in `inner::right`
3054
|
3155
note: found an item that was configured out
32-
--> $DIR/diagnostics-same-crate.rs:18:16
56+
--> $DIR/diagnostics-same-crate.rs:22:16
3357
|
3458
LL | pub fn meow() {}
3559
| ^^^^
3660
= note: the item is gated behind the `what-a-cool-feature` feature
3761

3862
error[E0425]: cannot find function `uwu` in this scope
39-
--> $DIR/diagnostics-same-crate.rs:28:5
63+
--> $DIR/diagnostics-same-crate.rs:41:5
4064
|
4165
LL | uwu();
4266
| ^^^ not found in this scope
4367

4468
error[E0425]: cannot find function `vanished` in this scope
45-
--> $DIR/diagnostics-same-crate.rs:48:5
69+
--> $DIR/diagnostics-same-crate.rs:61:5
4670
|
4771
LL | vanished();
4872
| ^^^^^^^^ not found in this scope
4973

50-
error: aborting due to 5 previous errors
74+
error: aborting due to 7 previous errors
5175

52-
Some errors have detailed explanations: E0425, E0433.
76+
Some errors have detailed explanations: E0425, E0432, E0433.
5377
For more information about an error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)