store segment and module in UnresolvedImportError

This commit is contained in:
bohan 2024-03-20 17:06:58 +08:00
parent b7dcabe55e
commit 7f45f53204
4 changed files with 98 additions and 25 deletions

View File

@ -19,6 +19,7 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::intern::Interned; use rustc_data_structures::intern::Interned;
use rustc_errors::{codes::*, pluralize, struct_span_code_err, Applicability, MultiSpan}; use rustc_errors::{codes::*, pluralize, struct_span_code_err, Applicability, MultiSpan};
use rustc_hir::def::{self, DefKind, PartialRes}; use rustc_hir::def::{self, DefKind, PartialRes};
use rustc_hir::def_id::DefId;
use rustc_middle::metadata::ModChild; use rustc_middle::metadata::ModChild;
use rustc_middle::metadata::Reexport; use rustc_middle::metadata::Reexport;
use rustc_middle::span_bug; use rustc_middle::span_bug;
@ -250,6 +251,9 @@ struct UnresolvedImportError {
note: Option<String>, note: Option<String>,
suggestion: Option<Suggestion>, suggestion: Option<Suggestion>,
candidates: Option<Vec<ImportSuggestion>>, candidates: Option<Vec<ImportSuggestion>>,
segment: Option<Symbol>,
/// comes from `PathRes::Failed { module }`
module: Option<DefId>,
} }
// Reexports of the form `pub use foo as bar;` where `foo` is `extern crate foo;` // 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> {
&import.kind, &import.kind,
import.span, import.span,
); );
let err = UnresolvedImportError {
span: import.span,
label: None,
note: None,
suggestion: None,
candidates: None,
};
// FIXME: there should be a better way of doing this than // FIXME: there should be a better way of doing this than
// formatting this as a string then checking for `::` // formatting this as a string then checking for `::`
if path.contains("::") { if path.contains("::") {
let err = UnresolvedImportError {
span: import.span,
label: None,
note: None,
suggestion: None,
candidates: None,
segment: None,
module: None,
};
errors.push((*import, err)) errors.push((*import, err))
} }
} }
@ -738,15 +744,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
} }
} }
match &import.kind { if matches!(import.kind, ImportKind::Single { .. })
ImportKind::Single { source, .. } => { && let Some(segment) = err.segment
if let Some(ModuleOrUniformRoot::Module(module)) = import.imported_module.get() && let Some(module) = err.module
&& let Some(module) = module.opt_def_id() {
{ self.find_cfg_stripped(&mut diag, &segment, module)
self.find_cfg_stripped(&mut diag, &source.name, module)
}
}
_ => {}
} }
} }
@ -916,10 +918,17 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
span, span,
label, label,
suggestion, suggestion,
module,
segment_name,
.. ..
} => { } => {
if no_ambiguity { if no_ambiguity {
assert!(import.imported_module.get().is_none()); assert!(import.imported_module.get().is_none());
let module = if let Some(ModuleOrUniformRoot::Module(m)) = module {
m.opt_def_id()
} else {
None
};
let err = match self.make_path_suggestion( let err = match self.make_path_suggestion(
span, span,
import.module_path.clone(), import.module_path.clone(),
@ -935,6 +944,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
)), )),
candidates: None, candidates: None,
segment: Some(segment_name),
module,
}, },
None => UnresolvedImportError { None => UnresolvedImportError {
span, span,
@ -942,6 +953,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
note: None, note: None,
suggestion, suggestion,
candidates: None, candidates: None,
segment: Some(segment_name),
module,
}, },
}; };
return Some(err); return Some(err);
@ -990,6 +1003,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
note: None, note: None,
suggestion: None, suggestion: None,
candidates: None, candidates: None,
segment: None,
module: None,
}); });
} }
} }
@ -1199,6 +1214,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
} else { } else {
None None
}, },
module: import.imported_module.get().and_then(|module| {
if let ModuleOrUniformRoot::Module(m) = module {
m.opt_def_id()
} else {
None
}
}),
segment: Some(ident.name),
}) })
} else { } else {
// `resolve_ident_in_module` reported a privacy error. // `resolve_ident_in_module` reported a privacy error.

View File

@ -415,6 +415,19 @@ enum PathResult<'a> {
label: String, label: String,
suggestion: Option<Suggestion>, suggestion: Option<Suggestion>,
is_error_from_last_segment: bool, is_error_from_last_segment: bool,
/// The final module being resolved, for instance:
///
/// ```compile_fail
/// mod a {
/// mod b {
/// mod c {}
/// }
/// }
///
/// use a::not_exist::c;
/// ```
///
/// In this case, `module` will point to `a`.
module: Option<ModuleOrUniformRoot<'a>>, module: Option<ModuleOrUniformRoot<'a>>,
/// The segment name of target /// The segment name of target
segment_name: Symbol, segment_name: Symbol,

View File

@ -4,8 +4,12 @@ pub mod inner {
//~^ NOTE found an item that was configured out //~^ NOTE found an item that was configured out
#[cfg(FALSE)] #[cfg(FALSE)]
pub mod doesnt_exist { //~ NOTE found an item that was configured out pub mod doesnt_exist {
//~^ NOTE found an item that was configured out
//~| NOTE found an item that was configured out
//~| NOTE found an item that was configured out
pub fn hello() {} pub fn hello() {}
pub mod hi {}
} }
pub mod wrong { pub mod wrong {
@ -20,6 +24,15 @@ pub mod inner {
} }
} }
mod placeholder {
use super::inner::doesnt_exist;
//~^ ERROR unresolved import `super::inner::doesnt_exist`
//~| NOTE no `doesnt_exist` in `inner`
use super::inner::doesnt_exist::hi;
//~^ ERROR unresolved import `super::inner::doesnt_exist`
//~| NOTE could not find `doesnt_exist` in `inner`
}
#[cfg(i_dont_exist_and_you_can_do_nothing_about_it)] #[cfg(i_dont_exist_and_you_can_do_nothing_about_it)]
pub fn vanished() {} pub fn vanished() {}

View File

@ -1,5 +1,29 @@
error[E0432]: unresolved import `super::inner::doesnt_exist`
--> $DIR/diagnostics-same-crate.rs:28:9
|
LL | use super::inner::doesnt_exist;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ no `doesnt_exist` in `inner`
|
note: found an item that was configured out
--> $DIR/diagnostics-same-crate.rs:7:13
|
LL | pub mod doesnt_exist {
| ^^^^^^^^^^^^
error[E0432]: unresolved import `super::inner::doesnt_exist`
--> $DIR/diagnostics-same-crate.rs:31:23
|
LL | use super::inner::doesnt_exist::hi;
| ^^^^^^^^^^^^ could not find `doesnt_exist` in `inner`
|
note: found an item that was configured out
--> $DIR/diagnostics-same-crate.rs:7:13
|
LL | pub mod doesnt_exist {
| ^^^^^^^^^^^^
error[E0433]: failed to resolve: could not find `doesnt_exist` in `inner` error[E0433]: failed to resolve: could not find `doesnt_exist` in `inner`
--> $DIR/diagnostics-same-crate.rs:37:12 --> $DIR/diagnostics-same-crate.rs:50:12
| |
LL | inner::doesnt_exist::hello(); LL | inner::doesnt_exist::hello();
| ^^^^^^^^^^^^ could not find `doesnt_exist` in `inner` | ^^^^^^^^^^^^ could not find `doesnt_exist` in `inner`
@ -11,7 +35,7 @@ LL | pub mod doesnt_exist {
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
error[E0425]: cannot find function `uwu` in module `inner` error[E0425]: cannot find function `uwu` in module `inner`
--> $DIR/diagnostics-same-crate.rs:32:12 --> $DIR/diagnostics-same-crate.rs:45:12
| |
LL | inner::uwu(); LL | inner::uwu();
| ^^^ not found in `inner` | ^^^ not found in `inner`
@ -23,31 +47,31 @@ LL | pub fn uwu() {}
| ^^^ | ^^^
error[E0425]: cannot find function `meow` in module `inner::right` error[E0425]: cannot find function `meow` in module `inner::right`
--> $DIR/diagnostics-same-crate.rs:41:19 --> $DIR/diagnostics-same-crate.rs:54:19
| |
LL | inner::right::meow(); LL | inner::right::meow();
| ^^^^ not found in `inner::right` | ^^^^ not found in `inner::right`
| |
note: found an item that was configured out note: found an item that was configured out
--> $DIR/diagnostics-same-crate.rs:18:16 --> $DIR/diagnostics-same-crate.rs:22:16
| |
LL | pub fn meow() {} LL | pub fn meow() {}
| ^^^^ | ^^^^
= note: the item is gated behind the `what-a-cool-feature` feature = note: the item is gated behind the `what-a-cool-feature` feature
error[E0425]: cannot find function `uwu` in this scope error[E0425]: cannot find function `uwu` in this scope
--> $DIR/diagnostics-same-crate.rs:28:5 --> $DIR/diagnostics-same-crate.rs:41:5
| |
LL | uwu(); LL | uwu();
| ^^^ not found in this scope | ^^^ not found in this scope
error[E0425]: cannot find function `vanished` in this scope error[E0425]: cannot find function `vanished` in this scope
--> $DIR/diagnostics-same-crate.rs:48:5 --> $DIR/diagnostics-same-crate.rs:61:5
| |
LL | vanished(); LL | vanished();
| ^^^^^^^^ not found in this scope | ^^^^^^^^ not found in this scope
error: aborting due to 5 previous errors error: aborting due to 7 previous errors
Some errors have detailed explanations: E0425, E0433. Some errors have detailed explanations: E0425, E0432, E0433.
For more information about an error, try `rustc --explain E0425`. For more information about an error, try `rustc --explain E0425`.