Skip to content

Commit 39f190b

Browse files
committed
internal: refactor unresolved extern crate diagnostic
1 parent 6383252 commit 39f190b

File tree

5 files changed

+60
-54
lines changed

5 files changed

+60
-54
lines changed

crates/hir/src/diagnostics.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,17 @@ macro_rules! diagnostics {
3232
};
3333
}
3434

35-
diagnostics![UnresolvedModule, MissingFields];
35+
diagnostics![UnresolvedModule, UnresolvedExternCrate, MissingFields];
3636

3737
#[derive(Debug)]
3838
pub struct UnresolvedModule {
3939
pub decl: InFile<AstPtr<ast::Module>>,
4040
pub candidate: String,
4141
}
4242

43-
// Diagnostic: unresolved-extern-crate
44-
//
45-
// This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate.
4643
#[derive(Debug)]
4744
pub struct UnresolvedExternCrate {
48-
pub file: HirFileId,
49-
pub item: AstPtr<ast::ExternCrate>,
50-
}
51-
52-
impl Diagnostic for UnresolvedExternCrate {
53-
fn code(&self) -> DiagnosticCode {
54-
DiagnosticCode("unresolved-extern-crate")
55-
}
56-
fn message(&self) -> String {
57-
"unresolved extern crate".to_string()
58-
}
59-
fn display_source(&self) -> InFile<SyntaxNodePtr> {
60-
InFile::new(self.file, self.item.clone().into())
61-
}
62-
fn as_any(&self) -> &(dyn Any + Send + 'static) {
63-
self
64-
}
45+
pub decl: InFile<AstPtr<ast::ExternCrate>>,
6546
}
6647

6748
#[derive(Debug)]

crates/hir/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,10 +484,12 @@ impl Module {
484484
}
485485
DefDiagnosticKind::UnresolvedExternCrate { ast } => {
486486
let item = ast.to_node(db.upcast());
487-
sink.push(UnresolvedExternCrate {
488-
file: ast.file_id,
489-
item: AstPtr::new(&item),
490-
});
487+
acc.push(
488+
UnresolvedExternCrate {
489+
decl: InFile::new(ast.file_id, AstPtr::new(&item)),
490+
}
491+
.into(),
492+
);
491493
}
492494

493495
DefDiagnosticKind::UnresolvedImport { id, index } => {

crates/hir_def/src/nameres/tests/diagnostics.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,6 @@ fn unresolved_import() {
2525
);
2626
}
2727

28-
#[test]
29-
fn unresolved_extern_crate() {
30-
check_diagnostics(
31-
r"
32-
//- /main.rs crate:main deps:core
33-
extern crate core;
34-
extern crate doesnotexist;
35-
//^^^^^^^^^^^^^^^^^^^^^^^^^^ UnresolvedExternCrate
36-
//- /lib.rs crate:core
37-
",
38-
);
39-
}
40-
41-
#[test]
42-
fn extern_crate_self_as() {
43-
cov_mark::check!(extern_crate_self_as);
44-
check_diagnostics(
45-
r"
46-
//- /lib.rs
47-
extern crate doesnotexist;
48-
//^^^^^^^^^^^^^^^^^^^^^^^^^^ UnresolvedExternCrate
49-
// Should not error.
50-
extern crate self as foo;
51-
struct Foo;
52-
use foo::Foo as Bar;
53-
",
54-
);
55-
}
56-
5728
#[test]
5829
fn dedup_unresolved_import_from_unresolved_crate() {
5930
check_diagnostics(

crates/ide/src/diagnostics.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! original files. So we need to map the ranges.
66
77
mod unresolved_module;
8+
mod unresolved_extern_crate;
89
mod missing_fields;
910

1011
mod fixes;
@@ -229,8 +230,10 @@ pub(crate) fn diagnostics(
229230

230231
let ctx = DiagnosticsContext { config, sema, resolve };
231232
for diag in diags {
233+
#[rustfmt::skip]
232234
let d = match diag {
233235
AnyDiagnostic::UnresolvedModule(d) => unresolved_module::unresolved_module(&ctx, &d),
236+
AnyDiagnostic::UnresolvedExternCrate(d) => unresolved_extern_crate::unresolved_extern_crate(&ctx, &d),
234237
AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d),
235238
};
236239
if let Some(code) = d.code {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use crate::diagnostics::{Diagnostic, DiagnosticsContext};
2+
3+
// Diagnostic: unresolved-extern-crate
4+
//
5+
// This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate.
6+
pub(super) fn unresolved_extern_crate(
7+
ctx: &DiagnosticsContext<'_>,
8+
d: &hir::UnresolvedExternCrate,
9+
) -> Diagnostic {
10+
Diagnostic::new(
11+
"unresolved-extern-crate",
12+
"unresolved extern crate",
13+
ctx.sema.diagnostics_display_range(d.decl.clone().map(|it| it.into())).range,
14+
)
15+
}
16+
17+
#[cfg(test)]
18+
mod tests {
19+
use crate::diagnostics::tests::check_diagnostics;
20+
21+
#[test]
22+
fn unresolved_extern_crate() {
23+
check_diagnostics(
24+
r#"
25+
//- /main.rs crate:main deps:core
26+
extern crate core;
27+
extern crate doesnotexist;
28+
//^^^^^^^^^^^^^^^^^^^^^^^^^^ unresolved extern crate
29+
//- /lib.rs crate:core
30+
"#,
31+
);
32+
}
33+
34+
#[test]
35+
fn extern_crate_self_as() {
36+
cov_mark::check!(extern_crate_self_as);
37+
check_diagnostics(
38+
r#"
39+
//- /lib.rs
40+
extern crate doesnotexist;
41+
//^^^^^^^^^^^^^^^^^^^^^^^^^^ unresolved extern crate
42+
// Should not error.
43+
extern crate self as foo;
44+
struct Foo;
45+
use foo::Foo as Bar;
46+
"#,
47+
);
48+
}
49+
}

0 commit comments

Comments
 (0)