Skip to content

Commit

Permalink
store segment and module in UnresolvedImportError
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi committed Mar 20, 2024
1 parent b7dcabe commit c3d953a
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 25 deletions.
55 changes: 39 additions & 16 deletions compiler/rustc_resolve/src/imports.rs
Expand Up @@ -19,6 +19,7 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::intern::Interned;
use rustc_errors::{codes::*, pluralize, struct_span_code_err, Applicability, MultiSpan};
use rustc_hir::def::{self, DefKind, PartialRes};
use rustc_hir::def_id::DefId;
use rustc_middle::metadata::ModChild;
use rustc_middle::metadata::Reexport;
use rustc_middle::span_bug;
Expand Down Expand Up @@ -250,6 +251,9 @@ struct UnresolvedImportError {
note: Option<String>,
suggestion: Option<Suggestion>,
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;`
Expand Down Expand Up @@ -579,16 +583,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
&import.kind,
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
// formatting this as a string then checking for `::`
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))
}
}
Expand Down Expand Up @@ -738,15 +744,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
}

match &import.kind {
ImportKind::Single { source, .. } => {
if let Some(ModuleOrUniformRoot::Module(module)) = import.imported_module.get()
&& let Some(module) = module.opt_def_id()
{
self.find_cfg_stripped(&mut diag, &source.name, module)
}
}
_ => {}
if matches!(import.kind, ImportKind::Single { .. })
&& let Some(segment) = err.segment
&& let Some(module) = err.module
{
self.find_cfg_stripped(&mut diag, &segment, module)
}
}

Expand Down Expand Up @@ -916,10 +918,17 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
span,
label,
suggestion,
module,
segment_name,
..
} => {
if no_ambiguity {
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(
span,
import.module_path.clone(),
Expand All @@ -935,13 +944,17 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
Applicability::MaybeIncorrect,
)),
candidates: None,
segment: Some(segment_name),
module,
},
None => UnresolvedImportError {
span,
label: Some(label),
note: None,
suggestion,
candidates: None,
segment: Some(segment_name),
module,
},
};
return Some(err);
Expand Down Expand Up @@ -990,6 +1003,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
note: None,
suggestion: None,
candidates: None,
segment: None,
module: None,
});
}
}
Expand Down Expand Up @@ -1199,6 +1214,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
} else {
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 {
// `resolve_ident_in_module` reported a privacy error.
Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_resolve/src/lib.rs
Expand Up @@ -415,6 +415,19 @@ enum PathResult<'a> {
label: String,
suggestion: Option<Suggestion>,
is_error_from_last_segment: bool,
/// The final module being resolved, for instance:
///
/// ```no_run
/// mod a {
/// mod b {
/// mod c {}
/// }
/// }
///
/// use a::not_exist::c;

Check failure on line 427 in compiler/rustc_resolve/src/lib.rs

View workflow job for this annotation

GitHub Actions / PR - x86_64-gnu-llvm-17

unresolved import `a::not_exist`
/// ```
///
/// In this case, `module` will point to `a`.
module: Option<ModuleOrUniformRoot<'a>>,
/// The segment name of target
segment_name: Symbol,
Expand Down
15 changes: 14 additions & 1 deletion tests/ui/cfg/diagnostics-same-crate.rs
Expand Up @@ -4,8 +4,12 @@ pub mod inner {
//~^ NOTE found an item that was configured out

#[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 mod hi {}
}

pub mod wrong {
Expand All @@ -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)]
pub fn vanished() {}

Expand Down
40 changes: 32 additions & 8 deletions tests/ui/cfg/diagnostics-same-crate.stderr
@@ -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`
--> $DIR/diagnostics-same-crate.rs:37:12
--> $DIR/diagnostics-same-crate.rs:50:12
|
LL | inner::doesnt_exist::hello();
| ^^^^^^^^^^^^ could not find `doesnt_exist` in `inner`
Expand All @@ -11,7 +35,7 @@ LL | pub mod doesnt_exist {
| ^^^^^^^^^^^^

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();
| ^^^ not found in `inner`
Expand All @@ -23,31 +47,31 @@ LL | pub fn uwu() {}
| ^^^

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();
| ^^^^ not found in `inner::right`
|
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() {}
| ^^^^
= note: the item is gated behind the `what-a-cool-feature` feature

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();
| ^^^ not found 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();
| ^^^^^^^^ 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`.

0 comments on commit c3d953a

Please sign in to comment.