2021-06-14 05:15:05 -05:00
|
|
|
use crate::{Diagnostic, DiagnosticsContext};
|
2021-06-13 08:42:34 -05:00
|
|
|
|
|
|
|
// Diagnostic: unresolved-import
|
|
|
|
//
|
|
|
|
// This diagnostic is triggered if rust-analyzer is unable to resolve a path in
|
|
|
|
// a `use` declaration.
|
2021-06-14 11:32:39 -05:00
|
|
|
pub(crate) fn unresolved_import(
|
2021-06-13 08:42:34 -05:00
|
|
|
ctx: &DiagnosticsContext<'_>,
|
|
|
|
d: &hir::UnresolvedImport,
|
|
|
|
) -> Diagnostic {
|
|
|
|
Diagnostic::new(
|
|
|
|
"unresolved-import",
|
|
|
|
"unresolved import",
|
|
|
|
ctx.sema.diagnostics_display_range(d.decl.clone().map(|it| it.into())).range,
|
|
|
|
)
|
|
|
|
// This currently results in false positives in the following cases:
|
|
|
|
// - `cfg_if!`-generated code in libstd (we don't load the sysroot correctly)
|
|
|
|
// - `core::arch` (we don't handle `#[path = "../<path>"]` correctly)
|
|
|
|
// - proc macros and/or proc macro generated code
|
|
|
|
.experimental()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-06-14 05:15:05 -05:00
|
|
|
use crate::tests::check_diagnostics;
|
2021-06-13 08:42:34 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unresolved_import() {
|
|
|
|
check_diagnostics(
|
|
|
|
r#"
|
|
|
|
use does_exist;
|
|
|
|
use does_not_exist;
|
2021-06-14 14:06:28 -05:00
|
|
|
//^^^^^^^^^^^^^^ error: unresolved import
|
2021-06-13 08:42:34 -05:00
|
|
|
|
|
|
|
mod does_exist {}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unresolved_import_in_use_tree() {
|
|
|
|
// Only the relevant part of a nested `use` item should be highlighted.
|
|
|
|
check_diagnostics(
|
|
|
|
r#"
|
|
|
|
use does_exist::{Exists, DoesntExist};
|
2021-06-14 14:06:28 -05:00
|
|
|
//^^^^^^^^^^^ error: unresolved import
|
2021-06-13 08:42:34 -05:00
|
|
|
|
|
|
|
use {does_not_exist::*, does_exist};
|
2021-06-14 14:06:28 -05:00
|
|
|
//^^^^^^^^^^^^^^^^^ error: unresolved import
|
2021-06-13 08:42:34 -05:00
|
|
|
|
|
|
|
use does_not_exist::{
|
|
|
|
a,
|
2021-06-14 14:06:28 -05:00
|
|
|
//^ error: unresolved import
|
2021-06-13 08:42:34 -05:00
|
|
|
b,
|
2021-06-14 14:06:28 -05:00
|
|
|
//^ error: unresolved import
|
2021-06-13 08:42:34 -05:00
|
|
|
c,
|
2021-06-14 14:06:28 -05:00
|
|
|
//^ error: unresolved import
|
2021-06-13 08:42:34 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
mod does_exist {
|
|
|
|
pub struct Exists;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dedup_unresolved_import_from_unresolved_crate() {
|
|
|
|
check_diagnostics(
|
|
|
|
r#"
|
|
|
|
//- /main.rs crate:main
|
|
|
|
mod a {
|
|
|
|
extern crate doesnotexist;
|
2021-06-14 14:06:28 -05:00
|
|
|
//^^^^^^^^^^^^^^^^^^^^^^^^^^ error: unresolved extern crate
|
2021-06-13 08:42:34 -05:00
|
|
|
|
|
|
|
// Should not error, since we already errored for the missing crate.
|
|
|
|
use doesnotexist::{self, bla, *};
|
|
|
|
|
|
|
|
use crate::doesnotexist;
|
2021-06-14 14:06:28 -05:00
|
|
|
//^^^^^^^^^^^^^^^^^^^ error: unresolved import
|
2021-06-13 08:42:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
mod m {
|
|
|
|
use super::doesnotexist;
|
2021-06-14 14:06:28 -05:00
|
|
|
//^^^^^^^^^^^^^^^^^^^ error: unresolved import
|
2021-06-13 08:42:34 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|