mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-28 11:07:42 +00:00
Merge #9247
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:
commit
e6fa9b016f
@ -32,7 +32,7 @@ macro_rules! diagnostics {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
diagnostics![UnresolvedModule, MissingFields];
|
diagnostics![UnresolvedModule, UnresolvedExternCrate, MissingFields];
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct UnresolvedModule {
|
pub struct UnresolvedModule {
|
||||||
@ -40,28 +40,9 @@ pub struct UnresolvedModule {
|
|||||||
pub candidate: String,
|
pub candidate: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Diagnostic: unresolved-extern-crate
|
|
||||||
//
|
|
||||||
// This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate.
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct UnresolvedExternCrate {
|
pub struct UnresolvedExternCrate {
|
||||||
pub file: HirFileId,
|
pub decl: InFile<AstPtr<ast::ExternCrate>>,
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -484,10 +484,12 @@ impl Module {
|
|||||||
}
|
}
|
||||||
DefDiagnosticKind::UnresolvedExternCrate { ast } => {
|
DefDiagnosticKind::UnresolvedExternCrate { ast } => {
|
||||||
let item = ast.to_node(db.upcast());
|
let item = ast.to_node(db.upcast());
|
||||||
sink.push(UnresolvedExternCrate {
|
acc.push(
|
||||||
file: ast.file_id,
|
UnresolvedExternCrate {
|
||||||
item: AstPtr::new(&item),
|
decl: InFile::new(ast.file_id, AstPtr::new(&item)),
|
||||||
});
|
}
|
||||||
|
.into(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
DefDiagnosticKind::UnresolvedImport { id, index } => {
|
DefDiagnosticKind::UnresolvedImport { id, index } => {
|
||||||
|
@ -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]
|
#[test]
|
||||||
fn dedup_unresolved_import_from_unresolved_crate() {
|
fn dedup_unresolved_import_from_unresolved_crate() {
|
||||||
check_diagnostics(
|
check_diagnostics(
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
//! original files. So we need to map the ranges.
|
//! original files. So we need to map the ranges.
|
||||||
|
|
||||||
mod unresolved_module;
|
mod unresolved_module;
|
||||||
|
mod unresolved_extern_crate;
|
||||||
mod missing_fields;
|
mod missing_fields;
|
||||||
|
|
||||||
mod fixes;
|
mod fixes;
|
||||||
@ -229,8 +230,10 @@ pub(crate) fn diagnostics(
|
|||||||
|
|
||||||
let ctx = DiagnosticsContext { config, sema, resolve };
|
let ctx = DiagnosticsContext { config, sema, resolve };
|
||||||
for diag in diags {
|
for diag in diags {
|
||||||
|
#[rustfmt::skip]
|
||||||
let d = match diag {
|
let d = match diag {
|
||||||
AnyDiagnostic::UnresolvedModule(d) => unresolved_module::unresolved_module(&ctx, &d),
|
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),
|
AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d),
|
||||||
};
|
};
|
||||||
if let Some(code) = d.code {
|
if let Some(code) = d.code {
|
||||||
|
49
crates/ide/src/diagnostics/unresolved_extern_crate.rs
Normal file
49
crates/ide/src/diagnostics/unresolved_extern_crate.rs
Normal 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;
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user