9247: internal: refactor unresolved extern crate diagnostic r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2021-06-13 13:06:27 +00:00 committed by GitHub
commit e6fa9b016f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 54 deletions

View File

@ -32,7 +32,7 @@ macro_rules! diagnostics {
};
}
diagnostics![UnresolvedModule, MissingFields];
diagnostics![UnresolvedModule, UnresolvedExternCrate, MissingFields];
#[derive(Debug)]
pub struct UnresolvedModule {
@ -40,28 +40,9 @@ pub struct UnresolvedModule {
pub candidate: String,
}
// Diagnostic: unresolved-extern-crate
//
// This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate.
#[derive(Debug)]
pub struct UnresolvedExternCrate {
pub file: HirFileId,
pub item: AstPtr<ast::ExternCrate>,
}
impl Diagnostic for UnresolvedExternCrate {
fn code(&self) -> DiagnosticCode {
DiagnosticCode("unresolved-extern-crate")
}
fn message(&self) -> String {
"unresolved extern crate".to_string()
}
fn display_source(&self) -> InFile<SyntaxNodePtr> {
InFile::new(self.file, self.item.clone().into())
}
fn as_any(&self) -> &(dyn Any + Send + 'static) {
self
}
pub decl: InFile<AstPtr<ast::ExternCrate>>,
}
#[derive(Debug)]

View File

@ -484,10 +484,12 @@ impl Module {
}
DefDiagnosticKind::UnresolvedExternCrate { ast } => {
let item = ast.to_node(db.upcast());
sink.push(UnresolvedExternCrate {
file: ast.file_id,
item: AstPtr::new(&item),
});
acc.push(
UnresolvedExternCrate {
decl: InFile::new(ast.file_id, AstPtr::new(&item)),
}
.into(),
);
}
DefDiagnosticKind::UnresolvedImport { id, index } => {

View File

@ -25,35 +25,6 @@ fn unresolved_import() {
);
}
#[test]
fn unresolved_extern_crate() {
check_diagnostics(
r"
//- /main.rs crate:main deps:core
extern crate core;
extern crate doesnotexist;
//^^^^^^^^^^^^^^^^^^^^^^^^^^ UnresolvedExternCrate
//- /lib.rs crate:core
",
);
}
#[test]
fn extern_crate_self_as() {
cov_mark::check!(extern_crate_self_as);
check_diagnostics(
r"
//- /lib.rs
extern crate doesnotexist;
//^^^^^^^^^^^^^^^^^^^^^^^^^^ UnresolvedExternCrate
// Should not error.
extern crate self as foo;
struct Foo;
use foo::Foo as Bar;
",
);
}
#[test]
fn dedup_unresolved_import_from_unresolved_crate() {
check_diagnostics(

View File

@ -5,6 +5,7 @@
//! original files. So we need to map the ranges.
mod unresolved_module;
mod unresolved_extern_crate;
mod missing_fields;
mod fixes;
@ -229,8 +230,10 @@ pub(crate) fn diagnostics(
let ctx = DiagnosticsContext { config, sema, resolve };
for diag in diags {
#[rustfmt::skip]
let d = match diag {
AnyDiagnostic::UnresolvedModule(d) => unresolved_module::unresolved_module(&ctx, &d),
AnyDiagnostic::UnresolvedExternCrate(d) => unresolved_extern_crate::unresolved_extern_crate(&ctx, &d),
AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d),
};
if let Some(code) = d.code {

View File

@ -0,0 +1,49 @@
use crate::diagnostics::{Diagnostic, DiagnosticsContext};
// Diagnostic: unresolved-extern-crate
//
// This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate.
pub(super) fn unresolved_extern_crate(
ctx: &DiagnosticsContext<'_>,
d: &hir::UnresolvedExternCrate,
) -> Diagnostic {
Diagnostic::new(
"unresolved-extern-crate",
"unresolved extern crate",
ctx.sema.diagnostics_display_range(d.decl.clone().map(|it| it.into())).range,
)
}
#[cfg(test)]
mod tests {
use crate::diagnostics::tests::check_diagnostics;
#[test]
fn unresolved_extern_crate() {
check_diagnostics(
r#"
//- /main.rs crate:main deps:core
extern crate core;
extern crate doesnotexist;
//^^^^^^^^^^^^^^^^^^^^^^^^^^ unresolved extern crate
//- /lib.rs crate:core
"#,
);
}
#[test]
fn extern_crate_self_as() {
cov_mark::check!(extern_crate_self_as);
check_diagnostics(
r#"
//- /lib.rs
extern crate doesnotexist;
//^^^^^^^^^^^^^^^^^^^^^^^^^^ unresolved extern crate
// Should not error.
extern crate self as foo;
struct Foo;
use foo::Foo as Bar;
"#,
);
}
}